Annotation of parser3/src/classes/hash.C, revision 1.120
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.120 ! misha 19: volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.119 2015/01/11 04:15:06 misha Exp $";
1.113 moko 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.116 misha 110: bool add_row_cell(SQL_Error& error, const char *str, size_t ) {
1.53 paf 111: try {
1.116 misha 112: const String& cell=str?*new String(str, String::L_TAINTED /* no length as 0x00 can be inside */):String::Empty;
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.115 moko 195: VHash& self=GET_SELF(r, VHash);
196: HashStringValue* src_hash;
197: HashStringValue* self_hash=&(self.hash());
198:
199: if(VHash* src=static_cast<VHash*>(vsrc.as(VHASH_TYPE))) {
200: src_hash=&(src->hash_ro());
201:
202: if(src_hash==self_hash) // same: doing nothing
1.66 paf 203: return;
1.72 paf 204:
1.115 moko 205: if(Value* vdefault=src->get_default())
206: if(vdefault->is_defined())
207: self.set_default(vdefault);
208: } else {
209: src_hash=vsrc.get_hash();
1.66 paf 210: }
1.115 moko 211:
212: if(src_hash)
213: src_hash->for_each<HashStringValue*>(copy_all_overwrite_to, self_hash);
1.20 parser 214: }
215: }
1.22 parser 216:
1.57 paf 217: static void _sub(Request& r, MethodParams& params) {
1.114 misha 218: if(HashStringValue* src=params.as_hash(0, "param")) {
1.66 paf 219: HashStringValue* self=&(GET_SELF(r, VHash).hash());
220: if(src==self) { // same: clearing
221: self->clear();
222: return;
223: }
1.77 paf 224: src->for_each<HashStringValue*>(remove_key_from, self);
1.66 paf 225: }
1.57 paf 226: }
227:
228: static void copy_all_dontoverwrite_to(
1.100 misha 229: HashStringValue::key_type key,
230: HashStringValue::value_type value,
231: HashStringValue* dest) {
1.57 paf 232: dest->put_dont_replace(key, value);
1.22 parser 233: }
1.57 paf 234: static void _union(Request& r, MethodParams& params) {
1.22 parser 235: // dest = copy of self
1.57 paf 236: Value& result=*new VHash(GET_SELF(r, VHash).hash());
1.22 parser 237: // dest += b
1.114 misha 238: if(HashStringValue* src=params.as_hash(0, "param"))
1.77 paf 239: src->for_each<HashStringValue*>(copy_all_dontoverwrite_to, result.get_hash());
1.22 parser 240:
241: // return result
242: r.write_no_lang(result);
243: }
244:
245: #ifndef DOXYGEN
246: struct Copy_intersection_to_info {
1.57 paf 247: HashStringValue* b;
248: HashStringValue* dest;
1.22 parser 249: };
250: #endif
1.57 paf 251: static void copy_intersection_to(
1.100 misha 252: HashStringValue::key_type key,
253: HashStringValue::value_type value,
254: Copy_intersection_to_info *info) {
1.57 paf 255: if(info->b->get(key))
256: info->dest->put_dont_replace(key, value);
1.22 parser 257: }
1.57 paf 258: static void _intersection(Request& r, MethodParams& params) {
259: Value& result=*new VHash;
1.22 parser 260: // dest += b
1.114 misha 261: if(HashStringValue* b=params.as_hash(0, "param")) {
1.57 paf 262: Copy_intersection_to_info info={b, result.get_hash()};
1.77 paf 263: GET_SELF(r, VHash).hash().for_each<Copy_intersection_to_info*>(copy_intersection_to, &info);
1.22 parser 264: }
265:
266: // return result
1.57 paf 267: r.write_no_lang(result);
1.22 parser 268: }
269:
1.57 paf 270: static bool intersects(
1.100 misha 271: HashStringValue::key_type key,
272: HashStringValue::value_type /*value*/,
273: HashStringValue* b) {
1.57 paf 274: return b->get(key)!=0;
1.22 parser 275: }
276:
1.57 paf 277: static void _intersects(Request& r, MethodParams& params) {
278: bool result=false;
279:
1.119 misha 280: if(HashStringValue* b=params.as_hash(0, "param")) {
281: HashStringValue* self=&(GET_SELF(r, VHash).hash());
1.120 ! misha 282: if(b==self) {
! 283: r.write_no_lang(VBool::get(true));
! 284: return;
! 285: }
1.119 misha 286: result=self->first_that<HashStringValue*>(intersects, b)!=0;
287: }
1.22 parser 288:
289: // return result
1.89 misha 290: r.write_no_lang(VBool::get(result));
1.22 parser 291: }
292:
293:
1.71 paf 294: extern String sql_bind_name;
1.57 paf 295: extern String sql_limit_name;
296: extern String sql_offset_name;
297: extern String sql_default_name;
298: extern String sql_distinct_name;
1.81 misha 299: extern String sql_value_type_name;
300: extern Table2hash_value_type get_value_type(Value& vvalue_type);
1.71 paf 301: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
302: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
303:
1.57 paf 304: static void _sql(Request& r, MethodParams& params) {
305: Value& statement=params.as_junction(0, "statement must be code");
1.2 parser 306:
1.71 paf 307: HashStringValue* bind=0;
1.87 misha 308: ulong limit=SQL_NO_LIMIT;
1.33 paf 309: ulong offset=0;
1.49 paf 310: bool distinct=false;
1.81 misha 311: Table2hash_value_type value_type=C_HASH;
1.110 misha 312: if(params.count()>1)
1.114 misha 313: if(HashStringValue* options=params.as_hash(1, "sql options")) {
1.110 misha 314: int valid_options=0;
315: if(Value* vbind=options->get(sql_bind_name)) {
316: valid_options++;
317: bind=vbind->get_hash();
318: }
319: if(Value* vlimit=options->get(sql_limit_name)) {
320: valid_options++;
321: limit=(ulong)r.process_to_value(*vlimit).as_double();
322: }
323: if(Value* voffset=options->get(sql_offset_name)) {
324: valid_options++;
325: offset=(ulong)r.process_to_value(*voffset).as_double();
326: }
327: if(Value* vdistinct=options->get(sql_distinct_name)) {
328: valid_options++;
329: distinct=r.process_to_value(*vdistinct).as_bool();
330: }
331: if(Value* vvalue_type=options->get(sql_value_type_name)) {
332: valid_options++;
333: value_type=get_value_type(r.process_to_value(*vvalue_type));
334: }
335: if(valid_options!=options->count())
336: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
337: }
1.2 parser 338:
1.71 paf 339: SQL_Driver::Placeholder* placeholders=0;
340: uint placeholders_count=0;
341: if(bind)
342: placeholders_count=marshal_binds(*bind, placeholders);
343:
1.57 paf 344: Temp_lang temp_lang(r, String::L_SQL);
1.37 paf 345: const String& statement_string=r.process_to_string(statement);
1.99 misha 346: const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection());
1.97 misha 347:
1.57 paf 348: HashStringValue& hash=GET_SELF(r, VHash).hash();
1.11 parser 349: hash.clear();
1.57 paf 350: Hash_sql_event_handlers handlers(
1.49 paf 351: statement_string, statement_cstr,
352: distinct,
1.86 misha 353: hash,
354: value_type);
1.87 misha 355:
1.57 paf 356: r.connection()->query(
1.70 paf 357: statement_cstr,
1.73 paf 358: placeholders_count, placeholders,
1.70 paf 359: offset, limit,
1.45 paf 360: handlers,
361: statement_string);
1.71 paf 362:
363: if(bind)
364: unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.2 parser 365: }
366:
1.57 paf 367: static void keys_collector(
1.100 misha 368: HashStringValue::key_type key,
369: HashStringValue::value_type,
370: Table *table) {
371: Table::element_type row(new ArrayString(1));
1.57 paf 372: *row+=new String(key, String::L_TAINTED);
373: *table+=row;
1.9 parser 374: }
1.68 paf 375: static void _keys(Request& r, MethodParams& params) {
376: const String* keys_column_name;
377: if(params.count()>0)
1.81 misha 378: keys_column_name=¶ms.as_string(0, COLUMN_NAME_MUST_BE_STRING);
1.68 paf 379: else
380: keys_column_name=new String("key");
381:
1.101 misha 382: Table::columns_type columns(new ArrayString(1));
1.68 paf 383: *columns+=keys_column_name;
1.57 paf 384: Table* table=new Table(columns);
1.9 parser 385:
1.115 moko 386: GET_SELF(r, VHash).hash_ro().for_each<Table*>(keys_collector, table);
1.9 parser 387:
1.57 paf 388: r.write_no_lang(*new VTable(table));
1.9 parser 389: }
390:
1.57 paf 391: static void _count(Request& r, MethodParams&) {
1.115 moko 392: r.write_no_lang(*new VInt(GET_SELF(r, VHash).hash_ro().count()));
1.16 parser 393: }
394:
1.57 paf 395: static void _delete(Request& r, MethodParams& params) {
1.119 misha 396: if(params.count()>0)
397: GET_SELF(r, VHash).hash().remove(params.as_string(0, "key must be string"));
398: else
399: GET_SELF(r, VHash).hash().clear();
1.25 paf 400: }
401:
1.82 misha 402: static void _contains(Request& r, MethodParams& params) {
1.115 moko 403: bool result=GET_SELF(r, VHash).hash_ro().contains(params.as_string(0, "key must be string"));
1.89 misha 404: r.write_no_lang(VBool::get(result));
1.80 misha 405: }
406:
1.26 paf 407: #ifndef DOXYGEN
1.59 paf 408: struct Foreach_info {
1.26 paf 409: Request *r;
410: const String* key_var_name;
411: const String* value_var_name;
1.57 paf 412: Value* body_code;
413: Value* delim_maybe_code;
1.26 paf 414:
1.84 misha 415: Value* var_context;
1.26 paf 416: bool need_delim;
417: };
418: #endif
1.90 misha 419: static bool one_foreach_cycle(
420: HashStringValue::key_type akey,
421: HashStringValue::value_type avalue,
422: Foreach_info *info) {
1.84 misha 423: Value& var_context=*info->var_context;
1.85 misha 424: if(info->key_var_name){
1.96 misha 425: VString* vkey=new VString(*new String(akey, String::L_TAINTED));
1.118 moko 426: info->r->put_element(var_context, *info->key_var_name, vkey);
1.85 misha 427: }
428: if(info->value_var_name)
1.118 moko 429: info->r->put_element(var_context, *info->value_var_name, avalue);
1.57 paf 430:
1.91 misha 431: if(info->delim_maybe_code){ // delimiter set
1.90 misha 432: StringOrValue sv_processed=info->r->process(*info->body_code);
433: Request::Skip lskip=info->r->get_skip(); info->r->set_skip(Request::SKIP_NOTHING);
434:
435: const String* s_processed=sv_processed.get_string();
1.91 misha 436: if(s_processed && !s_processed->is_empty()) { // we have body
1.90 misha 437: if(info->need_delim) // need delim & iteration produced string?
438: info->r->write_pass_lang(info->r->process(*info->delim_maybe_code));
439: else
440: info->need_delim=true;
441: }
442: info->r->write_pass_lang(sv_processed);
443: return lskip==Request::SKIP_BREAK;
444: } else {
445: info->r->process_write(*info->body_code);
446: Request::Skip lskip=info->r->get_skip(); info->r->set_skip(Request::SKIP_NOTHING);
447: return lskip==Request::SKIP_BREAK;
1.26 paf 448: }
449: }
1.57 paf 450: static void _foreach(Request& r, MethodParams& params) {
1.102 misha 451: InCycle temp(r);
1.77 paf 452:
1.85 misha 453: const String& key_var_name=params.as_string(0, "key-var name must be string");
454: const String& value_var_name=params.as_string(1, "value-var name must be string");
455:
1.63 paf 456: Foreach_info info={
457: &r,
1.85 misha 458: key_var_name.is_empty()? 0 : &key_var_name,
459: value_var_name.is_empty()? 0 : &value_var_name,
1.63 paf 460: ¶ms.as_junction(2, "body must be code"),
1.84 misha 461: /*delimiter*/params.count()>3?params.get(3):0,
462: /*var_context*/r.get_method_frame()->caller(),
1.85 misha 463: false
1.63 paf 464: };
1.57 paf 465:
466: VHash& self=GET_SELF(r, VHash);
1.115 moko 467: HashStringValue& hash=self.hash_ro();
1.28 paf 468: VHash_lock lock(self);
1.77 paf 469: hash.first_that<Foreach_info*>(one_foreach_cycle, &info);
1.26 paf 470: }
471:
1.105 misha 472: static void _at(Request& r, MethodParams& params) {
1.115 moko 473: HashStringValue& hash=GET_SELF(r, VHash).hash_ro();
1.105 misha 474: size_t count=hash.count();
475:
476: int pos=0;
477:
478: Value& vwhence=*params.get(0);
479: if(vwhence.is_string()){
480: const String& swhence=*vwhence.get_string();
481: if(swhence == "last")
482: pos=count-1;
483: else if(swhence != "first")
484: throw Exception(PARSER_RUNTIME,
485: &swhence,
486: "whence must be 'first', 'last' or expression");
487: } else {
488: pos=r.process_to_value(vwhence).as_int();
489: if(pos < 0)
490: pos+=count;
491: }
492:
1.106 misha 493: if(count && pos >= 0 && (size_t)pos < count){
1.105 misha 494: if(pos == 0)
495: r.write_assign_lang(*hash.first_value());
1.106 misha 496: else if((size_t)pos == count-1)
1.105 misha 497: r.write_assign_lang(*hash.last_value());
498: else
499: for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
500: if(!pos){
501: r.write_assign_lang(*i.value());
502: break;
503: }
504: }
505:
506: }
507:
1.1 paf 508: // constructor
509:
1.57 paf 510: MHash::MHash(): Methoded("hash")
1.39 paf 511: {
1.21 parser 512: // ^hash::create[[copy_from]]
1.57 paf 513: add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1);
1.22 parser 514: // ^hash.add[add_from]
1.57 paf 515: add_native_method("add", Method::CT_DYNAMIC, _create_or_add, 1, 1);
1.22 parser 516: // ^hash.sub[sub_from]
517: add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1);
518: // ^a.union[b] = hash
519: add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1);
520: // ^a.intersection[b] = hash
521: add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 1);
522: // ^a.intersects[b] = bool
523: add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1);
1.25 paf 524:
525: // ^a.delete[key]
1.119 misha 526: add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1);
1.2 parser 527:
1.82 misha 528: // ^a.contains[key]
529: add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1);
530: // backward
531: add_native_method("contain", Method::CT_DYNAMIC, _contains, 1, 1);
1.80 misha 532:
533: // ^hash::sql[query][options hash]
1.33 paf 534: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.2 parser 535:
1.68 paf 536: // ^hash._keys[[column name]]
537: add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1);
1.16 parser 538:
539: // ^hash._count[]
540: add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);
1.26 paf 541:
542: // ^hash.foreach[key;value]{code}[delim]
543: add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.105 misha 544:
545: // ^hash._at[first|last]
546: // ^hash._at([-]offset)
547: add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 1);
1.1 paf 548: }
E-mail: