Annotation of parser3/src/classes/hash.C, revision 1.116
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.116 ! misha 19: volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.115 2012-06-13 22:53:47 moko 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.114 misha 280: if(HashStringValue* b=params.as_hash(0, "param"))
1.77 paf 281: result=GET_SELF(r, VHash).hash().first_that<HashStringValue*>(intersects, b)!=0;
1.22 parser 282:
283: // return result
1.89 misha 284: r.write_no_lang(VBool::get(result));
1.22 parser 285: }
286:
287:
1.71 paf 288: extern String sql_bind_name;
1.57 paf 289: extern String sql_limit_name;
290: extern String sql_offset_name;
291: extern String sql_default_name;
292: extern String sql_distinct_name;
1.81 misha 293: extern String sql_value_type_name;
294: extern Table2hash_value_type get_value_type(Value& vvalue_type);
1.71 paf 295: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
296: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
297:
1.57 paf 298: static void _sql(Request& r, MethodParams& params) {
299: Value& statement=params.as_junction(0, "statement must be code");
1.2 parser 300:
1.71 paf 301: HashStringValue* bind=0;
1.87 misha 302: ulong limit=SQL_NO_LIMIT;
1.33 paf 303: ulong offset=0;
1.49 paf 304: bool distinct=false;
1.81 misha 305: Table2hash_value_type value_type=C_HASH;
1.110 misha 306: if(params.count()>1)
1.114 misha 307: if(HashStringValue* options=params.as_hash(1, "sql options")) {
1.110 misha 308: int valid_options=0;
309: if(Value* vbind=options->get(sql_bind_name)) {
310: valid_options++;
311: bind=vbind->get_hash();
312: }
313: if(Value* vlimit=options->get(sql_limit_name)) {
314: valid_options++;
315: limit=(ulong)r.process_to_value(*vlimit).as_double();
316: }
317: if(Value* voffset=options->get(sql_offset_name)) {
318: valid_options++;
319: offset=(ulong)r.process_to_value(*voffset).as_double();
320: }
321: if(Value* vdistinct=options->get(sql_distinct_name)) {
322: valid_options++;
323: distinct=r.process_to_value(*vdistinct).as_bool();
324: }
325: if(Value* vvalue_type=options->get(sql_value_type_name)) {
326: valid_options++;
327: value_type=get_value_type(r.process_to_value(*vvalue_type));
328: }
329: if(valid_options!=options->count())
330: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
331: }
1.2 parser 332:
1.71 paf 333: SQL_Driver::Placeholder* placeholders=0;
334: uint placeholders_count=0;
335: if(bind)
336: placeholders_count=marshal_binds(*bind, placeholders);
337:
1.57 paf 338: Temp_lang temp_lang(r, String::L_SQL);
1.37 paf 339: const String& statement_string=r.process_to_string(statement);
1.99 misha 340: const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection());
1.97 misha 341:
1.57 paf 342: HashStringValue& hash=GET_SELF(r, VHash).hash();
1.11 parser 343: hash.clear();
1.57 paf 344: Hash_sql_event_handlers handlers(
1.49 paf 345: statement_string, statement_cstr,
346: distinct,
1.86 misha 347: hash,
348: value_type);
1.87 misha 349:
1.57 paf 350: r.connection()->query(
1.70 paf 351: statement_cstr,
1.73 paf 352: placeholders_count, placeholders,
1.70 paf 353: offset, limit,
1.45 paf 354: handlers,
355: statement_string);
1.71 paf 356:
357: if(bind)
358: unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.2 parser 359: }
360:
1.57 paf 361: static void keys_collector(
1.100 misha 362: HashStringValue::key_type key,
363: HashStringValue::value_type,
364: Table *table) {
365: Table::element_type row(new ArrayString(1));
1.57 paf 366: *row+=new String(key, String::L_TAINTED);
367: *table+=row;
1.9 parser 368: }
1.68 paf 369: static void _keys(Request& r, MethodParams& params) {
370: const String* keys_column_name;
371: if(params.count()>0)
1.81 misha 372: keys_column_name=¶ms.as_string(0, COLUMN_NAME_MUST_BE_STRING);
1.68 paf 373: else
374: keys_column_name=new String("key");
375:
1.101 misha 376: Table::columns_type columns(new ArrayString(1));
1.68 paf 377: *columns+=keys_column_name;
1.57 paf 378: Table* table=new Table(columns);
1.9 parser 379:
1.115 moko 380: GET_SELF(r, VHash).hash_ro().for_each<Table*>(keys_collector, table);
1.9 parser 381:
1.57 paf 382: r.write_no_lang(*new VTable(table));
1.9 parser 383: }
384:
1.57 paf 385: static void _count(Request& r, MethodParams&) {
1.115 moko 386: r.write_no_lang(*new VInt(GET_SELF(r, VHash).hash_ro().count()));
1.16 parser 387: }
388:
1.57 paf 389: static void _delete(Request& r, MethodParams& params) {
1.25 paf 390:
1.57 paf 391: GET_SELF(r, VHash).hash().remove(params.as_string(0, "key must be string"));
1.25 paf 392: }
393:
1.82 misha 394: static void _contains(Request& r, MethodParams& params) {
1.115 moko 395: bool result=GET_SELF(r, VHash).hash_ro().contains(params.as_string(0, "key must be string"));
1.89 misha 396: r.write_no_lang(VBool::get(result));
1.80 misha 397: }
398:
1.26 paf 399: #ifndef DOXYGEN
1.59 paf 400: struct Foreach_info {
1.26 paf 401: Request *r;
402: const String* key_var_name;
403: const String* value_var_name;
1.57 paf 404: Value* body_code;
405: Value* delim_maybe_code;
1.26 paf 406:
1.84 misha 407: Value* var_context;
1.26 paf 408: bool need_delim;
409: };
410: #endif
1.90 misha 411: static bool one_foreach_cycle(
412: HashStringValue::key_type akey,
413: HashStringValue::value_type avalue,
414: Foreach_info *info) {
1.84 misha 415: Value& var_context=*info->var_context;
1.85 misha 416: if(info->key_var_name){
1.96 misha 417: VString* vkey=new VString(*new String(akey, String::L_TAINTED));
1.103 misha 418: var_context.put_element(*info->key_var_name, vkey, false);
1.85 misha 419: }
420: if(info->value_var_name)
1.103 misha 421: var_context.put_element(*info->value_var_name, avalue, false);
1.57 paf 422:
1.91 misha 423: if(info->delim_maybe_code){ // delimiter set
1.90 misha 424: StringOrValue sv_processed=info->r->process(*info->body_code);
425: Request::Skip lskip=info->r->get_skip(); info->r->set_skip(Request::SKIP_NOTHING);
426:
427: const String* s_processed=sv_processed.get_string();
1.91 misha 428: if(s_processed && !s_processed->is_empty()) { // we have body
1.90 misha 429: if(info->need_delim) // need delim & iteration produced string?
430: info->r->write_pass_lang(info->r->process(*info->delim_maybe_code));
431: else
432: info->need_delim=true;
433: }
434: info->r->write_pass_lang(sv_processed);
435: return lskip==Request::SKIP_BREAK;
436: } else {
437: info->r->process_write(*info->body_code);
438: Request::Skip lskip=info->r->get_skip(); info->r->set_skip(Request::SKIP_NOTHING);
439: return lskip==Request::SKIP_BREAK;
1.26 paf 440: }
441: }
1.57 paf 442: static void _foreach(Request& r, MethodParams& params) {
1.102 misha 443: InCycle temp(r);
1.77 paf 444:
1.85 misha 445: const String& key_var_name=params.as_string(0, "key-var name must be string");
446: const String& value_var_name=params.as_string(1, "value-var name must be string");
447:
1.63 paf 448: Foreach_info info={
449: &r,
1.85 misha 450: key_var_name.is_empty()? 0 : &key_var_name,
451: value_var_name.is_empty()? 0 : &value_var_name,
1.63 paf 452: ¶ms.as_junction(2, "body must be code"),
1.84 misha 453: /*delimiter*/params.count()>3?params.get(3):0,
454: /*var_context*/r.get_method_frame()->caller(),
1.85 misha 455: false
1.63 paf 456: };
1.57 paf 457:
458: VHash& self=GET_SELF(r, VHash);
1.115 moko 459: HashStringValue& hash=self.hash_ro();
1.28 paf 460: VHash_lock lock(self);
1.77 paf 461: hash.first_that<Foreach_info*>(one_foreach_cycle, &info);
1.26 paf 462: }
463:
1.105 misha 464: static void _at(Request& r, MethodParams& params) {
1.115 moko 465: HashStringValue& hash=GET_SELF(r, VHash).hash_ro();
1.105 misha 466: size_t count=hash.count();
467:
468: int pos=0;
469:
470: Value& vwhence=*params.get(0);
471: if(vwhence.is_string()){
472: const String& swhence=*vwhence.get_string();
473: if(swhence == "last")
474: pos=count-1;
475: else if(swhence != "first")
476: throw Exception(PARSER_RUNTIME,
477: &swhence,
478: "whence must be 'first', 'last' or expression");
479: } else {
480: pos=r.process_to_value(vwhence).as_int();
481: if(pos < 0)
482: pos+=count;
483: }
484:
1.106 misha 485: if(count && pos >= 0 && (size_t)pos < count){
1.105 misha 486: if(pos == 0)
487: r.write_assign_lang(*hash.first_value());
1.106 misha 488: else if((size_t)pos == count-1)
1.105 misha 489: r.write_assign_lang(*hash.last_value());
490: else
491: for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
492: if(!pos){
493: r.write_assign_lang(*i.value());
494: break;
495: }
496: }
497:
498: }
499:
1.1 paf 500: // constructor
501:
1.57 paf 502: MHash::MHash(): Methoded("hash")
1.39 paf 503: {
1.21 parser 504: // ^hash::create[[copy_from]]
1.57 paf 505: add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1);
1.22 parser 506: // ^hash.add[add_from]
1.57 paf 507: add_native_method("add", Method::CT_DYNAMIC, _create_or_add, 1, 1);
1.22 parser 508: // ^hash.sub[sub_from]
509: add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1);
510: // ^a.union[b] = hash
511: add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1);
512: // ^a.intersection[b] = hash
513: add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 1);
514: // ^a.intersects[b] = bool
515: add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1);
1.25 paf 516:
517: // ^a.delete[key]
518: add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
1.2 parser 519:
1.82 misha 520: // ^a.contains[key]
521: add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1);
522: // backward
523: add_native_method("contain", Method::CT_DYNAMIC, _contains, 1, 1);
1.80 misha 524:
525: // ^hash::sql[query][options hash]
1.33 paf 526: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.2 parser 527:
1.68 paf 528: // ^hash._keys[[column name]]
529: add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1);
1.16 parser 530:
531: // ^hash._count[]
532: add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);
1.26 paf 533:
534: // ^hash.foreach[key;value]{code}[delim]
535: add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.105 misha 536:
537: // ^hash._at[first|last]
538: // ^hash._at([-]offset)
539: add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 1);
1.1 paf 540: }
E-mail: