Annotation of parser3/src/classes/hash.C, revision 1.55
1.1 paf 1: /** @file
2: Parser: @b hash parser class.
3:
1.54 paf 4: Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.35 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.41 paf 6: */
1.1 paf 7:
1.55 ! paf 8: static const char* IDENT_HASH_C="$Date: 2003/01/21 15:51:06 $";
1.1 paf 9:
10: #include "classes.h"
11: #include "pa_request.h"
12: #include "pa_vhash.h"
1.6 parser 13: #include "pa_vvoid.h"
1.2 parser 14: #include "pa_sql_connection.h"
1.9 parser 15: #include "pa_vtable.h"
1.22 parser 16: #include "pa_vbool.h"
1.26 paf 17: #include "pa_vmethod_frame.h"
1.2 parser 18:
1.1 paf 19: // class
20:
21: class MHash : public Methoded {
1.2 parser 22: public: // VStateless_class
23: Value *create_new_value(Pool& pool) { return new(pool) VHash(pool); }
24:
1.1 paf 25: public:
26: MHash(Pool& pool);
27: public: // Methoded
1.2 parser 28: bool used_directly() { return true; }
1.1 paf 29: };
30:
31: // methods
32:
1.11 parser 33: #ifndef DOXYGEN
1.52 paf 34: class Hash_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.11 parser 35: public:
36: Hash_sql_event_handlers(Pool& apool, const String& amethod_name,
37: const String& astatement_string, const char *astatement_cstr,
1.49 paf 38: bool adistinct,
1.52 paf 39: Hash& arows_hash):
1.11 parser 40: pool(apool),
41: method_name(amethod_name),
42: statement_string(astatement_string),
43: statement_cstr(astatement_cstr),
1.49 paf 44: distinct(adistinct),
1.11 parser 45: rows_hash(arows_hash),
46: columns(pool),
47: row_index(0) {
48: }
1.53 paf 49: bool add_column(SQL_Error& error, void *ptr, size_t size) {
50: try {
51: String *column=new(pool) String(pool);
52: column->APPEND_TAINTED(
53: (const char *)ptr, size,
54: statement_cstr, 0);
55: columns+=column;
56:
57: return false;
58: } catch(...) {
59: error=SQL_Error("exception occured in Hash_sql_event_handlers::add_column");
60: return true;
61: }
1.11 parser 62: }
1.53 paf 63: bool before_rows(SQL_Error& error) {
64: if(columns.size()<=1) {
65: error=SQL_Error("parser.runtime",
1.11 parser 66: &method_name,
67: "column count must be more than 1 to create a hash");
1.53 paf 68: return true;
69: }
70:
71: return false;
1.11 parser 72: }
1.53 paf 73: bool add_row(SQL_Error& /*error*/) {
1.11 parser 74: column_index=0;
1.53 paf 75: return false;
1.11 parser 76: }
1.53 paf 77: bool add_row_cell(SQL_Error& error, void *ptr, size_t size) {
78: try {
79: String *cell=new(pool) String(pool);
80: if(size)
81: cell->APPEND_TAINTED(
82: (const char *)ptr, size,
83: statement_cstr, row_index++);
84: if(column_index==0) {
85: VHash *row_vhash=new(pool) VHash(pool);
86: row_hash=row_vhash->get_hash(0);
87: if(rows_hash.put_dont_replace(*cell, row_vhash)) // put. existed?
88: if(!distinct) {
89: error=SQL_Error("parser.runtime",
90: cell,
91: "duplicate key");
92: return true;
93: }
94: } else
95: row_hash->put(*columns.get_string(column_index), new(pool) VString(*cell));
96: column_index++;
97:
98: return false;
99: } catch(...) {
100: error=SQL_Error("exception occured in Hash_sql_event_handlers::add_row_cell");
101: return true;
102: }
1.11 parser 103: }
104:
105: private:
106: Pool& pool;
107: const String& method_name;
108: const String& statement_string; const char *statement_cstr;
1.49 paf 109: bool distinct;
1.11 parser 110: Hash& rows_hash;
111: Hash *row_hash;
112: int column_index;
113: Array columns;
114: int row_index;
115: };
116: #endif
117:
1.22 parser 118: static void copy_all_overwrite_to(const Hash::Key& key, Hash::Val *value, void *info) {
1.20 parser 119: Hash& dest=*static_cast<Hash *>(info);
120: dest.put(key, value);
121: }
1.55 ! paf 122: static void _create_or_add(Request& r, const String& method_name, MethodParams *params,
! 123: bool is_create) {
1.20 parser 124: Pool& pool=r.pool();
125:
126: if(params->size()) {
1.55 ! paf 127: Value& vb_maybehash=params->as_no_junction(0, "param must be hash");
! 128: if(VHash* vbh=static_cast<VHash*>(vb_maybehash.as("hash", false))) {
! 129: VHash& vah=*static_cast<VHash *>(r.get_self());
! 130:
! 131: Hash& a=vah.hash(&method_name);
! 132: Hash& b=vbh->hash(&method_name);
! 133: b.for_each(copy_all_overwrite_to, &a);
! 134:
! 135: if(is_create)
! 136: vah.set_default(vbh->get_default());
! 137: } else
! 138: throw Exception("parser.runtime",
! 139: &method_name,
! 140: "param must be hash");
! 141:
! 142:
1.20 parser 143: }
144: }
1.55 ! paf 145: static void _create(Request& r, const String& method_name, MethodParams *params) {
! 146: _create_or_add(r, method_name, params, true);
! 147: }
! 148: static void _add(Request& r, const String& method_name, MethodParams *params) {
! 149: _create_or_add(r, method_name, params, false);
! 150: }
! 151:
1.20 parser 152:
1.22 parser 153: static void remove_key_from(const Hash::Key& key, Hash::Val *value, void *info) {
154: Hash& dest=*static_cast<Hash *>(info);
1.25 paf 155: dest.remove(key);
1.22 parser 156: }
157: static void _sub(Request& r, const String& method_name, MethodParams *params) {
158: Pool& pool=r.pool();
159:
160: Value& vb=params->as_no_junction(0, "param must be hash");
1.24 parser 161: if(Hash *b=vb.get_hash(&method_name))
1.50 paf 162: b->for_each(remove_key_from, &static_cast<VHash *>(r.get_self())->hash(&method_name));
1.22 parser 163: }
164:
165: static void copy_all_dontoverwrite_to(const Hash::Key& key, Hash::Val *value, void *info) {
166: Hash& dest=*static_cast<Hash *>(info);
167: dest.put_dont_replace(key, value);
168: }
169: static void _union(Request& r, const String& method_name, MethodParams *params) {
170: Pool& pool=r.pool();
171:
172: // dest = copy of self
1.50 paf 173: Hash& dest=*new(pool) Hash(static_cast<VHash *>(r.get_self())->hash(&method_name));
1.22 parser 174: // dest += b
175: Value& vb=params->as_no_junction(0, "param must be hash");
1.24 parser 176: if(Hash *b=vb.get_hash(&method_name))
1.22 parser 177: b->for_each(copy_all_dontoverwrite_to, &dest);
178:
179: // return result
180: Value& result=*new(pool) VHash(pool, dest);
181: r.write_no_lang(result);
182: }
183:
184: #ifndef DOXYGEN
185: struct Copy_intersection_to_info {
186: Hash *b;
187: Hash *dest;
188: };
189: #endif
190:
191: static void copy_intersection_to(const Hash::Key& key, Hash::Val *value, void *info) {
192: Copy_intersection_to_info& i=*static_cast<Copy_intersection_to_info *>(info);
193:
194: if(i.b->get(key))
195: i.dest->put_dont_replace(key, value);
196: }
197: static void _intersection(Request& r, const String& method_name, MethodParams *params) {
198: Pool& pool=r.pool();
199:
200: // dest = copy of self
201: Hash& dest=*new(pool) Hash(pool);
202: // dest += b
203: Value& vb=params->as_no_junction(0, "param must be hash");
1.24 parser 204: if(Hash *b=vb.get_hash(&method_name)) {
1.22 parser 205: Copy_intersection_to_info info={
206: b,
207: &dest
208: };
1.50 paf 209: static_cast<VHash *>(r.get_self())->hash(&method_name).for_each(copy_intersection_to, &info);
1.22 parser 210: }
211:
212: // return result
1.39 paf 213: r.write_no_lang(*new(pool) VHash(pool, dest));
1.22 parser 214: }
215:
216: static void *intersects(const Hash::Key& key, Hash::Val *value, void *info) {
217: return static_cast<Hash *>(info)->get(key);
218: }
219:
220: static void _intersects(Request& r, const String& method_name, MethodParams *params) {
221: Pool& pool=r.pool();
222:
223: bool yes=false;
224:
225: // dest = copy of self
226: Hash& dest=*new(pool) Hash(pool);
227: // dest += b
228: Value& vb=params->as_no_junction(0, "param must be hash");
1.24 parser 229: if(Hash *b=vb.get_hash(&method_name))
1.50 paf 230: yes=static_cast<VHash *>(r.get_self())->hash(&method_name).first_that(intersects, b)!=0;
1.22 parser 231:
232: // return result
1.39 paf 233: r.write_no_lang(*new(pool) VBool(pool, yes));
1.22 parser 234: }
235:
236:
1.2 parser 237: static void _sql(Request& r, const String& method_name, MethodParams *params) {
238: Pool& pool=r.pool();
239:
1.10 parser 240: Value& statement=params->as_junction(0, "statement must be code");
1.2 parser 241:
242: ulong limit=0;
1.33 paf 243: ulong offset=0;
1.49 paf 244: bool distinct=false;
1.2 parser 245: if(params->size()>1) {
1.33 paf 246: Value& voptions=params->as_no_junction(1, "options must be hash, not code");
1.43 paf 247: if(!voptions.is_string())
1.33 paf 248: if(Hash *options=voptions.get_hash(&method_name)) {
1.49 paf 249: int valid_options=0;
250: if(Value *vlimit=(Value *)options->get(*sql_limit_name)) {
251: valid_options++;
1.37 paf 252: limit=(ulong)r.process_to_value(*vlimit).as_double();
1.49 paf 253: }
254: if(Value *voffset=(Value *)options->get(*sql_offset_name)) {
255: valid_options++;
1.37 paf 256: offset=(ulong)r.process_to_value(*voffset).as_double();
1.49 paf 257: }
258: if(Value *vdistinct=(Value *)options->get(*sql_distinct_name)) {
259: valid_options++;
260: distinct=r.process_to_value(*vdistinct).as_bool();
261: }
262: if(valid_options!=options->size())
263: throw Exception("parser.runtime",
264: &method_name,
265: "called with invalid option");
1.33 paf 266: } else
1.36 paf 267: throw Exception("parser.runtime",
1.33 paf 268: &method_name,
269: "options must be hash");
1.2 parser 270: }
271:
272: Temp_lang temp_lang(r, String::UL_SQL);
1.37 paf 273: const String& statement_string=r.process_to_string(statement);
1.2 parser 274: const char *statement_cstr=
1.32 paf 275: statement_string.cstr(String::UL_UNSPECIFIED, r.connection(&method_name));
1.50 paf 276: Hash& hash=static_cast<VHash *>(r.get_self())->hash(&method_name);
1.11 parser 277: hash.clear();
278: Hash_sql_event_handlers handlers(pool, method_name,
1.49 paf 279: statement_string, statement_cstr,
280: distinct,
281: hash);
1.23 parser 282:
1.32 paf 283: r.connection(&method_name)->query(
1.23 parser 284: statement_cstr, offset, limit,
1.45 paf 285: handlers,
286: statement_string);
1.2 parser 287: }
288:
1.9 parser 289: static void keys_collector(const Hash::Key& key, Hash::Val *value, void *info) {
290: Table& table=*static_cast<Table *>(info);
291: Pool& pool=table.pool();
292:
293: Array& row=*new(pool) Array(pool);
294: row+=&key;
295: table+=&row;
296: }
297: static void _keys(Request& r, const String& method_name, MethodParams *) {
298: Pool& pool=r.pool();
299:
300: Array& columns=*new(pool) Array(pool);
301: columns+=new(pool) String(pool, "key");
302: Table& table=*new(pool) Table(pool, &method_name, &columns);
303:
1.50 paf 304: static_cast<VHash *>(r.get_self())->hash(&method_name).for_each(keys_collector, &table);
1.9 parser 305:
1.39 paf 306: r.write_no_lang(*new(pool) VTable(pool, &table));
1.9 parser 307: }
308:
1.16 parser 309: static void _count(Request& r, const String& method_name, MethodParams *) {
310: Pool& pool=r.pool();
311:
1.39 paf 312: r.write_no_lang(
1.50 paf 313: *new(pool) VInt(pool, static_cast<VHash *>(r.get_self())->hash(&method_name).size()));
1.16 parser 314: }
315:
1.25 paf 316: static void _delete(Request& r, const String& method_name, MethodParams *params) {
317: Pool& pool=r.pool();
318:
1.50 paf 319: static_cast<VHash *>(r.get_self())->hash(&method_name).remove(params->as_string(0, "key must be string"));
1.25 paf 320: }
321:
1.26 paf 322: #ifndef DOXYGEN
323: struct Foreach_info {
324: Request *r;
325: const String* key_var_name;
326: const String* value_var_name;
327: Value *body_code;
328: Value *delim_maybe_code;
329:
330: VString *vkey;
331: bool need_delim;
332: };
333: #endif
334:
335: static void one_foreach_cycle(const Hash::Key& akey, Hash::Val *avalue,
336: void *info) {
337: Foreach_info& i=*static_cast<Foreach_info *>(info);
338:
339: i.vkey->set_string(akey);
1.51 paf 340: Value& ncontext=*i.r->get_method_frame()->caller();
341: ncontext.put_element(*i.key_var_name, i.vkey, false);
342: ncontext.put_element(*i.value_var_name, static_cast<Value *>(avalue), false);
1.26 paf 343:
1.47 paf 344: StringOrValue sv_processed=i.r->process(*i.body_code);
345: const String *s_processed=sv_processed.get_string();
346: if(i.delim_maybe_code && s_processed && s_processed->size()) { // delimiter set and we have body
347: if(i.need_delim) // need delim & iteration produced string?
1.38 paf 348: i.r->write_pass_lang(i.r->process(*i.delim_maybe_code));
1.26 paf 349: i.need_delim=true;
350: }
1.47 paf 351: i.r->write_pass_lang(sv_processed);
1.26 paf 352: }
353: static void _foreach(Request& r, const String& method_name, MethodParams *params) {
354: Pool& pool=r.pool();
355: const String& key_var_name=params->as_string(0, "key-var name must be string");
356: const String& value_var_name=params->as_string(1, "value-var name must be string");
357: Value& body_code=params->as_junction(2, "body must be code");
358: Value *delim_maybe_code=params->size()>3?¶ms->get(3):0;
359:
360: Foreach_info info={
361: &r,
362: &key_var_name, &value_var_name,
363: &body_code,
364: delim_maybe_code,
365:
366: new(pool) VString(pool),
367: false
368: };
1.50 paf 369: VHash& self=*static_cast<VHash *>(r.get_self());
1.28 paf 370: Hash& hash=self.hash(&method_name);
371: VHash_lock lock(self);
372: hash.for_each(one_foreach_cycle, &info);
1.26 paf 373: }
374:
1.1 paf 375: // constructor
376:
1.39 paf 377: MHash::MHash(Pool& apool) : Methoded(apool, "hash")
378: {
1.21 parser 379: // ^hash::create[[copy_from]]
1.55 ! paf 380: add_native_method("create", Method::CT_DYNAMIC, _create, 0, 1);
1.22 parser 381: // ^hash.add[add_from]
1.55 ! paf 382: add_native_method("add", Method::CT_DYNAMIC, _add, 1, 1);
1.22 parser 383: // ^hash.sub[sub_from]
384: add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1);
385: // ^a.union[b] = hash
386: add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1);
387: // ^a.intersection[b] = hash
388: add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 1);
389: // ^a.intersects[b] = bool
390: add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1);
1.25 paf 391:
392: // ^a.delete[key]
393: add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
1.2 parser 394:
1.33 paf 395: // ^hash:sql[query][$.limit(1) $.offset(2)]
396: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.2 parser 397:
1.14 parser 398: // ^hash._keys[]
1.13 parser 399: add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 0);
1.16 parser 400:
401: // ^hash._count[]
402: add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);
1.26 paf 403:
404: // ^hash.foreach[key;value]{code}[delim]
405: add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.1 paf 406: }
407:
408: // global variable
409:
1.40 paf 410: Methoded *hash_class;
1.1 paf 411:
412: // creator
413:
414: Methoded *MHash_create(Pool& pool) {
1.40 paf 415: return hash_class=new(pool) MHash(pool);
1.1 paf 416: }
E-mail: