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

1.1       paf         1: /** @file
                      2:        Parser: @b hash parser class.
                      3: 
1.75      paf         4:        Copyright (c) 2001-2005 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.77    ! paf         8: static const char * const IDENT_HASH_C="$Date: 2006/04/09 12:25:03 $";
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.74      paf        25:        Value* create_new_value(Pool&, HashStringValue&) { 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.77    ! paf        37: // externs
        !            38: 
        !            39: extern String cycle_data_name;
        !            40: 
1.1       paf        41: // methods
                     42: 
1.11      parser     43: #ifndef DOXYGEN
1.52      paf        44: class Hash_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.57      paf        45:        const String& statement_string; const char* statement_cstr;
                     46:        bool distinct;
                     47:        HashStringValue& rows_hash;
                     48:        HashStringValue* row_hash;
                     49:        int column_index;
                     50:        ArrayString columns;
1.67      paf        51:        bool only_one_column;
                     52:        static VBool only_one_column_value;
1.11      parser     53: public:
1.57      paf        54:        Hash_sql_event_handlers(
                     55:                const String& astatement_string, const char* astatement_cstr,
1.49      paf        56:                bool adistinct,
1.57      paf        57:                HashStringValue& arows_hash): 
                     58:                statement_string(astatement_string), statement_cstr(astatement_cstr),
1.49      paf        59:                distinct(adistinct),
1.11      parser     60:                rows_hash(arows_hash),
1.57      paf        61:                row_hash(0),
1.67      paf        62:                column_index(0),
                     63:                only_one_column(false) {
1.11      parser     64:        }
1.57      paf        65:        bool add_column(SQL_Error& error, const char* str, size_t length) {
1.53      paf        66:                try {
1.57      paf        67:                        columns+=new String(str, length, true);
1.53      paf        68:                        return false;
                     69:                } catch(...) {
                     70:                        error=SQL_Error("exception occured in Hash_sql_event_handlers::add_column");
                     71:                        return true;
                     72:                }
1.11      parser     73:        }
1.53      paf        74:        bool before_rows(SQL_Error& error) { 
1.67      paf        75:                if(columns.count()<1) {
                     76:                        error=SQL_Error("parser.runtime", "no columns");
1.53      paf        77:                        return true;
                     78:                }
1.67      paf        79:                only_one_column=columns.count()==1;
1.53      paf        80: 
                     81:                return false;
1.11      parser     82:        }
1.53      paf        83:        bool add_row(SQL_Error& /*error*/) {
1.11      parser     84:                column_index=0;
1.53      paf        85:                return false;
1.11      parser     86:        }
1.57      paf        87:        bool add_row_cell(SQL_Error& error, const char *ptr, size_t length) {
1.53      paf        88:                try {
1.57      paf        89:                        String& cell=*new String;
                     90:                        if(length)
                     91:                                cell.append_know_length(ptr, length, String::L_TAINTED);
1.69      paf        92:                        bool duplicate=false;
                     93:                        if(only_one_column) {
                     94:                                duplicate=rows_hash.put_dont_replace(cell, &only_one_column_value);  // put. existed?
                     95:                        } else if(column_index==0) {
1.57      paf        96:                                VHash* row_vhash=new VHash;
                     97:                                row_hash=&row_vhash->hash();
1.69      paf        98:                                duplicate=rows_hash.put_dont_replace(cell, row_vhash); // put. existed?
1.53      paf        99:                        } else
1.57      paf       100:                                row_hash->put(*columns[column_index], new VString(cell));
1.69      paf       101: 
                    102:                        if(duplicate & !distinct) {
                    103:                                error=SQL_Error("parser.runtime", "duplicate key");
                    104:                                return true;
                    105:                        }
                    106: 
1.53      paf       107:                        column_index++;
                    108:                        return false;
                    109:                } catch(...) {
                    110:                        error=SQL_Error("exception occured in Hash_sql_event_handlers::add_row_cell");
                    111:                        return true;
                    112:                }
1.11      parser    113:        }
                    114: 
                    115: };
1.67      paf       116: VBool Hash_sql_event_handlers::only_one_column_value(true);
                    117: 
1.11      parser    118: #endif
                    119: 
1.57      paf       120: static void copy_all_overwrite_to(
                    121:                                                                  HashStringValue::key_type key, 
                    122:                                                                  HashStringValue::value_type value, 
                    123:                                                                  HashStringValue* dest) {
                    124:        dest->put(key, value);
                    125: }
                    126: static void _create_or_add(Request& r, MethodParams& params) {
                    127:        if(params.count()) {
1.66      paf       128:                Value& vsrc=params.as_no_junction(0, "param must be hash");
                    129:                if(HashStringValue* src=vsrc.get_hash()) {
1.72      paf       130:                        VHash& self=GET_SELF(r, VHash);
                    131:                        HashStringValue* self_hash=&(self.hash());
                    132:                        if(src==self_hash) // same: doing nothing
1.66      paf       133:                                return;
1.77    ! paf       134:                        src->for_each<HashStringValue*>(copy_all_overwrite_to, self_hash);
1.72      paf       135: 
                    136:                        if(VHash* vhash_src=static_cast<VHash*>(vsrc.as(VHASH_TYPE, false)))
                    137:                                self.set_default(vhash_src->get_default());
1.66      paf       138:                }
1.20      parser    139:        }
                    140: }
1.22      parser    141: 
1.57      paf       142: static void remove_key_from(
                    143:                                                        HashStringValue::key_type key, 
                    144:                                                        HashStringValue::value_type /*value*/, 
                    145:                                                        HashStringValue* dest) {
                    146:        dest->remove(key);
                    147: }
                    148: static void _sub(Request& r, MethodParams& params) {
1.66      paf       149:        Value& vsrc=params.as_no_junction(0, "param must be hash");
                    150:        if(HashStringValue* src=vsrc.get_hash()) {
                    151:                HashStringValue* self=&(GET_SELF(r, VHash).hash());
                    152:                if(src==self) { // same: clearing
                    153:                        self->clear();
                    154:                        return;
                    155:                }
1.77    ! paf       156:                src->for_each<HashStringValue*>(remove_key_from, self);
1.66      paf       157:        }
1.57      paf       158: }
                    159: 
                    160: static void copy_all_dontoverwrite_to(
                    161:                                                                  HashStringValue::key_type key, 
                    162:                                                                  HashStringValue::value_type value, 
                    163:                                                                  HashStringValue* dest) {
                    164:        dest->put_dont_replace(key, value);
1.22      parser    165: }
1.57      paf       166: static void _union(Request& r, MethodParams& params) {
1.22      parser    167:        // dest = copy of self
1.57      paf       168:        Value& result=*new VHash(GET_SELF(r, VHash).hash());
1.22      parser    169:        // dest += b
1.66      paf       170:        Value& vsrc=params.as_no_junction(0, "param must be hash");
                    171:        if(HashStringValue* src=vsrc.get_hash())
1.77    ! paf       172:                src->for_each<HashStringValue*>(copy_all_dontoverwrite_to, result.get_hash());
1.22      parser    173: 
                    174:        // return result
                    175:        r.write_no_lang(result);
                    176: }
                    177: 
                    178: #ifndef DOXYGEN
                    179: struct Copy_intersection_to_info {
1.57      paf       180:        HashStringValue* b;
                    181:        HashStringValue* dest;
1.22      parser    182: };
                    183: #endif
1.57      paf       184: static void copy_intersection_to(
                    185:                                                                 HashStringValue::key_type key, 
                    186:                                                                 HashStringValue::value_type value, 
                    187:                                                                 Copy_intersection_to_info *info) {
                    188:        if(info->b->get(key))
                    189:                info->dest->put_dont_replace(key, value);
1.22      parser    190: }
1.57      paf       191: static void _intersection(Request& r, MethodParams& params) {
                    192:        Value& result=*new VHash;
1.22      parser    193:        // dest += b
1.57      paf       194:        Value& vb=params.as_no_junction(0, "param must be hash");
                    195:        if(HashStringValue* b=vb.get_hash()) {
                    196:                Copy_intersection_to_info info={b, result.get_hash()};
1.77    ! paf       197:                GET_SELF(r, VHash).hash().for_each<Copy_intersection_to_info*>(copy_intersection_to, &info);
1.22      parser    198:        }
                    199: 
                    200:        // return result
1.57      paf       201:        r.write_no_lang(result);
1.22      parser    202: }
                    203: 
1.57      paf       204: static bool intersects(
                    205:                                           HashStringValue::key_type key, 
                    206:                                           HashStringValue::value_type /*value*/, 
                    207:                                           HashStringValue* b) {
                    208:        return b->get(key)!=0;
1.22      parser    209: }
                    210: 
1.57      paf       211: static void _intersects(Request& r, MethodParams& params) {
                    212:        bool result=false;
                    213: 
                    214:        Value& vb=params.as_no_junction(0, "param must be hash");
                    215:        if(HashStringValue* b=vb.get_hash())
1.77    ! paf       216:                result=GET_SELF(r, VHash).hash().first_that<HashStringValue*>(intersects, b)!=0;
1.22      parser    217: 
                    218:        // return result
1.57      paf       219:        r.write_no_lang(*new VBool(result));
1.22      parser    220: }
                    221: 
                    222: 
1.71      paf       223: extern String sql_bind_name;
1.57      paf       224: extern String sql_limit_name;
                    225: extern String sql_offset_name;
                    226: extern String sql_default_name;
                    227: extern String sql_distinct_name;
1.71      paf       228: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
                    229: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
                    230: 
1.57      paf       231: static void _sql(Request& r, MethodParams& params) {
                    232:        Value& statement=params.as_junction(0, "statement must be code");
1.2       parser    233: 
1.71      paf       234:        HashStringValue* bind=0;
1.2       parser    235:        ulong limit=0;
1.33      paf       236:        ulong offset=0;
1.49      paf       237:        bool distinct=false;
1.57      paf       238:        if(params.count()>1) {
                    239:                Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.76      paf       240:                if(voptions.is_defined() && !voptions.is_string())
1.57      paf       241:                        if(HashStringValue* options=voptions.get_hash()) {
1.49      paf       242:                                int valid_options=0;
1.71      paf       243:                                if(Value* vbind=options->get(sql_bind_name)) {
                    244:                                        valid_options++;
                    245:                                        bind=vbind->get_hash();
                    246:                                }
1.57      paf       247:                                if(Value* vlimit=options->get(sql_limit_name)) {
1.49      paf       248:                                        valid_options++;
1.37      paf       249:                                        limit=(ulong)r.process_to_value(*vlimit).as_double();
1.49      paf       250:                                }
1.57      paf       251:                                if(Value* voffset=options->get(sql_offset_name)) {
1.49      paf       252:                                        valid_options++;
1.37      paf       253:                                        offset=(ulong)r.process_to_value(*voffset).as_double();
1.49      paf       254:                                }
1.57      paf       255:                                if(Value* vdistinct=options->get(sql_distinct_name)) {
1.49      paf       256:                                        valid_options++;
                    257:                                        distinct=r.process_to_value(*vdistinct).as_bool();
                    258:                                }
1.57      paf       259:                                if(valid_options!=options->count())
1.49      paf       260:                                        throw Exception("parser.runtime",
1.57      paf       261:                                                0,
1.49      paf       262:                                                "called with invalid option");
1.33      paf       263:                        } else
1.36      paf       264:                                throw Exception("parser.runtime",
1.57      paf       265:                                        0,
1.33      paf       266:                                        "options must be hash");
1.2       parser    267:        }
                    268: 
1.71      paf       269:        SQL_Driver::Placeholder* placeholders=0;
                    270:        uint placeholders_count=0;
                    271:        if(bind)
                    272:                placeholders_count=marshal_binds(*bind, placeholders);
                    273: 
1.57      paf       274:        Temp_lang temp_lang(r, String::L_SQL);
1.37      paf       275:        const String& statement_string=r.process_to_string(statement);
1.57      paf       276:        const char* statement_cstr=
                    277:                statement_string.cstr(String::L_UNSPECIFIED, r.connection());
                    278:        HashStringValue& hash=GET_SELF(r, VHash).hash();
1.11      parser    279:        hash.clear();   
1.57      paf       280:        Hash_sql_event_handlers handlers(
1.49      paf       281:                statement_string, statement_cstr, 
                    282:                distinct,
                    283:                hash);
1.57      paf       284:        r.connection()->query(
1.70      paf       285:                statement_cstr, 
1.73      paf       286:                placeholders_count, placeholders,
1.70      paf       287:                offset, limit,
1.45      paf       288:                handlers,
                    289:                statement_string);
1.71      paf       290: 
                    291:        if(bind)
                    292:                unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.2       parser    293: }
                    294: 
1.57      paf       295: static void keys_collector(
                    296:                           HashStringValue::key_type key, 
1.61      paf       297:                           HashStringValue::value_type, 
1.57      paf       298:                           Table *table) {
                    299:        Table::element_type row(new ArrayString);
                    300:        *row+=new String(key, String::L_TAINTED);
                    301:        *table+=row;
1.9       parser    302: }
1.68      paf       303: static void _keys(Request& r, MethodParams& params) {
                    304:        const String* keys_column_name;
                    305:        if(params.count()>0)
                    306:                keys_column_name=&params.as_string(0, "column name must be string");
                    307:        else 
                    308:                keys_column_name=new String("key");
                    309: 
1.57      paf       310:        Table::columns_type columns(new ArrayString);
1.68      paf       311:        *columns+=keys_column_name;
1.57      paf       312:        Table* table=new Table(columns);
1.9       parser    313: 
1.77    ! paf       314:        GET_SELF(r, VHash).hash().for_each<Table*>(keys_collector, table);
1.9       parser    315: 
1.57      paf       316:        r.write_no_lang(*new VTable(table));
1.9       parser    317: }
                    318: 
1.57      paf       319: static void _count(Request& r, MethodParams&) {
                    320:        r.write_no_lang(*new VInt(GET_SELF(r, VHash).hash().count()));
1.16      parser    321: }
                    322: 
1.57      paf       323: static void _delete(Request& r, MethodParams& params) {
1.25      paf       324: 
1.57      paf       325:        GET_SELF(r, VHash).hash().remove(params.as_string(0, "key must be string"));
1.25      paf       326: }
                    327: 
1.26      paf       328: #ifndef DOXYGEN
1.59      paf       329: struct Foreach_info {
1.26      paf       330:        Request *r;
                    331:        const String* key_var_name;
                    332:        const String* value_var_name;
1.57      paf       333:        Value* body_code;
                    334:        Value* delim_maybe_code;
1.26      paf       335: 
1.57      paf       336:        VString* vkey;
1.26      paf       337:        bool need_delim;
                    338: };
                    339: #endif
1.77    ! paf       340: static bool one_foreach_cycle(                                            
1.57      paf       341:                              HashStringValue::key_type akey, 
                    342:                              HashStringValue::value_type avalue, 
1.59      paf       343:                              Foreach_info *info) {
1.57      paf       344:        info->vkey->set_string(*new String(akey, String::L_TAINTED));
                    345:        Value& ncontext=*info->r->get_method_frame()->caller();
1.74      paf       346:        ncontext.put_element(ncontext, *info->key_var_name, info->vkey, false);
                    347:        ncontext.put_element(ncontext, *info->value_var_name, avalue, false);
1.57      paf       348: 
                    349:        StringOrValue sv_processed=info->r->process(*info->body_code);
1.77    ! paf       350:        Request::Skip lskip=info->r->get_skip(); info->r->set_skip(Request::SKIP_NOTHING);
1.57      paf       351:        const String* s_processed=sv_processed.get_string();
                    352:        if(info->delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body
                    353:                if(info->need_delim) // need delim & iteration produced string?
                    354:                        info->r->write_pass_lang(info->r->process(*info->delim_maybe_code));
                    355:                info->need_delim=true;
1.26      paf       356:        }
1.57      paf       357:        info->r->write_pass_lang(sv_processed);
1.77    ! paf       358: 
        !           359:        return lskip==Request::SKIP_BREAK;
1.26      paf       360: }
1.57      paf       361: static void _foreach(Request& r, MethodParams& params) {
1.77    ! paf       362:        Temp_hash_value<const String::Body, void*> 
        !           363:                cycle_data_setter(r.classes_conf, cycle_data_name, /*any not null flag*/&r);
        !           364: 
1.63      paf       365:        Foreach_info info={
                    366:                &r,
                    367:                &params.as_string(0, "key-var name must be string"),
                    368:                &params.as_string(1, "value-var name must be string"),
                    369:                &params.as_junction(2, "body must be code"),
                    370:                params.count()>3?params.get(3):0,
1.64      paf       371:                /*vkey=*/new VString,
1.63      paf       372:                false
                    373:        };
1.57      paf       374: 
                    375:        VHash& self=GET_SELF(r, VHash);
                    376:        HashStringValue& hash=self.hash();
1.28      paf       377:        VHash_lock lock(self);
1.77    ! paf       378:        hash.first_that<Foreach_info*>(one_foreach_cycle, &info);
1.26      paf       379: }
                    380: 
1.1       paf       381: // constructor
                    382: 
1.57      paf       383: MHash::MHash(): Methoded("hash") 
1.39      paf       384: {
1.21      parser    385:        // ^hash::create[[copy_from]]
1.57      paf       386:        add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1);
1.22      parser    387:        // ^hash.add[add_from]
1.57      paf       388:        add_native_method("add", Method::CT_DYNAMIC, _create_or_add, 1, 1);
1.22      parser    389:        // ^hash.sub[sub_from]
                    390:        add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1);
                    391:        // ^a.union[b] = hash
                    392:        add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1);
                    393:        // ^a.intersection[b] = hash
                    394:        add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 1);
                    395:        // ^a.intersects[b] = bool
                    396:        add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1);
1.25      paf       397: 
                    398:        // ^a.delete[key]
                    399:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
1.2       parser    400: 
1.71      paf       401:        // ^hash:sql[query][options hash]
1.33      paf       402:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.2       parser    403: 
1.68      paf       404:        // ^hash._keys[[column name]]
                    405:        add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1);    
1.16      parser    406: 
                    407:        // ^hash._count[]
                    408:        add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);  
1.26      paf       409: 
                    410:        // ^hash.foreach[key;value]{code}[delim]
                    411:        add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.1       paf       412: }

E-mail: