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

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

E-mail: