Annotation of parser3/src/classes/hash.C, revision 1.113
1.1 paf 1: /** @file
2: Parser: @b hash parser class.
3:
1.113 ! moko 4: Copyright (c) 2001-2012 Art. Lebedev Studio (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:
8: #include "classes.h"
1.57 paf 9: #include "pa_vmethod_frame.h"
10:
1.1 paf 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.113 ! moko 19: volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.112 2012-02-28 05:07:44 moko Exp $";
! 20:
1.1 paf 21: // class
22:
1.57 paf 23: class MHash: public Methoded {
1.2 parser 24: public: // VStateless_class
1.103 misha 25: Value* create_new_value(Pool&) { return new VHash(); }
1.2 parser 26:
1.1 paf 27: public:
1.57 paf 28: MHash();
1.1 paf 29: };
30:
1.57 paf 31: // global variable
32:
33: DECLARE_CLASS_VAR(hash, new MHash, 0);
34:
1.1 paf 35: // methods
36:
1.11 parser 37: #ifndef DOXYGEN
1.52 paf 38: class Hash_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.57 paf 39: const String& statement_string; const char* statement_cstr;
40: bool distinct;
41: HashStringValue& rows_hash;
1.86 misha 42: Value* row_value;
1.57 paf 43: int column_index;
1.86 misha 44: ArrayString& columns;
45: bool one_bool_column;
1.67 paf 46: static VBool only_one_column_value;
1.86 misha 47: Table2hash_value_type value_type;
48: int columns_count;
49: public:
50: Table* empty;
1.11 parser 51: public:
1.57 paf 52: Hash_sql_event_handlers(
1.86 misha 53: const String& astatement_string,
54: const char* astatement_cstr,
1.49 paf 55: bool adistinct,
1.86 misha 56: HashStringValue& arows_hash,
57: Table2hash_value_type avalue_type)
58: :
59: statement_string(astatement_string),
60: statement_cstr(astatement_cstr),
1.49 paf 61: distinct(adistinct),
1.11 parser 62: rows_hash(arows_hash),
1.86 misha 63: value_type(avalue_type),
64: row_value(0),
1.67 paf 65: column_index(0),
1.86 misha 66: one_bool_column(false),
67: columns(*new ArrayString),
68: empty(0) {
1.11 parser 69: }
1.86 misha 70:
1.109 misha 71: bool add_column(SQL_Error& error, const char* str, size_t ) {
1.53 paf 72: try {
1.107 moko 73: columns+=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */);
1.53 paf 74: return false;
75: } catch(...) {
76: error=SQL_Error("exception occured in Hash_sql_event_handlers::add_column");
77: return true;
78: }
1.11 parser 79: }
1.86 misha 80:
1.53 paf 81: bool before_rows(SQL_Error& error) {
1.67 paf 82: if(columns.count()<1) {
1.79 misha 83: error=SQL_Error(PARSER_RUNTIME, "no columns");
1.53 paf 84: return true;
85: }
1.86 misha 86: switch(value_type){
87: case C_STRING: {
88: if(columns.count()>2){
89: error=SQL_Error(PARSER_RUNTIME, "only 2 columns allowed for $.type[string].");
90: return true;
91: }
92: }
93: case C_TABLE: {
94: // create empty table which we'll copy later
95: empty=new Table(&columns);
96: columns_count=columns.count();
97: }
98: case C_HASH: {
99: one_bool_column=columns.count()==1;
100: }
101: }
1.53 paf 102: return false;
1.11 parser 103: }
1.86 misha 104:
1.53 paf 105: bool add_row(SQL_Error& /*error*/) {
1.11 parser 106: column_index=0;
1.53 paf 107: return false;
1.11 parser 108: }
1.86 misha 109:
1.109 misha 110: bool add_row_cell(SQL_Error& error, const char *ptr, size_t ) {
1.53 paf 111: try {
1.107 moko 112: String& cell=*new String(ptr, String::L_TAINTED /* no length as 0x00 can be inside */);
1.86 misha 113:
1.69 paf 114: bool duplicate=false;
1.86 misha 115: if(one_bool_column) {
1.69 paf 116: duplicate=rows_hash.put_dont_replace(cell, &only_one_column_value); // put. existed?
117: } else if(column_index==0) {
1.86 misha 118: switch(value_type){
119: case C_HASH: {
120: VHash* row_vhash=new VHash;
121: row_value=row_vhash;
122: duplicate=rows_hash.put_dont_replace(cell, row_vhash); // put. existed?
123: break;
124: }
125: case C_STRING: {
126: VString* row_vstring=new VString();
127: row_value=row_vstring;
128: duplicate=rows_hash.put_dont_replace(cell, row_vstring); // put. existed?
129: break;
130: }
131: case C_TABLE: {
132: VTable* vtable=(VTable*)rows_hash.get(cell);
133: Table* table;
134:
135: if(vtable) { // table with this key exist?
136: if(!distinct) {
137: duplicate=true;
138: break;
139: }
140: table=vtable->get_table();
141: } else {
142: // no? creating table of same structure as source
143: Table::Action_options table_options(0, 0);
144: table=new Table(*empty, table_options/*no rows, just structure*/);
145: vtable=new VTable(table);
146: rows_hash.put(cell, vtable); // put
147: }
148: ArrayString* row=new ArrayString(columns_count);
149: row_value=(Value*)row;
150: *row+=&cell;
151: *table+=row;
152: break;
153: }
154: }
155: } else {
156: switch(value_type) {
157: case C_HASH: {
158: row_value->get_hash()->put(*columns[column_index], new VString(cell));
159: break;
160: }
161: case C_STRING: {
162: VString* row_string=(VString*)row_value;
163: row_string->set_string(cell);
164: break;
165: }
166: case C_TABLE: {
167: ArrayString* row=(ArrayString*)row_value;
168: *row+=&cell;
169: break;
170: }
171: }
172: }
1.69 paf 173:
174: if(duplicate & !distinct) {
1.79 misha 175: error=SQL_Error(PARSER_RUNTIME, "duplicate key");
1.69 paf 176: return true;
177: }
178:
1.53 paf 179: column_index++;
180: return false;
181: } catch(...) {
182: error=SQL_Error("exception occured in Hash_sql_event_handlers::add_row_cell");
183: return true;
184: }
1.11 parser 185: }
186:
187: };
1.67 paf 188: VBool Hash_sql_event_handlers::only_one_column_value(true);
189:
1.11 parser 190: #endif
191:
1.57 paf 192: static void _create_or_add(Request& r, MethodParams& params) {
193: if(params.count()) {
1.111 misha 194: Value& vsrc=params.as_no_junction(0, PARAM_MUST_BE_HASH);
1.66 paf 195: if(HashStringValue* src=vsrc.get_hash()) {
1.72 paf 196: VHash& self=GET_SELF(r, VHash);
197: HashStringValue* self_hash=&(self.hash());
198: if(src==self_hash) // same: doing nothing
1.66 paf 199: return;
1.77 paf 200: src->for_each<HashStringValue*>(copy_all_overwrite_to, self_hash);
1.72 paf 201:
1.103 misha 202: if(VHash* vhash_src=static_cast<VHash*>(vsrc.as(VHASH_TYPE)))
1.78 paf 203: if(Value* vdefault=vhash_src->get_default())
204: if(vdefault->is_defined())
205: self.set_default(vdefault);
1.66 paf 206: }
1.20 parser 207: }
208: }
1.22 parser 209:
1.57 paf 210: static void _sub(Request& r, MethodParams& params) {
1.111 misha 211: if(HashStringValue* src=params.as_no_junction(0, PARAM_MUST_BE_HASH).get_hash()) {
1.66 paf 212: HashStringValue* self=&(GET_SELF(r, VHash).hash());
213: if(src==self) { // same: clearing
214: self->clear();
215: return;
216: }
1.77 paf 217: src->for_each<HashStringValue*>(remove_key_from, self);
1.66 paf 218: }
1.57 paf 219: }
220:
221: static void copy_all_dontoverwrite_to(
1.100 misha 222: HashStringValue::key_type key,
223: HashStringValue::value_type value,
224: HashStringValue* dest) {
1.57 paf 225: dest->put_dont_replace(key, value);
1.22 parser 226: }
1.57 paf 227: static void _union(Request& r, MethodParams& params) {
1.22 parser 228: // dest = copy of self
1.57 paf 229: Value& result=*new VHash(GET_SELF(r, VHash).hash());
1.22 parser 230: // dest += b
1.111 misha 231: if(HashStringValue* src=params.as_no_junction(0, PARAM_MUST_BE_HASH).get_hash())
1.77 paf 232: src->for_each<HashStringValue*>(copy_all_dontoverwrite_to, result.get_hash());
1.22 parser 233:
234: // return result
235: r.write_no_lang(result);
236: }
237:
238: #ifndef DOXYGEN
239: struct Copy_intersection_to_info {
1.57 paf 240: HashStringValue* b;
241: HashStringValue* dest;
1.22 parser 242: };
243: #endif
1.57 paf 244: static void copy_intersection_to(
1.100 misha 245: HashStringValue::key_type key,
246: HashStringValue::value_type value,
247: Copy_intersection_to_info *info) {
1.57 paf 248: if(info->b->get(key))
249: info->dest->put_dont_replace(key, value);
1.22 parser 250: }
1.57 paf 251: static void _intersection(Request& r, MethodParams& params) {
252: Value& result=*new VHash;
1.22 parser 253: // dest += b
1.111 misha 254: if(HashStringValue* b=params.as_no_junction(0, PARAM_MUST_BE_HASH).get_hash()) {
1.57 paf 255: Copy_intersection_to_info info={b, result.get_hash()};
1.77 paf 256: GET_SELF(r, VHash).hash().for_each<Copy_intersection_to_info*>(copy_intersection_to, &info);
1.22 parser 257: }
258:
259: // return result
1.57 paf 260: r.write_no_lang(result);
1.22 parser 261: }
262:
1.57 paf 263: static bool intersects(
1.100 misha 264: HashStringValue::key_type key,
265: HashStringValue::value_type /*value*/,
266: HashStringValue* b) {
1.57 paf 267: return b->get(key)!=0;
1.22 parser 268: }
269:
1.57 paf 270: static void _intersects(Request& r, MethodParams& params) {
271: bool result=false;
272:
1.111 misha 273: if(HashStringValue* b=params.as_no_junction(0, PARAM_MUST_BE_HASH).get_hash())
1.77 paf 274: result=GET_SELF(r, VHash).hash().first_that<HashStringValue*>(intersects, b)!=0;
1.22 parser 275:
276: // return result
1.89 misha 277: r.write_no_lang(VBool::get(result));
1.22 parser 278: }
279:
280:
1.71 paf 281: extern String sql_bind_name;
1.57 paf 282: extern String sql_limit_name;
283: extern String sql_offset_name;
284: extern String sql_default_name;
285: extern String sql_distinct_name;
1.81 misha 286: extern String sql_value_type_name;
287: extern Table2hash_value_type get_value_type(Value& vvalue_type);
1.71 paf 288: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
289: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
290:
1.57 paf 291: static void _sql(Request& r, MethodParams& params) {
292: Value& statement=params.as_junction(0, "statement must be code");
1.2 parser 293:
1.71 paf 294: HashStringValue* bind=0;
1.87 misha 295: ulong limit=SQL_NO_LIMIT;
1.33 paf 296: ulong offset=0;
1.49 paf 297: bool distinct=false;
1.81 misha 298: Table2hash_value_type value_type=C_HASH;
1.110 misha 299: if(params.count()>1)
300: if(HashStringValue* options=params.as_hash(1)) {
301: int valid_options=0;
302: if(Value* vbind=options->get(sql_bind_name)) {
303: valid_options++;
304: bind=vbind->get_hash();
305: }
306: if(Value* vlimit=options->get(sql_limit_name)) {
307: valid_options++;
308: limit=(ulong)r.process_to_value(*vlimit).as_double();
309: }
310: if(Value* voffset=options->get(sql_offset_name)) {
311: valid_options++;
312: offset=(ulong)r.process_to_value(*voffset).as_double();
313: }
314: if(Value* vdistinct=options->get(sql_distinct_name)) {
315: valid_options++;
316: distinct=r.process_to_value(*vdistinct).as_bool();
317: }
318: if(Value* vvalue_type=options->get(sql_value_type_name)) {
319: valid_options++;
320: value_type=get_value_type(r.process_to_value(*vvalue_type));
321: }
322: if(valid_options!=options->count())
323: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
324: }
1.2 parser 325:
1.71 paf 326: SQL_Driver::Placeholder* placeholders=0;
327: uint placeholders_count=0;
328: if(bind)
329: placeholders_count=marshal_binds(*bind, placeholders);
330:
1.57 paf 331: Temp_lang temp_lang(r, String::L_SQL);
1.37 paf 332: const String& statement_string=r.process_to_string(statement);
1.99 misha 333: const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection());
1.97 misha 334:
1.57 paf 335: HashStringValue& hash=GET_SELF(r, VHash).hash();
1.11 parser 336: hash.clear();
1.57 paf 337: Hash_sql_event_handlers handlers(
1.49 paf 338: statement_string, statement_cstr,
339: distinct,
1.86 misha 340: hash,
341: value_type);
1.87 misha 342:
1.57 paf 343: r.connection()->query(
1.70 paf 344: statement_cstr,
1.73 paf 345: placeholders_count, placeholders,
1.70 paf 346: offset, limit,
1.45 paf 347: handlers,
348: statement_string);
1.71 paf 349:
350: if(bind)
351: unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.2 parser 352: }
353:
1.57 paf 354: static void keys_collector(
1.100 misha 355: HashStringValue::key_type key,
356: HashStringValue::value_type,
357: Table *table) {
358: Table::element_type row(new ArrayString(1));
1.57 paf 359: *row+=new String(key, String::L_TAINTED);
360: *table+=row;
1.9 parser 361: }
1.68 paf 362: static void _keys(Request& r, MethodParams& params) {
363: const String* keys_column_name;
364: if(params.count()>0)
1.81 misha 365: keys_column_name=¶ms.as_string(0, COLUMN_NAME_MUST_BE_STRING);
1.68 paf 366: else
367: keys_column_name=new String("key");
368:
1.101 misha 369: Table::columns_type columns(new ArrayString(1));
1.68 paf 370: *columns+=keys_column_name;
1.57 paf 371: Table* table=new Table(columns);
1.9 parser 372:
1.77 paf 373: GET_SELF(r, VHash).hash().for_each<Table*>(keys_collector, table);
1.9 parser 374:
1.57 paf 375: r.write_no_lang(*new VTable(table));
1.9 parser 376: }
377:
1.57 paf 378: static void _count(Request& r, MethodParams&) {
379: r.write_no_lang(*new VInt(GET_SELF(r, VHash).hash().count()));
1.16 parser 380: }
381:
1.57 paf 382: static void _delete(Request& r, MethodParams& params) {
1.25 paf 383:
1.57 paf 384: GET_SELF(r, VHash).hash().remove(params.as_string(0, "key must be string"));
1.25 paf 385: }
386:
1.82 misha 387: static void _contains(Request& r, MethodParams& params) {
1.89 misha 388: bool result=GET_SELF(r, VHash).hash().contains(params.as_string(0, "key must be string"));
389: r.write_no_lang(VBool::get(result));
1.80 misha 390: }
391:
1.26 paf 392: #ifndef DOXYGEN
1.59 paf 393: struct Foreach_info {
1.26 paf 394: Request *r;
395: const String* key_var_name;
396: const String* value_var_name;
1.57 paf 397: Value* body_code;
398: Value* delim_maybe_code;
1.26 paf 399:
1.84 misha 400: Value* var_context;
1.26 paf 401: bool need_delim;
402: };
403: #endif
1.90 misha 404: static bool one_foreach_cycle(
405: HashStringValue::key_type akey,
406: HashStringValue::value_type avalue,
407: Foreach_info *info) {
1.84 misha 408: Value& var_context=*info->var_context;
1.85 misha 409: if(info->key_var_name){
1.96 misha 410: VString* vkey=new VString(*new String(akey, String::L_TAINTED));
1.103 misha 411: var_context.put_element(*info->key_var_name, vkey, false);
1.85 misha 412: }
413: if(info->value_var_name)
1.103 misha 414: var_context.put_element(*info->value_var_name, avalue, false);
1.57 paf 415:
1.91 misha 416: if(info->delim_maybe_code){ // delimiter set
1.90 misha 417: StringOrValue sv_processed=info->r->process(*info->body_code);
418: Request::Skip lskip=info->r->get_skip(); info->r->set_skip(Request::SKIP_NOTHING);
419:
420: const String* s_processed=sv_processed.get_string();
1.91 misha 421: if(s_processed && !s_processed->is_empty()) { // we have body
1.90 misha 422: if(info->need_delim) // need delim & iteration produced string?
423: info->r->write_pass_lang(info->r->process(*info->delim_maybe_code));
424: else
425: info->need_delim=true;
426: }
427: info->r->write_pass_lang(sv_processed);
428: return lskip==Request::SKIP_BREAK;
429: } else {
430: info->r->process_write(*info->body_code);
431: Request::Skip lskip=info->r->get_skip(); info->r->set_skip(Request::SKIP_NOTHING);
432: return lskip==Request::SKIP_BREAK;
1.26 paf 433: }
434: }
1.57 paf 435: static void _foreach(Request& r, MethodParams& params) {
1.102 misha 436: InCycle temp(r);
1.77 paf 437:
1.85 misha 438: const String& key_var_name=params.as_string(0, "key-var name must be string");
439: const String& value_var_name=params.as_string(1, "value-var name must be string");
440:
1.63 paf 441: Foreach_info info={
442: &r,
1.85 misha 443: key_var_name.is_empty()? 0 : &key_var_name,
444: value_var_name.is_empty()? 0 : &value_var_name,
1.63 paf 445: ¶ms.as_junction(2, "body must be code"),
1.84 misha 446: /*delimiter*/params.count()>3?params.get(3):0,
447: /*var_context*/r.get_method_frame()->caller(),
1.85 misha 448: false
1.63 paf 449: };
1.57 paf 450:
451: VHash& self=GET_SELF(r, VHash);
452: HashStringValue& hash=self.hash();
1.28 paf 453: VHash_lock lock(self);
1.77 paf 454: hash.first_that<Foreach_info*>(one_foreach_cycle, &info);
1.26 paf 455: }
456:
1.105 misha 457: static void _at(Request& r, MethodParams& params) {
458: HashStringValue& hash=GET_SELF(r, VHash).hash();
459: size_t count=hash.count();
460:
461: int pos=0;
462:
463: Value& vwhence=*params.get(0);
464: if(vwhence.is_string()){
465: const String& swhence=*vwhence.get_string();
466: if(swhence == "last")
467: pos=count-1;
468: else if(swhence != "first")
469: throw Exception(PARSER_RUNTIME,
470: &swhence,
471: "whence must be 'first', 'last' or expression");
472: } else {
473: pos=r.process_to_value(vwhence).as_int();
474: if(pos < 0)
475: pos+=count;
476: }
477:
1.106 misha 478: if(count && pos >= 0 && (size_t)pos < count){
1.105 misha 479: if(pos == 0)
480: r.write_assign_lang(*hash.first_value());
1.106 misha 481: else if((size_t)pos == count-1)
1.105 misha 482: r.write_assign_lang(*hash.last_value());
483: else
484: for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
485: if(!pos){
486: r.write_assign_lang(*i.value());
487: break;
488: }
489: }
490:
491: }
492:
1.1 paf 493: // constructor
494:
1.57 paf 495: MHash::MHash(): Methoded("hash")
1.39 paf 496: {
1.21 parser 497: // ^hash::create[[copy_from]]
1.57 paf 498: add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1);
1.22 parser 499: // ^hash.add[add_from]
1.57 paf 500: add_native_method("add", Method::CT_DYNAMIC, _create_or_add, 1, 1);
1.22 parser 501: // ^hash.sub[sub_from]
502: add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1);
503: // ^a.union[b] = hash
504: add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1);
505: // ^a.intersection[b] = hash
506: add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 1);
507: // ^a.intersects[b] = bool
508: add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1);
1.25 paf 509:
510: // ^a.delete[key]
511: add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
1.2 parser 512:
1.82 misha 513: // ^a.contains[key]
514: add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1);
515: // backward
516: add_native_method("contain", Method::CT_DYNAMIC, _contains, 1, 1);
1.80 misha 517:
518: // ^hash::sql[query][options hash]
1.33 paf 519: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.2 parser 520:
1.68 paf 521: // ^hash._keys[[column name]]
522: add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1);
1.16 parser 523:
524: // ^hash._count[]
525: add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);
1.26 paf 526:
527: // ^hash.foreach[key;value]{code}[delim]
528: add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.105 misha 529:
530: // ^hash._at[first|last]
531: // ^hash._at([-]offset)
532: add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 1);
1.1 paf 533: }
E-mail: