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

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

E-mail: