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

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

E-mail: