1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * $Id: db_n_n.pike,v 1.2 2009/08/07 15:22:36 nicke Exp $
29 private string sDbTable;
34 return copy_value(sDbTable);
38 * connect a db_mapping with database.pike
42 final void load_db_mapping()
44 // get database access function and tablename
46 [fDb , sDbTable]= _Database->connect_db_mapping();
48 // we are in secure code, so create table according to
49 // values from database.
50 if( search(fDb()->list_tables(), "mi_"+sDbTable ) == -1 )
52 fDb()->big_query("create table mi_"+sDbTable+
53 "(k char(255) not null, v text,"+
54 (fDb()->get_database() == "postgres" ?
55 "unique (v, k))":"unique(v(60),k))"));
56 //FIXME: postgres needs this as:
57 //(k char(255) not null unique, v text unique)
64 * get a list of all values associated with
65 * @param string key - the key to access
66 * @result mixed value - the datastructure set with set_value
70 mixed get_value(string|int|object key)
75 // LOG_DEBUG("db_n_n.get_value "+sprintf("%O",key));
76 if (objectp(key) && !IS_PROXY(key))
77 throw(({"Illegal object given as key to get_value", backtrace()}));
81 // LOG_DB("search "+sDbTable +" for "+ (string) key);
83 fDb()->big_query("select v from mi_"+sDbTable+
84 " where k like '"+fDb()->quote_index(key)+"'");
86 while (res && (row=res->fetch_row()))
87 d+= ({ unserialize(row[0])});
95 * since the n_n module is symmetric, it might be interesting to retreive
96 * all keys associated to a value.
97 * @param string value - the value to access
98 * @result array keys - a list (may be empty) of the keys denoting the val
101 array get_key(string|int|object value)
106 if (objectp(value) && !IS_PROXY(value))
107 throw(({"Illeagal object given as value to get_key", backtrace()}));
109 string svalue = serialize(value);
110 // LOG("search "+sDbTable +" for "+ (string) key);
112 fDb()->big_query("select k from mi_"+sDbTable+
113 " where v like '"+ fDb()->quote(svalue) +"'");
115 while (res && (row=res->fetch_row()))
116 d+= ({ unserialize(row[0])});
124 * Add an entry into the list, there is no duplicate check
125 * The serialization of the given value will be stored to the database
126 * @param array key - the keys to store
127 * @param mixed value - the value to associate with the keys
128 * if you pass an array to value, all of the keys given
129 * will be registered for each of "values" members.
133 int set_value(mixed keys, mixed values)
139 LOG_DEBUG("db_n_n.set_value:"+sprintf("%O",keys)+" "+sprintf("%O",values)+"\n");
140 if (zero_type(keys) || zero_type(values))
146 values = ({ values });
148 delete_value(values);
154 // LOG_DB("inserting "+master()->detailed_describe(value)+","+
155 // master()->detailed_describe(key));
156 if(sizeof(fDb()->query("SELECT k FROM mi_"+sDbTable+" WHERE k='"
157 +fDb()->quote(serialize(key))+"'")))
159 fDb()->big_query("UPDATE mi_"+sDbTable+
160 " SET v='"+ fDb()->quote(serialize(val))+"'"
161 " WHERE k='"+fDb()->quote(serialize(key))+"'");
165 fDb()->big_query("INSERT INTO mi_" + sDbTable +
166 " VALUES('" + fDb()->quote(serialize(key)) +
167 "', '" + fDb()->quote(serialize(val)) + "')");
177 * delete all entries associated to a key, or a key value pair from the
179 * The NIL value below is defined in macros.h to create a zero_type
180 * If used as an argument NIL matches all values. (use like an *)
181 * @param string|int|NIL key
182 * @param string|int|void value
183 * @result int - Number of deleted entries
186 int delete(string|int|void|object key, string|int|void|object value)
190 if (zero_type(key) && zero_type(key))
192 if (objectp(key) && (!IS_PROXY(key)))
194 if (objectp(value) && (!IS_PROXY(value)))
197 if (!zero_type(value))
198 value = serialize(value);
200 key = serialize(key);
202 string bquery = "delete from mi_"+sDbTable+" where "+
203 (!zero_type(key) ? "k = '" +fDb()->quote(key)+"'" :"") +
204 (!zero_type(value) ? (!zero_type(key) ? "and " :"")+"v='" +svalue + "'" : "");
206 fDb()->big_query(bquery);
215 * same as delete, but also reports which elements got deleted
216 * @param string|int key
217 * @param string|int|void value
218 * @result array(string|int) keys of elements deleted
221 array(string|int|object)
222 report_deleted(string|int|object|void key, string|int|object|void value)
226 if (zero_type(key) && zero_type(key))
229 if (objectp(key) && (!IS_PROXY(key)))
231 if (objectp(value) && (!IS_PROXY(value)))
234 if (!zero_type(value))
235 value = serialize(value);
237 key = serialize(key);
239 string bquery = "mi_"+sDbTable+" where "+
240 (!zero_type(key) ? "k = '" +fDb()->quote(key)+"'" :"") +
241 (!zero_type(value) ? (!zero_type(key) ? "and " :"")+ "v='" +svalue + "'" : "");
244 res = fDb()->big_query("select k from "+bquery);
248 while (res && (row=res->fetch_row()))
249 tmp+= ({ unserialize(row[0]) });
251 fDb()->big_query("delete from "+bquery);
258 * delete all entries with a matching value
259 * @param string|int|object value
260 * @result int - Number of deleted entries
261 * @see delete with first Argument NIL
264 int delete_value(int|string|object value)
267 if (objectp(value) && !IS_PROXY(value))
268 throw(({"Illegal Object passed as value to delete_value", backtrace()}));
270 string svalue = serialize(value);
271 fDb()->big_query("delete from mi_"+ sDbTable+
272 " where v = '" + svalue + "'");
273 return fDb()->master_sql->affected_rows();
279 * give a list of all indices (keys) of the database table
281 * @return an array containing the keys
282 * @see maapping.indices
288 fDb()->big_query("select k from mi_"+sDbTable);
289 int sz = res->num_rows();
290 array sIndices = allocate(sz);
293 sIndices[i] = unserialize(res->fetch_row()[0]);
299 string get_table_name() { return (string)get_object_id(); }