Annotation of parser3/src/classes/hash.C, revision 1.147

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.147   ! moko       20: volatile const char * IDENT_HASH_C="$Id: hash.C,v 1.146 2020/12/15 17:10:28 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.130     moko      241: static void copy_intersection_to(HashStringValue::key_type key, HashStringValue::value_type value, Copy_intersection_to_info *info) {
1.57      paf       242:        if(info->b->get(key))
                    243:                info->dest->put_dont_replace(key, value);
1.22      parser    244: }
1.57      paf       245: static void _intersection(Request& r, MethodParams& params) {
                    246:        Value& result=*new VHash;
1.22      parser    247:        // dest += b
1.114     misha     248:        if(HashStringValue* b=params.as_hash(0, "param")) {
1.57      paf       249:                Copy_intersection_to_info info={b, result.get_hash()};
1.130     moko      250:                GET_SELF(r, VHashBase).hash().for_each<Copy_intersection_to_info*>(copy_intersection_to, &info);
1.22      parser    251:        }
                    252: 
                    253:        // return result
1.137     moko      254:        r.write(result);
1.22      parser    255: }
                    256: 
1.130     moko      257: static bool intersects(        HashStringValue::key_type key, HashStringValue::value_type /*value*/, HashStringValue* b) {
1.57      paf       258:        return b->get(key)!=0;
1.22      parser    259: }
                    260: 
1.57      paf       261: static void _intersects(Request& r, MethodParams& params) {
                    262:        bool result=false;
                    263: 
1.119     misha     264:        if(HashStringValue* b=params.as_hash(0, "param")) {
1.130     moko      265:                HashStringValue* self=&(GET_SELF(r, VHashBase).hash());
1.120     misha     266:                if(b==self) {
1.137     moko      267:                        r.write(VBool::get(true));
1.120     misha     268:                        return;
                    269:                }
1.119     misha     270:                result=self->first_that<HashStringValue*>(intersects, b)!=0;
                    271:        }
1.22      parser    272: 
                    273:        // return result
1.137     moko      274:        r.write(VBool::get(result));
1.22      parser    275: }
                    276: 
                    277: 
1.81      misha     278: extern Table2hash_value_type get_value_type(Value& vvalue_type);
1.71      paf       279: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
                    280: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
                    281: 
1.57      paf       282: static void _sql(Request& r, MethodParams& params) {
                    283:        Value& statement=params.as_junction(0, "statement must be code");
1.2       parser    284: 
1.71      paf       285:        HashStringValue* bind=0;
1.87      misha     286:        ulong limit=SQL_NO_LIMIT;
1.33      paf       287:        ulong offset=0;
1.49      paf       288:        bool distinct=false;
1.81      misha     289:        Table2hash_value_type value_type=C_HASH;
1.110     misha     290:        if(params.count()>1)
1.114     misha     291:                if(HashStringValue* options=params.as_hash(1, "sql options")) {
1.110     misha     292:                        int valid_options=0;
                    293:                        if(Value* vbind=options->get(sql_bind_name)) {
                    294:                                valid_options++;
                    295:                                bind=vbind->get_hash();
                    296:                        }
                    297:                        if(Value* vlimit=options->get(sql_limit_name)) {
                    298:                                valid_options++;
1.133     moko      299:                                limit=(ulong)r.process(*vlimit).as_double();
1.110     misha     300:                        }
                    301:                        if(Value* voffset=options->get(sql_offset_name)) {
                    302:                                valid_options++;
1.133     moko      303:                                offset=(ulong)r.process(*voffset).as_double();
1.110     misha     304:                        }
                    305:                        if(Value* vdistinct=options->get(sql_distinct_name)) {
                    306:                                valid_options++;
1.133     moko      307:                                distinct=r.process(*vdistinct).as_bool();
1.110     misha     308:                        }
                    309:                        if(Value* vvalue_type=options->get(sql_value_type_name)) {
                    310:                                valid_options++;
1.133     moko      311:                                value_type=get_value_type(r.process(*vvalue_type));
1.110     misha     312:                        }
                    313:                        if(valid_options!=options->count())
                    314:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    315:                }
1.2       parser    316: 
1.71      paf       317:        SQL_Driver::Placeholder* placeholders=0;
                    318:        uint placeholders_count=0;
                    319:        if(bind)
                    320:                placeholders_count=marshal_binds(*bind, placeholders);
                    321: 
1.37      paf       322:        const String& statement_string=r.process_to_string(statement);
1.136     moko      323:        const char* statement_cstr=statement_string.untaint_cstr(String::L_SQL, r.connection());
1.97      misha     324: 
1.130     moko      325:        HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.145     moko      326:        hash.clear();
                    327:        Hash_sql_event_handlers handlers(distinct, hash, value_type);
                    328: 
                    329:        r.connection()->query(statement_cstr, placeholders_count, placeholders, offset, limit, handlers, statement_string);
1.71      paf       330: 
                    331:        if(bind)
                    332:                unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.2       parser    333: }
                    334: 
1.130     moko      335: static void keys_collector(HashStringValue::key_type key, HashStringValue::value_type, Table *table) {
1.100     misha     336:        Table::element_type row(new ArrayString(1));
1.57      paf       337:        *row+=new String(key, String::L_TAINTED);
                    338:        *table+=row;
1.9       parser    339: }
1.68      paf       340: static void _keys(Request& r, MethodParams& params) {
                    341:        const String* keys_column_name;
                    342:        if(params.count()>0)
1.81      misha     343:                keys_column_name=&params.as_string(0, COLUMN_NAME_MUST_BE_STRING);
1.68      paf       344:        else 
                    345:                keys_column_name=new String("key");
                    346: 
1.101     misha     347:        Table::columns_type columns(new ArrayString(1));
1.68      paf       348:        *columns+=keys_column_name;
1.57      paf       349:        Table* table=new Table(columns);
1.9       parser    350: 
1.130     moko      351:        GET_SELF(r, VHashBase).hash().for_each<Table*>(keys_collector, table);
1.9       parser    352: 
1.137     moko      353:        r.write(*new VTable(table));
1.9       parser    354: }
                    355: 
1.57      paf       356: static void _count(Request& r, MethodParams&) {
1.137     moko      357:        r.write(*new VInt(GET_SELF(r, VHashBase).hash().count()));
1.16      parser    358: }
                    359: 
1.57      paf       360: static void _delete(Request& r, MethodParams& params) {
1.119     misha     361:        if(params.count()>0)
1.130     moko      362:                GET_SELF(r, VHashBase).hash().remove(params.as_string(0, "key must be string"));
1.119     misha     363:        else
1.130     moko      364:                GET_SELF(r, VHashBase).hash().clear();
1.25      paf       365: }
                    366: 
1.82      misha     367: static void _contains(Request& r, MethodParams& params) {
1.130     moko      368:        VHashBase& self=GET_SELF(r, VHashBase);
1.129     moko      369:        const String& key_name=params.as_string(0, "key must be string");
                    370:        bool result=SYMBOLS_EQ(key_name,_DEFAULT_SYMBOL) ? (self.get_default() != 0) : self.hash().contains(key_name);
1.137     moko      371:        r.write(VBool::get(result));
1.80      misha     372: }
                    373: 
1.57      paf       374: static void _foreach(Request& r, MethodParams& params) {
1.102     misha     375:        InCycle temp(r);
1.77      paf       376: 
1.128     moko      377:        const String* key_var_name=&params.as_string(0, "key-var name must be string");
                    378:        const String* value_var_name=&params.as_string(1, "value-var name must be string");
                    379:        Value* body_code=&params.as_junction(2, "body must be code");
1.134     moko      380:        Value* delim_maybe_code=params.count()>3?&params[3]:0;
1.128     moko      381:        Value& caller=*r.get_method_frame()->caller();
1.85      misha     382: 
1.128     moko      383:        if(key_var_name->is_empty()) key_var_name=0;
                    384:        if(value_var_name->is_empty()) value_var_name=0;
1.57      paf       385: 
1.130     moko      386:        HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.128     moko      387: 
                    388:        if(delim_maybe_code){ // delimiter set
                    389:                bool need_delim=false;;
                    390:                for(HashStringValue::Iterator i(hash); i; i.next()){
                    391:                        if(key_var_name){
                    392:                                VString* vkey=new VString(*new String(i.key(), String::L_TAINTED));
                    393:                                r.put_element(caller, *key_var_name, vkey);
                    394:                        }
                    395: 
                    396:                        if(value_var_name)
                    397:                                r.put_element(caller, *value_var_name, i.value());
                    398: 
1.132     moko      399:                        Value& sv_processed=r.process(*body_code);
1.140     moko      400:                        TempSkip4Delimiter skip(r);
1.128     moko      401: 
                    402:                        const String* s_processed=sv_processed.get_string();
                    403:                        if(s_processed && !s_processed->is_empty()) { // we have body
                    404:                                if(need_delim) // need delim & iteration produced string?
1.137     moko      405:                                        r.write(r.process(*delim_maybe_code));
1.128     moko      406:                                else
                    407:                                        need_delim=true;
                    408:                        }
                    409: 
1.137     moko      410:                        r.write(sv_processed);
1.128     moko      411: 
1.140     moko      412:                        if(skip.check_break())
1.128     moko      413:                                break;
                    414:                }
                    415:        } else {
                    416:                for(HashStringValue::Iterator i(hash); i; i.next()){
                    417:                        if(key_var_name){
                    418:                                VString* vkey=new VString(*new String(i.key(), String::L_TAINTED));
                    419:                                r.put_element(caller, *key_var_name, vkey);
                    420:                        }
                    421: 
                    422:                        if(value_var_name)
                    423:                                r.put_element(caller, *value_var_name, i.value());
                    424: 
                    425:                        r.process_write(*body_code);
                    426: 
1.140     moko      427:                        if(r.check_skip_break())
1.128     moko      428:                                break;
                    429:                }
                    430:        }
1.26      paf       431: }
                    432: 
1.121     misha     433: enum AtResultType {
                    434:        AtResultTypeValue = 0,
                    435:        AtResultTypeKey = 1,
                    436:        AtResultTypeHash = 2
                    437: };
                    438: 
                    439: inline Value& SingleElementHash(String::Body akey, Value* avalue) {
                    440:        Value& result=*new VHash;
                    441:        result.put_element(*new String(akey, String::L_TAINTED), avalue);
                    442:        return result;
                    443: }
                    444: 
1.122     moko      445: #ifndef DOXYGEN
1.143     moko      446: struct Hash_seq_item : public PA_Allocated {
1.122     moko      447:        HashStringValue::Pair *hash_pair;
                    448:        union {
                    449:                const char *c_str;
                    450:                double d;
                    451:        } value;
                    452: };
                    453: #endif
                    454: static int sort_cmp_string(const void *a, const void *b) {
                    455:        return strcmp(
                    456:                static_cast<const Hash_seq_item *>(a)->value.c_str,
                    457:                static_cast<const Hash_seq_item *>(b)->value.c_str
                    458:        );
                    459: }
                    460: static int sort_cmp_double(const void *a, const void *b) {
                    461:        double va=static_cast<const Hash_seq_item *>(a)->value.d;
                    462:        double vb=static_cast<const Hash_seq_item *>(b)->value.d;
                    463:        if(va<vb)
                    464:                return -1;
                    465:        else if(va>vb)
                    466:                return +1;
                    467:        else 
                    468:                return 0;
                    469: }
                    470: static void _sort(Request& r, MethodParams& params){
1.123     moko      471: #ifdef HASH_ORDER
1.122     moko      472:        const String& key_var_name=params.as_string(0, "key-var name must be string");
                    473:        const String& value_var_name=params.as_string(1, "value-var name must be string");
                    474:        Value& key_maker=params.as_junction(2, "key-maker must be code");
1.139     moko      475:        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      476: 
                    477:        const String* key_var=key_var_name.is_empty()? 0 : &key_var_name;
                    478:        const String* value_var=value_var_name.is_empty()? 0 : &value_var_name;
                    479:        VMethodFrame* context=r.get_method_frame()->caller();
                    480: 
1.130     moko      481:        HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.122     moko      482:        int count=hash.count();
                    483: 
1.143     moko      484:        Hash_seq_item* seq=new Hash_seq_item[count];
1.122     moko      485:        int pos=0;
                    486:        bool key_values_are_strings=true;
                    487: 
                    488:        for(HashStringValue::Iterator i(hash); i; i.next(), pos++ ){
                    489:                if(key_var)
                    490:                        r.put_element(*context, *key_var, new VString(*new String(i.key(), String::L_TAINTED)));
                    491:                if(value_var)
                    492:                        r.put_element(*context, *value_var, i.value());
                    493:        
1.133     moko      494:                Value& value=r.process(key_maker);
1.122     moko      495:                if(pos==0) // determining key values type by first one
                    496:                        key_values_are_strings=value.is_string();
                    497: 
                    498:                seq[pos].hash_pair=i.pair();
                    499:                if(key_values_are_strings)
                    500:                        seq[pos].value.c_str=value.as_string().cstr();
                    501:                else
                    502:                        seq[pos].value.d=value.as_expr_result().as_double();
                    503:        }
                    504: 
                    505:        // @todo: handle this elsewhere
                    506:        if(r.charsets.source().NAME()=="KOI8-R" && key_values_are_strings)
                    507:                for(pos=0; pos<count; pos++)
                    508:                        if(*seq[pos].value.c_str)
1.131     moko      509:                                seq[pos].value.c_str=Charset::transcode(seq[pos].value.c_str, r.charsets.source(), pa_UTF8_charset).cstr();
1.122     moko      510: 
                    511:        // sort keys
                    512:        qsort(seq, count, sizeof(Hash_seq_item), key_values_are_strings?sort_cmp_string:sort_cmp_double);
                    513: 
                    514:        // reorder hash as required in 'seq'
                    515:        hash.order_clear();
                    516:        if(reverse)
                    517:                for(pos=count-1; pos>=0; pos--)
                    518:                        hash.order_next(seq[pos].hash_pair);
                    519:        else
                    520:                for(pos=0; pos<count; pos++)
                    521:                        hash.order_next(seq[pos].hash_pair);
                    522: 
                    523:        delete[] seq;
1.123     moko      524: #endif
1.122     moko      525: }
                    526: 
1.105     misha     527: static void _at(Request& r, MethodParams& params) {
1.130     moko      528:        HashStringValue& hash=GET_SELF(r, VHashBase).hash();
1.105     misha     529:        size_t count=hash.count();
                    530: 
                    531:        int pos=0;
                    532: 
1.121     misha     533:        // misha@
                    534:        // I do not like that type is checked before whence.
1.133     moko      535:        // But I do not like the idea to move it after whence (where process can be called) even more.
1.121     misha     536:        AtResultType result_type=AtResultTypeValue;
                    537:        if(params.count() > 1) {
                    538:                const String& stype=params.as_string(1, "type must be string");
                    539:                if(stype == "key")
                    540:                        result_type=AtResultTypeKey;
                    541:                else if(stype == "hash")
                    542:                        result_type=AtResultTypeHash;
                    543:                else if(stype != "value")
                    544:                        throw Exception(PARSER_RUNTIME, &stype, "type must be 'key', 'value' or 'hash'");
                    545:        }
                    546: 
1.134     moko      547:        Value& vwhence=params[0];
1.121     misha     548:        if(vwhence.is_string()) {
1.105     misha     549:                const String& swhence=*vwhence.get_string();
                    550:                if(swhence == "last")
                    551:                        pos=count-1;
                    552:                else if(swhence != "first")
                    553:                        throw Exception(PARSER_RUNTIME,
                    554:                                &swhence,
                    555:                                "whence must be 'first', 'last' or expression");
                    556:        } else {
1.133     moko      557:                pos=r.process(vwhence).as_int();
1.105     misha     558:                if(pos < 0)
                    559:                        pos+=count;
                    560:        }
                    561: 
1.106     misha     562:        if(count && pos >= 0 && (size_t)pos < count){
1.121     misha     563:                switch(result_type) {
                    564:                        case AtResultTypeKey:
                    565:                                {
1.123     moko      566: #ifdef HASH_ORDER
1.121     misha     567:                                        if(pos == 0) {
1.137     moko      568:                                                r.write(*new VString(*new String(hash.first_key(), String::L_TAINTED)));
1.121     misha     569:                                        } else if((size_t)pos == count-1) {
1.137     moko      570:                                                r.write(*new VString(*new String(hash.last_key(), String::L_TAINTED)));
1.123     moko      571:                                        } else
                    572: #endif
                    573:                                        {
1.121     misha     574:                                                for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
                    575:                                                        if(!pos){
1.137     moko      576:                                                                r.write(*new VString(*new String(i.key(), String::L_TAINTED)));
1.121     misha     577:                                                                break;
                    578:                                                        }
                    579:                                        }
1.105     misha     580:                                        break;
                    581:                                }
1.121     misha     582:                        case AtResultTypeValue:
                    583:                                {
1.123     moko      584: #ifdef HASH_ORDER
1.121     misha     585:                                        if(pos == 0) {
1.137     moko      586:                                                r.write(*hash.first_value());
1.121     misha     587:                                        } else if((size_t)pos == count-1) {
1.137     moko      588:                                                r.write(*hash.last_value());
1.123     moko      589:                                        } else
                    590: #endif
                    591:                                        {
1.121     misha     592:                                                for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
                    593:                                                        if(!pos){
1.137     moko      594:                                                                r.write(*i.value());
1.121     misha     595:                                                                break;
                    596:                                                        }
                    597:                                        }
                    598:                                        break;
                    599:                                }
                    600:                        case AtResultTypeHash:
                    601:                                {
1.123     moko      602: #ifdef HASH_ORDER
1.121     misha     603:                                        if(pos == 0) {
1.137     moko      604:                                                r.write(SingleElementHash(hash.first_key(), hash.first_value()));
1.121     misha     605:                                        } else if((size_t)pos == count-1) {
1.137     moko      606:                                                r.write(SingleElementHash(hash.last_key(), hash.last_value()));
1.123     moko      607:                                        } else
                    608: #endif
                    609:                                        {
1.121     misha     610:                                                for(HashStringValue::Iterator i(hash); i; i.next(), pos-- )
                    611:                                                        if(!pos){
1.137     moko      612:                                                                r.write(SingleElementHash(i.key(), i.value()));
1.121     misha     613:                                                                break;
                    614:                                                        }
                    615:                                        }
                    616:                                        break;
                    617:                                }
                    618:                }
1.105     misha     619:        }
                    620: }
                    621: 
1.147   ! moko      622: extern String table_reverse_name;
        !           623: 
        !           624: static void _select(Request& r, MethodParams& params) {
        !           625:        const String* key_var_name=&params.as_string(0, "key-var name must be string");
        !           626:        const String* value_var_name=&params.as_string(1, "value-var name must be string");
        !           627:        Value& vcondition=params.as_expression(2, "condition must be number, bool or expression");
        !           628: 
        !           629:        if(key_var_name->is_empty()) key_var_name=0;
        !           630:        if(value_var_name->is_empty()) value_var_name=0;
        !           631: 
        !           632:        HashStringValue& source_hash=GET_SELF(r, VHashBase).hash();
        !           633:        Value& caller=*r.get_method_frame()->caller();
        !           634: 
        !           635:        int limit=source_hash.count();
        !           636:        bool reverse=false;
        !           637: 
        !           638:        if(params.count()>3)
        !           639:                if(HashStringValue* options=params.as_hash(3)) {
        !           640:                        int valid_options=0;
        !           641:                        if(Value* vlimit=options->get(sql_limit_name)) {
        !           642:                                valid_options++;
        !           643:                                limit=r.process(*vlimit).as_int();
        !           644:                        }
        !           645:                        if(Value* vreverse=options->get(table_reverse_name)) {
        !           646:                                valid_options++;
        !           647:                                reverse=r.process(*vreverse).as_bool();
        !           648:                        }
        !           649:                        if(valid_options!=options->count())
        !           650:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
        !           651:                }
        !           652: 
        !           653:        HashStringValue& result_hash=*new HashStringValue();
        !           654: 
        !           655:        if(limit>0){
        !           656: #ifdef HASH_ORDER
        !           657:                if(reverse){
        !           658:                        for(HashStringValue::ReverseIterator i(source_hash); i; i.prev()){
        !           659:                                if(key_var_name)
        !           660:                                        r.put_element(caller, *key_var_name, new VString(*new String(i.key(), String::L_TAINTED)));
        !           661:                                if(value_var_name)
        !           662:                                        r.put_element(caller, *value_var_name, i.value());
        !           663: 
        !           664:                                if(r.process(vcondition).as_bool()){
        !           665:                                        result_hash.put(i.key(), i.value());
        !           666:                                        if(!--limit)
        !           667:                                                break;
        !           668:                                }
        !           669:                        }
        !           670:                } else {
        !           671: #else
        !           672:                {
        !           673: #endif
        !           674:                        for(HashStringValue::Iterator i(source_hash); i; i.next() ){
        !           675:                                if(key_var_name)
        !           676:                                        r.put_element(caller, *key_var_name, new VString(*new String(i.key(), String::L_TAINTED)));
        !           677:                                if(value_var_name)
        !           678:                                        r.put_element(caller, *value_var_name, i.value());
        !           679: 
        !           680:                                if(r.process(vcondition).as_bool()){
        !           681:                                        result_hash.put(i.key(), i.value());
        !           682:                                        if(!--limit)
        !           683:                                                break;
        !           684:                                }
        !           685:                        }
        !           686:                }
        !           687:        }
        !           688: 
        !           689:        r.write(*new VHash(result_hash));
        !           690: }
        !           691: 
        !           692: static void _reverse(Request& r, MethodParams& params) {
        !           693:        HashStringValue& source_hash=GET_SELF(r, VHashBase).hash();
        !           694:        HashStringValue& result_hash=*new HashStringValue();
        !           695: 
        !           696: #ifdef HASH_ORDER
        !           697:        for(HashStringValue::ReverseIterator i(source_hash); i; i.prev())
        !           698:                result_hash.put(i.key(), i.value());
        !           699: #else
        !           700:        for(HashStringValue::Iterator i(source_hash); i; i.next() )
        !           701:                result_hash.put(i.key(), i.value());
        !           702: #endif
        !           703:        r.write(*new VHash(result_hash));
        !           704: }
        !           705: 
1.1       paf       706: // constructor
                    707: 
1.57      paf       708: MHash::MHash(): Methoded("hash") 
1.39      paf       709: {
1.21      parser    710:        // ^hash::create[[copy_from]]
1.57      paf       711:        add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1);
1.22      parser    712:        // ^hash.add[add_from]
1.57      paf       713:        add_native_method("add", Method::CT_DYNAMIC, _create_or_add, 1, 1);
1.22      parser    714:        // ^hash.sub[sub_from]
                    715:        add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1);
                    716:        // ^a.union[b] = hash
                    717:        add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1);
                    718:        // ^a.intersection[b] = hash
                    719:        add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 1);
                    720:        // ^a.intersects[b] = bool
                    721:        add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1);
1.25      paf       722: 
                    723:        // ^a.delete[key]
1.119     misha     724:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1);
1.2       parser    725: 
1.82      misha     726:        // ^a.contains[key]
                    727:        add_native_method("contains", Method::CT_DYNAMIC, _contains, 1, 1);
                    728:        // backward
                    729:        add_native_method("contain", Method::CT_DYNAMIC, _contains, 1, 1);
1.80      misha     730: 
                    731:        // ^hash::sql[query][options hash]
1.33      paf       732:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.2       parser    733: 
1.68      paf       734:        // ^hash._keys[[column name]]
1.124     moko      735:        add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1);
1.16      parser    736: 
                    737:        // ^hash._count[]
1.124     moko      738:        add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);
1.26      paf       739: 
                    740:        // ^hash.foreach[key;value]{code}[delim]
                    741:        add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.105     misha     742: 
1.122     moko      743:        // ^hash.sort[key;value]{string-key-maker}[[asc|desc]]
                    744:        // ^hash.sort[key;value](numeric-key-maker)[[asc|desc]]
1.147   ! moko      745:        add_native_method("sort", Method::CT_DYNAMIC, _sort, 3, 4);
        !           746: 
        !           747:        // ^hash.select[key;value](bool-condition)[options hash]
        !           748:        add_native_method("select", Method::CT_DYNAMIC, _select, 3, 4);
        !           749: 
        !           750:        // ^hash.reverse[]
        !           751:        add_native_method("reverse", Method::CT_DYNAMIC, _reverse, 0, 0);
1.122     moko      752: 
1.121     misha     753:        // ^hash._at[first|last[;'key'|'value'|'hash']]
                    754:        // ^hash._at([-+]offset)[['key'|'value'|'hash']]
                    755:        add_native_method("_at", Method::CT_DYNAMIC, _at, 1, 2);
1.124     moko      756: 
                    757: #ifdef FEATURE_GET_ELEMENT4CALL
                    758:        // aliases without "_"
                    759:        add_native_method("keys", Method::CT_DYNAMIC, _keys, 0, 1);
                    760:        add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
                    761:        add_native_method("at", Method::CT_DYNAMIC, _at, 1, 2);
                    762: #endif
                    763: 
1.1       paf       764: }

E-mail: