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

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

E-mail: