Annotation of parser3/src/classes/table.C, revision 1.172.2.14.2.1

1.20      paf         1: /** @file
1.47      paf         2:        Parser: @b table parser class.
1.20      paf         3: 
1.172.2.2  paf         4:        Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.144     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.154     paf         6: */
1.20      paf         7: 
1.172.2.14.2.  (paf        8:): static const char* IDENT_TABLE_C="$Date: 2003/03/08 12:22:06 $";
1.1       paf         9: 
1.105     parser     10: #include "classes.h"
1.172.2.4  paf        11: #include "pa_vmethod_frame.h"
                     12: 
1.16      paf        13: #include "pa_common.h"
1.1       paf        14: #include "pa_request.h"
                     15: #include "pa_vtable.h"
1.6       paf        16: #include "pa_vint.h"
1.51      paf        17: #include "pa_sql_connection.h"
1.72      paf        18: #include "pa_vbool.h"
1.1       paf        19: 
1.66      paf        20: // class
                     21: 
1.172.2.9  paf        22: class MTable: public Methoded {
1.66      paf        23: public: // VStateless_class
1.172.2.1  paf        24:        ValuePtr create_new_value() { return ValuePtr(new VTable()); }
1.66      paf        25: 
                     26: public:
1.172.2.6  paf        27:        MTable();
1.70      paf        28: 
                     29: public: // Methoded
1.66      paf        30:        bool used_directly() { return true; }
                     31: };
1.1       paf        32: 
1.172.2.6  paf        33: // global variable
                     34: 
1.172.2.11  paf        35: DECLARE_CLASS_VAR(table, new MTable, 0);
1.172.2.7  paf        36: 
                     37: // defines for globals
                     38: 
                     39: #define SQL_LIMIT_NAME "limit"
                     40: #define SQL_OFFSET_NAME "offset"
                     41: #define SQL_DEFAULT_NAME "default"
                     42: #define SQL_DISTINCT_NAME "distinct"
                     43: 
                     44: // globals
                     45: 
1.172.2.14.2.  (paf       46:): const String& sql_limit_name(new String(SQL_LIMIT_NAME));
                     47:): const String& sql_offset_name(new String(SQL_OFFSET_NAME));
                     48:): const String& sql_default_name(new String(SQL_DEFAULT_NAME));
                     49:): const String& sql_distinct_name(new String(SQL_DISTINCT_NAME));
1.172.2.6  paf        50: 
1.1       paf        51: // methods
1.66      paf        52: 
1.172.2.14.2.  (paf       53:): static void get_copy_options(Request& r, const String& method_name, MethodParams* params, int param_index,
1.157     paf        54:                                                         const Table& source,
                     55:                                                         int& offset,
                     56:                                                         int& limit) {
1.172.2.14.2.  (paf       57:):      =r.pool();
1.172.2.6  paf        58: 
1.157     paf        59:        offset=0;
                     60:        limit=0;
1.172.2.10  paf        61:        if(params->count()<=param_index)
1.157     paf        62:                return;
                     63: 
1.172.2.10  paf        64:        ValuePtr voptions=params->as_no_junction(param_index, "options must be hash, not code");
1.172.2.6  paf        65:        if(!voptions->is_string()) {
                     66:                if(HashStringValue* options=voptions->get_hash(method_name)) {
1.164     paf        67:                        int valid_options=0;
1.172.2.6  paf        68:                        if(ValuePtr voffset=options->get(sql_offset_name)) {
1.164     paf        69:                                valid_options++;
1.157     paf        70:                                if(voffset->is_string()) {
1.172.2.14.2.  (paf       71:):                                      const String&  soffset=voffset->get_string(&pool);
1.172.2.6  paf        72:                                        if(*soffset == "cur")
1.157     paf        73:                                                offset=source.current();
                     74:                                        else
                     75:                                                throw Exception("parser.runtime",
1.172.2.6  paf        76:                                                        soffset,
1.157     paf        77:                                                        "must be 'cur' string or expression");
                     78:                                } else 
1.172.2.6  paf        79:                                        offset=r.process_to_value(voffset)->as_int();
1.164     paf        80:                        }
1.172.2.6  paf        81:                        if(ValuePtr vlimit=options->get(sql_limit_name)) {
1.164     paf        82:                                valid_options++;
1.172.2.6  paf        83:                                limit=r.process_to_value(vlimit)->as_int();
1.172.2.12  paf        84:                                if(!limit) // zero limit = should be 'nothing to copy', for methods zero means 'all'
                     85:                                        limit=-1; // thus fixing
1.164     paf        86:                        }
1.172.2.6  paf        87:                        if(valid_options!=options->count())
1.164     paf        88:                                throw Exception("parser.runtime",
1.172.2.5  paf        89:                                        method_name,
1.164     paf        90:                                        "called with invalid option");
1.157     paf        91:                } else
                     92:                        throw Exception("parser.runtime",
1.172.2.5  paf        93:                                method_name,
1.157     paf        94:                                "options must be hash");
1.158     paf        95:        }
1.157     paf        96: }
                     97: 
1.172.2.14.2.  (paf       98:): static void _create(Request& r, const String& method_name, MethodParams* params) {
                     99:):      =r.pool();
1.157     paf       100:        // clone/copy part?
1.172.2.10  paf       101:        if(const Table *source=(*params)[0]->get_table()) {
1.157     paf       102:                int offset, limit;
                    103:                get_copy_options(r, method_name, params, 1, *source, 
                    104:                        offset, limit);
1.172.2.14.2.  (paf      105:):              GET_SELF(r, VTable).set_table(Table*(new Table(*source, offset, limit)));
1.157     paf       106:                return;
                    107:        }
1.142     paf       108: 
1.1       paf       109:        // data is last parameter
1.45      paf       110:        Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.172.2.14.2.  (paf      111:):      const String&  data=
1.172.2.10  paf       112:                r.process_to_string(params->as_junction(params->count()-1, "body must be code"));
1.44      paf       113: 
                    114:        size_t pos_after=0;
                    115:        // parse columns
1.172.2.6  paf       116:        Table::columns_type columns;
1.172.2.10  paf       117:        if(params->count()==2) {
1.172.2.6  paf       118:                columns=Table::columns_type(0); // nameless
1.21      paf       119:        } else {
1.172.2.6  paf       120:                columns=Table::columns_type(new ArrayString);
1.44      paf       121: 
1.172.2.6  paf       122:                ArrayString head;
                    123:                data->split(head, &pos_after, "\n", 1, String::UL_AS_IS, 1);
                    124:                if(head.count())
                    125:                        head[0]->split(*columns, 0, "\t", 1, String::UL_AS_IS);
1.44      paf       126:        }
                    127: 
1.172.2.14.2.  (paf      128:):      Table* table(new Table(method_name, columns));
1.44      paf       129:        // parse cells
1.172.2.6  paf       130: 
                    131:        ArrayString rows;
                    132:        data->split(rows, &pos_after, "\n", 1, String::UL_AS_IS);
1.172.2.14.2.  (paf      133:):      Array_iterator<String* > i(rows);
1.98      parser    134:        while(i.has_next()) {
1.172.2.6  paf       135:                Table::element_type row(new ArrayString);
1.172.2.14.2.  (paf      136:):              const String&  string=i.next();
1.118     parser    137:                // remove comment lines
1.172.2.6  paf       138:                if(!string->size())
1.44      paf       139:                        continue;
                    140: 
1.172.2.6  paf       141:                string->split(*row, 0, "\t", 1, String::UL_AS_IS);
                    142:                *table+=row;
1.17      paf       143:        }
1.1       paf       144: 
1.44      paf       145:        // replace any previous table value
1.172.2.6  paf       146:        GET_SELF(r, VTable).set_table(table);
1.44      paf       147: }
                    148: 
1.172.2.14.2.  (paf      149:): static void _load(Request& r, const String& method_name, MethodParams* params) {
                    150:):      =r.pool();
                    151:):      const String&  first_param=params->as_string(0, "file name must be string");
1.168     paf       152:        int filename_param_index=0;
1.172.2.6  paf       153:        bool nameless=*first_param=="nameless";
1.168     paf       154:        if(nameless)
                    155:                filename_param_index++;
                    156:        int options_param_index=filename_param_index+1;
                    157:        
1.44      paf       158:        // loading text
1.172.2.6  paf       159:        char *data=file_read_text(pool, r.charsets.source(),
1.172.2.10  paf       160:                r.absolute(params->as_string(filename_param_index, "file name must be string")),
1.168     paf       161:                true,
1.172.2.10  paf       162:                options_param_index<params->count()?params->as_no_junction(options_param_index, "additional params must be hash")->get_hash(method_name):0
1.168     paf       163:        );
1.44      paf       164: 
1.1       paf       165:        // parse columns
1.172.2.6  paf       166:        Table::columns_type columns;
1.1       paf       167: #ifndef NO_STRING_ORIGIN
1.172.2.6  paf       168:        const String_fragment::Origin& origin=method_name->origin();
1.172.2.2  paf       169:        const char* file=origin.file;
1.1       paf       170:        uint line=origin.line;
                    171: #endif
1.168     paf       172:        if(nameless) {
1.172.2.6  paf       173:                columns=Table::columns_type(0); // nameless
1.1       paf       174:        } else {
1.172.2.6  paf       175:                columns=Table::columns_type(new ArrayString);
1.1       paf       176: 
1.139     paf       177:                while(char *row_chars=getrow(&data)) {
                    178:                        // remove empty&comment lines
                    179:                        if(!*row_chars || *row_chars == '#')
                    180:                                continue;
1.1       paf       181:                        do {
1.172.2.14.2.  (paf      182:):                              const String& name(new String);
1.45      paf       183:                                name->APPEND_TAINTED(lsplit(&row_chars, '\t'), 0, file, line++);
1.1       paf       184:                                *columns+=name;
                    185:                        } while(row_chars);
1.139     paf       186: 
                    187:                        break;
                    188:                }
1.1       paf       189:        }
                    190: 
                    191:        // parse cells
1.172.2.14.2.  (paf      192:):      Table* table(new Table(method_name, columns));
1.1       paf       193:        char *row_chars;
                    194:        while(row_chars=getrow(&data)) {
1.118     parser    195:                // remove empty&comment lines
                    196:                if(!*row_chars || *row_chars == '#')
1.28      paf       197:                        continue;
1.172.2.6  paf       198:                Table::element_type row(new ArrayString);
1.1       paf       199:                while(char *cell_chars=lsplit(&row_chars, '\t')) {
1.172.2.14.2.  (paf      200:):                      const String& cell(new String);
1.44      paf       201:                        cell->APPEND_TAINTED(cell_chars, 0, file, line);
1.1       paf       202:                        *row+=cell;
                    203:                }
1.107     parser    204: #ifndef NO_STRING_ORIGIN
1.1       paf       205:                line++;
1.107     parser    206: #endif
1.172.2.6  paf       207:                *table+=row;
1.1       paf       208:        };
                    209: 
                    210:        // replace any previous table value
1.172.2.6  paf       211:        GET_SELF(r, VTable).set_table(table);
1.1       paf       212: }
1.2       paf       213: 
1.146     paf       214: /// @todo "x\nx" "xxx""xx"
1.172.2.14.2.  (paf      215:): static void _save(Request& r, const String& method_name, MethodParams* params) {
                    216:):      =r.pool();
1.172.2.10  paf       217:        ValuePtr vfile_name=params->as_no_junction(params->count()-1, 
1.61      paf       218:                "file name must not be code");
1.22      paf       219: 
1.172.2.6  paf       220:        Table& table=GET_SELF(r, VTable).table(method_name);
1.31      paf       221: 
1.129     paf       222:        bool do_append=false;
1.172.2.6  paf       223:        String sdata;
1.172.2.10  paf       224:        if(params->count()==1) { // named output
1.31      paf       225:                // write out names line
                    226:                if(table.columns()) { // named table
1.172.2.14.2.  (paf      227:):                      Array_iterator<String* > i(*table.columns());
1.98      parser    228:                        while(i.has_next()) {
1.172.2.6  paf       229:                                sdata.append(*i.next(), String::UL_TABLE);
1.98      parser    230:                                if(i.has_next())
1.31      paf       231:                                        sdata.APPEND_CONST("\t");
                    232:                        }
                    233:                } else { // nameless table
1.172.2.6  paf       234:                        if(int lsize=table.count()?table[0]->count():0)
1.31      paf       235:                                for(int column=0; column<lsize; column++) {
1.172.2.14.2.  (paf      236:):                                      char *cindex_tab=new char[MAX_NUMBER];
1.31      paf       237:                                        snprintf(cindex_tab, MAX_NUMBER, "%d\t", column);
                    238:                                        sdata.APPEND_CONST(cindex_tab);
                    239:                                }
                    240:                        else
                    241:                                sdata.APPEND_CONST("empty nameless table");
                    242:                }
                    243:                sdata.APPEND_CONST("\n");
1.129     paf       244:        } else { // mode specified
1.172.2.14.2.  (paf      245:):              const String&  mode=params->as_string(0, "mode must be string");
1.172.2.6  paf       246:                if(*mode=="append")
1.129     paf       247:                        do_append=true;
1.172.2.6  paf       248:                else if(*mode=="nameless")
1.129     paf       249:                        /*ok, already skipped names output*/;
                    250:                else
1.146     paf       251:                        throw Exception("parser.runtime",
1.172.2.6  paf       252:                                mode,
1.129     paf       253:                                "unknown mode, must be 'append'");
                    254: 
1.31      paf       255:        }
                    256:        // data lines
1.172.2.14.2.  (paf      257:):      Array_iterator<ArrayString* > i(table);
1.98      parser    258:        while(i.has_next()) {
1.172.2.14.2.  (paf      259:):              Array_iterator<String* > c(*i.next());
1.98      parser    260:                while(c.has_next()) {
1.172.2.14.2.  (paf      261:):                      if(const String& s=c.next())
1.172.2.6  paf       262:                                sdata.append(*s, String::UL_TABLE);
1.98      parser    263:                        if(c.has_next())
1.31      paf       264:                                sdata.APPEND_CONST("\t");
                    265:                }
                    266:                sdata.APPEND_CONST("\n");
                    267:        }
                    268: 
                    269:        // write
1.172.2.6  paf       270:        file_write(r.absolute(vfile_name->as_string(&pool)), 
1.129     paf       271:                sdata.cstr(), sdata.size(), true, do_append);
1.22      paf       272: }
                    273: 
1.172.2.14.2.  (paf      274:): static void _count(Request& r, const String& method_name, MethodParams* ) {
1.172.2.6  paf       275:        int result=GET_SELF(r, VTable).table(method_name).count();
                    276:        r.write_no_lang(ValuePtr(new VInt(result)));
1.6       paf       277: }
                    278: 
1.172.2.14.2.  (paf      279:): static void _line(Request& r, const String& method_name, MethodParams* ) {
1.172.2.6  paf       280:        int result=1+GET_SELF(r, VTable).table(method_name).current();
                    281:        r.write_no_lang(ValuePtr(new VInt(result)));
1.6       paf       282: }
                    283: 
1.172.2.14.2.  (paf      284:): static void _offset(Request& r, const String& method_name, MethodParams* params) {
                    285:):      =r.pool();
1.172.2.6  paf       286:        Table& table=GET_SELF(r, VTable).table(method_name);
1.172.2.10  paf       287:        if(params->count()) {
1.133     paf       288:                bool absolute=false;
1.172.2.10  paf       289:                if(params->count()>1) {
1.172.2.14.2.  (paf      290:):                  const String&  whence=params->as_string(0, "whence must be string");
1.172.2.6  paf       291:                    if(*whence=="cur")
1.134     paf       292:                                absolute=false;
1.172.2.6  paf       293:                    else if(*whence=="set")
1.134     paf       294:                                absolute=true;
1.133     paf       295:                    else
1.146     paf       296:                                throw Exception("parser.runtime",
1.172.2.6  paf       297:                                        whence,
1.134     paf       298:                                        "is invalid whence, valid are 'cur' or 'set'");
1.157     paf       299:                }
1.133     paf       300:                
1.172.2.10  paf       301:                ValuePtr offset_expr=params->as_junction(params->count()-1, "offset must be expression");
1.172.2.6  paf       302:                table.offset(absolute, r.process_to_value(offset_expr)->as_int());
1.151     paf       303:        } else
1.172.2.6  paf       304:                r.write_no_lang(ValuePtr(new VInt(table.current())));
1.6       paf       305: }
                    306: 
1.172.2.14.2.  (paf      307:): static void _menu(Request& r, const String& method_name, MethodParams* params) {
1.172.2.10  paf       308:        ValuePtr body_code=params->as_junction(0, "body must be code");
1.7       paf       309:        
1.172.2.10  paf       310:        ValuePtr delim_maybe_code=params->count()>1?(*params)[1]:ValuePtr(0);
1.7       paf       311: 
1.172.2.6  paf       312:        Table& table=GET_SELF(r, VTable).table(method_name);
1.7       paf       313:        bool need_delim=false;
1.99      parser    314:        int saved_current=table.current();
1.172.2.6  paf       315:        int size=table.count();
1.99      parser    316:        for(int row=0; row<size; row++) {
1.22      paf       317:                table.set_current(row);
1.7       paf       318: 
1.161     paf       319:                StringOrValue sv_processed=r.process(body_code);
1.172.2.14.2.  (paf      320:):              const String& s_processed=sv_processed.get_string();
1.161     paf       321:                if(delim_maybe_code && s_processed && s_processed->size()) { // delimiter set and we have body
                    322:                        if(need_delim) // need delim & iteration produced string?
1.172.2.6  paf       323:                                r.write_pass_lang(r.process(delim_maybe_code));
1.7       paf       324:                        need_delim=true;
                    325:                }
1.161     paf       326:                r.write_pass_lang(sv_processed);
1.7       paf       327:        }
1.99      parser    328:        table.set_current(saved_current);
1.7       paf       329: }
                    330: 
1.110     parser    331: #ifndef DOXYGEN
1.74      paf       332: struct Row_info {
1.166     paf       333:        Request *r;
1.74      paf       334:        Table *table;
1.172.2.6  paf       335:        ValuePtr key_code;
1.74      paf       336:        int key_field;
1.172.2.6  paf       337:        Array<int>* value_fields;
                    338:        HashStringValue* hash;
1.163     paf       339:        bool distinct;
1.167     paf       340:        int row;
1.74      paf       341: };
1.110     parser    342: #endif
1.172.2.6  paf       343: static void table_row_to_hash(Table::element_type row, Row_info *info) {
1.172.2.14.2.  (paf      344:):      =info->r->pool();
1.172.2.6  paf       345: 
1.172.2.14.2.  (paf      346:):      const String& key;
1.172.2.6  paf       347:        if(info->key_code) {
                    348:                info->table->set_current(info->row++); // change context row
                    349:                StringOrValue sv_processed=info->r->process(info->key_code);
                    350:                key=sv_processed.as_string(&pool);
1.166     paf       351:        } else
1.172.2.14.2.  (paf      352:):              key=info->key_field<row->count()?row->get(info->key_field):0;
1.166     paf       353: 
                    354:        if(!key)
                    355:                return; // ignore rows without key [too-short-record_array if-indexed]
1.74      paf       356:                
1.172.2.6  paf       357:        VHashPtr vhash(new VHash);
                    358:        HashStringValue& hash=vhash->hash();
                    359:        for(int i=0; i<info->value_fields->count(); i++) {
                    360:                int value_field=info->value_fields->get(i);
                    361:                if(value_field<row->count())
1.166     paf       362:                        hash.put(
1.172.2.6  paf       363:                                info->table->columns()->get(value_field), 
                    364:                                ValuePtr(new VString(row->get(value_field))));
1.74      paf       365:        }
1.166     paf       366: 
1.172.2.6  paf       367:        if(info->hash->put_dont_replace(key, vhash)) // put. existed?
                    368:                if(!info->distinct)
1.166     paf       369:                        throw Exception("parser.runtime",
                    370:                                key,
                    371:                                "duplicate key");
1.74      paf       372: }
1.172.2.14.2.  (paf      373:): static void _hash(Request& r, const String& method_name, MethodParams* params) {
                    374:):      =r.pool();
1.172.2.6  paf       375:        Table& self_table=GET_SELF(r, VTable).table(method_name);
                    376:        VHashPtr result(new VHash);
                    377:        if(Table::columns_type columns=self_table.columns())
                    378:                if(columns->count()>0) {
1.163     paf       379:                        bool distinct=false;
1.172.2.10  paf       380:                        int param_index=params->count()-1;
1.166     paf       381:                        if(param_index>0) {
1.172.2.6  paf       382:                                if(HashStringValue* options=
1.172.2.10  paf       383:                                        params->as_no_junction(param_index, "param must not be code")->get_hash(method_name)) {
1.166     paf       384:                                        --param_index;
                    385:                                        int valid_options=0;
1.172.2.6  paf       386:                                        if(ValuePtr vdistinct=options->get(sql_distinct_name)) {
1.166     paf       387:                                                valid_options++;
1.172.2.6  paf       388:                                                distinct=r.process_to_value(vdistinct)->as_bool();
1.166     paf       389:                                        }
1.172.2.6  paf       390:                                        if(valid_options!=options->count())
1.166     paf       391:                                                throw Exception("parser.runtime",
1.172.2.5  paf       392:                                                        method_name,
1.166     paf       393:                                                        "called with invalid option");
1.163     paf       394:                                }
                    395:                        }
                    396:                        if(param_index==2) // bad options param type
                    397:                                throw Exception("parser.runtime",
1.172.2.5  paf       398:                                        method_name,
1.163     paf       399:                                        "options must be hash");
                    400: 
1.172.2.6  paf       401:                        Array<int> value_fields;
1.163     paf       402:                        if(param_index>0) {
1.172.2.10  paf       403:                                ValuePtr value_fields_param=params->as_no_junction(param_index, "value field(s) must not be code");
1.172.2.6  paf       404:                                if(value_fields_param->is_string()) {
                    405:                                        value_fields+=self_table.column_name2index(
                    406:                                                value_fields_param->as_string(&pool), true);
                    407:                                } else if(Table* value_fields_table=value_fields_param->get_table()) {
                    408:                                        for(int i=0; i<value_fields_table->count(); i++) {
1.172.2.14.2.  (paf      409:):                                              const String&  value_field_name=
1.172.2.6  paf       410:                                                        value_fields_table->get(i)->get(0);
1.123     parser    411:                                                value_fields+=self_table.column_name2index(value_field_name, true);
                    412:                                        }
                    413:                                } else
1.146     paf       414:                                        throw Exception("parser.runtime",
1.172.2.5  paf       415:                                                method_name,
1.123     parser    416:                                                "value field(s) must be string or self_table"
                    417:                                        );
                    418:                        } else { // by all columns, including key
1.172.2.6  paf       419:                                for(int i=0; i<columns->count(); i++)
1.123     parser    420:                                        value_fields+=i;
1.74      paf       421:                        }
                    422: 
1.172.2.6  paf       423: 
                    424:                        {
                    425:                                Row_info info;
                    426:                                info.r=&r;
                    427:                                info.table=&self_table;
1.172.2.10  paf       428:                                ValuePtr key_param=(*params)[0];
1.172.2.6  paf       429:                                info.key_code=key_param->get_junction()?key_param:ValuePtr(0);
                    430:                                info.key_field=info.key_code?-1
                    431:                                        :self_table.column_name2index(key_param->as_string(&pool), true);
                    432:                                info.value_fields=&value_fields;
                    433:                                info.hash=&result->hash();
                    434:                                info.distinct=distinct;
1.172.2.12  paf       435:                                info.row=0;
1.172.2.6  paf       436: 
                    437:                                int saved_current=self_table.current();
                    438:                                self_table.for_each(table_row_to_hash, &info);
                    439:                                self_table.set_current(saved_current);
                    440:                        }
1.74      paf       441:                }
1.97      parser    442:        r.write_no_lang(result);
1.74      paf       443: }
                    444: 
1.110     parser    445: #ifndef DOXYGEN
1.78      paf       446: struct Table_seq_item {
1.172.2.13  paf       447:        ArrayString* row;
1.34      paf       448:        union {
                    449:                char *c_str;
                    450:                double d;
                    451:        } value;
1.32      paf       452: };
1.110     parser    453: #endif
1.34      paf       454: static int sort_cmp_string(const void *a, const void *b) {
                    455:        return strcmp(
1.78      paf       456:                static_cast<const Table_seq_item *>(a)->value.c_str, 
                    457:                static_cast<const Table_seq_item *>(b)->value.c_str
1.34      paf       458:        );
                    459: }
                    460: static int sort_cmp_double(const void *a, const void *b) {
1.78      paf       461:        double va=static_cast<const Table_seq_item *>(a)->value.d;
                    462:        double vb=static_cast<const Table_seq_item *>(b)->value.d;
1.34      paf       463:        if(va<vb)
                    464:                return -1;
                    465:        else if(va>vb)
                    466:                return +1;
                    467:        else 
                    468:                return 0;
                    469: }
1.172.2.14.2.  (paf      470:): static void _sort(Request& r, const String& method_name, MethodParams* params) {
1.172.2.6  paf       471:        Pool local_pool;
1.172.2.10  paf       472:        ValuePtr key_maker=params->as_junction(0, "key-maker must be code");
1.61      paf       473: 
1.172.2.10  paf       474:        bool reverse=params->count()>1/*..[desc|asc|]*/?
                    475:                reverse=*params->as_no_junction(1, "order must not be code")->as_string(&local_pool)=="desc":
1.104     parser    476:                false; // default=asc
1.32      paf       477: 
1.172.2.6  paf       478:        Table& old_table=GET_SELF(r, VTable).table(method_name);
1.172.2.14.2.  (paf      479:):      Table* new_table(new Table(method_name, old_table.columns()));
1.34      paf       480: 
1.172.2.6  paf       481:        smart_ptr<Table_seq_item> seq(new Table_seq_item[old_table.count()]);
1.34      paf       482:        int i;
                    483: 
                    484:        // calculate key values
                    485:        bool key_values_are_strings=true;
1.172.2.6  paf       486:        for(i=0; i<old_table.count(); i++) {
1.101     parser    487:                old_table.set_current(i);
1.32      paf       488:                // calculate key value
1.172.2.13  paf       489:                seq[i].row=old_table[i].get();
1.172.2.6  paf       490:                ValuePtr value=r.process_to_value(key_maker)->as_expr_result(true/*return string as-is*/);
1.34      paf       491:                if(i==0) // determining key values type by first one
1.172.2.6  paf       492:                        key_values_are_strings=value->is_string();
1.34      paf       493: 
                    494:                if(key_values_are_strings)
1.172.2.6  paf       495:                        seq[i].value.c_str=value->as_string(&local_pool)->cstr(local_pool);
1.34      paf       496:                else
1.172.2.6  paf       497:                        seq[i].value.d=value->as_double();
1.32      paf       498:        }
                    499:        // sort keys
1.172.2.6  paf       500:        _qsort(seq, old_table.count(), sizeof(Table_seq_item), 
1.34      paf       501:                key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32      paf       502: 
1.34      paf       503:        // reorder table as they require in 'seq'
1.172.2.6  paf       504:        for(i=0; i<old_table.count(); i++)
1.172.2.13  paf       505:                *new_table+=Table::element_type(seq[reverse?old_table.count()-1-i:i].row);
1.32      paf       506: 
1.116     parser    507:        // replace any previous table value
1.172.2.6  paf       508:        GET_SELF(r, VTable).set_table(new_table);
1.32      paf       509: }
                    510: 
1.172.2.14.2.  (paf      511:): static bool _locate_expression(Request& r, const String& method_name, MethodParams* params) {
1.172.2.10  paf       512:        if(params->count()>1)
1.146     paf       513:                throw Exception("parser.runtime", 
1.172.2.5  paf       514:                        method_name,
1.145     paf       515:                        "locate by expression has only one parameter - expression");
                    516: 
1.172.2.10  paf       517:        ValuePtr expression_code=params->as_junction(0, "must be expression");
1.145     paf       518: 
1.172.2.6  paf       519:        Table& table=GET_SELF(r, VTable).table(method_name);
1.145     paf       520:        int saved_current=table.current();
1.172.2.6  paf       521:        int size=table.count();
1.145     paf       522:        for(int row=0; row<size; row++) {
                    523:                table.set_current(row);
                    524: 
1.172.2.6  paf       525:                if(r.process_to_value(expression_code)->as_bool())
1.145     paf       526:                        return true;
                    527:        }
                    528:        table.set_current(saved_current);
                    529:        return false;
                    530: }
1.172.2.14.2.  (paf      531:): static bool _locate_name_value(Request& r, const String& method_name, MethodParams* params) {
1.172.2.10  paf       532:        if(params->count()>2)
1.146     paf       533:                throw Exception("parser.runtime", 
1.172.2.5  paf       534:                        method_name,
1.145     paf       535:                        "locate by name and value has only two parameters - name and value");
1.72      paf       536: 
1.172.2.6  paf       537:        Table& table=GET_SELF(r, VTable).table(method_name);
1.145     paf       538:        return table.locate(
1.172.2.10  paf       539:                params->as_string(0, "column name must be string"),
                    540:                params->as_string(1, "value must be string")
1.145     paf       541:        );
                    542: }
1.172.2.14.2.  (paf      543:): static void _locate(Request& r, const String& method_name, MethodParams* params) {
                    544:):      =r.pool();
1.172.2.10  paf       545:        bool result=(*params)[0]->get_junction()?
1.145     paf       546:                _locate_expression(r, method_name, params) :
1.151     paf       547:                _locate_name_value(r, method_name, params);
1.172.2.6  paf       548:        r.write_no_lang(ValuePtr(new VBool(result)));
1.37      paf       549: }
                    550: 
1.172.2.14.2.  (paf      551:): static void _flip(Request& r, const String& method_name, MethodParams* params) {
                    552:):      =r.pool();
1.172.2.6  paf       553:        Table& old_table=GET_SELF(r, VTable).table(method_name);
1.172.2.14.2.  (paf      554:):      Table* new_table(new Table(method_name, old_table.columns()));
1.172.2.6  paf       555:        if(old_table.count())
                    556:                if(int old_cols=old_table[0]->count()) 
1.39      paf       557:                        for(int column=0; column<old_cols; column++) {
1.172.2.6  paf       558:                                Table::element_type new_row(new ArrayString(old_table.count()));
                    559:                                for(int i=0; i<old_table.count(); i++) {
                    560:                                        Table::element_type old_row=old_table[i];
1.172.2.14.2.  (paf      561:):                                      *new_row+=column<old_row->count()?old_row->get(column):String* (new String);
1.39      paf       562:                                }
1.172.2.6  paf       563:                                *new_table+=new_row;
1.39      paf       564:                        }
                    565: 
1.172.2.6  paf       566:        r.write_no_lang(ValuePtr(new VTable(new_table)));
1.39      paf       567: }
                    568: 
1.172.2.14.2.  (paf      569:): static void _append(Request& r, const String& method_name, MethodParams* params) {
                    570:):      =r.pool();
1.134     paf       571:        // data
                    572:        Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.172.2.14.2.  (paf      573:):      const String& string=r.process_to_string(params->as_junction(0, "body must be code"));
1.41      paf       574: 
                    575:        // parse cells
1.172.2.14.2.  (paf      576:):      ArrayString&  row(new ArrayString);
1.172.2.6  paf       577:        string->split(*row, 0, "\t", 1, String::UL_AS_IS);
1.41      paf       578: 
1.172.2.6  paf       579:        GET_SELF(r, VTable).table(method_name)+=row;
1.41      paf       580: }
                    581: 
1.172.2.14.2.  (paf      582:): static void _join(Request& r, const String& method_name, MethodParams* params) {
                    583:):      =r.pool();
1.42      paf       584: 
1.172.2.10  paf       585:        Table* maybe_src=params->as_no_junction(0, "table ref must not be code")->get_table();
1.42      paf       586:        if(!maybe_src)
1.146     paf       587:                throw Exception("parser.runtime", 
1.172.2.5  paf       588:                        method_name, 
1.42      paf       589:                        "source is not a table");
                    590: 
                    591:        Table& src=*maybe_src;
1.172.2.6  paf       592:        Table& dest=GET_SELF(r, VTable).table(method_name);
1.42      paf       593:        if(&src == &dest)
1.146     paf       594:                throw Exception("parser.runtime", 
1.172.2.5  paf       595:                        method_name, 
1.42      paf       596:                        "source and destination are same table");
                    597: 
1.157     paf       598:        int offset, limit;
                    599:        get_copy_options(r, method_name, params, 1, src, 
                    600:                offset, limit);
                    601: 
1.172.2.6  paf       602:        if(Table::columns_type dest_columns=dest.columns()) { // dest is named
1.42      paf       603:                int saved_src_current=src.current();
1.172.2.6  paf       604:                int m=src.count()-offset;
1.157     paf       605:                if(!limit || limit>m)
                    606:                        limit=m;
                    607:                int end=offset+limit;
                    608:                for(int src_row=offset; src_row<end; src_row++) {
1.42      paf       609:                        src.set_current(src_row);
1.172.2.6  paf       610:                        Table::element_type dest_row(new ArrayString(src.count()));
                    611:                        for(int dest_column=0; dest_column<dest_columns->count(); dest_column++) {
1.172.2.14.2.  (paf      612:):                              const String& src_item=src.item(dest_columns->get(dest_column));
                    613:):                              *dest_row+=src_item?src_item:String* (new String);
1.140     paf       614:                        }
1.172.2.6  paf       615:                        dest+=dest_row;
1.42      paf       616:                }
                    617:                src.set_current(saved_src_current);
                    618:        } else { // dest is nameless
1.172.2.6  paf       619:                for(int src_row=0; src_row<src.count(); src_row++)
                    620:                        dest+=src[src_row];
1.42      paf       621:        }
                    622: }
                    623: 
1.94      parser    624: #ifndef DOXYGEN
1.169     paf       625: class Table_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.172.2.14.2.  (paf      626:):      const String&  method_name;
                    627:):      const String&  statement_string; const char* statement_cstr;
                    628:):      ArrayString&  columns;
1.172.2.6  paf       629:        ArrayString* row;
                    630: public:
1.172.2.14.2.  (paf      631:):      Table* table;
1.94      parser    632: public:
1.172.2.14.2.  (paf      633:):      Table_sql_event_handlers(const String& amethod_name,
                    634:):              const String& astatement_string, const char* astatement_cstr) :
1.94      parser    635:                method_name(amethod_name),
1.172.2.14  paf       636:                statement_string(astatement_string), statement_cstr(astatement_cstr),
                    637:                columns(new ArrayString), row(0) {
                    638:        }
1.94      parser    639: 
1.170     paf       640:        bool add_column(SQL_Error& error, void *ptr, size_t size) {
                    641:                try {
1.172.2.14.2.  (paf      642:):                      const String& column(new String);
1.170     paf       643:                        column->APPEND_TAINTED(
1.172.2.2  paf       644:                                (const char* )ptr, size, 
1.170     paf       645:                                statement_cstr, 0);
1.172.2.6  paf       646:                        *columns+=column;
1.170     paf       647:                        return false;
                    648:                } catch(...) {
                    649:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_column");
                    650:                        return true;
                    651:                }
                    652:        }
                    653:        bool before_rows(SQL_Error& error) { 
                    654:                try {
1.172.2.14.2.  (paf      655:):                      table=Table*(new Table( method_name, columns));
1.170     paf       656:                        return false;
                    657:                } catch(...) {
                    658:                        error=SQL_Error("exception occured in Table_sql_event_handlers::before_rows");
                    659:                        return true;
                    660:                }
                    661:        }
                    662:        bool add_row(SQL_Error& error) {
                    663:                try {
1.172.2.14.2.  (paf      664:):                      *table+=(ArrayString* (row=new ArrayString));
1.170     paf       665:                        return false;
                    666:                } catch(...) {
                    667:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_row");
                    668:                        return true;
                    669:                }
                    670:        }
                    671:        bool add_row_cell(SQL_Error& error, void *ptr, size_t size) {
                    672:                try {
1.172.2.14.2.  (paf      673:):                      const String& cell(new String);
1.170     paf       674:                        if(size)
                    675:                                cell->APPEND_TAINTED(
1.172.2.2  paf       676:                                        (const char* )ptr, size, 
1.172.2.6  paf       677:                                        statement_cstr, table->count()-1);
                    678:                        *row+=cell;
1.170     paf       679:                        return false;
                    680:                } catch(...) {
                    681:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_row_cell");
                    682:                        return true;
                    683:                }
1.94      parser    684:        }
                    685: };
                    686: #endif
1.172.2.14.2.  (paf      687:): static void _sql(Request& r, const String& method_name, MethodParams* params) {
                    688:):      =r.pool();
1.49      paf       689: 
1.172.2.10  paf       690:        ValuePtr statement=params->as_junction(0, "statement must be code");
1.49      paf       691: 
1.53      paf       692:        ulong limit=0;
1.100     parser    693:        ulong offset=0;
1.172.2.10  paf       694:        if(params->count()>1) {
                    695:                ValuePtr voptions=params->as_no_junction(1, "options must be hash, not code");
1.172.2.6  paf       696:                if(!voptions->is_string())
                    697:                        if(HashStringValue* options=voptions->get_hash(method_name)) {
1.164     paf       698:                                int valid_options=0;
1.172.2.6  paf       699:                                if(ValuePtr vlimit=options->get(sql_limit_name)) {
1.164     paf       700:                                        valid_options++;
1.172.2.6  paf       701:                                        limit=(ulong)r.process_to_value(vlimit)->as_double();
1.164     paf       702:                                }
1.172.2.6  paf       703:                                if(ValuePtr voffset=options->get(sql_offset_name)) {
1.164     paf       704:                                        valid_options++;
1.172.2.6  paf       705:                                        offset=(ulong)r.process_to_value(voffset)->as_double();
1.164     paf       706:                                }
1.172.2.6  paf       707:                                if(valid_options!=options->count())
1.164     paf       708:                                        throw Exception("parser.runtime",
1.172.2.5  paf       709:                                                method_name,
1.164     paf       710:                                                "called with invalid option");
1.109     parser    711:                        } else
1.146     paf       712:                                throw Exception("parser.runtime",
1.172.2.5  paf       713:                                        method_name,
1.109     parser    714:                                        "options must be hash");
1.49      paf       715:        }
                    716: 
1.54      paf       717:        Temp_lang temp_lang(r, String::UL_SQL);
1.172.2.14.2.  (paf      718:):      const String&  statement_string=r.process_to_string(statement);
1.172.2.2  paf       719:        const char* statement_cstr=
1.172.2.6  paf       720:                statement_string->cstr(pool, String::UL_UNSPECIFIED, r.connection(method_name));
1.172.2.14  paf       721:        Table_sql_event_handlers handlers(method_name, 
1.94      parser    722:                statement_string, statement_cstr);
1.135     paf       723: #ifdef RESOURCES_DEBUG
                    724:        struct timeval mt[2];
                    725:        //measure:before
                    726:        gettimeofday(&mt[0],NULL);
                    727: #endif 
1.172.2.6  paf       728:        r.connection(method_name)->query(
1.160     paf       729:                statement_cstr, offset, limit, 
                    730:                handlers,
                    731:                statement_string);
1.135     paf       732:        
                    733: #ifdef RESOURCES_DEBUG
                    734:                //measure:after connect
                    735:        gettimeofday(&mt[1],NULL);
                    736:        
                    737:        double t[2];
                    738:        for(int i=0;i<2;i++)
                    739:            t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
                    740:            
                    741:        r.sql_request_time+=t[1]-t[0];
                    742: #endif                         
1.96      parser    743: 
1.172.2.14.2.  (paf      744:):      Table* result=
1.96      parser    745:                handlers.table?handlers.table: // query resulted in table? return it
1.172.2.14.2.  (paf      746:):              Table*(new Table(method_name, Table::columns_type(0))); // query returned no table, fake it
1.49      paf       747: 
                    748:        // replace any previous table value
1.172.2.6  paf       749:        GET_SELF(r, VTable).set_table(result);
1.49      paf       750: }
                    751: 
1.172.2.14.2.  (paf      752:): static void _columns(Request& r, const String& method_name, MethodParams* ) {
                    753:):      =r.pool();
1.88      parser    754: 
1.172.2.6  paf       755:        Table::columns_type result_columns(new ArrayString);
1.172.2.14.2.  (paf      756:):      *result_columns+=String* (new String("column"));
                    757:):      Table* result_table(new Table(method_name, result_columns));
1.172.2.6  paf       758: 
                    759:        Table& source_table=GET_SELF(r, VTable).table(method_name);
                    760:        if(Table::columns_type source_columns=source_table.columns()) {
1.172.2.14.2.  (paf      761:):              Array_iterator<String* > i(*source_columns);
1.98      parser    762:                while(i.has_next()) {
1.172.2.6  paf       763:                        Table::element_type result_row(new ArrayString);
                    764:                        *result_row+=i.next();
                    765:                        *result_table+=result_row;
1.88      parser    766:                }
                    767:        }
                    768: 
1.172.2.6  paf       769:        r.write_no_lang(ValuePtr(new VTable(result_table)));
1.88      parser    770: }
                    771: 
1.172.2.14.2.  (paf      772:): static void _select(Request& r, const String& method_name, MethodParams* params) {
                    773:):      =r.pool();
1.148     paf       774: 
1.172.2.10  paf       775:        ValuePtr vcondition=params->as_junction(0, "condition must be expression");
1.148     paf       776: 
1.172.2.6  paf       777:        Table& source_table=GET_SELF(r, VTable).table(method_name);
1.172.2.14.2.  (paf      778:):      Table* result_table(new Table(
1.148     paf       779:                source_table.origin_string(), 
                    780:                source_table.columns()
1.172.2.6  paf       781:        ));
1.148     paf       782: 
                    783:        int saved_current=source_table.current();
1.172.2.6  paf       784:        int size=source_table.count();
1.148     paf       785:        for(int row=0; row<size; row++) {
                    786:                source_table.set_current(row);
                    787: 
1.150     paf       788:                bool condition=r.process_to_value(vcondition, 
1.149     paf       789:                                /*0/*no name* /,*/
1.172.2.6  paf       790:                                false/*don't intercept string*/)->as_bool();
1.148     paf       791: 
                    792:                if(condition) // ...condition is true=
1.172.2.6  paf       793:                        *result_table+=source_table[row]; // =green light to go to result
1.148     paf       794:        }
                    795:        source_table.set_current(saved_current);
                    796: 
1.172.2.6  paf       797:        r.write_no_lang(ValuePtr(new VTable(result_table)));
1.148     paf       798: }
                    799: 
1.66      paf       800: // constructor
                    801: 
1.172.2.6  paf       802: MTable::MTable(): Methoded("table") {
1.142     paf       803:        // ^table::create{data}
                    804:        // ^table::create[nameless]{data}
                    805:        // ^table::create[table]
                    806:        add_native_method("create", Method::CT_DYNAMIC, _create, 1, 2);
                    807:        // old name for compatibility with <= v 1.141 2002/01/25 11:33:45 paf
                    808:        add_native_method("set", Method::CT_DYNAMIC, _create, 1, 2); 
1.2       paf       809: 
1.142     paf       810:        // ^table::load[file]  
                    811:        // ^table::load[nameless;file]
1.168     paf       812:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 3);
1.22      paf       813: 
                    814:        // ^table.save[file]  
                    815:        // ^table.save[nameless;file]
1.66      paf       816:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.6       paf       817: 
                    818:        // ^table.count[]
1.66      paf       819:        add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6       paf       820: 
                    821:        // ^table.line[]
1.66      paf       822:        add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6       paf       823: 
1.10      paf       824:        // ^table.offset[]  
1.133     paf       825:        // ^table.offset(offset)
                    826:        // ^table.offset[cur|set](offset)
                    827:        add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 2);
1.7       paf       828: 
1.10      paf       829:        // ^table.menu{code}  
                    830:        // ^table.menu{code}[delim]
1.66      paf       831:        add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.74      paf       832: 
1.79      paf       833:        // ^table:hash[key field name]
1.123     parser    834:        // ^table:hash[key field name][value field name(s) string/table]
1.163     paf       835:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 3);
1.32      paf       836: 
1.102     parser    837:        // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc]
                    838:        // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc]
1.66      paf       839:        add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8       paf       840: 
1.36      paf       841:        // ^table.locate[field;value]
1.145     paf       842:        add_native_method("locate", Method::CT_DYNAMIC, _locate, 1, 2);
1.39      paf       843: 
                    844:        // ^table.flip[]
1.66      paf       845:        add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41      paf       846: 
                    847:        // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
1.66      paf       848:        add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42      paf       849: 
1.157     paf       850:        // ^table.join[table][$.limit(10) $.offset(1) $.offset[cur] ]
                    851:        add_native_method("join", Method::CT_DYNAMIC, _join, 1, 2);
1.49      paf       852: 
                    853: 
1.100     parser    854:        // ^table:sql[query]
                    855:        // ^table:sql[query][$.limit(1) $.offset(2)]
                    856:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.88      parser    857: 
                    858:        // ^table:columns[]
                    859:        add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 0);
1.148     paf       860: 
                    861:        // ^table.select(expression) = table
                    862:        add_native_method("select", Method::CT_DYNAMIC, _select, 1, 1);
1.40      paf       863: }

E-mail: