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_mapping.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $
20
constant cvs_version="$Id: db_mapping.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $";
23
//! This class simulates a mapping inside the database.
24
//! Call get_value() and set_value() functions.
28
private static Sql.sql_result oDbResult;
29
private static string sDbTable;
30
//private static mapping mData;
31
private static function fDb;
37
return copy_value(sDbTable);
41
* connect a db_mapping with database.pike
43
static final void load_db_mapping()
45
// get database access function and tablename
47
[fDb , sDbTable]= _Database->connect_db_mapping();
49
// we are in secure code, so create table according to
50
// values from database.
51
if( search(fDb()->list_tables(), "i_"+sDbTable ) == -1 )
53
fDb()->big_query("create table i_"+sDbTable+
54
"(k char(255) not null, v text,"+
60
* Index Operator for mapping emulation
61
* @param string key - the key to access
62
* @result mixed value - the datastructure set with `[]= if any
64
static mixed get_value(string|int key) {
68
// if (d = mData[key])
71
// LOG("db_mapping.get_value("+key+")");
73
fDb()->big_query("select v from i_"+sDbTable+
74
" where k = '"+fDb()->quote((string)key)+"'");
77
else if ( !(row=res->fetch_row())) {
81
// mData[key] = unserialize(row[0]);
84
return unserialize(row[0]);
88
* Write Index Operator for mapping emulation
89
* The serialization of the given value will be stored to the database
90
* @param string key - the key to access
91
* @param mixed value - the value
92
* @return value| throw
94
static mixed set_value(string|int key, mixed value) {
96
//write("setting:"+serialize(value)+"\n");
97
if(sizeof(fDb()->query("SELECT k FROM i_"+sDbTable+
98
" WHERE k='"+fDb()->quote((string)key)+"'")))
100
fDb()->big_query("UPDATE i_"+sDbTable+
101
" SET v='"+ fDb()->quote(serialize(value))+ "'"
102
" WHERE k='"+ fDb()->quote((string)key)+"'");
106
fDb()->big_query("INSERT INTO i_" + sDbTable +
107
" VALUES('" + fDb()->quote((string)key) + "', '" +
108
fDb()->quote(serialize(value)) + "')");
114
* delete a key from the database mapping emulation.
115
* @param string|int key
116
* @result int (0|1) - Number of deleted entries
118
static int delete(string|int key) {
119
fDb()->big_query("delete from i_"+ sDbTable+" where k like '"+key+"'");
120
// m_delete(mData, (string) key);
121
return fDb()->master_sql->affected_rows();
125
* select keys from the database like the given expression.
126
* @param string|int keyexpression
127
* @result array(int|string)
129
static array report_delete(string|int key) {
130
mixed aResult = ({});
133
object handle = fDb();
134
Sql.sql_result res = handle->big_query("select k from i_"+ sDbTable +
135
" where k like '"+ key+"'");
136
if (!res || !res->num_rows())
139
aResult = allocate(sz=res->num_rows());
141
aResult[i] = res->fetch_row()[0];
143
fDb()->big_query("delete from i_"+ sDbTable+" where k like '"+key+"'");
144
// m_delete(mData, (string) key);
150
* give a list of all indices (keys) of the database table
152
* @return an array containing the keys
153
* @see maapping.indices
155
array(string) index()
157
// LOG("getting index()\n");
159
Sql.sql_result res = fDb()->big_query("select k from i_"+sDbTable);
162
int sz = res->num_rows();
163
array(string) sIndices = allocate(sz);
165
for ( i = 0; i < sz; i++ )
167
string sres = copy_value(res->fetch_row()[0]);
171
array(string) sIndices = ({});
173
while (mres = res->fetch_row())
180
string get_table_name() { return (string)get_object_id(); }