~ubuntu-branches/ubuntu/wily/steam/wily

« back to all changes in this revision

Viewing changes to server/kernel/db_n_n.pike

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (1.1.4) (0.1.4 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

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_n_n.pike,v 1.1.1.1 2006/03/27 12:40:10 exodusd Exp $
18
 
 */
19
 
 
20
 
constant cvs_version="$Id: db_n_n.pike,v 1.1.1.1 2006/03/27 12:40:10 exodusd Exp $";
21
 
 
22
 
 
23
 
#include <macros.h>
24
 
 
25
 
int get_object_id();
26
 
 
27
 
private static Sql.sql_result oDbResult;
28
 
private static string          sDbTable;
29
 
private static function             fDb;
30
 
 
31
 
string tablename()
32
 
{
33
 
    return copy_value(sDbTable);
34
 
}
35
 
 
36
 
/**
37
 
 * connect a db_mapping with database.pike
38
 
 * @param  none
39
 
 */
40
 
static final void load_db_mapping()
41
 
{
42
 
    // get database access function and tablename
43
 
 
44
 
    [fDb , sDbTable]= _Database->connect_db_mapping();
45
 
    
46
 
    // we are in secure code, so create table according to
47
 
    // values from database.
48
 
    if( search(fDb()->list_tables(), "mi_"+sDbTable ) == -1 ) 
49
 
    {
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)
55
 
    }
56
 
}
57
 
    
58
 
/**
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
62
 
 * @see     set_value
63
 
 */
64
 
static mixed get_value(string|int|object key)
65
 
{
66
 
    mixed d = ({});
67
 
    mixed row;
68
 
 
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()}));
72
 
    
73
 
    key = serialize(key);
74
 
 
75
 
    //    LOG_DB("search "+sDbTable +" for "+ (string) key);
76
 
    Sql.sql_result res =
77
 
        fDb()->big_query("select v from mi_"+sDbTable+
78
 
                         " where k like '"+fDb()->quote(key)+"'");
79
 
 
80
 
    while (res && (row=res->fetch_row()))
81
 
        d+= ({ unserialize(row[0])});
82
 
    destruct(res);
83
 
    return d;
84
 
}
85
 
 
86
 
/**
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
91
 
 */
92
 
static array get_key(string|int|object value)
93
 
{
94
 
    mixed d = ({});
95
 
    mixed row;
96
 
 
97
 
    if (objectp(value) && !IS_PROXY(value))
98
 
        throw(({"Illeagal object given as value to get_key", backtrace()}));
99
 
 
100
 
    string svalue = serialize(value);
101
 
    //    LOG("search "+sDbTable +" for "+ (string) key);
102
 
    Sql.sql_result res =
103
 
        fDb()->big_query("select k from mi_"+sDbTable+
104
 
                         " where v like '"+ fDb()->quote(svalue) +"'");
105
 
 
106
 
    while (res && (row=res->fetch_row()))
107
 
        d+= ({ unserialize(row[0])});
108
 
    destruct(res);
109
 
    return d;
110
 
}
111
 
    
112
 
/**
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.
119
 
 * @return  1| throw
120
 
 */
121
 
static int set_value(mixed keys, mixed values)
122
 
{
123
 
    mixed key;
124
 
    mixed val;
125
 
 
126
 
 
127
 
    LOG_DEBUG("db_n_n.set_value:"+sprintf("%O",keys)+" "+sprintf("%O",values)+"\n");
128
 
    if (zero_type(keys) || zero_type(values))
129
 
        return 0;
130
 
    
131
 
    if (!arrayp(keys))
132
 
        keys = ({ keys });
133
 
    if (!arrayp(values))
134
 
        values = ({ values });
135
 
 
136
 
    delete_value(values);
137
 
 
138
 
    foreach(keys, key)
139
 
    {
140
 
        foreach(values, val)
141
 
        {
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))+"'")))
146
 
            {
147
 
              fDb()->big_query("UPDATE mi_"+sDbTable+ 
148
 
                               " SET v='"+ fDb()->quote(serialize(val))+"'"
149
 
                               " WHERE k='"+fDb()->quote(serialize(key))+"'");
150
 
            }
151
 
            else
152
 
            {
153
 
              fDb()->big_query("INSERT INTO mi_" + sDbTable +
154
 
                               " VALUES('" + fDb()->quote(serialize(key)) +
155
 
                               "', '" + fDb()->quote(serialize(val)) + "')");
156
 
            }
157
 
        }
158
 
    }
159
 
    return 1;
160
 
}
161
 
 
162
 
/**
163
 
 * delete all entries associated to a key, or a key value pair from the
164
 
 * database.
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
170
 
 */
171
 
static int delete(string|int|void|object key, string|int|void|object value)
172
 
{
173
 
    string svalue;
174
 
 
175
 
    if (zero_type(key) && zero_type(key))
176
 
        return 0;
177
 
    if (objectp(key) && (!IS_PROXY(key)))
178
 
        return 0;
179
 
    if (objectp(value) && (!IS_PROXY(value)))
180
 
        return 0;
181
 
 
182
 
    if (!zero_type(value))
183
 
        value = serialize(value);
184
 
    if (!zero_type(key))
185
 
        key = serialize(key);
186
 
 
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 + "'" : "");
190
 
 
191
 
    fDb()->big_query(bquery);
192
 
    return fDb()->master_sql->affected_rows();
193
 
}
194
 
 
195
 
 
196
 
/**
197
 
 * report_delete
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
202
 
 */
203
 
static array(string|int|object)
204
 
report_deleted(string|int|object|void key, string|int|object|void value)
205
 
{
206
 
    string svalue;
207
 
 
208
 
        if (zero_type(key) && zero_type(key))
209
 
        return 0;
210
 
 
211
 
    if (objectp(key) && (!IS_PROXY(key)))
212
 
        return 0;
213
 
    if (objectp(value) && (!IS_PROXY(value)))
214
 
        return 0;
215
 
 
216
 
    if (!zero_type(value))
217
 
        value = serialize(value);
218
 
    if (!zero_type(key))
219
 
        key = serialize(key);
220
 
 
221
 
    string bquery = "mi_"+sDbTable+" where "+
222
 
        (!zero_type(key) ? "k = '" +fDb()->quote(key)+"'" :"") +
223
 
        (!zero_type(value) ? (!zero_type(key) ? "and " :"")+ "v='" +svalue + "'" : "");
224
 
 
225
 
    Sql.sql_result
226
 
        res = fDb()->big_query("select k from "+bquery);
227
 
 
228
 
    array tmp = ({});
229
 
    mixed row;
230
 
    while (res && (row=res->fetch_row()))
231
 
        tmp+= ({ unserialize(row[0]) });
232
 
    
233
 
    fDb()->big_query("delete from "+bquery);
234
 
    return tmp;
235
 
}
236
 
 
237
 
/**
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
242
 
 */
243
 
static int delete_value(int|string|object value)
244
 
{
245
 
 
246
 
    if (objectp(value) && !IS_PROXY(value))
247
 
        throw(({"Illegal Object passed as value to delete_value", backtrace()}));
248
 
 
249
 
    string svalue = serialize(value);
250
 
    fDb()->big_query("delete from mi_"+ sDbTable+
251
 
                     " where v = '" + svalue + "'");
252
 
    return fDb()->master_sql->affected_rows();
253
 
}
254
 
 
255
 
/**
256
 
 * give a list of all indices (keys) of the database table
257
 
 * @param   none
258
 
 * @return  an array containing the keys
259
 
 * @see     maapping.indices
260
 
 */
261
 
static array(string) index()
262
 
{
263
 
    Sql.sql_result res =
264
 
        fDb()->big_query("select k from mi_"+sDbTable);
265
 
    int sz = res->num_rows();
266
 
    array(string) sIndices = allocate(sz);
267
 
    int i;
268
 
    for(i=0; i<sz; i++)
269
 
        sIndices[i] = unserialize(res->fetch_row()[0]);
270
 
    return sIndices;
271
 
}
272
 
 
273
 
string get_table_name() { return (string)get_object_id(); }
274