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

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.79    ! misha       8: static const char * const IDENT_HASH_C="$Date: 2006/06/09 19:03:49 $";
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) {
1.79    ! misha      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) {
1.79    ! misha     103:                                error=SQL_Error(PARSER_RUNTIME, "duplicate key");
1.69      paf       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)))
1.78      paf       137:                        {
                    138:                                if(Value* vdefault=vhash_src->get_default())
                    139:                                {
                    140:                                        if(vdefault->is_defined())
                    141:                                        {
                    142:                                                self.set_default(vdefault);
                    143:                                        }
                    144:                                }
                    145:                        }
1.66      paf       146:                }
1.20      parser    147:        }
                    148: }
1.22      parser    149: 
1.57      paf       150: static void remove_key_from(
                    151:                                                        HashStringValue::key_type key, 
                    152:                                                        HashStringValue::value_type /*value*/, 
                    153:                                                        HashStringValue* dest) {
                    154:        dest->remove(key);
                    155: }
                    156: static void _sub(Request& r, MethodParams& params) {
1.66      paf       157:        Value& vsrc=params.as_no_junction(0, "param must be hash");
                    158:        if(HashStringValue* src=vsrc.get_hash()) {
                    159:                HashStringValue* self=&(GET_SELF(r, VHash).hash());
                    160:                if(src==self) { // same: clearing
                    161:                        self->clear();
                    162:                        return;
                    163:                }
1.77      paf       164:                src->for_each<HashStringValue*>(remove_key_from, self);
1.66      paf       165:        }
1.57      paf       166: }
                    167: 
                    168: static void copy_all_dontoverwrite_to(
                    169:                                                                  HashStringValue::key_type key, 
                    170:                                                                  HashStringValue::value_type value, 
                    171:                                                                  HashStringValue* dest) {
                    172:        dest->put_dont_replace(key, value);
1.22      parser    173: }
1.57      paf       174: static void _union(Request& r, MethodParams& params) {
1.22      parser    175:        // dest = copy of self
1.57      paf       176:        Value& result=*new VHash(GET_SELF(r, VHash).hash());
1.22      parser    177:        // dest += b
1.66      paf       178:        Value& vsrc=params.as_no_junction(0, "param must be hash");
                    179:        if(HashStringValue* src=vsrc.get_hash())
1.77      paf       180:                src->for_each<HashStringValue*>(copy_all_dontoverwrite_to, result.get_hash());
1.22      parser    181: 
                    182:        // return result
                    183:        r.write_no_lang(result);
                    184: }
                    185: 
                    186: #ifndef DOXYGEN
                    187: struct Copy_intersection_to_info {
1.57      paf       188:        HashStringValue* b;
                    189:        HashStringValue* dest;
1.22      parser    190: };
                    191: #endif
1.57      paf       192: static void copy_intersection_to(
                    193:                                                                 HashStringValue::key_type key, 
                    194:                                                                 HashStringValue::value_type value, 
                    195:                                                                 Copy_intersection_to_info *info) {
                    196:        if(info->b->get(key))
                    197:                info->dest->put_dont_replace(key, value);
1.22      parser    198: }
1.57      paf       199: static void _intersection(Request& r, MethodParams& params) {
                    200:        Value& result=*new VHash;
1.22      parser    201:        // dest += b
1.57      paf       202:        Value& vb=params.as_no_junction(0, "param must be hash");
                    203:        if(HashStringValue* b=vb.get_hash()) {
                    204:                Copy_intersection_to_info info={b, result.get_hash()};
1.77      paf       205:                GET_SELF(r, VHash).hash().for_each<Copy_intersection_to_info*>(copy_intersection_to, &info);
1.22      parser    206:        }
                    207: 
                    208:        // return result
1.57      paf       209:        r.write_no_lang(result);
1.22      parser    210: }
                    211: 
1.57      paf       212: static bool intersects(
                    213:                                           HashStringValue::key_type key, 
                    214:                                           HashStringValue::value_type /*value*/, 
                    215:                                           HashStringValue* b) {
                    216:        return b->get(key)!=0;
1.22      parser    217: }
                    218: 
1.57      paf       219: static void _intersects(Request& r, MethodParams& params) {
                    220:        bool result=false;
                    221: 
                    222:        Value& vb=params.as_no_junction(0, "param must be hash");
                    223:        if(HashStringValue* b=vb.get_hash())
1.77      paf       224:                result=GET_SELF(r, VHash).hash().first_that<HashStringValue*>(intersects, b)!=0;
1.22      parser    225: 
                    226:        // return result
1.57      paf       227:        r.write_no_lang(*new VBool(result));
1.22      parser    228: }
                    229: 
                    230: 
1.71      paf       231: extern String sql_bind_name;
1.57      paf       232: extern String sql_limit_name;
                    233: extern String sql_offset_name;
                    234: extern String sql_default_name;
                    235: extern String sql_distinct_name;
1.71      paf       236: extern int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders);
                    237: extern void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders);
                    238: 
1.57      paf       239: static void _sql(Request& r, MethodParams& params) {
                    240:        Value& statement=params.as_junction(0, "statement must be code");
1.2       parser    241: 
1.71      paf       242:        HashStringValue* bind=0;
1.2       parser    243:        ulong limit=0;
1.33      paf       244:        ulong offset=0;
1.49      paf       245:        bool distinct=false;
1.57      paf       246:        if(params.count()>1) {
                    247:                Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.76      paf       248:                if(voptions.is_defined() && !voptions.is_string())
1.57      paf       249:                        if(HashStringValue* options=voptions.get_hash()) {
1.49      paf       250:                                int valid_options=0;
1.71      paf       251:                                if(Value* vbind=options->get(sql_bind_name)) {
                    252:                                        valid_options++;
                    253:                                        bind=vbind->get_hash();
                    254:                                }
1.57      paf       255:                                if(Value* vlimit=options->get(sql_limit_name)) {
1.49      paf       256:                                        valid_options++;
1.37      paf       257:                                        limit=(ulong)r.process_to_value(*vlimit).as_double();
1.49      paf       258:                                }
1.57      paf       259:                                if(Value* voffset=options->get(sql_offset_name)) {
1.49      paf       260:                                        valid_options++;
1.37      paf       261:                                        offset=(ulong)r.process_to_value(*voffset).as_double();
1.49      paf       262:                                }
1.57      paf       263:                                if(Value* vdistinct=options->get(sql_distinct_name)) {
1.49      paf       264:                                        valid_options++;
                    265:                                        distinct=r.process_to_value(*vdistinct).as_bool();
                    266:                                }
1.57      paf       267:                                if(valid_options!=options->count())
1.79    ! misha     268:                                        throw Exception(PARSER_RUNTIME,
1.57      paf       269:                                                0,
1.49      paf       270:                                                "called with invalid option");
1.33      paf       271:                        } else
1.79    ! misha     272:                                throw Exception(PARSER_RUNTIME,
1.57      paf       273:                                        0,
1.33      paf       274:                                        "options must be hash");
1.2       parser    275:        }
                    276: 
1.71      paf       277:        SQL_Driver::Placeholder* placeholders=0;
                    278:        uint placeholders_count=0;
                    279:        if(bind)
                    280:                placeholders_count=marshal_binds(*bind, placeholders);
                    281: 
1.57      paf       282:        Temp_lang temp_lang(r, String::L_SQL);
1.37      paf       283:        const String& statement_string=r.process_to_string(statement);
1.57      paf       284:        const char* statement_cstr=
                    285:                statement_string.cstr(String::L_UNSPECIFIED, r.connection());
                    286:        HashStringValue& hash=GET_SELF(r, VHash).hash();
1.11      parser    287:        hash.clear();   
1.57      paf       288:        Hash_sql_event_handlers handlers(
1.49      paf       289:                statement_string, statement_cstr, 
                    290:                distinct,
                    291:                hash);
1.57      paf       292:        r.connection()->query(
1.70      paf       293:                statement_cstr, 
1.73      paf       294:                placeholders_count, placeholders,
1.70      paf       295:                offset, limit,
1.45      paf       296:                handlers,
                    297:                statement_string);
1.71      paf       298: 
                    299:        if(bind)
                    300:                unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.2       parser    301: }
                    302: 
1.57      paf       303: static void keys_collector(
                    304:                           HashStringValue::key_type key, 
1.61      paf       305:                           HashStringValue::value_type, 
1.57      paf       306:                           Table *table) {
                    307:        Table::element_type row(new ArrayString);
                    308:        *row+=new String(key, String::L_TAINTED);
                    309:        *table+=row;
1.9       parser    310: }
1.68      paf       311: static void _keys(Request& r, MethodParams& params) {
                    312:        const String* keys_column_name;
                    313:        if(params.count()>0)
                    314:                keys_column_name=&params.as_string(0, "column name must be string");
                    315:        else 
                    316:                keys_column_name=new String("key");
                    317: 
1.57      paf       318:        Table::columns_type columns(new ArrayString);
1.68      paf       319:        *columns+=keys_column_name;
1.57      paf       320:        Table* table=new Table(columns);
1.9       parser    321: 
1.77      paf       322:        GET_SELF(r, VHash).hash().for_each<Table*>(keys_collector, table);
1.9       parser    323: 
1.57      paf       324:        r.write_no_lang(*new VTable(table));
1.9       parser    325: }
                    326: 
1.57      paf       327: static void _count(Request& r, MethodParams&) {
                    328:        r.write_no_lang(*new VInt(GET_SELF(r, VHash).hash().count()));
1.16      parser    329: }
                    330: 
1.57      paf       331: static void _delete(Request& r, MethodParams& params) {
1.25      paf       332: 
1.57      paf       333:        GET_SELF(r, VHash).hash().remove(params.as_string(0, "key must be string"));
1.25      paf       334: }
                    335: 
1.26      paf       336: #ifndef DOXYGEN
1.59      paf       337: struct Foreach_info {
1.26      paf       338:        Request *r;
                    339:        const String* key_var_name;
                    340:        const String* value_var_name;
1.57      paf       341:        Value* body_code;
                    342:        Value* delim_maybe_code;
1.26      paf       343: 
1.57      paf       344:        VString* vkey;
1.26      paf       345:        bool need_delim;
                    346: };
                    347: #endif
1.77      paf       348: static bool one_foreach_cycle(                                            
1.57      paf       349:                              HashStringValue::key_type akey, 
                    350:                              HashStringValue::value_type avalue, 
1.59      paf       351:                              Foreach_info *info) {
1.57      paf       352:        info->vkey->set_string(*new String(akey, String::L_TAINTED));
                    353:        Value& ncontext=*info->r->get_method_frame()->caller();
1.74      paf       354:        ncontext.put_element(ncontext, *info->key_var_name, info->vkey, false);
                    355:        ncontext.put_element(ncontext, *info->value_var_name, avalue, false);
1.57      paf       356: 
                    357:        StringOrValue sv_processed=info->r->process(*info->body_code);
1.77      paf       358:        Request::Skip lskip=info->r->get_skip(); info->r->set_skip(Request::SKIP_NOTHING);
1.57      paf       359:        const String* s_processed=sv_processed.get_string();
                    360:        if(info->delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body
                    361:                if(info->need_delim) // need delim & iteration produced string?
                    362:                        info->r->write_pass_lang(info->r->process(*info->delim_maybe_code));
                    363:                info->need_delim=true;
1.26      paf       364:        }
1.57      paf       365:        info->r->write_pass_lang(sv_processed);
1.77      paf       366: 
                    367:        return lskip==Request::SKIP_BREAK;
1.26      paf       368: }
1.57      paf       369: static void _foreach(Request& r, MethodParams& params) {
1.77      paf       370:        Temp_hash_value<const String::Body, void*> 
                    371:                cycle_data_setter(r.classes_conf, cycle_data_name, /*any not null flag*/&r);
                    372: 
1.63      paf       373:        Foreach_info info={
                    374:                &r,
                    375:                &params.as_string(0, "key-var name must be string"),
                    376:                &params.as_string(1, "value-var name must be string"),
                    377:                &params.as_junction(2, "body must be code"),
                    378:                params.count()>3?params.get(3):0,
1.64      paf       379:                /*vkey=*/new VString,
1.63      paf       380:                false
                    381:        };
1.57      paf       382: 
                    383:        VHash& self=GET_SELF(r, VHash);
                    384:        HashStringValue& hash=self.hash();
1.28      paf       385:        VHash_lock lock(self);
1.77      paf       386:        hash.first_that<Foreach_info*>(one_foreach_cycle, &info);
1.26      paf       387: }
                    388: 
1.1       paf       389: // constructor
                    390: 
1.57      paf       391: MHash::MHash(): Methoded("hash") 
1.39      paf       392: {
1.21      parser    393:        // ^hash::create[[copy_from]]
1.57      paf       394:        add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1);
1.22      parser    395:        // ^hash.add[add_from]
1.57      paf       396:        add_native_method("add", Method::CT_DYNAMIC, _create_or_add, 1, 1);
1.22      parser    397:        // ^hash.sub[sub_from]
                    398:        add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1);
                    399:        // ^a.union[b] = hash
                    400:        add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1);
                    401:        // ^a.intersection[b] = hash
                    402:        add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 1);
                    403:        // ^a.intersects[b] = bool
                    404:        add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1);
1.25      paf       405: 
                    406:        // ^a.delete[key]
                    407:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
1.2       parser    408: 
1.71      paf       409:        // ^hash:sql[query][options hash]
1.33      paf       410:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.2       parser    411: 
1.68      paf       412:        // ^hash._keys[[column name]]
                    413:        add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1);    
1.16      parser    414: 
                    415:        // ^hash._count[]
                    416:        add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);  
1.26      paf       417: 
                    418:        // ^hash.foreach[key;value]{code}[delim]
                    419:        add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.1       paf       420: }

E-mail: