db_n_one._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
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.
7  *
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.
12  *
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
16  *
17  * $Id: db_n_one.pike,v 1.2 2009/05/06 19:23:10 astra Exp $
18  */
19 #include <macros.h>
20 class db_n_one {
21 public:
22 
23 
24 
25 
26 /*
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
30  *
31  */
32 
33 
34 
35 private string sDbTable;
36 private function fDb;
37 
38 string tablename()
39 {
40  return copy_value(sDbTable);
41 }
42 
43 /**
44  * connect a db_n_one table with the according database table
45  * from database.pike
46  *
47  * @param none
48  */
49 protected:
50  final void load_db_mapping()
51 {
52  // get database access function and tablename
53 
54  [fDb , sDbTable]= _Database->connect_db_mapping();
55 
56  // we are in secure code, so create table according to
57  // values from database.
58  if( search(fDb()->list_tables(), "mi_"+sDbTable ) == -1 )
59  {
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)
66  }
67 }
68 
69 public:
70 
71 /**
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
75  */
76 protected:
77  mixed get_value(string|int key) {
78  mixed d = ({});
79  mixed row;
80 
81  // LOG("search "+sDbTable +" for "+ (string) key);
82  Sql.sql_result res =
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])});
87  destruct(res);
88  return d;
89 }
90 
91 public:
92 
93 /**
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
98  * @return 1| throw
99  */
100 protected:
101  int set_value(string|int key, mixed value)
102 {
103  string tbl = "mi_"+sDbTable;
104  string qkey = "'"+fDb()->quote_index((string)key)+"'";
105  string qval = "'"+fDb()->quote_index((string)serialize(value))+"'";
106 
107 
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);
110  else
111  fDb()->big_query("INSERT INTO %s VALUES (%s, %s)", tbl, qkey, qval);
112  return 1;
113 }
114 
115 public:
116 
117 /**
118  * delete all entries associated to a key, or a key value pair from the
119  * database.
120  * @param string|int key
121  * @param string|int|void value
122  * @result int - Number of deleted entries
123  */
124 protected:
125  int delete(string|int|void key, mixed|void value) {
126  string svalue;
127 
128  if (!intp(key) && !stringp(key) &&
129  !intp(value) && !stringp(value) && !objectp(value))
130  return 0;
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 + "'"));
137  return 1;
138 }
139 
140 public:
141 /**
142  * delete all entries associated to a key, or a key value pair from the
143  * database.
144  * @param string|int key
145  * @param string|int|void value
146  * @result int - Number of deleted entries
147  */
148 protected:
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();
154 }
155 
156 public:
157 
158 /**
159  * give a list of all indices (keys) of the database table
160  * @param none
161  * @return an array containing the keys
162  * @see maapping.indices
163  */
164 protected:
165  array index() {
166  Sql.sql_result res =
167  fDb()->big_query("select k from mi_"+sDbTable);
168  int sz = res->num_rows();
169  array sIndices = allocate(sz);
170  int i;
171  for(i=0; i<sz; i++)
172  sIndices[i] = res->fetch_row()[0];
173  return sIndices;
174 }
175 
176 public:
177 
178 
179 
180 };