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_one.pike,v 1.2 2009/05/06 19:23:10 astra Exp $
27 * $Id: db_n_one.pike,v 1.2 2009/05/06 19:23:10 astra Exp $
28 * Database Table with non unique indexing abilities,
29 * result of a query is always an array of keys
35 private string sDbTable;
40 return copy_value(sDbTable);
44 * connect a db_n_one table with the according database table
50 final void load_db_mapping()
52 // get database access function and tablename
54 [fDb , sDbTable]= _Database->connect_db_mapping();
56 // we are in secure code, so create table according to
57 // values from database.
58 if( search(fDb()->list_tables(), "mi_"+sDbTable ) == -1 )
60 fDb()->big_query("create table mi_"+sDbTable+
61 "(k char(255) not null, v text,"+
62 (fDb()->get_database() == "postgres" ?
63 "unique(v))" : "unique(v(60)))"));
64 //FIXME: postgres needs this as:
65 //(k char(255) not null, v text unique)
72 * get a list of all values associated with
73 * @param string key - the key to access
74 * @result mixed value - the datastructure set with `[]= if any
77 mixed get_value(string|int key) {
81 // LOG("search "+sDbTable +" for "+ (string) key);
83 fDb()->big_query("select v from mi_"+sDbTable+
84 " where k like '"+fDb()->quote_index(key)+"'");
85 while (res && (row=res->fetch_row()))
86 d+= ({ unserialize(row[0])});
94 * Add an entry into the list, there is no duplicate check
95 * The serialization of the given value will be stored to the database
96 * @param string key - the key to access
97 * @param mixed value - the value
101 int set_value(string|int key, mixed value)
103 string tbl = "mi_"+sDbTable;
104 string qkey = "'"+fDb()->quote_index((string)key)+"'";
105 string qval = "'"+fDb()->quote_index((string)serialize(value))+"'";
108 if( sizeof(fDb()->query("SELECT k FROM %s WHERE v=%s", tbl, qval)) )
109 fDb()->big_query("UPDATE %s SET k=%s WHERE v=%s", tbl, qkey, qval);
111 fDb()->big_query("INSERT INTO %s VALUES (%s, %s)", tbl, qkey, qval);
118 * delete all entries associated to a key, or a key value pair from the
120 * @param string|int key
121 * @param string|int|void value
122 * @result int - Number of deleted entries
125 int delete(string|int|void key, mixed|void value) {
128 if (!intp(key) && !stringp(key) &&
129 !intp(value) && !stringp(value) && !objectp(value))
131 if (stringp(value) || intp(value))
132 svalue = serialize(value);
133 fDb()->big_query("delete from mi_"+ sDbTable+" where "+
134 ((stringp(key) || intp(key)) ? "k = '" + key + "'" +
135 ((stringp(value) || intp(value)) ? "and v = '" +
136 svalue + "'" : "") : "v = '" + svalue + "'"));
142 * delete all entries associated to a key, or a key value pair from the
144 * @param string|int key
145 * @param string|int|void value
146 * @result int - Number of deleted entries
149 int delete_value(mixed value) {
150 string svalue = serialize(value);
151 fDb()->big_query("delete from mi_"+ sDbTable+
152 " where v = '" + svalue + "'");
153 return fDb()->master_sql->affected_rows();
159 * give a list of all indices (keys) of the database table
161 * @return an array containing the keys
162 * @see maapping.indices
167 fDb()->big_query("select k from mi_"+sDbTable);
168 int sz = res->num_rows();
169 array sIndices = allocate(sz);
172 sIndices[i] = res->fetch_row()[0];