Annotation of parser3/src/classes/hash.C, revision 1.155
1.1 paf 1: /** @file
2: Parser: @b hash parser class.
3:
1.146 moko 4: Copyright (c) 2001-2020 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"
1.122 moko 12: #include "pa_charsets.h"
1.1 paf 13: #include "pa_vhash.h"
1.6 parser 14: #include "pa_vvoid.h"
1.2 parser 15: #include "pa_sql_connection.h"
1.9 parser 16: #include "pa_vtable.h"
1.22 parser 17: #include "pa_vbool.h"
1.26 paf 18: #include "pa_vmethod_frame.h"
1.2 parser 19:
1.155 ! moko 20: volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.154 2021/12/21 14:24:54 moko Exp $";
1.113 moko 21:
1.1 paf 22: // class
23:
1.57 paf 24: class MHash: public Methoded {
1.2 parser 25: public: // VStateless_class
1.103 misha 26: Value* create_new_value(Pool&) { return new VHash(); }
1.2 parser 27:
1.1 paf 28: public:
1.57 paf 29: MHash();
1.1 paf 30: };
31:
1.57 paf 32: // global variable
33:
1.126 moko 34: DECLARE_CLASS_VAR(hash, new MHash);
1.57 paf 35:
1.1 paf 36: // methods
37:
1.11 parser 38: #ifndef DOXYGEN
1.52 paf 39: class Hash_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.57 paf 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.145 moko 52: Hash_sql_event_handlers(bool adistinct, HashStringValue& arows_hash, Table2hash_value_type avalue_type):
1.49 paf 53: distinct(adistinct),
1.11 parser 54: rows_hash(arows_hash),
1.86 misha 55: row_value(0),
1.67 paf 56: column_index(0),
1.139 moko 57: columns(*new ArrayString),
1.86 misha 58: one_bool_column(false),
1.139 moko 59: value_type(avalue_type),
1.86 misha 60: empty(0) {
1.11 parser 61: }
1.86 misha 62:
1.109 misha 63: bool add_column(SQL_Error& error, const char* str, size_t ) {
1.53 paf 64: try {
1.107 moko 65: columns+=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */);
1.53 paf 66: return false;
67: } catch(...) {
1.142 moko 68: error=SQL_Error("exception occurred in Hash_sql_event_handlers::add_column");
1.53 paf 69: return true;
70: }
1.11 parser 71: }
1.86 misha 72:
1.53 paf 73: bool before_rows(SQL_Error& error) {
1.67 paf 74: if(columns.count()<1) {
1.145 moko 75: error=SQL_Error("no columns");
1.53 paf 76: return true;
77: }
1.86 misha 78: switch(value_type){
79: case C_STRING: {
80: if(columns.count()>2){
1.145 moko 81: error=SQL_Error("only 2 columns allowed for $.type[string].");
1.86 misha 82: return true;
83: }
84: }
85: case C_TABLE: {
86: // create empty table which we'll copy later
87: empty=new Table(&columns);
88: columns_count=columns.count();
89: }
90: case C_HASH: {
91: one_bool_column=columns.count()==1;
92: }
93: }
1.53 paf 94: return false;
1.11 parser 95: }
1.86 misha 96:
1.53 paf 97: bool add_row(SQL_Error& /*error*/) {
1.11 parser 98: column_index=0;
1.53 paf 99: return false;
1.11 parser 100: }
1.86 misha 101:
1.116 misha 102: bool add_row_cell(SQL_Error& error, const char *str, size_t ) {
1.53 paf 103: try {
1.145 moko 104: const String& cell=str ? *new String(str, String::L_TAINTED /* no length as 0x00 can be inside */) : String::Empty;
1.86 misha 105:
1.69 paf 106: bool duplicate=false;
1.86 misha 107: if(one_bool_column) {
1.69 paf 108: duplicate=rows_hash.put_dont_replace(cell, &only_one_column_value); // put. existed?
109: } else if(column_index==0) {
1.86 misha 110: switch(value_type){
111: case C_HASH: {
112: VHash* row_vhash=new VHash;
113: row_value=row_vhash;
114: duplicate=rows_hash.put_dont_replace(cell, row_vhash); // put. existed?
115: break;
116: }
117: case C_STRING: {
118: VString* row_vstring=new VString();
119: row_value=row_vstring;
120: duplicate=rows_hash.put_dont_replace(cell, row_vstring); // put. existed?
121: break;
122: }
123: case C_TABLE: {
124: VTable* vtable=(VTable*)rows_hash.get(cell);
125: Table* table;
126:
127: if(vtable) { // table with this key exist?
128: if(!distinct) {
129: duplicate=true;
130: break;
131: }
132: table=vtable->get_table();
133: } else {
134: // no? creating table of same structure as source
135: Table::Action_options table_options(0, 0);
136: table=new Table(*empty, table_options/*no rows, just structure*/);
137: vtable=new VTable(table);
138: rows_hash.put(cell, vtable); // put
139: }
140: ArrayString* row=new ArrayString(columns_count);
141: row_value=(Value*)row;
142: *row+=&cell;
143: *table+=row;
144: break;
145: }
146: }
147: } else {
148: switch(value_type) {
149: case C_HASH: {
150: row_value->get_hash()->put(*columns[column_index], new VString(cell));
151: break;
152: }
153: case C_STRING: {
154: VString* row_string=(VString*)row_value;
155: row_string->set_string(cell);
156: break;
157: }
158: case C_TABLE: {
159: ArrayString* row=(ArrayString*)row_value;
160: *row+=&cell;
161: break;
162: }
163: }
164: }
1.69 paf 165:
166: if(duplicate & !distinct) {
1.145 moko 167: error=SQL_Error("duplicate key");
1.69 paf 168: return true;
169: }
170:
1.53 paf 171: column_index++;
172: return false;
173: } catch(...) {
1.142 moko 174: error=SQL_Error("exception occurred in Hash_sql_event_handlers::add_row_cell");
1.53 paf 175: return true;
176: }
1.11 parser 177: }
178:
179: };
1.67 paf 180: VBool Hash_sql_event_handlers::only_one_column_value(true);
181:
1.11 parser 182: #endif
183:
1.57 paf 184: static void _create_or_add(Request& r, MethodParams& params) {
185: if(params.count()) {
1.111 misha 186: Value& vsrc=params.as_no_junction(0, PARAM_MUST_BE_HASH);
1.130 moko 187: VHashBase& self=GET_SELF(r, VHashBase);
188: HashStringValue* self_hash=&(self.hash());
1.115 moko 189: HashStringValue* src_hash;
190:
1.130 moko 191: if(VHashBase* src=static_cast<VHashBase*>(vsrc.as(VHASH_TYPE))) {
1.127 moko 192: src_hash=&(src->hash());
1.115 moko 193:
194: if(src_hash==self_hash) // same: doing nothing
1.66 paf 195: return;
1.72 paf 196:
1.115 moko 197: if(Value* vdefault=src->get_default())
1.129 moko 198: self.set_default(vdefault);
1.115 moko 199: } else {
200: src_hash=vsrc.get_hash();
1.66 paf 201: }
1.115 moko 202:
203: if(src_hash)
1.138 moko 204: for(HashStringValue::Iterator i(*src_hash); i; i.next())
205: self_hash->put(i.key(), i.value());
1.20 parser 206: }
207: }
1.22 parser 208:
1.57 paf 209: static void _sub(Request& r, MethodParams& params) {
1.114 misha 210: if(HashStringValue* src=params.as_hash(0, "param")) {
1.130 moko 211: HashStringValue* self=&(GET_SELF(r, VHashBase).hash());
1.66 paf 212: if(src==self) { // same: clearing
213: self->clear();
214: return;
215: }
1.138 moko 216: for(HashStringValue::Iterator i(*src); i; i.next())
217: self->remove(i.key());
1.66 paf 218: }
1.57 paf 219: }
220:
1.130 moko 221: static void copy_all_dontoverwrite_to(HashStringValue::key_type key, HashStringValue::value_type value, HashStringValue* dest) {
1.57 paf 222: dest->put_dont_replace(key, value);
1.22 parser 223: }
1.57 paf 224: static void _union(Request& r, MethodParams& params) {
1.22 parser 225: // dest = copy of self
1.130 moko 226: Value& result=*new VHash(GET_SELF(r, VHashBase).hash());
1.22 parser 227: // dest += b
1.114 misha 228: if(HashStringValue* src=params.as_hash(0, "param"))
1.77 paf 229: src->for_each<HashStringValue*>(copy_all_dontoverwrite_to, result.get_hash());
1.22 parser 230:
231: // return result
1.137 moko 232: r.write(result);
1.22 parser 233: }
234:
235: #ifndef DOXYGEN
236: struct Copy_intersection_to_info {
1.57 paf 237: HashStringValue* b;
238: HashStringValue* dest;
1.22 parser 239: };
240: #endif
1.155 ! moko 241:
! 242: static void copy_intersection_by_arg(HashStringValue::key_type key, HashStringValue::value_type, Copy_intersection_to_info *info) {
! 243: if(HashStringValue::value_type value=info->b->get(key))
! 244: info->dest->put_dont_replace(key, value);
! 245: }
! 246:
! 247: static void copy_intersection_by_self(HashStringValue::key_type key, HashStringValue::value_type value, 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.155 ! moko 251:
1.57 paf 252: static void _intersection(Request& r, MethodParams& params) {
253: Value& result=*new VHash;
1.155 ! moko 254:
! 255: bool order_by_arg=true;
! 256: if(params.count()>1)
! 257: if(HashStringValue* options=params.as_hash(1, "options")) {
! 258: int valid_options=0;
! 259: if(Value* vorder=options->get("order")) {
! 260: const String &sorder=r.process(*vorder).as_string();
! 261: if(sorder == "self")
! 262: order_by_arg=false;
! 263: else if(sorder != "arg")
! 264: throw Exception(PARSER_RUNTIME, &sorder, "'order' must be 'self' or 'arg'");
! 265: valid_options++;
! 266: }
! 267: if(valid_options!=options->count())
! 268: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
! 269: }
! 270:
1.114 misha 271: if(HashStringValue* b=params.as_hash(0, "param")) {
1.155 ! moko 272: if(order_by_arg){
! 273: Copy_intersection_to_info info={&GET_SELF(r, VHashBase).hash(), result.get_hash()};
! 274: b->for_each<Copy_intersection_to_info*>(copy_intersection_by_arg, &info);
! 275: } else {
! 276: Copy_intersection_to_info info={b, result.get_hash()};
! 277: GET_SELF(r, VHashBase).hash().for_each<Copy_intersection_to_info*>(copy_intersection_by_self, &info);
! 278: }
1.22 parser 279: }
280:
1.137 moko 281: r.write(result);
1.22 parser 282: }
283:
1.130 moko 284: static bool intersects( HashStringValue::key_type key, HashStringValue::value_type /*value*/, HashStringValue* b) {
1.57 paf 285: return b->get(key)!=0;
1.22 parser 286: }
287:
1.57 paf 288: static void _intersects(Request& r, MethodParams& params) {
289: bool result=false;
290:
1.119 misha 291: if(HashStringValue* b=params.as_hash(0, "param")) {
1.130 moko 292: HashStringValue* self=&(GET_SELF(r, VHashBase).hash());
1.120 misha 293: if(b==self) {
1.137 moko 294: r.write(VBool::get(true));
1.120 misha 295: return;
296: }
1.119 misha 297: result=self->first_that<HashStringValue*>(intersects, b)!=0;
298: }
1.22 parser 299:
300: // return result
1.137 moko 301: r.write(VBool::get(result));
1.22 parser 302: }
303:
304:
1.81 misha 305: extern Table2hash_value_type get_value_type(Value& vvalue_type);
1.71 paf 306: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
307: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
308:
1.57 paf 309: static void _sql(Request& r, MethodParams& params) {
310: Value& statement=params.as_junction(0, "statement must be code");
1.2 parser 311:
1.71 paf 312: HashStringValue* bind=0;
1.87 misha 313: ulong limit=SQL_NO_LIMIT;
1.33 paf 314: ulong offset=0;
1.49 paf 315: bool distinct=false;
1.81 misha 316: Table2hash_value_type value_type=C_HASH;
1.110 misha 317: if(params.count()>1)
1.114 misha 318: if(HashStringValue* options=params.as_hash(1, "sql options")) {
1.110 misha 319: int valid_options=0;
320: if(Value* vbind=options->get(sql_bind_name)) {
321: valid_options++;
322: bind=vbind->get_hash();
323: }
324: if(Value* vlimit=options->get(sql_limit_name)) {
325: valid_options++;
1.133 moko 326: limit=(ulong)r.process(*vlimit).as_double();
1.110 misha 327: }
328: if(Value* voffset=options->get(sql_offset_name)) {
329: valid_options++;
1.133 moko 330: offset=(ulong)r.process(*voffset).as_double();
1.110 misha 331: }
332: if(Value* vdistinct=options->get(sql_distinct_name)) {
333: valid_options++;
1.133 moko 334: distinct=r.process(*vdistinct).as_bool();
1.110 misha 335: }
336: if(Value* vvalue_type=options->get(sql_value_type_name)) {
337: valid_options++;
1.133 moko 338: value_type=get_value_type(r.process(*vvalue_type));
1.110 misha 339: }
340: if(valid_options!=options->count())
341: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
342: }
1.2 parser 343:
1.71 paf 344: SQL_Driver::Placeholder* placeholders=0;
345: uint placeholders_count=0;
346: if(bind)
347: placeholders_count=marshal_binds(*bind, placeholders);
348:
1.37 paf 349: const String& statement_string=r.process_to_string(statement);
1.136 moko 350: const char* statement_cstr=statement_string.untaint_cstr(String::L_SQL, r.connection());
1.97 misha 351:
1.130 moko 352: HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.145 moko 353: hash.clear();
354: Hash_sql_event_handlers handlers(distinct, hash, value_type);
355:
356: r.connection()->query(statement_cstr, placeholders_count, placeholders, offset, limit, handlers, statement_string);
1.71 paf 357:
358: if(bind)
359: unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.2 parser 360: }
361:
1.130 moko 362: static void keys_collector(HashStringValue::key_type key, HashStringValue::value_type, Table *table) {
1.100 misha 363: Table::element_type row(new ArrayString(1));
1.57 paf 364: *row+=new String(key, String::L_TAINTED);
365: *table+=row;
1.9 parser 366: }
1.68 paf 367: static void _keys(Request& r, MethodParams& params) {
368: const String* keys_column_name;
369: if(params.count()>0)
1.81 misha 370: keys_column_name=¶ms.as_string(0, COLUMN_NAME_MUST_BE_STRING);
1.68 paf 371: else
372: keys_column_name=new String("key");
373:
1.101 misha 374: Table::columns_type columns(new ArrayString(1));
1.68 paf 375: *columns+=keys_column_name;
1.57 paf 376: Table* table=new Table(columns);
1.9 parser 377:
1.130 moko 378: GET_SELF(r, VHashBase).hash().for_each<Table*>(keys_collector, table);
1.9 parser 379:
1.137 moko 380: r.write(*new VTable(table));
1.9 parser 381: }
382:
1.57 paf 383: static void _count(Request& r, MethodParams&) {
1.137 moko 384: r.write(*new VInt(GET_SELF(r, VHashBase).hash().count()));
1.16 parser 385: }
386:
1.57 paf 387: static void _delete(Request& r, MethodParams& params) {
1.119 misha 388: if(params.count()>0)
1.130 moko 389: GET_SELF(r, VHashBase).hash().remove(params.as_string(0, "key must be string"));
1.119 misha 390: else
1.130 moko 391: GET_SELF(r, VHashBase).hash().clear();
1.25 paf 392: }
393:
1.82 misha 394: static void _contains(Request& r, MethodParams& params) {
1.130 moko 395: VHashBase& self=GET_SELF(r, VHashBase);
1.129 moko 396: const String& key_name=params.as_string(0, "key must be string");
397: bool result=SYMBOLS_EQ(key_name,_DEFAULT_SYMBOL) ? (self.get_default() != 0) : self.hash().contains(key_name);
1.137 moko 398: r.write(VBool::get(result));
1.80 misha 399: }
400:
1.57 paf 401: static void _foreach(Request& r, MethodParams& params) {
1.102 misha 402: InCycle temp(r);
1.77 paf 403:
1.128 moko 404: const String* key_var_name=¶ms.as_string(0, "key-var name must be string");
405: const String* value_var_name=¶ms.as_string(1, "value-var name must be string");
406: Value* body_code=¶ms.as_junction(2, "body must be code");
1.134 moko 407: Value* delim_maybe_code=params.count()>3?¶ms[3]:0;
1.128 moko 408: Value& caller=*r.get_method_frame()->caller();
1.85 misha 409:
1.128 moko 410: if(key_var_name->is_empty()) key_var_name=0;
411: if(value_var_name->is_empty()) value_var_name=0;
1.57 paf 412:
1.130 moko 413: HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.128 moko 414:
415: if(delim_maybe_code){ // delimiter set
416: bool need_delim=false;;
417: for(HashStringValue::Iterator i(hash); i; i.next()){
418: if(key_var_name){
419: VString* vkey=new VString(*new String(i.key(), String::L_TAINTED));
420: r.put_element(caller, *key_var_name, vkey);
421: }
422:
423: if(value_var_name)
424: r.put_element(caller, *value_var_name, i.value());
425:
1.132 moko 426: Value& sv_processed=r.process(*body_code);
1.140 moko 427: TempSkip4Delimiter skip(r);
1.128 moko 428:
429: const String* s_processed=sv_processed.get_string();
430: if(s_processed && !s_processed->is_empty()) { // we have body
431: if(need_delim) // need delim & iteration produced string?
1.137 moko 432: r.write(r.process(*delim_maybe_code));
1.128 moko 433: else
434: need_delim=true;
435: }
436:
1.137 moko 437: r.write(sv_processed);
1.128 moko 438:
1.140 moko 439: if(skip.check_break())
1.128 moko 440: break;
441: }
442: } else {
443: for(HashStringValue::Iterator i(hash); i; i.next()){
444: if(key_var_name){
445: VString* vkey=new VString(*new String(i.key(), String::L_TAINTED));
446: r.put_element(caller, *key_var_name, vkey);
447: }
448:
449: if(value_var_name)
450: r.put_element(caller, *value_var_name, i.value());
451:
452: r.process_write(*body_code);
453:
1.140 moko 454: if(r.check_skip_break())
1.128 moko 455: break;
456: }
457: }
1.26 paf 458: }
459:
1.121 misha 460: enum AtResultType {
461: AtResultTypeValue = 0,
462: AtResultTypeKey = 1,
463: AtResultTypeHash = 2
464: };
465:
466: inline Value& SingleElementHash(String::Body akey, Value* avalue) {
467: Value& result=*new VHash;
468: result.put_element(*new String(akey, String::L_TAINTED), avalue);
469: return result;
470: }
471:
1.122 moko 472: #ifndef DOXYGEN
1.143 moko 473: struct Hash_seq_item : public PA_Allocated {
1.122 moko 474: HashStringValue::Pair *hash_pair;
475: union {
476: const char *c_str;
477: double d;
478: } value;
479: };
480: #endif
481: static int sort_cmp_string(const void *a, const void *b) {
482: return strcmp(
483: static_cast<const Hash_seq_item *>(a)->value.c_str,
484: static_cast<const Hash_seq_item *>(b)->value.c_str
485: );
486: }
487: static int sort_cmp_double(const void *a, const void *b) {
488: double va=static_cast<const Hash_seq_item *>(a)->value.d;
489: double vb=static_cast<const Hash_seq_item *>(b)->value.d;
490: if(va<vb)
491: return -1;
492: else if(va>vb)
493: return +1;
494: else
495: return 0;
496: }
497: static void _sort(Request& r, MethodParams& params){
1.123 moko 498: #ifdef HASH_ORDER
1.122 moko 499: const String& key_var_name=params.as_string(0, "key-var name must be string");
500: const String& value_var_name=params.as_string(1, "value-var name must be string");
501: Value& key_maker=params.as_junction(2, "key-maker must be code");
1.139 moko 502: bool reverse=params.count()>3 /*..[desc|asc|]*/ && params.as_no_junction(3, "order must not be code").as_string()=="desc"; // default=asc
1.122 moko 503:
504: const String* key_var=key_var_name.is_empty()? 0 : &key_var_name;
505: const String* value_var=value_var_name.is_empty()? 0 : &value_var_name;
506: VMethodFrame* context=r.get_method_frame()->caller();
507:
1.130 moko 508: HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.122 moko 509: int count=hash.count();
510:
1.143 moko 511: Hash_seq_item* seq=new Hash_seq_item[count];
1.122 moko 512: int pos=0;
513: bool key_values_are_strings=true;
514:
515: for(HashStringValue::Iterator i(hash); i; i.next(), pos++ ){
516: if(key_var)
517: r.put_element(*context, *key_var, new VString(*new String(i.key(), String::L_TAINTED)));
518: if(value_var)
519: r.put_element(*context, *value_var, i.value());
520:
1.133 moko 521: Value& value=r.process(key_maker);
1.122 moko 522: if(pos==0) // determining key values type by first one
523: key_values_are_strings=value.is_string();
524:
525: seq[pos].hash_pair=i.pair();
526: if(key_values_are_strings)
527: seq[pos].value.c_str=value.as_string().cstr();
528: else
529: seq[pos].value.d=value.as_expr_result().as_double();
530: }
531:
532: // @todo: handle this elsewhere
533: if(r.charsets.source().NAME()=="KOI8-R" && key_values_are_strings)
534: for(pos=0; pos<count; pos++)
535: if(*seq[pos].value.c_str)
1.131 moko 536: seq[pos].value.c_str=Charset::transcode(seq[pos].value.c_str, r.charsets.source(), pa_UTF8_charset).cstr();
1.122 moko 537:
538: // sort keys
539: qsort(seq, count, sizeof(Hash_seq_item), key_values_are_strings?sort_cmp_string:sort_cmp_double);
540:
541: // reorder hash as required in 'seq'
542: hash.order_clear();
543: if(reverse)
544: for(pos=count-1; pos>=0; pos--)
545: hash.order_next(seq[pos].hash_pair);
546: else
547: for(pos=0; pos<count; pos++)
548: hash.order_next(seq[pos].hash_pair);
549:
550: delete[] seq;
1.123 moko 551: #endif
1.122 moko 552: }
553:
1.105 misha 554: static void _at(Request& r, MethodParams& params) {
1.130 moko 555: HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.105 misha 556: size_t count=hash.count();
557:
558: int pos=0;
559:
1.121 misha 560: // misha@
561: // I do not like that type is checked before whence.
1.133 moko 562: // But I do not like the idea to move it after whence (where process can be called) even more.
1.121 misha 563: AtResultType result_type=AtResultTypeValue;
564: if(params.count() > 1) {
565: const String& stype=params.as_string(1, "type must be string");
566: if(stype == "key")
567: result_type=AtResultTypeKey;
568: else if(stype == "hash")
569: result_type=AtResultTypeHash;
570: else if(stype != "value")
571: throw Exception(PARSER_RUNTIME, &stype, "type must be 'key', 'value' or 'hash'");
572: }
573:
1.134 moko 574: Value& vwhence=params[0];
1.121 misha 575: if(vwhence.is_string()) {
1.105 misha 576: const String& swhence=*vwhence.get_string();
577: if(swhence == "last")
578: pos=count-1;
579: else if(swhence != "first")
580: throw Exception(PARSER_RUNTIME,
581: &swhence,
582: "whence must be 'first', 'last' or expression");
583: } else {
1.133 moko 584: pos=r.process(vwhence).as_int();
1.105 misha 585: if(pos < 0)
586: pos+=count;
587: }
588:
1.106 misha 589: if(count && pos >= 0 && (size_t)pos < count){
1.121 misha 590: switch(result_type) {
591: case AtResultTypeKey:
592: {
1.123 moko 593: #ifdef HASH_ORDER
1.121 misha 594: if(pos == 0) {
1.137 moko 595: r.write(*new VString(*new String(hash.first_key(), String::L_TAINTED)));
1.121 misha 596: } else if((size_t)pos == count-1) {
1.137 moko 597: r.write(*new VString(*new String(hash.last_key(), String::L_TAINTED)));
1.123 moko 598: } else
599: #endif
600: {
1.121 misha 601: for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
602: if(!pos){
1.137 moko 603: r.write(*new VString(*new String(i.key(), String::L_TAINTED)));
1.121 misha 604: break;
605: }
606: }
1.105 misha 607: break;
608: }
1.121 misha 609: case AtResultTypeValue:
610: {
1.123 moko 611: #ifdef HASH_ORDER
1.121 misha 612: if(pos == 0) {
1.137 moko 613: r.write(*hash.first_value());
1.121 misha 614: } else if((size_t)pos == count-1) {
1.137 moko 615: r.write(*hash.last_value());
1.123 moko 616: } else
617: #endif
618: {
1.121 misha 619: for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
620: if(!pos){
1.137 moko 621: r.write(*i.value());
1.121 misha 622: break;
623: }
624: }
625: break;
626: }
627: case AtResultTypeHash:
628: {
1.123 moko 629: #ifdef HASH_ORDER
1.121 misha 630: if(pos == 0) {
1.137 moko 631: r.write(SingleElementHash(hash.first_key(), hash.first_value()));
1.121 misha 632: } else if((size_t)pos == count-1) {
1.137 moko 633: r.write(SingleElementHash(hash.last_key(), hash.last_value()));
1.123 moko 634: } else
635: #endif
636: {
1.121 misha 637: for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
638: if(!pos){
1.137 moko 639: r.write(SingleElementHash(i.key(), i.value()));
1.121 misha 640: break;
641: }
642: }
643: break;
644: }
645: }
1.105 misha 646: }
647: }
648:
1.147 moko 649: extern String table_reverse_name;
650:
651: static void _select(Request& r, MethodParams& params) {
1.154 moko 652: InCycle temp(r);
1.147 moko 653: const String* key_var_name=¶ms.as_string(0, "key-var name must be string");
654: const String* value_var_name=¶ms.as_string(1, "value-var name must be string");
655: Value& vcondition=params.as_expression(2, "condition must be number, bool or expression");
656:
657: if(key_var_name->is_empty()) key_var_name=0;
658: if(value_var_name->is_empty()) value_var_name=0;
659:
660: HashStringValue& source_hash=GET_SELF(r, VHashBase).hash();
661: Value& caller=*r.get_method_frame()->caller();
662:
663: int limit=source_hash.count();
664: bool reverse=false;
1.149 moko 665: bool copy_default=false;
1.147 moko 666:
667: if(params.count()>3)
668: if(HashStringValue* options=params.as_hash(3)) {
669: int valid_options=0;
670: if(Value* vlimit=options->get(sql_limit_name)) {
671: valid_options++;
672: limit=r.process(*vlimit).as_int();
673: }
674: if(Value* vreverse=options->get(table_reverse_name)) {
675: valid_options++;
676: reverse=r.process(*vreverse).as_bool();
677: }
1.149 moko 678: if(Value* vcopy_default=options->get(sql_default_name)) {
679: valid_options++;
680: copy_default=r.process(*vcopy_default).as_bool();
681: }
1.147 moko 682: if(valid_options!=options->count())
683: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
684: }
685:
686: HashStringValue& result_hash=*new HashStringValue();
687:
688: if(limit>0){
1.154 moko 689:
1.147 moko 690: #ifdef HASH_ORDER
691: if(reverse){
692: for(HashStringValue::ReverseIterator i(source_hash); i; i.prev()){
693: if(key_var_name)
694: r.put_element(caller, *key_var_name, new VString(*new String(i.key(), String::L_TAINTED)));
695: if(value_var_name)
696: r.put_element(caller, *value_var_name, i.value());
697:
1.154 moko 698: bool condition=r.process(vcondition).as_bool();
699:
700: if(r.check_skip_break())
701: break;
702:
703: if(condition){
1.147 moko 704: result_hash.put(i.key(), i.value());
705: if(!--limit)
706: break;
707: }
708: }
709: } else {
710: #else
711: {
712: #endif
713: for(HashStringValue::Iterator i(source_hash); i; i.next() ){
714: if(key_var_name)
715: r.put_element(caller, *key_var_name, new VString(*new String(i.key(), String::L_TAINTED)));
716: if(value_var_name)
717: r.put_element(caller, *value_var_name, i.value());
718:
1.154 moko 719: bool condition=r.process(vcondition).as_bool();
720:
721: if(r.check_skip_break())
722: break;
723:
724: if(condition){
1.147 moko 725: result_hash.put(i.key(), i.value());
726: if(!--limit)
727: break;
728: }
729: }
730: }
731: }
732:
1.149 moko 733: VHash *result=new VHash(result_hash);
734: if(copy_default){
735: result->set_default(GET_SELF(r, VHashBase).get_default());
736: }
737:
738: r.write(*result);
1.147 moko 739: }
740:
741: static void _reverse(Request& r, MethodParams& params) {
1.148 moko 742: VHashBase& self=GET_SELF(r, VHashBase);
743: HashStringValue& source_hash=self.hash();
1.147 moko 744: HashStringValue& result_hash=*new HashStringValue();
745:
746: #ifdef HASH_ORDER
747: for(HashStringValue::ReverseIterator i(source_hash); i; i.prev())
748: result_hash.put(i.key(), i.value());
749: #else
750: for(HashStringValue::Iterator i(source_hash); i; i.next() )
751: result_hash.put(i.key(), i.value());
752: #endif
1.148 moko 753:
754: VHashBase& result=*new VHash(result_hash);
755: if(Value* vdefault=self.get_default())
756: result.set_default(vdefault);
757:
758: r.write(result);
1.147 moko 759: }
760:
1.150 moko 761:
762: static void _rename(Request& r, MethodParams& params) {
1.151 moko 763: HashStringValue& hash=GET_SELF(r, VHashBase).hash();
764:
765: if(params.count()>1){
766: const String& key_from=params.as_string(0, "from key must be string");
767: const String& key_to=params.as_string(1, "to key must be string");
768:
769: hash.rename(key_from, key_to);
1.152 moko 770: } else {
1.153 moko 771: HashStringValue* names=params.as_hash(0,"single parameter");
1.150 moko 772:
1.151 moko 773: for(HashStringValue::Iterator i(*names); i; i.next())
774: hash.rename(i.key(), i.value()->as_string());
775: }
1.150 moko 776: }
777:
778:
1.1 paf 779: // constructor
780:
1.57 paf 781: MHash::MHash(): Methoded("hash")
1.39 paf 782: {
1.21 parser 783: // ^hash::create[[copy_from]]
1.57 paf 784: add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1);
1.22 parser 785: // ^hash.add[add_from]
1.57 paf 786: add_native_method("add", Method::CT_DYNAMIC, _create_or_add, 1, 1);
1.22 parser 787: // ^hash.sub[sub_from]
788: add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1);
789: // ^a.union[b] = hash
790: add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1);
1.155 ! moko 791: // ^a.intersection[b][options hash] = hash
! 792: add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 2);
1.22 parser 793: // ^a.intersects[b] = bool
794: add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1);
1.25 paf 795:
796: // ^a.delete[key]
1.119 misha 797: add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1);
1.2 parser 798:
1.82 misha 799: // ^a.contains[key]
800: add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1);
801: // backward
802: add_native_method("contain", Method::CT_DYNAMIC, _contains, 1, 1);
1.80 misha 803:
804: // ^hash::sql[query][options hash]
1.33 paf 805: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.2 parser 806:
1.68 paf 807: // ^hash._keys[[column name]]
1.124 moko 808: add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1);
1.16 parser 809:
810: // ^hash._count[]
1.124 moko 811: add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);
1.26 paf 812:
813: // ^hash.foreach[key;value]{code}[delim]
814: add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.105 misha 815:
1.122 moko 816: // ^hash.sort[key;value]{string-key-maker}[[asc|desc]]
817: // ^hash.sort[key;value](numeric-key-maker)[[asc|desc]]
1.147 moko 818: add_native_method("sort", Method::CT_DYNAMIC, _sort, 3, 4);
819:
820: // ^hash.select[key;value](bool-condition)[options hash]
821: add_native_method("select", Method::CT_DYNAMIC, _select, 3, 4);
822:
823: // ^hash.reverse[]
824: add_native_method("reverse", Method::CT_DYNAMIC, _reverse, 0, 0);
1.122 moko 825:
1.121 misha 826: // ^hash._at[first|last[;'key'|'value'|'hash']]
827: // ^hash._at([-+]offset)[['key'|'value'|'hash']]
828: add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 2);
1.124 moko 829:
1.150 moko 830: // ^hash.rename[from;to]
1.151 moko 831: // ^hash.rename[ $.from[to] ... ]
832: add_native_method("rename", Method::CT_DYNAMIC, _rename, 1, 2);
1.150 moko 833:
1.124 moko 834: #ifdef FEATURE_GET_ELEMENT4CALL
835: // aliases without "_"
836: add_native_method("keys", Method::CT_DYNAMIC, _keys, 0, 1);
837: add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
838: add_native_method("at", Method::CT_DYNAMIC, _at, 1, 2);
839: #endif
840:
1.1 paf 841: }
E-mail: