~ubuntu-branches/ubuntu/natty/steam/natty

« back to all changes in this revision

Viewing changes to server/kernel/db_mapping.pike

  • Committer: Bazaar Package Importer
  • Author(s): Alain Schroeder
  • Date: 2005-05-14 16:33:35 UTC
  • Revision ID: james.westby@ubuntu.com-20050514163335-5v7lbxibmlww15dx
Tags: upstream-1.6.3
ImportĀ upstreamĀ versionĀ 1.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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_mapping.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $
 
18
 */
 
19
 
 
20
constant cvs_version="$Id: db_mapping.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $";
 
21
 
 
22
 
 
23
//! This class simulates a mapping inside the database.
 
24
//! Call get_value() and set_value() functions.
 
25
 
 
26
#include <macros.h>
 
27
 
 
28
private static Sql.sql_result oDbResult;
 
29
private static string          sDbTable;
 
30
//private static mapping            mData;
 
31
private static function             fDb;
 
32
 
 
33
int get_object_id();
 
34
 
 
35
string tablename()
 
36
{
 
37
    return copy_value(sDbTable);
 
38
}
 
39
 
 
40
/**
 
41
 * connect a db_mapping with database.pike
 
42
 */
 
43
static final void load_db_mapping()
 
44
{
 
45
    // get database access function and tablename
 
46
    //    mData = ([]);
 
47
    [fDb , sDbTable]= _Database->connect_db_mapping();
 
48
 
 
49
    // we are in secure code, so create table according to
 
50
    // values from database.
 
51
    if( search(fDb()->list_tables(), "i_"+sDbTable ) == -1 )
 
52
    {
 
53
        fDb()->big_query("create table i_"+sDbTable+
 
54
                        "(k char(255) not null, v text,"+
 
55
                        "UNIQUE(k))");
 
56
    }
 
57
}
 
58
    
 
59
/**
 
60
 * Index Operator for mapping emulation
 
61
 * @param   string key  - the key to access
 
62
 * @result  mixed value - the datastructure set with `[]= if any
 
63
 */
 
64
static mixed get_value(string|int key) {
 
65
    mixed d;
 
66
    mixed row;
 
67
 
 
68
    //    if (d = mData[key])
 
69
    //  return d;
 
70
 
 
71
    //    LOG("db_mapping.get_value("+key+")");
 
72
    Sql.sql_result res =
 
73
        fDb()->big_query("select v from i_"+sDbTable+
 
74
                         " where k = '"+fDb()->quote((string)key)+"'");
 
75
    if (!objectp(res) )
 
76
        return 0;
 
77
    else if ( !(row=res->fetch_row())) {
 
78
        destruct(res);
 
79
        return 0;
 
80
    }
 
81
    //    mData[key] = unserialize(row[0]);
 
82
    destruct(res);
 
83
    //    return mData[key];
 
84
    return unserialize(row[0]);
 
85
}
 
86
    
 
87
/**
 
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
 
93
 */
 
94
static mixed set_value(string|int key, mixed value) {
 
95
    //    mData[key]=value;
 
96
    //write("setting:"+serialize(value)+"\n");
 
97
    if(sizeof(fDb()->query("SELECT k FROM i_"+sDbTable+
 
98
                           " WHERE k='"+fDb()->quote((string)key)+"'")))
 
99
    {
 
100
      fDb()->big_query("UPDATE i_"+sDbTable+
 
101
                       " SET v='"+ fDb()->quote(serialize(value))+ "'"
 
102
                       " WHERE k='"+ fDb()->quote((string)key)+"'");
 
103
    }
 
104
    else
 
105
    {
 
106
      fDb()->big_query("INSERT INTO i_" + sDbTable +
 
107
                       " VALUES('" + fDb()->quote((string)key) + "', '" +
 
108
                       fDb()->quote(serialize(value)) + "')");
 
109
    }
 
110
    return value;
 
111
}
 
112
 
 
113
/**
 
114
 * delete a key from the database mapping emulation.
 
115
 * @param   string|int key
 
116
 * @result  int (0|1) - Number of deleted entries
 
117
 */
 
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();
 
122
}
 
123
 
 
124
/**
 
125
 * select keys from the database like the given expression.
 
126
 * @param   string|int keyexpression
 
127
 * @result  array(int|string)  
 
128
 */
 
129
static array report_delete(string|int key) {
 
130
    mixed aResult = ({});
 
131
    int i, sz;
 
132
    
 
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())
 
137
        return ({ });
 
138
          
 
139
    aResult = allocate(sz=res->num_rows());
 
140
    for (i=0;i<sz;i++)
 
141
        aResult[i] = res->fetch_row()[0];
 
142
 
 
143
    fDb()->big_query("delete from i_"+ sDbTable+" where k like '"+key+"'");
 
144
    //    m_delete(mData, (string) key);
 
145
 
 
146
    return aResult;
 
147
}
 
148
 
 
149
/**
 
150
 * give a list of all indices (keys) of the database table
 
151
 * @param   none
 
152
 * @return  an array containing the keys
 
153
 * @see     maapping.indices
 
154
 */
 
155
array(string) index()
 
156
{
 
157
    //    LOG("getting index()\n");
 
158
    
 
159
    Sql.sql_result res = fDb()->big_query("select k from i_"+sDbTable);
 
160
    //    LOG("done...");
 
161
#if 1
 
162
    int sz = res->num_rows();
 
163
    array(string) sIndices = allocate(sz);
 
164
    int i;
 
165
    for ( i = 0; i < sz; i++ )
 
166
    {
 
167
        string sres = copy_value(res->fetch_row()[0]);
 
168
        sIndices[i] = sres;
 
169
    }
 
170
#else
 
171
    array(string) sIndices = ({}); 
 
172
    array mres;
 
173
    while (mres = res->fetch_row())
 
174
        sIndices+=mres;
 
175
#endif
 
176
    destruct(res);
 
177
    return sIndices;
 
178
}
 
179
 
 
180
string get_table_name() { return (string)get_object_id(); }