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.1.1.1 2006/03/27 12:40:10 exodusd Exp $
20
constant cvs_version="$Id: db_n_n.pike,v 1.1.1.1 2006/03/27 12:40:10 exodusd Exp $";
27
private static Sql.sql_result oDbResult;
28
private static string sDbTable;
29
private static function fDb;
33
return copy_value(sDbTable);
37
* connect a db_mapping with database.pike
40
static final void load_db_mapping()
42
// get database access function and tablename
44
[fDb , sDbTable]= _Database->connect_db_mapping();
46
// we are in secure code, so create table according to
47
// values from database.
48
if( search(fDb()->list_tables(), "mi_"+sDbTable ) == -1 )
50
fDb()->big_query("create table mi_"+sDbTable+
51
"(k char(255) not null, v text,"+
52
"unique (v(200), k(200)))");
53
//FIXME: postgres needs this as:
54
//(k char(255) not null unique, v text unique)
59
* get a list of all values associated with
60
* @param string key - the key to access
61
* @result mixed value - the datastructure set with set_value
64
static mixed get_value(string|int|object key)
69
// LOG_DEBUG("db_n_n.get_value "+sprintf("%O",key));
70
if (objectp(key) && !IS_PROXY(key))
71
throw(({"Illegal object given as key to get_value", backtrace()}));
75
// LOG_DB("search "+sDbTable +" for "+ (string) key);
77
fDb()->big_query("select v from mi_"+sDbTable+
78
" where k like '"+fDb()->quote(key)+"'");
80
while (res && (row=res->fetch_row()))
81
d+= ({ unserialize(row[0])});
87
* since the n_n module is symmetric, it might be interesting to retreive
88
* all keys associated to a value.
89
* @param string value - the value to access
90
* @result array keys - a list (may be empty) of the keys denoting the val
92
static array get_key(string|int|object value)
97
if (objectp(value) && !IS_PROXY(value))
98
throw(({"Illeagal object given as value to get_key", backtrace()}));
100
string svalue = serialize(value);
101
// LOG("search "+sDbTable +" for "+ (string) key);
103
fDb()->big_query("select k from mi_"+sDbTable+
104
" where v like '"+ fDb()->quote(svalue) +"'");
106
while (res && (row=res->fetch_row()))
107
d+= ({ unserialize(row[0])});
113
* Add an entry into the list, there is no duplicate check
114
* The serialization of the given value will be stored to the database
115
* @param array key - the keys to store
116
* @param mixed value - the value to associate with the keys
117
* if you pass an array to value, all of the keys given
118
* will be registered for each of "values" members.
121
static int set_value(mixed keys, mixed values)
127
LOG_DEBUG("db_n_n.set_value:"+sprintf("%O",keys)+" "+sprintf("%O",values)+"\n");
128
if (zero_type(keys) || zero_type(values))
134
values = ({ values });
136
delete_value(values);
142
// LOG_DB("inserting "+master()->detailed_describe(value)+","+
143
// master()->detailed_describe(key));
144
if(sizeof(fDb()->query("SELECT k FROM mi_"+sDbTable+" WHERE k='"
145
+fDb()->quote(serialize(key))+"'")))
147
fDb()->big_query("UPDATE mi_"+sDbTable+
148
" SET v='"+ fDb()->quote(serialize(val))+"'"
149
" WHERE k='"+fDb()->quote(serialize(key))+"'");
153
fDb()->big_query("INSERT INTO mi_" + sDbTable +
154
" VALUES('" + fDb()->quote(serialize(key)) +
155
"', '" + fDb()->quote(serialize(val)) + "')");
163
* delete all entries associated to a key, or a key value pair from the
165
* The NIL value below is defined in macros.h to create a zero_type
166
* If used as an argument NIL matches all values. (use like an *)
167
* @param string|int|NIL key
168
* @param string|int|void value
169
* @result int - Number of deleted entries
171
static int delete(string|int|void|object key, string|int|void|object value)
175
if (zero_type(key) && zero_type(key))
177
if (objectp(key) && (!IS_PROXY(key)))
179
if (objectp(value) && (!IS_PROXY(value)))
182
if (!zero_type(value))
183
value = serialize(value);
185
key = serialize(key);
187
string bquery = "delete from mi_"+sDbTable+" where "+
188
(!zero_type(key) ? "k = '" +fDb()->quote(key)+"'" :"") +
189
(!zero_type(value) ? (!zero_type(key) ? "and " :"")+"v='" +svalue + "'" : "");
191
fDb()->big_query(bquery);
192
return fDb()->master_sql->affected_rows();
198
* same as delete, but also reports which elements got deleted
199
* @param string|int key
200
* @param string|int|void value
201
* @result array(string|int) keys of elements deleted
203
static array(string|int|object)
204
report_deleted(string|int|object|void key, string|int|object|void value)
208
if (zero_type(key) && zero_type(key))
211
if (objectp(key) && (!IS_PROXY(key)))
213
if (objectp(value) && (!IS_PROXY(value)))
216
if (!zero_type(value))
217
value = serialize(value);
219
key = serialize(key);
221
string bquery = "mi_"+sDbTable+" where "+
222
(!zero_type(key) ? "k = '" +fDb()->quote(key)+"'" :"") +
223
(!zero_type(value) ? (!zero_type(key) ? "and " :"")+ "v='" +svalue + "'" : "");
226
res = fDb()->big_query("select k from "+bquery);
230
while (res && (row=res->fetch_row()))
231
tmp+= ({ unserialize(row[0]) });
233
fDb()->big_query("delete from "+bquery);
238
* delete all entries with a matching value
239
* @param string|int|object value
240
* @result int - Number of deleted entries
241
* @see delete with first Argument NIL
243
static int delete_value(int|string|object value)
246
if (objectp(value) && !IS_PROXY(value))
247
throw(({"Illegal Object passed as value to delete_value", backtrace()}));
249
string svalue = serialize(value);
250
fDb()->big_query("delete from mi_"+ sDbTable+
251
" where v = '" + svalue + "'");
252
return fDb()->master_sql->affected_rows();
256
* give a list of all indices (keys) of the database table
258
* @return an array containing the keys
259
* @see maapping.indices
261
static array(string) index()
264
fDb()->big_query("select k from mi_"+sDbTable);
265
int sz = res->num_rows();
266
array(string) sIndices = allocate(sz);
269
sIndices[i] = unserialize(res->fetch_row()[0]);
273
string get_table_name() { return (string)get_object_id(); }