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

1.1       paf         1: /** @file
                      2:        Parser: @b hash parser class.
                      3: 
1.57    ! paf         4:        Copyright (c) 2001-2003 ArtLebedev Group (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: 
1.57    ! paf         8: static const char* IDENT_HASH_C="$Date: 2003/06/26 11:51:30 $";
1.1       paf         9: 
                     10: #include "classes.h"
1.57    ! paf        11: #include "pa_vmethod_frame.h"
        !            12: 
1.1       paf        13: #include "pa_request.h"
                     14: #include "pa_vhash.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.1       paf        21: // class
                     22: 
1.57    ! paf        23: class MHash: public Methoded {
1.2       parser     24: public: // VStateless_class
1.57    ! paf        25:        Value* create_new_value() { return new VHash(); }
1.2       parser     26: 
1.1       paf        27: public:
1.57    ! paf        28:        MHash();
1.1       paf        29: public: // Methoded
1.2       parser     30:        bool used_directly() { return true; }
1.1       paf        31: };
                     32: 
1.57    ! paf        33: // global variable
        !            34: 
        !            35: DECLARE_CLASS_VAR(hash, new MHash, 0);
        !            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:        const String& statement_string; const char* statement_cstr;
        !            42:        bool distinct;
        !            43:        HashStringValue& rows_hash;
        !            44:        HashStringValue* row_hash;
        !            45:        int column_index;
        !            46:        ArrayString columns;
1.11      parser     47: public:
1.57    ! paf        48:        Hash_sql_event_handlers(
        !            49:                const String& astatement_string, const char* astatement_cstr,
1.49      paf        50:                bool adistinct,
1.57    ! paf        51:                HashStringValue& arows_hash): 
        !            52:                statement_string(astatement_string), statement_cstr(astatement_cstr),
1.49      paf        53:                distinct(adistinct),
1.11      parser     54:                rows_hash(arows_hash),
1.57    ! paf        55:                row_hash(0),
        !            56:                column_index(0) {
1.11      parser     57:        }
1.57    ! paf        58:        bool add_column(SQL_Error& error, const char* str, size_t length) {
1.53      paf        59:                try {
1.57    ! paf        60:                        columns+=new String(str, length, true);
1.53      paf        61:                        return false;
                     62:                } catch(...) {
                     63:                        error=SQL_Error("exception occured in Hash_sql_event_handlers::add_column");
                     64:                        return true;
                     65:                }
1.11      parser     66:        }
1.53      paf        67:        bool before_rows(SQL_Error& error) { 
1.57    ! paf        68:                if(columns.count()<=1) {
1.53      paf        69:                        error=SQL_Error("parser.runtime",
1.57    ! paf        70:                                /*method_name,*/
1.11      parser     71:                                "column count must be more than 1 to create a hash");
1.53      paf        72:                        return true;
                     73:                }
                     74: 
                     75:                return false;
1.11      parser     76:        }
1.53      paf        77:        bool add_row(SQL_Error& /*error*/) {
1.11      parser     78:                column_index=0;
1.53      paf        79:                return false;
1.11      parser     80:        }
1.57    ! paf        81:        bool add_row_cell(SQL_Error& error, const char *ptr, size_t length) {
1.53      paf        82:                try {
1.57    ! paf        83:                        String& cell=*new String;
        !            84:                        if(length)
        !            85:                                cell.append_know_length(ptr, length, String::L_TAINTED);
1.53      paf        86:                        if(column_index==0) {
1.57    ! paf        87:                                VHash* row_vhash=new VHash;
        !            88:                                row_hash=&row_vhash->hash();
        !            89:                                if(rows_hash.put_dont_replace(cell, row_vhash)) // put. existed?
1.53      paf        90:                                        if(!distinct) {
                     91:                                                error=SQL_Error("parser.runtime",
1.57    ! paf        92:                                                        /*cell,*/
1.53      paf        93:                                                        "duplicate key");
                     94:                                                return true;
                     95:                                        }
                     96:                        } else
1.57    ! paf        97:                                row_hash->put(*columns[column_index], new VString(cell));
1.53      paf        98:                        column_index++;
                     99: 
                    100:                        return false;
                    101:                } catch(...) {
                    102:                        error=SQL_Error("exception occured in Hash_sql_event_handlers::add_row_cell");
                    103:                        return true;
                    104:                }
1.11      parser    105:        }
                    106: 
                    107: };
                    108: #endif
                    109: 
1.57    ! paf       110: static void copy_all_overwrite_to(
        !           111:                                                                  HashStringValue::key_type key, 
        !           112:                                                                  HashStringValue::value_type value, 
        !           113:                                                                  HashStringValue* dest) {
        !           114:        dest->put(key, value);
        !           115: }
        !           116: static void _create_or_add(Request& r, MethodParams& params) {
        !           117:        if(params.count()) {
        !           118:                Value& vb=params.as_no_junction(0, "param must be hash");
        !           119:                if(HashStringValue* b=vb.get_hash())
        !           120:                        b->for_each(copy_all_overwrite_to, &(GET_SELF(r, VHash).hash()));
1.20      parser    121:        }
                    122: }
1.22      parser    123: 
1.57    ! paf       124: static void remove_key_from(
        !           125:                                                        HashStringValue::key_type key, 
        !           126:                                                        HashStringValue::value_type /*value*/, 
        !           127:                                                        HashStringValue* dest) {
        !           128:        dest->remove(key);
        !           129: }
        !           130: static void _sub(Request& r, MethodParams& params) {
        !           131:        Value& vb=params.as_no_junction(0, "param must be hash");
        !           132:        if(HashStringValue* b=vb.get_hash())
        !           133:                b->for_each(remove_key_from, &GET_SELF(r, VHash).hash());
        !           134: }
        !           135: 
        !           136: static void copy_all_dontoverwrite_to(
        !           137:                                                                  HashStringValue::key_type key, 
        !           138:                                                                  HashStringValue::value_type value, 
        !           139:                                                                  HashStringValue* dest) {
        !           140:        dest->put_dont_replace(key, value);
1.22      parser    141: }
1.57    ! paf       142: static void _union(Request& r, MethodParams& params) {
1.22      parser    143:        // dest = copy of self
1.57    ! paf       144:        Value& result=*new VHash(GET_SELF(r, VHash).hash());
1.22      parser    145:        // dest += b
1.57    ! paf       146:        Value& vb=params.as_no_junction(0, "param must be hash");
        !           147:        if(HashStringValue* b=vb.get_hash())
        !           148:                b->for_each(copy_all_dontoverwrite_to, result.get_hash());
1.22      parser    149: 
                    150:        // return result
                    151:        r.write_no_lang(result);
                    152: }
                    153: 
                    154: #ifndef DOXYGEN
                    155: struct Copy_intersection_to_info {
1.57    ! paf       156:        HashStringValue* b;
        !           157:        HashStringValue* dest;
1.22      parser    158: };
                    159: #endif
1.57    ! paf       160: static void copy_intersection_to(
        !           161:                                                                 HashStringValue::key_type key, 
        !           162:                                                                 HashStringValue::value_type value, 
        !           163:                                                                 Copy_intersection_to_info *info) {
        !           164:        if(info->b->get(key))
        !           165:                info->dest->put_dont_replace(key, value);
1.22      parser    166: }
1.57    ! paf       167: static void _intersection(Request& r, MethodParams& params) {
        !           168:        Value& result=*new VHash;
1.22      parser    169:        // dest += b
1.57    ! paf       170:        Value& vb=params.as_no_junction(0, "param must be hash");
        !           171:        if(HashStringValue* b=vb.get_hash()) {
        !           172:                Copy_intersection_to_info info={b, result.get_hash()};
        !           173:                GET_SELF(r, VHash).hash().for_each(copy_intersection_to, &info);
1.22      parser    174:        }
                    175: 
                    176:        // return result
1.57    ! paf       177:        r.write_no_lang(result);
1.22      parser    178: }
                    179: 
1.57    ! paf       180: static bool intersects(
        !           181:                                           HashStringValue::key_type key, 
        !           182:                                           HashStringValue::value_type /*value*/, 
        !           183:                                           HashStringValue* b) {
        !           184:        return b->get(key)!=0;
1.22      parser    185: }
                    186: 
1.57    ! paf       187: static void _intersects(Request& r, MethodParams& params) {
        !           188:        bool result=false;
        !           189: 
        !           190:        Value& vb=params.as_no_junction(0, "param must be hash");
        !           191:        if(HashStringValue* b=vb.get_hash())
        !           192:                result=GET_SELF(r, VHash).hash().first_that(intersects, b)!=0;
1.22      parser    193: 
                    194:        // return result
1.57    ! paf       195:        r.write_no_lang(*new VBool(result));
1.22      parser    196: }
                    197: 
                    198: 
1.57    ! paf       199: extern String sql_limit_name;
        !           200: extern String sql_offset_name;
        !           201: extern String sql_default_name;
        !           202: extern String sql_distinct_name;
        !           203: static void _sql(Request& r, MethodParams& params) {
        !           204:        Value& statement=params.as_junction(0, "statement must be code");
1.2       parser    205: 
                    206:        ulong limit=0;
1.33      paf       207:        ulong offset=0;
1.49      paf       208:        bool distinct=false;
1.57    ! paf       209:        if(params.count()>1) {
        !           210:                Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.43      paf       211:                if(!voptions.is_string())
1.57    ! paf       212:                        if(HashStringValue* options=voptions.get_hash()) {
1.49      paf       213:                                int valid_options=0;
1.57    ! paf       214:                                if(Value* vlimit=options->get(sql_limit_name)) {
1.49      paf       215:                                        valid_options++;
1.37      paf       216:                                        limit=(ulong)r.process_to_value(*vlimit).as_double();
1.49      paf       217:                                }
1.57    ! paf       218:                                if(Value* voffset=options->get(sql_offset_name)) {
1.49      paf       219:                                        valid_options++;
1.37      paf       220:                                        offset=(ulong)r.process_to_value(*voffset).as_double();
1.49      paf       221:                                }
1.57    ! paf       222:                                if(Value* vdistinct=options->get(sql_distinct_name)) {
1.49      paf       223:                                        valid_options++;
                    224:                                        distinct=r.process_to_value(*vdistinct).as_bool();
                    225:                                }
1.57    ! paf       226:                                if(valid_options!=options->count())
1.49      paf       227:                                        throw Exception("parser.runtime",
1.57    ! paf       228:                                                0,
1.49      paf       229:                                                "called with invalid option");
1.33      paf       230:                        } else
1.36      paf       231:                                throw Exception("parser.runtime",
1.57    ! paf       232:                                        0,
1.33      paf       233:                                        "options must be hash");
1.2       parser    234:        }
                    235: 
1.57    ! paf       236:        Temp_lang temp_lang(r, String::L_SQL);
1.37      paf       237:        const String& statement_string=r.process_to_string(statement);
1.57    ! paf       238:        const char* statement_cstr=
        !           239:                statement_string.cstr(String::L_UNSPECIFIED, r.connection());
        !           240:        HashStringValue& hash=GET_SELF(r, VHash).hash();
1.11      parser    241:        hash.clear();   
1.57    ! paf       242:        Hash_sql_event_handlers handlers(
1.49      paf       243:                statement_string, statement_cstr, 
                    244:                distinct,
                    245:                hash);
1.57    ! paf       246:        r.connection()->query(
1.23      parser    247:                statement_cstr, offset, limit,
1.45      paf       248:                handlers,
                    249:                statement_string);
1.2       parser    250: }
                    251: 
1.57    ! paf       252: static void keys_collector(
        !           253:                           HashStringValue::key_type key, 
        !           254:                           HashStringValue::value_type value, 
        !           255:                           Table *table) {
        !           256:        Table::element_type row(new ArrayString);
        !           257:        *row+=new String(key, String::L_TAINTED);
        !           258:        *table+=row;
1.9       parser    259: }
1.57    ! paf       260: static void _keys(Request& r, MethodParams&) {
        !           261:        Table::columns_type columns(new ArrayString);
        !           262:        *columns+=new String("key");
        !           263:        Table* table=new Table(columns);
1.9       parser    264: 
1.57    ! paf       265:        GET_SELF(r, VHash).hash().for_each(keys_collector, table);
1.9       parser    266: 
1.57    ! paf       267:        r.write_no_lang(*new VTable(table));
1.9       parser    268: }
                    269: 
1.57    ! paf       270: static void _count(Request& r, MethodParams&) {
        !           271:        r.write_no_lang(*new VInt(GET_SELF(r, VHash).hash().count()));
1.16      parser    272: }
                    273: 
1.57    ! paf       274: static void _delete(Request& r, MethodParams& params) {
1.25      paf       275: 
1.57    ! paf       276:        GET_SELF(r, VHash).hash().remove(params.as_string(0, "key must be string"));
1.25      paf       277: }
                    278: 
1.26      paf       279: #ifndef DOXYGEN
                    280: struct Foreach_info {
                    281:        Request *r;
                    282:        const String* key_var_name;
                    283:        const String* value_var_name;
1.57    ! paf       284:        Value* body_code;
        !           285:        Value* delim_maybe_code;
1.26      paf       286: 
1.57    ! paf       287:        VString* vkey;
1.26      paf       288:        bool need_delim;
                    289: };
                    290: #endif
1.57    ! paf       291: static void one_foreach_cycle(                                            
        !           292:                              HashStringValue::key_type akey, 
        !           293:                              HashStringValue::value_type avalue, 
        !           294:                              Foreach_info *info) {
        !           295:        info->vkey->set_string(*new String(akey, String::L_TAINTED));
        !           296:        Value& ncontext=*info->r->get_method_frame()->caller();
        !           297:        ncontext.put_element(*info->key_var_name, info->vkey, false);
        !           298:        ncontext.put_element(*info->value_var_name, avalue, false);
        !           299: 
        !           300:        StringOrValue sv_processed=info->r->process(*info->body_code);
        !           301:        const String* s_processed=sv_processed.get_string();
        !           302:        if(info->delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body
        !           303:                if(info->need_delim) // need delim & iteration produced string?
        !           304:                        info->r->write_pass_lang(info->r->process(*info->delim_maybe_code));
        !           305:                info->need_delim=true;
1.26      paf       306:        }
1.57    ! paf       307:        info->r->write_pass_lang(sv_processed);
1.26      paf       308: }
1.57    ! paf       309: static void _foreach(Request& r, MethodParams& params) {
        !           310:        Foreach_info info={0};
        !           311:        info.r=&r;
        !           312:        info.key_var_name=&params.as_string(0, "key-var name must be string");
        !           313:        info.value_var_name=&params.as_string(1, "value-var name must be string");
        !           314:        info.body_code=&params.as_junction(2, "body must be code");
        !           315:        info.delim_maybe_code=params.count()>3?params.get(3):0;
        !           316:        info.vkey=new VString;
        !           317: 
        !           318:        VHash& self=GET_SELF(r, VHash);
        !           319:        HashStringValue& hash=self.hash();
1.28      paf       320:        VHash_lock lock(self);
                    321:        hash.for_each(one_foreach_cycle, &info);
1.26      paf       322: }
                    323: 
1.1       paf       324: // constructor
                    325: 
1.57    ! paf       326: MHash::MHash(): Methoded("hash") 
1.39      paf       327: {
1.21      parser    328:        // ^hash::create[[copy_from]]
1.57    ! paf       329:        add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1);
1.22      parser    330:        // ^hash.add[add_from]
1.57    ! paf       331:        add_native_method("add", Method::CT_DYNAMIC, _create_or_add, 1, 1);
1.22      parser    332:        // ^hash.sub[sub_from]
                    333:        add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1);
                    334:        // ^a.union[b] = hash
                    335:        add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1);
                    336:        // ^a.intersection[b] = hash
                    337:        add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 1);
                    338:        // ^a.intersects[b] = bool
                    339:        add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1);
1.25      paf       340: 
                    341:        // ^a.delete[key]
                    342:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
1.2       parser    343: 
1.33      paf       344:        // ^hash:sql[query][$.limit(1) $.offset(2)]
                    345:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.2       parser    346: 
1.14      parser    347:        // ^hash._keys[]
1.13      parser    348:        add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 0);    
1.16      parser    349: 
                    350:        // ^hash._count[]
                    351:        add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);  
1.26      paf       352: 
                    353:        // ^hash.foreach[key;value]{code}[delim]
                    354:        add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.1       paf       355: }

E-mail: