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