Annotation of parser3/src/classes/hash.C, revision 1.168
1.1 paf 1: /** @file
2: Parser: @b hash parser class.
3:
1.168 ! moko 4: Copyright (c) 2001-2024 Art. Lebedev Studio (http://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.168 ! moko 21: volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.167 2024/10/28 00:18:25 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())
204: self_hash->put(i.key(), i.value());
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.110 misha 336: int valid_options=0;
1.161 moko 337: for(HashStringValue::Iterator i(*options); i; i.next() ){
338: String::Body key=i.key();
339: Value* value=i.value();
340: if(key == sql_bind_name) {
341: bind=value->get_hash();
342: valid_options++;
343: } else if(key == sql_limit_name) {
344: limit=(ulong)r.process(*value).as_double();
345: valid_options++;
346: } else if(key == sql_offset_name) {
347: offset=(ulong)r.process(*value).as_double();
348: valid_options++;
349: } else if (key == sql_distinct_name) {
350: distinct=r.process(*value).as_bool();
351: valid_options++;
352: } else if (key == sql_value_type_name) {
353: value_type=get_value_type(r.process(*value));
354: valid_options++;
355: }
1.110 misha 356: }
357: if(valid_options!=options->count())
358: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
359: }
1.2 parser 360:
1.71 paf 361: SQL_Driver::Placeholder* placeholders=0;
362: uint placeholders_count=0;
363: if(bind)
364: placeholders_count=marshal_binds(*bind, placeholders);
365:
1.37 paf 366: const String& statement_string=r.process_to_string(statement);
1.136 moko 367: const char* statement_cstr=statement_string.untaint_cstr(String::L_SQL, r.connection());
1.97 misha 368:
1.130 moko 369: HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.145 moko 370: hash.clear();
371: Hash_sql_event_handlers handlers(distinct, hash, value_type);
372:
373: r.connection()->query(statement_cstr, placeholders_count, placeholders, offset, limit, handlers, statement_string);
1.71 paf 374:
375: if(bind)
376: unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.2 parser 377: }
378:
1.130 moko 379: static void keys_collector(HashStringValue::key_type key, HashStringValue::value_type, Table *table) {
1.100 misha 380: Table::element_type row(new ArrayString(1));
1.57 paf 381: *row+=new String(key, String::L_TAINTED);
382: *table+=row;
1.9 parser 383: }
1.68 paf 384: static void _keys(Request& r, MethodParams& params) {
385: const String* keys_column_name;
386: if(params.count()>0)
1.81 misha 387: keys_column_name=¶ms.as_string(0, COLUMN_NAME_MUST_BE_STRING);
1.68 paf 388: else
389: keys_column_name=new String("key");
390:
1.101 misha 391: Table::columns_type columns(new ArrayString(1));
1.68 paf 392: *columns+=keys_column_name;
1.57 paf 393: Table* table=new Table(columns);
1.9 parser 394:
1.130 moko 395: GET_SELF(r, VHashBase).hash().for_each<Table*>(keys_collector, table);
1.9 parser 396:
1.137 moko 397: r.write(*new VTable(table));
1.9 parser 398: }
399:
1.57 paf 400: static void _count(Request& r, MethodParams&) {
1.137 moko 401: r.write(*new VInt(GET_SELF(r, VHashBase).hash().count()));
1.16 parser 402: }
403:
1.57 paf 404: static void _delete(Request& r, MethodParams& params) {
1.119 misha 405: if(params.count()>0)
1.130 moko 406: GET_SELF(r, VHashBase).hash().remove(params.as_string(0, "key must be string"));
1.119 misha 407: else
1.130 moko 408: GET_SELF(r, VHashBase).hash().clear();
1.25 paf 409: }
410:
1.82 misha 411: static void _contains(Request& r, MethodParams& params) {
1.130 moko 412: VHashBase& self=GET_SELF(r, VHashBase);
1.129 moko 413: const String& key_name=params.as_string(0, "key must be string");
414: bool result=SYMBOLS_EQ(key_name,_DEFAULT_SYMBOL) ? (self.get_default() != 0) : self.hash().contains(key_name);
1.137 moko 415: r.write(VBool::get(result));
1.80 misha 416: }
417:
1.57 paf 418: static void _foreach(Request& r, MethodParams& params) {
1.102 misha 419: InCycle temp(r);
1.77 paf 420:
1.128 moko 421: const String* key_var_name=¶ms.as_string(0, "key-var name must be string");
422: const String* value_var_name=¶ms.as_string(1, "value-var name must be string");
423: Value* body_code=¶ms.as_junction(2, "body must be code");
1.134 moko 424: Value* delim_maybe_code=params.count()>3?¶ms[3]:0;
1.128 moko 425: Value& caller=*r.get_method_frame()->caller();
1.85 misha 426:
1.128 moko 427: if(key_var_name->is_empty()) key_var_name=0;
428: if(value_var_name->is_empty()) value_var_name=0;
1.57 paf 429:
1.130 moko 430: HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.128 moko 431:
432: if(delim_maybe_code){ // delimiter set
433: bool need_delim=false;;
434: for(HashStringValue::Iterator i(hash); i; i.next()){
435: if(key_var_name){
436: VString* vkey=new VString(*new String(i.key(), String::L_TAINTED));
437: r.put_element(caller, *key_var_name, vkey);
438: }
439:
440: if(value_var_name)
441: r.put_element(caller, *value_var_name, i.value());
442:
1.132 moko 443: Value& sv_processed=r.process(*body_code);
1.140 moko 444: TempSkip4Delimiter skip(r);
1.128 moko 445:
446: const String* s_processed=sv_processed.get_string();
447: if(s_processed && !s_processed->is_empty()) { // we have body
448: if(need_delim) // need delim & iteration produced string?
1.137 moko 449: r.write(r.process(*delim_maybe_code));
1.128 moko 450: else
451: need_delim=true;
452: }
453:
1.137 moko 454: r.write(sv_processed);
1.128 moko 455:
1.140 moko 456: if(skip.check_break())
1.128 moko 457: break;
458: }
459: } else {
460: for(HashStringValue::Iterator i(hash); i; i.next()){
461: if(key_var_name){
462: VString* vkey=new VString(*new String(i.key(), String::L_TAINTED));
463: r.put_element(caller, *key_var_name, vkey);
464: }
465:
466: if(value_var_name)
467: r.put_element(caller, *value_var_name, i.value());
468:
469: r.process_write(*body_code);
470:
1.140 moko 471: if(r.check_skip_break())
1.128 moko 472: break;
473: }
474: }
1.26 paf 475: }
476:
1.121 misha 477: enum AtResultType {
478: AtResultTypeValue = 0,
479: AtResultTypeKey = 1,
480: AtResultTypeHash = 2
481: };
482:
1.165 moko 483: static Value& SingleElementHash(String::Body akey, Value* avalue) {
1.121 misha 484: Value& result=*new VHash;
485: result.put_element(*new String(akey, String::L_TAINTED), avalue);
486: return result;
487: }
488:
1.122 moko 489: #ifndef DOXYGEN
1.143 moko 490: struct Hash_seq_item : public PA_Allocated {
1.122 moko 491: HashStringValue::Pair *hash_pair;
492: union {
493: const char *c_str;
494: double d;
495: } value;
496: };
497: #endif
498: static int sort_cmp_string(const void *a, const void *b) {
499: return strcmp(
500: static_cast<const Hash_seq_item *>(a)->value.c_str,
501: static_cast<const Hash_seq_item *>(b)->value.c_str
502: );
503: }
504: static int sort_cmp_double(const void *a, const void *b) {
505: double va=static_cast<const Hash_seq_item *>(a)->value.d;
506: double vb=static_cast<const Hash_seq_item *>(b)->value.d;
507: if(va<vb)
508: return -1;
509: else if(va>vb)
510: return +1;
511: else
512: return 0;
513: }
514: static void _sort(Request& r, MethodParams& params){
1.123 moko 515: #ifdef HASH_ORDER
1.122 moko 516: const String& key_var_name=params.as_string(0, "key-var name must be string");
517: const String& value_var_name=params.as_string(1, "value-var name must be string");
518: Value& key_maker=params.as_junction(2, "key-maker must be code");
1.139 moko 519: 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 520:
521: const String* key_var=key_var_name.is_empty()? 0 : &key_var_name;
522: const String* value_var=value_var_name.is_empty()? 0 : &value_var_name;
523: VMethodFrame* context=r.get_method_frame()->caller();
524:
1.130 moko 525: HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.122 moko 526: int count=hash.count();
527:
1.143 moko 528: Hash_seq_item* seq=new Hash_seq_item[count];
1.122 moko 529: int pos=0;
530: bool key_values_are_strings=true;
531:
532: for(HashStringValue::Iterator i(hash); i; i.next(), pos++ ){
533: if(key_var)
534: r.put_element(*context, *key_var, new VString(*new String(i.key(), String::L_TAINTED)));
535: if(value_var)
536: r.put_element(*context, *value_var, i.value());
537:
1.133 moko 538: Value& value=r.process(key_maker);
1.122 moko 539: if(pos==0) // determining key values type by first one
540: key_values_are_strings=value.is_string();
541:
542: seq[pos].hash_pair=i.pair();
543: if(key_values_are_strings)
544: seq[pos].value.c_str=value.as_string().cstr();
545: else
546: seq[pos].value.d=value.as_expr_result().as_double();
547: }
548:
549: // @todo: handle this elsewhere
550: if(r.charsets.source().NAME()=="KOI8-R" && key_values_are_strings)
551: for(pos=0; pos<count; pos++)
552: if(*seq[pos].value.c_str)
1.131 moko 553: seq[pos].value.c_str=Charset::transcode(seq[pos].value.c_str, r.charsets.source(), pa_UTF8_charset).cstr();
1.122 moko 554:
555: // sort keys
556: qsort(seq, count, sizeof(Hash_seq_item), key_values_are_strings?sort_cmp_string:sort_cmp_double);
557:
558: // reorder hash as required in 'seq'
559: hash.order_clear();
560: if(reverse)
561: for(pos=count-1; pos>=0; pos--)
562: hash.order_next(seq[pos].hash_pair);
563: else
564: for(pos=0; pos<count; pos++)
565: hash.order_next(seq[pos].hash_pair);
566:
567: delete[] seq;
1.123 moko 568: #endif
1.122 moko 569: }
570:
1.105 misha 571: static void _at(Request& r, MethodParams& params) {
1.130 moko 572: HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.105 misha 573: size_t count=hash.count();
574:
1.121 misha 575: // misha@
576: // I do not like that type is checked before whence.
1.133 moko 577: // But I do not like the idea to move it after whence (where process can be called) even more.
1.121 misha 578: AtResultType result_type=AtResultTypeValue;
579: if(params.count() > 1) {
580: const String& stype=params.as_string(1, "type must be string");
581: if(stype == "key")
582: result_type=AtResultTypeKey;
583: else if(stype == "hash")
584: result_type=AtResultTypeHash;
585: else if(stype != "value")
586: throw Exception(PARSER_RUNTIME, &stype, "type must be 'key', 'value' or 'hash'");
587: }
588:
1.166 moko 589: int pos=params.as_index(0, count, r);
1.105 misha 590:
1.106 misha 591: if(count && pos >= 0 && (size_t)pos < count){
1.121 misha 592: switch(result_type) {
593: case AtResultTypeKey:
594: {
1.123 moko 595: #ifdef HASH_ORDER
1.121 misha 596: if(pos == 0) {
1.137 moko 597: r.write(*new VString(*new String(hash.first_key(), String::L_TAINTED)));
1.121 misha 598: } else if((size_t)pos == count-1) {
1.137 moko 599: r.write(*new VString(*new String(hash.last_key(), String::L_TAINTED)));
1.123 moko 600: } else
601: #endif
602: {
1.121 misha 603: for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
604: if(!pos){
1.137 moko 605: r.write(*new VString(*new String(i.key(), String::L_TAINTED)));
1.121 misha 606: break;
607: }
608: }
1.105 misha 609: break;
610: }
1.121 misha 611: case AtResultTypeValue:
612: {
1.123 moko 613: #ifdef HASH_ORDER
1.121 misha 614: if(pos == 0) {
1.137 moko 615: r.write(*hash.first_value());
1.121 misha 616: } else if((size_t)pos == count-1) {
1.137 moko 617: r.write(*hash.last_value());
1.123 moko 618: } else
619: #endif
620: {
1.121 misha 621: for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
622: if(!pos){
1.137 moko 623: r.write(*i.value());
1.121 misha 624: break;
625: }
626: }
627: break;
628: }
629: case AtResultTypeHash:
630: {
1.123 moko 631: #ifdef HASH_ORDER
1.121 misha 632: if(pos == 0) {
1.137 moko 633: r.write(SingleElementHash(hash.first_key(), hash.first_value()));
1.121 misha 634: } else if((size_t)pos == count-1) {
1.137 moko 635: r.write(SingleElementHash(hash.last_key(), hash.last_value()));
1.123 moko 636: } else
637: #endif
638: {
1.121 misha 639: for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
640: if(!pos){
1.137 moko 641: r.write(SingleElementHash(i.key(), i.value()));
1.121 misha 642: break;
643: }
644: }
645: break;
646: }
647: }
1.105 misha 648: }
649: }
650:
1.167 moko 651: static void _set(Request& r, MethodParams& params) {
652: HashStringValue& hash=GET_SELF(r, VHashBase).hash();
653: size_t count=hash.count();
654:
655: int pos=params.as_index(0, count, r);
656:
657: if(count && pos >= 0 && (size_t)pos < count){
658: #ifdef HASH_ORDER
659: if((size_t)pos == count-1) {
660: hash.last_pair()->value=&r.process(params[1]);
661: return;
662: } else
663: #endif
664: {
665: for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
666: if(!pos){
667: i.pair()->value=&r.process(params[1]);
668: return;
669: }
670: }
671: }
672:
673: if(count)
674: throw Exception(PARSER_RUNTIME, 0, "index '%d' is out of range 0..%d", pos, count-1);
675: throw Exception(PARSER_RUNTIME, 0, "index '%d' is out of range: hash is empty", pos);
676: }
677:
1.147 moko 678: extern String table_reverse_name;
679:
680: static void _select(Request& r, MethodParams& params) {
1.154 moko 681: InCycle temp(r);
1.147 moko 682: const String* key_var_name=¶ms.as_string(0, "key-var name must be string");
683: const String* value_var_name=¶ms.as_string(1, "value-var name must be string");
684: Value& vcondition=params.as_expression(2, "condition must be number, bool or expression");
685:
686: if(key_var_name->is_empty()) key_var_name=0;
687: if(value_var_name->is_empty()) value_var_name=0;
688:
689: HashStringValue& source_hash=GET_SELF(r, VHashBase).hash();
690: Value& caller=*r.get_method_frame()->caller();
691:
692: int limit=source_hash.count();
693: bool reverse=false;
1.149 moko 694: bool copy_default=false;
1.147 moko 695:
696: if(params.count()>3)
697: if(HashStringValue* options=params.as_hash(3)) {
698: int valid_options=0;
699: if(Value* vlimit=options->get(sql_limit_name)) {
700: valid_options++;
701: limit=r.process(*vlimit).as_int();
702: }
703: if(Value* vreverse=options->get(table_reverse_name)) {
704: valid_options++;
705: reverse=r.process(*vreverse).as_bool();
706: }
1.149 moko 707: if(Value* vcopy_default=options->get(sql_default_name)) {
708: valid_options++;
709: copy_default=r.process(*vcopy_default).as_bool();
710: }
1.147 moko 711: if(valid_options!=options->count())
712: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
713: }
714:
715: HashStringValue& result_hash=*new HashStringValue();
716:
717: if(limit>0){
1.154 moko 718:
1.147 moko 719: #ifdef HASH_ORDER
720: if(reverse){
721: for(HashStringValue::ReverseIterator i(source_hash); i; i.prev()){
722: if(key_var_name)
723: r.put_element(caller, *key_var_name, new VString(*new String(i.key(), String::L_TAINTED)));
724: if(value_var_name)
725: r.put_element(caller, *value_var_name, i.value());
726:
1.154 moko 727: bool condition=r.process(vcondition).as_bool();
728:
729: if(r.check_skip_break())
730: break;
731:
732: if(condition){
1.147 moko 733: result_hash.put(i.key(), i.value());
734: if(!--limit)
735: break;
736: }
737: }
738: } else {
739: #else
740: {
741: #endif
742: for(HashStringValue::Iterator i(source_hash); i; i.next() ){
743: if(key_var_name)
744: r.put_element(caller, *key_var_name, new VString(*new String(i.key(), String::L_TAINTED)));
745: if(value_var_name)
746: r.put_element(caller, *value_var_name, i.value());
747:
1.154 moko 748: bool condition=r.process(vcondition).as_bool();
749:
750: if(r.check_skip_break())
751: break;
752:
753: if(condition){
1.147 moko 754: result_hash.put(i.key(), i.value());
755: if(!--limit)
756: break;
757: }
758: }
759: }
760: }
761:
1.149 moko 762: VHash *result=new VHash(result_hash);
763: if(copy_default){
764: result->set_default(GET_SELF(r, VHashBase).get_default());
765: }
766:
767: r.write(*result);
1.147 moko 768: }
769:
770: static void _reverse(Request& r, MethodParams& params) {
1.148 moko 771: VHashBase& self=GET_SELF(r, VHashBase);
772: HashStringValue& source_hash=self.hash();
1.147 moko 773: HashStringValue& result_hash=*new HashStringValue();
774:
775: #ifdef HASH_ORDER
776: for(HashStringValue::ReverseIterator i(source_hash); i; i.prev())
777: result_hash.put(i.key(), i.value());
778: #else
779: for(HashStringValue::Iterator i(source_hash); i; i.next() )
780: result_hash.put(i.key(), i.value());
781: #endif
1.148 moko 782:
783: VHashBase& result=*new VHash(result_hash);
784: if(Value* vdefault=self.get_default())
785: result.set_default(vdefault);
786:
787: r.write(result);
1.147 moko 788: }
789:
1.150 moko 790:
791: static void _rename(Request& r, MethodParams& params) {
1.151 moko 792: HashStringValue& hash=GET_SELF(r, VHashBase).hash();
793:
794: if(params.count()>1){
795: const String& key_from=params.as_string(0, "from key must be string");
796: const String& key_to=params.as_string(1, "to key must be string");
797:
798: hash.rename(key_from, key_to);
1.152 moko 799: } else {
1.153 moko 800: HashStringValue* names=params.as_hash(0,"single parameter");
1.150 moko 801:
1.151 moko 802: for(HashStringValue::Iterator i(*names); i; i.next())
803: hash.rename(i.key(), i.value()->as_string());
804: }
1.150 moko 805: }
806:
807:
1.1 paf 808: // constructor
809:
1.57 paf 810: MHash::MHash(): Methoded("hash")
1.39 paf 811: {
1.21 parser 812: // ^hash::create[[copy_from]]
1.57 paf 813: add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1);
1.22 parser 814: // ^hash.add[add_from]
1.57 paf 815: add_native_method("add", Method::CT_DYNAMIC, _create_or_add, 1, 1);
1.22 parser 816: // ^hash.sub[sub_from]
817: add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1);
818: // ^a.union[b] = hash
819: add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1);
1.155 moko 820: // ^a.intersection[b][options hash] = hash
821: add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 2);
1.22 parser 822: // ^a.intersects[b] = bool
823: add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1);
1.25 paf 824:
825: // ^a.delete[key]
1.119 misha 826: add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1);
1.2 parser 827:
1.82 misha 828: // ^a.contains[key]
829: add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1);
830: // backward
831: add_native_method("contain", Method::CT_DYNAMIC, _contains, 1, 1);
1.80 misha 832:
833: // ^hash::sql[query][options hash]
1.33 paf 834: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.2 parser 835:
1.68 paf 836: // ^hash._keys[[column name]]
1.124 moko 837: add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1);
1.16 parser 838:
839: // ^hash._count[]
1.124 moko 840: add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);
1.26 paf 841:
842: // ^hash.foreach[key;value]{code}[delim]
843: add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.105 misha 844:
1.122 moko 845: // ^hash.sort[key;value]{string-key-maker}[[asc|desc]]
846: // ^hash.sort[key;value](numeric-key-maker)[[asc|desc]]
1.147 moko 847: add_native_method("sort", Method::CT_DYNAMIC, _sort, 3, 4);
848:
849: // ^hash.select[key;value](bool-condition)[options hash]
850: add_native_method("select", Method::CT_DYNAMIC, _select, 3, 4);
851:
852: // ^hash.reverse[]
853: add_native_method("reverse", Method::CT_DYNAMIC, _reverse, 0, 0);
1.122 moko 854:
1.121 misha 855: // ^hash._at[first|last[;'key'|'value'|'hash']]
1.167 moko 856: // ^hash._at([-+]index)[['key'|'value'|'hash']]
1.121 misha 857: add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 2);
1.124 moko 858:
1.167 moko 859: // ^hash.set[first|last;value]
860: // ^hash.set([-+]index)[value]
861: add_native_method("set", Method::CT_DYNAMIC, _set, 2, 2);
862:
1.150 moko 863: // ^hash.rename[from;to]
1.151 moko 864: // ^hash.rename[ $.from[to] ... ]
865: add_native_method("rename", Method::CT_DYNAMIC, _rename, 1, 2);
1.150 moko 866:
1.124 moko 867: #ifdef FEATURE_GET_ELEMENT4CALL
868: // aliases without "_"
869: add_native_method("keys", Method::CT_DYNAMIC, _keys, 0, 1);
870: add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
871: add_native_method("at", Method::CT_DYNAMIC, _at, 1, 2);
872: #endif
873:
1.1 paf 874: }
E-mail: