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

1.20      paf         1: /** @file
1.47      paf         2:        Parser: @b table parser class.
1.20      paf         3: 
1.210     paf         4:        Copyright (c) 2001-2005 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.221     paf         7: 
1.223   ! paf         8: static const char * const IDENT_TABLE_C="$Date: 2006/06/09 19:08:12 $";
1.1       paf         9: 
1.105     parser     10: #include "classes.h"
1.182     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.182     paf        22: class MTable: public Methoded {
1.66      paf        23: public: // VStateless_class
1.209     paf        24:        Value* create_new_value(Pool&, HashStringValue&) { return new VTable(); }
1.66      paf        25: 
                     26: public:
1.182     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.182     paf        33: // global variable
                     34: 
                     35: DECLARE_CLASS_VAR(table, new MTable, 0);
                     36: 
1.221     paf        37: // externs
                     38: 
                     39: extern String cycle_data_name;
                     40: 
1.182     paf        41: // defines for globals
                     42: 
1.203     paf        43: #define SQL_BIND_NAME "bind"
1.182     paf        44: #define SQL_DEFAULT_NAME "default"
                     45: #define SQL_DISTINCT_NAME "distinct"
                     46: #define TABLE_REVERSE_NAME "reverse"
                     47: 
                     48: // globals
                     49: 
1.203     paf        50: String sql_bind_name(SQL_BIND_NAME);
1.214     paf        51: String sql_limit_name(PA_SQL_LIMIT_NAME);
                     52: String sql_offset_name(PA_SQL_OFFSET_NAME);
1.182     paf        53: String sql_default_name(SQL_DEFAULT_NAME);
                     54: String sql_distinct_name(SQL_DISTINCT_NAME);
                     55: String table_reverse_name(TABLE_REVERSE_NAME);
                     56: 
1.1       paf        57: // methods
1.66      paf        58: 
1.182     paf        59: static Table::Action_options get_action_options(Request& r, MethodParams& params, 
                     60:                                                const Table& source) {
1.176     paf        61:        Table::Action_options result;
1.182     paf        62:        if(!params.count())
                     63:                return result;
                     64: 
                     65:        Value& maybe_options=params.last();
                     66: /* can not do it: 
                     67:        want to enable ^table::create[$source;
                     68: #              $.option[]
                     69:        ]
                     70:        but there is ^table.locate[name;value]
1.176     paf        71: 
1.212     paf        72:        ...if(voptions.is_defined() && !voptions.is_string()))
1.182     paf        73:        if(maybe_options.is_string()) { // allow empty options
                     74:                result.defined=true;
1.176     paf        75:                return result;
1.182     paf        76:        }
                     77: */
                     78:        HashStringValue* options=maybe_options.get_hash();
1.176     paf        79:        if(!options)
                     80:                return result;
                     81: 
                     82:        result.defined=true;
                     83:        bool defined_offset=false;
                     84: 
                     85:        int valid_options=0;
1.182     paf        86:        if(Value* voffset=options->get(sql_offset_name)) {
1.176     paf        87:                valid_options++;
                     88:                defined_offset=true;
                     89:                if(voffset->is_string()) {
1.182     paf        90:                        const String&  soffset=*voffset->get_string();
1.176     paf        91:                        if(soffset == "cur")
                     92:                                result.offset=source.current();
                     93:                        else
1.164     paf        94:                                throw Exception("parser.runtime",
1.176     paf        95:                                        &soffset,
                     96:                                        "must be 'cur' string or expression");
                     97:                } else 
                     98:                        result.offset=r.process_to_value(*voffset).as_int();
                     99:        }
1.182     paf       100:        if(Value* vlimit=options->get(sql_limit_name)) {
1.176     paf       101:                valid_options++;
                    102:                result.limit=r.process_to_value(*vlimit).as_int();
                    103:        }
1.182     paf       104:        if(Value *vreverse=(Value *)options->get(table_reverse_name)) { 
                    105:                valid_options++; 
                    106:                result.reverse=r.process_to_value(*vreverse).as_bool(); 
                    107:                if(result.reverse && !defined_offset) 
                    108:                        result.offset=source.count()-1; 
                    109:        } 
                    110: 
                    111:        if(valid_options!=options->count())
1.176     paf       112:                throw Exception("parser.runtime",
1.182     paf       113:                        0,
1.176     paf       114:                        "called with invalid option");
                    115: 
                    116:        return result;
                    117: }
1.180     paf       118: static void check_option_param(bool options_defined, 
1.182     paf       119:                          MethodParams& params, size_t next_param_index,
1.176     paf       120:                          const char *msg) {
1.182     paf       121:        if(next_param_index+(options_defined?1:0) != params.count())
1.176     paf       122:                throw Exception("parser.runtime",
1.182     paf       123:                        0,
1.176     paf       124:                        "%s", msg);
1.157     paf       125: }
                    126: 
1.182     paf       127: static void _create(Request& r, MethodParams& params) {
1.157     paf       128:        // clone/copy part?
1.182     paf       129:        if(Table *source=params[0].get_table()) {
                    130:                Table::Action_options o=get_action_options(r, params, *source);
                    131:                check_option_param(o.defined, params, 1, 
1.176     paf       132:                        "too many parameters");
1.182     paf       133:                GET_SELF(r, VTable).set_table(*new Table(*source, o));
1.157     paf       134:                return;
                    135:        }
1.142     paf       136: 
1.1       paf       137:        // data is last parameter
1.182     paf       138:        Temp_lang temp_lang(r, String::L_PASS_APPENDED);
                    139:        const String&  data=
                    140:                r.process_to_string(params.as_junction(params.count()-1, "body must be code"));
1.44      paf       141: 
                    142:        // parse columns
1.182     paf       143:        size_t raw_pos_after=0;
                    144:        Table::columns_type columns;
                    145:        if(params.count()==2) {
1.216     paf       146:                const String& snameless=params.as_string(0, "called with two params, first param may only be string 'nameless'");
1.213     paf       147:                if(snameless!="nameless")
                    148:                        throw Exception("parser.runtime",
                    149:                                &snameless,
                    150:                                "table::create called with two params, first param may only be 'nameless'");
1.182     paf       151:                columns=Table::columns_type(0); // nameless
1.21      paf       152:        } else {
1.182     paf       153:                columns=Table::columns_type(new ArrayString);
1.44      paf       154: 
1.182     paf       155:                ArrayString head;
                    156:                data.split(head, raw_pos_after, "\n", String::L_AS_IS, 1);
                    157:                if(head.count()) {
                    158:                        size_t col_pos_after=0;
                    159:                        head[0]->split(*columns, col_pos_after, "\t", String::L_AS_IS);
                    160:                }
1.44      paf       161:        }
                    162: 
1.182     paf       163:        Table& table=*new Table(columns);
1.44      paf       164:        // parse cells
1.182     paf       165: 
                    166:        ArrayString rows;
                    167:        data.split(rows, raw_pos_after, "\n", String::L_AS_IS);
                    168:        Array_iterator<const String*> i(rows);
1.98      parser    169:        while(i.has_next()) {
1.182     paf       170:                Table::element_type row(new ArrayString);
                    171:                const String&  string=*i.next();
1.118     parser    172:                // remove comment lines
1.182     paf       173:                if(!string.length())
1.44      paf       174:                        continue;
                    175: 
1.182     paf       176:                size_t col_pos_after=0;
                    177:                string.split(*row, col_pos_after, "\t", String::L_AS_IS);
                    178:                table+=row;
1.17      paf       179:        }
1.1       paf       180: 
1.44      paf       181:        // replace any previous table value
1.182     paf       182:        GET_SELF(r, VTable).set_table(table);
1.44      paf       183: }
                    184: 
1.187     paf       185: struct lsplit_result {
                    186:        char* piece;
                    187:        char delim;
                    188: 
                    189:        operator bool() { return piece!=0; }
                    190: };
                    191: 
                    192: inline lsplit_result lsplit(char* string, char delim1, char delim2) {
                    193:        lsplit_result result;
                    194:     if(string) {
                    195:                char delims[]={delim1, delim2, 0};
                    196:                if(char* v=strpbrk(string, delims)) {
                    197:                        result.delim=*v;
                    198:                        *v=0;
                    199:                        result.piece=v+1;
                    200:                        return result;
                    201:                }
                    202:     }
                    203:        result.piece=0;
                    204:        result.delim=0;
                    205:     return result;
                    206: }
                    207: 
                    208: inline lsplit_result lsplit(char* *string_ref, char delim1, char delim2) {
                    209:     lsplit_result result;
                    210:        result.piece=*string_ref;
                    211:        lsplit_result next=lsplit(*string_ref, delim1, delim2);
                    212:        result.delim=next.delim;
                    213:        *string_ref=next.piece;
                    214:     return result;
                    215: }
                    216: 
                    217: static lsplit_result lsplit(char** string_ref, char delim1, char delim2, char encloser) {
                    218:        lsplit_result result;
                    219: 
                    220:     if(char* string=*string_ref) {
                    221:                if(encloser && *string==encloser) {
                    222:             string++;
                    223:                        
                    224:                        char *read;
                    225:                        char *write;
                    226:                        write=read=string;
                    227:                        char c;
1.193     paf       228:                        while((c=*read++)) {
1.187     paf       229:                                if(c==encloser) {
                    230:                                        char n=*read;
                    231:                                        if(n==encloser) // double-encloser stands for encloser
                    232:                                                read++;
                    233:                                        else if(n==delim1 || n==delim2) {
                    234:                                                result.delim=n;
                    235:                                                read++;
                    236:                                                break;
                    237:                                        }
                    238:                                }
                    239: 
                    240:                                *write++=c;
                    241:                        }
                    242:                        *write=0; // terminate
                    243:                        *string_ref=c? read: 0;
                    244:                        result.piece=string;
                    245:                        return result;
                    246:                } else
                    247:                        return lsplit(string_ref, delim1, delim2);
                    248:     }
                    249:        result.piece=0;
                    250:     return result;
                    251: }
                    252: 
                    253: static void skip_empty_and_comment_lines( char** data_ref ) {
                    254:        if(char *data=*data_ref) {
                    255:                while( char c=*data ) {
                    256:                        if( c== '\n' || c == '#' ) {
                    257:                                /*nowhere=*/getrow(&data); // remove empty&comment lines
1.199     paf       258:                                if(!(*data_ref=data))
                    259:                                        break;
1.187     paf       260:                                continue;
                    261:                        }
                    262:                        break;
                    263:                }
                    264:        }
                    265: }
                    266: 
                    267: struct TableSeparators {
1.206     paf       268:        char column;  const String* scolumn;
                    269:        char encloser; const String* sencloser;
                    270: 
                    271:        TableSeparators():
                    272:                column('\t'), scolumn(new String("\t", false)),
                    273:                encloser(0), sencloser(0)
                    274:        {}
1.214     paf       275:        int load( HashStringValue& options ) {
                    276:                int result=0;
                    277:                if(Value* vseparator=options.get(PA_COLUMN_SEPARATOR_NAME)) {
1.206     paf       278:                        scolumn=&vseparator->as_string();
                    279:                        if(scolumn->length()!=1)
1.187     paf       280:                                throw Exception("parser.runtime",
1.206     paf       281:                                        scolumn,
1.187     paf       282:                                        "separator must be one character long");
1.206     paf       283:                        column=scolumn->first_char();
1.214     paf       284:                        result++;
1.187     paf       285:                }
1.214     paf       286:                if(Value* vencloser=options.get(PA_COLUMN_ENCLOSER_NAME)) {
1.188     paf       287:                        sencloser=&vencloser->as_string();
                    288:                        if(sencloser->length()!=1)
1.187     paf       289:                                throw Exception("parser.runtime",
1.188     paf       290:                                        sencloser,
1.187     paf       291:                                        "encloser must be one character long");
1.188     paf       292:                        encloser=sencloser->first_char();
1.214     paf       293:                        result++;
1.187     paf       294:                }
1.214     paf       295:                return result;
1.187     paf       296:        }
                    297: };
                    298: 
1.182     paf       299: static void _load(Request& r, MethodParams& params) {
                    300:        const String&  first_param=params.as_string(0, "file name must be string");
1.168     paf       301:        int filename_param_index=0;
                    302:        bool nameless=first_param=="nameless";
                    303:        if(nameless)
                    304:                filename_param_index++;
1.182     paf       305:        size_t options_param_index=filename_param_index+1;
1.186     paf       306: 
                    307:        HashStringValue *options=0;
1.187     paf       308:        TableSeparators separators;
1.186     paf       309:        if(options_param_index<params.count()
                    310:                && (options=params.as_no_junction(options_param_index, "additional params must be hash").get_hash())) {
                    311:                // cloning, so that we could change [to simplify checks on params inside file_read_text
                    312:                options=new HashStringValue(*options);
1.187     paf       313:                separators.load(*options);
1.186     paf       314:        }
                    315: 
1.44      paf       316:        // loading text
1.182     paf       317:        char *data=file_read_text(r.charsets,
                    318:                r.absolute(params.as_string(filename_param_index, "file name must be string")),
1.168     paf       319:                true,
1.186     paf       320:                options
1.168     paf       321:        );
1.44      paf       322: 
1.1       paf       323:        // parse columns
1.182     paf       324:        Table::columns_type columns;
1.168     paf       325:        if(nameless) {
1.182     paf       326:                columns=Table::columns_type(0); // nameless
1.1       paf       327:        } else {
1.182     paf       328:                columns=Table::columns_type(new ArrayString);
1.1       paf       329: 
1.187     paf       330:                skip_empty_and_comment_lines(&data);
                    331:                while( lsplit_result sr=lsplit(&data, separators.column, '\n', separators.encloser) ) {
                    332:                        *columns+=new String(sr.piece, 0, true);
                    333:                        if(sr.delim=='\n') 
                    334:                                break;
1.139     paf       335:                }
1.1       paf       336:        }
1.187     paf       337:        
                    338:        Table& table=*new Table(columns);
1.219     paf       339:        int columns_count=columns? columns->count(): 0;
1.1       paf       340: 
                    341:        // parse cells
1.218     paf       342:        Table::element_type row(new ArrayString(columns_count));
1.187     paf       343:        skip_empty_and_comment_lines(&data);
                    344:        while( lsplit_result sr=lsplit(&data, separators.column, '\n', separators.encloser) ) {
1.201     paf       345:                if(!*sr.piece && !sr.delim && !row->count()) // append last empty column [if without \n]
                    346:                        break;
1.187     paf       347:                *row+=new String(sr.piece, 0, true);
                    348:                if(sr.delim=='\n') {
                    349:                        table+=row;
1.218     paf       350:                        row=new ArrayString(columns_count);
1.187     paf       351:                        skip_empty_and_comment_lines(&data);
1.1       paf       352:                }
1.187     paf       353:        }
                    354:        // last line [if without \n]
                    355:        if(row->count())
1.1       paf       356:                table+=row;
1.187     paf       357:        
1.1       paf       358:        // replace any previous table value
1.182     paf       359:        GET_SELF(r, VTable).set_table(table);
1.1       paf       360: }
1.2       paf       361: 
1.188     paf       362: static void maybe_enclose( String& to, const String& from, char encloser, const String* sencloser ) {
                    363:        if(encloser) {
                    364:                to<<*sencloser;
                    365:                // while we have 'encloser'...
1.189     paf       366:                size_t pos_after=0;
1.220     paf       367:                for( size_t pos_before; (pos_before=from.pos( encloser, pos_after ))!=STRING_NOT_FOUND; pos_after=pos_before) {
                    368:                        pos_before++; // including first encloser (and skipping it for next pos)
                    369:             to<<from.mid(pos_after, pos_before);
                    370:                        to<<*sencloser; // doubling encloser
1.188     paf       371:                }
                    372:                // last piece
                    373:                size_t from_length=from.length();
                    374:                if(pos_after<from_length)
                    375:                        to<<from.mid(pos_after, from_length);
                    376: 
                    377:                to<<*sencloser;
                    378:        } else
1.202     paf       379:                to<<from;
1.188     paf       380: }
                    381: 
                    382: 
1.182     paf       383: static void _save(Request& r, MethodParams& params) {
1.188     paf       384:        const String& first_arg=params.as_string(0, "first argument must not be code");
                    385:        size_t param_index=1;
                    386: 
                    387:        bool do_append=false;
                    388:        bool output_column_names=true;
                    389: 
                    390:        // mode?
                    391:        if(first_arg=="append")
                    392:                do_append=true;
                    393:        else if(first_arg=="nameless")
                    394:                output_column_names=false;
                    395:        else
                    396:                --param_index;
                    397: 
                    398:        const String& file_name=params.as_string(param_index++, "file name must not be code");
                    399: 
                    400:        TableSeparators separators;
                    401:        if(param_index<params.count()) {
1.222     paf       402:                Value& voptions=params.as_no_junction(param_index++, "additional params must be hash");
                    403:                if( voptions.is_defined() && !voptions.is_string() ) {
                    404:                        if(HashStringValue* options=voptions.get_hash()) {
1.223   ! paf       405:                                int valid_options=separators.load(*options);
        !           406:                                if(valid_options!=options->count())
        !           407:                                        throw Exception("parser.runtime",
        !           408:                                                0,
        !           409:                                                "invalid option passed");
        !           410:                        } else {
1.188     paf       411:                                throw Exception("parser.runtime",
                    412:                                        0,
1.223   ! paf       413:                                        "additional params must be hash (did you spell mode parameter correctly?)");
        !           414:                        }
1.222     paf       415:                }
1.188     paf       416:        }
                    417:        if(param_index<params.count())
                    418:                throw Exception("parser.runtime",
                    419:                        0,
                    420:                        "bad mode (must be nameless or append)");
1.22      paf       421: 
1.182     paf       422:        Table& table=GET_SELF(r, VTable).table();
1.31      paf       423: 
1.182     paf       424:        String sdata;
1.188     paf       425:        if(output_column_names) {
1.31      paf       426:                if(table.columns()) { // named table
1.182     paf       427:                        for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
1.188     paf       428:                                maybe_enclose( sdata, *i.next(), separators.encloser, separators.sencloser );
1.98      parser    429:                                if(i.has_next())
1.206     paf       430:                                        sdata<<*separators.scolumn;
1.31      paf       431:                        }
1.188     paf       432:                } else { // nameless table [we were asked to output column names]
1.182     paf       433:                        if(int lsize=table.count()?table[0]->count():0)
1.31      paf       434:                                for(int column=0; column<lsize; column++) {
1.182     paf       435:                                        char *cindex_tab=new(PointerFreeGC) char[MAX_NUMBER];
                    436:                                        sdata.append_know_length(cindex_tab, 
1.185     paf       437:                                                snprintf(cindex_tab, MAX_NUMBER, 
1.206     paf       438:                                                        column<lsize-1?"%d%c":"%d", column, separators.column),
1.185     paf       439:                                                        String::L_CLEAN);
1.31      paf       440:                                }
                    441:                        else
1.182     paf       442:                                sdata.append_help_length("empty nameless table", 0, String::L_CLEAN);
1.31      paf       443:                }
1.182     paf       444:                sdata.append_know_length("\n", 1, String::L_CLEAN);
1.188     paf       445:        }
1.129     paf       446: 
1.31      paf       447:        // data lines
1.182     paf       448:        Array_iterator<ArrayString*> i(table);
1.98      parser    449:        while(i.has_next()) {
1.182     paf       450:                for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
1.188     paf       451:                        maybe_enclose( sdata, *c.next(), separators.encloser, separators.sencloser );
1.98      parser    452:                        if(c.has_next())
1.206     paf       453:                                sdata<<*separators.scolumn;
1.31      paf       454:                }
1.182     paf       455:                sdata.append_know_length("\n", 1, String::L_CLEAN);
1.31      paf       456:        }
                    457: 
                    458:        // write
1.217     paf       459:        {
                    460:                const char* data_cstr=sdata.cstr();
                    461:                file_write(r.absolute(file_name), 
                    462:                        data_cstr, sdata.length(), true, do_append);
                    463:                if(*data_cstr) // not empty (when empty it's not heap memory)
                    464:                        pa_free((void*)data_cstr); // not needed anymore
                    465:        }
1.22      paf       466: }
                    467: 
1.182     paf       468: static void _count(Request& r, MethodParams&) {
                    469:        int result=GET_SELF(r, VTable).table().count();
                    470:        r.write_no_lang(*new VInt(result));
1.6       paf       471: }
                    472: 
1.182     paf       473: static void _line(Request& r, MethodParams&) {
                    474:        int result=1+GET_SELF(r, VTable).table().current();
                    475:        r.write_no_lang(*new VInt(result));
1.6       paf       476: }
                    477: 
1.182     paf       478: static void _offset(Request& r, MethodParams& params) {
                    479:        Table& table=GET_SELF(r, VTable).table();
                    480:        if(params.count()) {
1.133     paf       481:                bool absolute=false;
1.182     paf       482:                if(params.count()>1) {
                    483:                    const String&  whence=params.as_string(0, "whence must be string");
1.133     paf       484:                    if(whence=="cur")
1.134     paf       485:                                absolute=false;
1.133     paf       486:                    else if(whence=="set")
1.134     paf       487:                                absolute=true;
1.133     paf       488:                    else
1.146     paf       489:                                throw Exception("parser.runtime",
1.134     paf       490:                                        &whence,
                    491:                                        "is invalid whence, valid are 'cur' or 'set'");
1.157     paf       492:                }
1.133     paf       493:                
1.211     paf       494:                int offset=params.as_int(params.count()-1, "offset must be expression", r);
                    495:                table.offset(absolute, offset);
1.151     paf       496:        } else
1.182     paf       497:                r.write_no_lang(*new VInt(table.current()));
1.6       paf       498: }
                    499: 
1.182     paf       500: static void _menu(Request& r, MethodParams& params) {
1.221     paf       501:        Temp_hash_value<const String::Body, void*> 
                    502:                cycle_data_setter(r.classes_conf, cycle_data_name, /*any not null flag*/&r);
                    503: 
1.182     paf       504:        Value& body_code=params.as_junction(0, "body must be code");
1.7       paf       505:        
1.182     paf       506:        Value* delim_maybe_code=params.count()>1?&params[1]:0;
1.7       paf       507: 
1.182     paf       508:        Table& table=GET_SELF(r, VTable).table();
1.7       paf       509:        bool need_delim=false;
1.99      parser    510:        int saved_current=table.current();
1.182     paf       511:        int size=table.count();
1.99      parser    512:        for(int row=0; row<size; row++) {
1.22      paf       513:                table.set_current(row);
1.7       paf       514: 
1.161     paf       515:                StringOrValue sv_processed=r.process(body_code);
1.221     paf       516:                Request::Skip lskip=r.get_skip(); r.set_skip(Request::SKIP_NOTHING);
1.182     paf       517:                const String* s_processed=sv_processed.get_string();
                    518:                if(delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body
1.161     paf       519:                        if(need_delim) // need delim & iteration produced string?
1.150     paf       520:                                r.write_pass_lang(r.process(*delim_maybe_code));
1.7       paf       521:                        need_delim=true;
                    522:                }
1.161     paf       523:                r.write_pass_lang(sv_processed);
1.221     paf       524: 
                    525:                if(lskip==Request::SKIP_BREAK)
                    526:                        break;
1.7       paf       527:        }
1.99      parser    528:        table.set_current(saved_current);
1.7       paf       529: }
                    530: 
1.110     parser    531: #ifndef DOXYGEN
1.174     paf       532: enum Table2hash_distint { D_ILLEGAL, D_FIRST, D_TABLES };
1.74      paf       533: struct Row_info {
1.166     paf       534:        Request *r;
1.74      paf       535:        Table *table;
1.182     paf       536:        Value* key_code;
                    537:        size_t key_field;
                    538:        Array<int>* value_fields;
                    539:        HashStringValue* hash;
1.174     paf       540:        Table2hash_distint distinct;
1.182     paf       541:        size_t row;
1.74      paf       542: };
1.110     parser    543: #endif
1.182     paf       544: static void table_row_to_hash(Table::element_type row, Row_info *info) {
                    545:        const String* key;
                    546:        if(info->key_code) {
                    547:                info->table->set_current(info->row++); // change context row
                    548:                StringOrValue sv_processed=info->r->process(*info->key_code);
1.166     paf       549:                key=&sv_processed.as_string();
                    550:        } else
1.182     paf       551:                key=info->key_field<row->count()?row->get(info->key_field):0;
1.166     paf       552: 
                    553:        if(!key)
                    554:                return; // ignore rows without key [too-short-record_array if-indexed]
1.74      paf       555:                
1.182     paf       556:        switch(info->distinct) {
1.174     paf       557:        case D_ILLEGAL: case D_FIRST:
                    558:                {
1.182     paf       559:                        VHash* vhash=new VHash;
                    560:                        HashStringValue& hash=vhash->hash();
                    561:                        for(Array_iterator<int> i(*info->value_fields); i.has_next(); ) {
                    562:                                size_t value_field=i.next();
                    563:                                if(value_field<row->count())
1.174     paf       564:                                        hash.put(
1.182     paf       565:                                                *info->table->columns()->get(value_field), 
                    566:                                                new VString(*row->get(value_field)));
1.174     paf       567:                        }
                    568: 
1.182     paf       569:                        if(info->hash->put_dont_replace(*key, vhash)) // put. existed?
                    570:                                if(info->distinct==D_ILLEGAL)
1.174     paf       571:                                        throw Exception("parser.runtime",
                    572:                                                key,
                    573:                                                "duplicate key");
                    574:                }
                    575:                break;
                    576:        case D_TABLES:
                    577:                {
1.182     paf       578:                        VTable* vtable=(VTable*)info->hash->get(*key); // put. table existed?
1.174     paf       579:                        Table* table;
                    580:                        if(vtable) 
                    581:                                table=vtable->get_table();
                    582:                        else {
                    583:                                // no? creating table of same structure as source
1.179     paf       584:                                Table::Action_options table_options(0, 0);
1.182     paf       585:                                table=new Table(*info->table, table_options/*no rows, just structure*/);
                    586:                                info->hash->put(*key, new VTable(table));
1.174     paf       587:                        }
1.182     paf       588:                        *table+=row;
1.174     paf       589:                }
                    590:                break;
                    591:        default:
                    592:                throw Exception(0,
                    593:                        0,
1.182     paf       594:                        "invalid distinct code (#%d)", info->distinct);
1.74      paf       595:        }
                    596: }
1.182     paf       597: static void _hash(Request& r, MethodParams& params) {
                    598:        Table& self_table=GET_SELF(r, VTable).table();
                    599:        VHash& result=*new VHash;
                    600:        if(Table::columns_type columns=self_table.columns())
                    601:                if(columns->count()>0) {
1.174     paf       602:                        Table2hash_distint distinct=D_ILLEGAL;
1.182     paf       603:                        int param_index=params.count()-1;
1.166     paf       604:                        if(param_index>0) {
1.182     paf       605:                                if(HashStringValue* options=
                    606:                                        params.as_no_junction(param_index, "param must not be code").get_hash()) {
1.166     paf       607:                                        --param_index;
                    608:                                        int valid_options=0;
1.182     paf       609:                                        if(Value* vdistinct_code=options->get(sql_distinct_name)) {
1.166     paf       610:                                                valid_options++;
1.174     paf       611:                                                Value& vdistinct_value=r.process_to_value(*vdistinct_code);
                    612:                                                if(vdistinct_value.is_string()) {
                    613:                                                        const String& sdistinct=*vdistinct_value.get_string();
                    614:                                                        if(sdistinct=="tables")
                    615:                                                                distinct=D_TABLES;
                    616:                                                        else
                    617:                                                                throw Exception("parser.runtime",
                    618:                                                                        &sdistinct,
                    619:                                                                        "must be 'tables' or true/false");
                    620:                                                } else
                    621:                                                        distinct=vdistinct_value.as_bool()?D_FIRST:D_ILLEGAL;
1.166     paf       622:                                        }
1.182     paf       623:                                        if(valid_options!=options->count())
1.166     paf       624:                                                throw Exception("parser.runtime",
1.182     paf       625:                                                        0,
1.166     paf       626:                                                        "called with invalid option");
1.163     paf       627:                                }
                    628:                        }
                    629:                        if(param_index==2) // bad options param type
                    630:                                throw Exception("parser.runtime",
1.182     paf       631:                                        0,
1.163     paf       632:                                        "options must be hash");
                    633: 
1.182     paf       634:                        Array<int> value_fields;
1.163     paf       635:                        if(param_index>0) {
1.174     paf       636:                                if(distinct!=D_ILLEGAL && distinct!=D_FIRST)
                    637:                                        throw Exception("parser.runtime",
                    638:                                                0,
                    639:                                                "in distinct[tables] mode you may not specify value field(s)");
1.182     paf       640:                                Value& value_fields_param=params.as_no_junction(param_index, "value field(s) must not be code");
1.123     parser    641:                                if(value_fields_param.is_string()) {
1.182     paf       642:                                        value_fields+=self_table.column_name2index(
                    643:                                                *value_fields_param.get_string(), true);
                    644:                                } else if(Table* value_fields_table=value_fields_param.get_table()) {
                    645:                                        for(Array_iterator<Table::element_type> i(*value_fields_table); 
                    646:                                                i.has_next(); ) {
                    647:                                                const String& value_field_name
                    648:                                                        =*i.next()->get(0);
                    649:                                                value_fields
                    650:                                                        +=self_table.column_name2index(value_field_name, true);
1.123     parser    651:                                        }
                    652:                                } else
1.146     paf       653:                                        throw Exception("parser.runtime",
1.182     paf       654:                                                0,
1.197     paf       655:                                                "value field(s) must be string or table"
1.123     parser    656:                                        );
                    657:                        } else { // by all columns, including key
1.174     paf       658:                                if(!(distinct!=D_ILLEGAL && distinct!=D_FIRST))
1.182     paf       659:                                        for(size_t i=0; i<columns->count(); i++)
1.174     paf       660:                                                value_fields+=i;
1.74      paf       661:                        }
                    662: 
1.182     paf       663: 
                    664:                        {
                    665:                                Value* key_param=&params[0];
1.193     paf       666:                                Row_info info={
                    667:                                        &r,
                    668:                                        &self_table,
                    669:                                        /*key_code=*/key_param->get_junction()?key_param:0,
1.197     paf       670:                                        /*key_field=*/0/*filled below*/,
1.193     paf       671:                                        &value_fields,
                    672:                                        &result.hash(),
                    673:                                        distinct,
                    674:                                        /*row=*/0
                    675:                                };
1.195     paf       676:                                info.key_field=(info.key_code?-1
                    677:                                                :self_table.column_name2index(key_param->as_string(), true));
1.182     paf       678: 
                    679:                                int saved_current=self_table.current();
                    680:                                self_table.for_each(table_row_to_hash, &info);
                    681:                                self_table.set_current(saved_current);
1.207     paf       682: 
                    683:                                result.extract_default();
1.182     paf       684:                        }
1.74      paf       685:                }
1.97      parser    686:        r.write_no_lang(result);
1.74      paf       687: }
                    688: 
1.110     parser    689: #ifndef DOXYGEN
1.78      paf       690: struct Table_seq_item {
1.182     paf       691:        ArrayString* row;
1.34      paf       692:        union {
1.182     paf       693:                const char *c_str;
1.34      paf       694:                double d;
                    695:        } value;
1.32      paf       696: };
1.110     parser    697: #endif
1.34      paf       698: static int sort_cmp_string(const void *a, const void *b) {
                    699:        return strcmp(
1.78      paf       700:                static_cast<const Table_seq_item *>(a)->value.c_str, 
                    701:                static_cast<const Table_seq_item *>(b)->value.c_str
1.34      paf       702:        );
                    703: }
                    704: static int sort_cmp_double(const void *a, const void *b) {
1.78      paf       705:        double va=static_cast<const Table_seq_item *>(a)->value.d;
                    706:        double vb=static_cast<const Table_seq_item *>(b)->value.d;
1.34      paf       707:        if(va<vb)
                    708:                return -1;
                    709:        else if(va>vb)
                    710:                return +1;
                    711:        else 
                    712:                return 0;
                    713: }
1.182     paf       714: static void _sort(Request& r, MethodParams& params) {
                    715:        Value& key_maker=params.as_junction(0, "key-maker must be code");
1.61      paf       716: 
1.182     paf       717:        bool reverse=params.count()>1/*..[desc|asc|]*/?
                    718:                reverse=params.as_no_junction(1, "order must not be code").as_string()=="desc":
1.104     parser    719:                false; // default=asc
1.32      paf       720: 
1.182     paf       721:        Table& old_table=GET_SELF(r, VTable).table();
                    722:        Table& new_table=*new Table(old_table.columns());
1.34      paf       723: 
1.182     paf       724:        Table_seq_item* seq=new(PointerFreeGC) Table_seq_item[old_table.count()];
1.34      paf       725:        int i;
                    726: 
                    727:        // calculate key values
                    728:        bool key_values_are_strings=true;
1.182     paf       729:        int old_count=old_table.count();
                    730:        for(i=0; i<old_count; i++) {
1.101     parser    731:                old_table.set_current(i);
1.32      paf       732:                // calculate key value
1.182     paf       733:                seq[i].row=old_table[i];
                    734:                Value& value=r.process_to_value(key_maker).as_expr_result(true/*return string as-is*/);
1.34      paf       735:                if(i==0) // determining key values type by first one
                    736:                        key_values_are_strings=value.is_string();
                    737: 
                    738:                if(key_values_are_strings)
                    739:                        seq[i].value.c_str=value.as_string().cstr();
                    740:                else
                    741:                        seq[i].value.d=value.as_double();
1.32      paf       742:        }
                    743:        // sort keys
1.182     paf       744:        _qsort(seq, old_count, sizeof(Table_seq_item), 
1.34      paf       745:                key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32      paf       746: 
1.34      paf       747:        // reorder table as they require in 'seq'
1.182     paf       748:        for(i=0; i<old_count; i++)
                    749:                new_table+=Table::element_type(seq[reverse?old_count-1-i:i].row);
                    750: 
                    751:        delete[] seq;
1.32      paf       752: 
1.116     parser    753:        // replace any previous table value
1.182     paf       754:        GET_SELF(r, VTable).set_table(new_table);
1.32      paf       755: }
                    756: 
1.176     paf       757: #ifndef DOXYGEN
1.182     paf       758: struct Expression_is_true_info {
1.176     paf       759:        Request* r;
                    760:        Value* expression_code;
                    761: };
                    762: #endif
1.191     paf       763: static bool expression_is_true(Table&, Expression_is_true_info* info) {
                    764:        return info->r->process_to_value(*info->expression_code).as_bool();
1.176     paf       765: }
                    766: static bool _locate_expression(Table& table, Table::Action_options o,
1.182     paf       767:                               Request& r, MethodParams& params) {
                    768:        check_option_param(o.defined, params, 1,
1.176     paf       769:                "locate by expression only has parameters: expression and, maybe, options");
1.182     paf       770:        Value& expression_code=params.as_junction(0, "must be expression");
1.145     paf       771: 
1.182     paf       772:        Expression_is_true_info info={&r, &expression_code};
                    773:        return table.table_first_that(expression_is_true, &info, o);
1.145     paf       774: }
1.176     paf       775: static bool _locate_name_value(Table& table, Table::Action_options o,
1.191     paf       776:                               Request&, MethodParams& params) {
1.182     paf       777:        check_option_param(o.defined, params, 2,
1.176     paf       778:                "locate by locate by name has parameters: name, value and, maybe, options");
1.182     paf       779:        const String& name=params.as_string(0, "column name must be string");
                    780:        const String& value=params.as_string(1, "value must be string");
                    781:        return table.locate(name, value, o);
                    782: }
                    783: static void _locate(Request& r, MethodParams& params) {
                    784:        Table& table=GET_SELF(r, VTable).table();
1.72      paf       785: 
1.182     paf       786:        Table::Action_options o=get_action_options(r, params, table);
                    787: 
                    788:        bool result=params[0].get_junction()?
                    789:                _locate_expression(table, o, r, params) :
                    790:                _locate_name_value(table, o, r, params);
                    791:        r.write_no_lang(*new VBool(result));
1.145     paf       792: }
1.182     paf       793: 
                    794: 
1.191     paf       795: static void _flip(Request& r, MethodParams&) {
1.182     paf       796:        Table& old_table=GET_SELF(r, VTable).table();
1.184     paf       797:        Table& new_table=*new Table(0);
1.182     paf       798:        if(size_t old_count=old_table.count())
                    799:                if(size_t old_cols=old_table[0]->count()) 
                    800:                        for(size_t column=0; column<old_cols; column++) {
                    801:                                Table::element_type new_row(new ArrayString(old_count));
                    802:                                for(size_t i=0; i<old_count; i++) {
                    803:                                        Table::element_type old_row=old_table[i];
                    804:                                        *new_row+=column<old_row->count()?old_row->get(column):new String;
1.39      paf       805:                                }
1.182     paf       806:                                new_table+=new_row;
1.39      paf       807:                        }
                    808: 
1.182     paf       809:        r.write_no_lang(*new VTable(&new_table));
1.39      paf       810: }
                    811: 
1.182     paf       812: static void _append(Request& r, MethodParams& params) {
1.134     paf       813:        // data
1.182     paf       814:        Temp_lang temp_lang(r, String::L_PASS_APPENDED);
                    815:        const String& string=r.process_to_string(params.as_junction(0, "body must be code"));
1.41      paf       816: 
                    817:        // parse cells
1.182     paf       818:        Table::element_type row=new ArrayString;
                    819:        size_t pos_after=0;
                    820:        string.split(*row, pos_after, "\t", String::L_AS_IS);
1.41      paf       821: 
1.182     paf       822:        GET_SELF(r, VTable).table()+=row;
1.41      paf       823: }
                    824: 
1.182     paf       825: static void join_named_row(Table& src, Table* dest) {
                    826:        Table::columns_type dest_columns=dest->columns();
                    827:        size_t dest_columns_count=dest_columns->count();
                    828:        Table::element_type dest_row(new ArrayString(dest_columns_count));
                    829:        for(size_t dest_column=0; dest_column<dest_columns_count; dest_column++) {
                    830:                const String* src_item=src.item(*dest_columns->get(dest_column));
                    831:                *dest_row+=src_item?src_item:new String;
                    832:        }
                    833:        *dest+=dest_row;
                    834: }
                    835: static void join_nameless_row(Table& src, Table* dest) {
                    836:        *dest+=src[src.current()];
                    837: }
                    838: static void _join(Request& r, MethodParams& params) {
                    839:        Table* maybe_src=params.as_no_junction(0, "table ref must not be code").get_table();
1.42      paf       840:        if(!maybe_src)
1.146     paf       841:                throw Exception("parser.runtime", 
1.182     paf       842:                        0, 
1.42      paf       843:                        "source is not a table");
1.176     paf       844:        Table& src=*maybe_src;
                    845: 
1.182     paf       846:        Table::Action_options o=get_action_options(r, params, src);
                    847:        check_option_param(o.defined, params, 1,
1.176     paf       848:                "invalid extra parameter");
1.42      paf       849: 
1.182     paf       850:        Table& dest=GET_SELF(r, VTable).table();
1.42      paf       851:        if(&src == &dest)
1.146     paf       852:                throw Exception("parser.runtime", 
1.182     paf       853:                        0, 
1.42      paf       854:                        "source and destination are same table");
                    855: 
1.193     paf       856:        if(dest.columns()) // dest is named
1.182     paf       857:                src.table_for_each(join_named_row, &dest, o);
                    858:        else // dest is nameless
                    859:                src.table_for_each(join_nameless_row, &dest, o);
1.42      paf       860: }
                    861: 
1.94      parser    862: #ifndef DOXYGEN
1.169     paf       863: class Table_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.182     paf       864:        ArrayString& columns;
1.218     paf       865:        int columns_count;
1.182     paf       866:        ArrayString* row;
1.94      parser    867: public:
1.182     paf       868:        Table* table;
                    869: public:
                    870:        Table_sql_event_handlers() :
                    871:                columns(*new ArrayString), row(0), table(0) {
1.94      parser    872:        }
                    873: 
1.182     paf       874:        bool add_column(SQL_Error& error, const char *str, size_t length) {
1.170     paf       875:                try {
1.182     paf       876:                        columns+=new String(str, length, true);
1.170     paf       877:                        return false;
                    878:                } catch(...) {
                    879:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_column");
                    880:                        return true;
                    881:                }
                    882:        }
                    883:        bool before_rows(SQL_Error& error) { 
                    884:                try {
1.182     paf       885:                        table=new Table(&columns);
1.218     paf       886:                        columns_count=columns.count();
1.170     paf       887:                        return false;
                    888:                } catch(...) {
                    889:                        error=SQL_Error("exception occured in Table_sql_event_handlers::before_rows");
                    890:                        return true;
                    891:                }
                    892:        }
                    893:        bool add_row(SQL_Error& error) {
                    894:                try {
1.218     paf       895:                        *table+=row=new ArrayString(columns_count);
1.170     paf       896:                        return false;
                    897:                } catch(...) {
                    898:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_row");
                    899:                        return true;
                    900:                }
                    901:        }
1.182     paf       902:        bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.170     paf       903:                try {
1.182     paf       904:                        String& cell=*new String;
                    905:                        if(length)
                    906:                                cell.append_know_length(str, length, String::L_TAINTED);
                    907:                        *row+=&cell;
1.170     paf       908:                        return false;
                    909:                } catch(...) {
                    910:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_row_cell");
                    911:                        return true;
                    912:                }
1.94      parser    913:        }
                    914: };
                    915: #endif
1.204     paf       916: 
                    917: static void marshal_bind(
                    918:                                                 HashStringValue::key_type aname, 
                    919:                                                 HashStringValue::value_type avalue,
                    920:                                                 SQL_Driver::Placeholder** pptr) 
                    921: {
                    922:        SQL_Driver::Placeholder& ph=**pptr;
                    923:        ph.name=aname.cstr();
1.205     paf       924:        ph.value=avalue->as_string().cstr(String::L_UNSPECIFIED);
1.204     paf       925:        ph.is_null=avalue->get_class()==void_class;
                    926:        ph.were_updated=false;
                    927: 
                    928:        (*pptr)++;
                    929: }
                    930: 
                    931: // not static, used elsewhere
                    932: int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders) {
                    933:        int hash_count=hash.count();
                    934:        placeholders=new(UseGC) SQL_Driver::Placeholder[hash_count];
                    935:        SQL_Driver::Placeholder* ptr=placeholders;
1.221     paf       936:        hash.for_each<SQL_Driver::Placeholder**>(marshal_bind, &ptr);
1.204     paf       937:        return hash_count;
                    938: }
                    939: 
                    940: // not static, used elsewhere
                    941: void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders) {
                    942:        SQL_Driver::Placeholder* ph=placeholders;
                    943:        for(int i=0; i<placeholder_count; i++, ph++)
                    944:                if(ph->were_updated) {
                    945:                        Value* value;
                    946:                        if(ph->is_null)
                    947:                                value=new VVoid();
                    948:                        else
                    949:                                if(ph->value)
                    950:                                        value=new VString(*new String(ph->value, 0, true/*tainted*/));
                    951:                                else
                    952:                                        value=new VString(*new String());                                       
                    953:                        hash.put(ph->name, value);
                    954:                }
                    955: }
                    956: 
1.182     paf       957: static void _sql(Request& r, MethodParams& params) {
                    958:        Value& statement=params.as_junction(0, "statement must be code");
1.49      paf       959: 
1.204     paf       960:        HashStringValue* bind=0;
1.53      paf       961:        ulong limit=0;
1.100     parser    962:        ulong offset=0;
1.182     paf       963:        if(params.count()>1) {
                    964:                Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.212     paf       965:                if(voptions.is_defined() && !voptions.is_string())
1.182     paf       966:                        if(HashStringValue* options=voptions.get_hash()) {
1.164     paf       967:                                int valid_options=0;
1.204     paf       968:                                if(Value* vbind=options->get(sql_bind_name)) {
                    969:                                        valid_options++;
                    970:                                        bind=vbind->get_hash();
                    971:                                }
1.182     paf       972:                                if(Value* vlimit=options->get(sql_limit_name)) {
1.164     paf       973:                                        valid_options++;
1.147     paf       974:                                        limit=(ulong)r.process_to_value(*vlimit).as_double();
1.164     paf       975:                                }
1.182     paf       976:                                if(Value* voffset=options->get(sql_offset_name)) {
1.164     paf       977:                                        valid_options++;
1.147     paf       978:                                        offset=(ulong)r.process_to_value(*voffset).as_double();
1.164     paf       979:                                }
1.182     paf       980:                                if(valid_options!=options->count())
1.164     paf       981:                                        throw Exception("parser.runtime",
1.182     paf       982:                                                0,
1.164     paf       983:                                                "called with invalid option");
1.109     parser    984:                        } else
1.146     paf       985:                                throw Exception("parser.runtime",
1.182     paf       986:                                        0,
1.109     parser    987:                                        "options must be hash");
1.49      paf       988:        }
                    989: 
1.204     paf       990:        SQL_Driver::Placeholder* placeholders=0;
                    991:        uint placeholders_count=0;
                    992:        if(bind)
                    993:                placeholders_count=marshal_binds(*bind, placeholders);
                    994: 
1.182     paf       995:        Temp_lang temp_lang(r, String::L_SQL);
                    996:        const String&  statement_string=r.process_to_string(statement);
                    997:        const char* statement_cstr=
                    998:                statement_string.cstr(String::L_UNSPECIFIED, r.connection());
                    999:        Table_sql_event_handlers handlers;
1.135     paf      1000: #ifdef RESOURCES_DEBUG
                   1001:        struct timeval mt[2];
                   1002:        //measure:before
                   1003:        gettimeofday(&mt[0],NULL);
                   1004: #endif 
1.182     paf      1005:        r.connection()->query(
1.203     paf      1006:                statement_cstr, 
1.208     paf      1007:                placeholders_count, placeholders,
1.203     paf      1008:                offset, limit, 
1.160     paf      1009:                handlers,
                   1010:                statement_string);
1.135     paf      1011:        
                   1012: #ifdef RESOURCES_DEBUG
                   1013:                //measure:after connect
                   1014:        gettimeofday(&mt[1],NULL);
                   1015:        
                   1016:        double t[2];
                   1017:        for(int i=0;i<2;i++)
                   1018:            t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
                   1019:            
                   1020:        r.sql_request_time+=t[1]-t[0];
                   1021: #endif                         
1.204     paf      1022: 
                   1023:        if(bind)
                   1024:                unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.96      parser   1025: 
1.182     paf      1026:        Table& result=
                   1027:                handlers.table?*handlers.table: // query resulted in table? return it
                   1028:                *new Table(Table::columns_type(0)); // query returned no table, fake it
1.49      paf      1029: 
                   1030:        // replace any previous table value
1.182     paf      1031:        GET_SELF(r, VTable).set_table(result);
1.49      paf      1032: }
                   1033: 
1.182     paf      1034: static void _columns(Request& r, MethodParams&) {
1.88      parser   1035: 
1.182     paf      1036:        Table::columns_type result_columns(new ArrayString);
                   1037:        *result_columns+=new String("column");
                   1038:        Table& result_table=*new Table(result_columns);
1.88      parser   1039: 
1.182     paf      1040:        Table& source_table=GET_SELF(r, VTable).table();
                   1041:        if(Table::columns_type source_columns=source_table.columns()) {
                   1042:                for(Array_iterator<const String*> i(*source_columns); i.has_next(); ) {
                   1043:                        Table::element_type result_row(new ArrayString);
                   1044:                        *result_row+=i.next();
                   1045:                        result_table+=result_row;
1.88      parser   1046:                }
                   1047:        }
                   1048: 
1.182     paf      1049:        r.write_no_lang(*new VTable(&result_table));
1.88      parser   1050: }
                   1051: 
1.182     paf      1052: static void _select(Request& r, MethodParams& params) {
                   1053:        Value& vcondition=params.as_junction(0, "condition must be expression");
1.148     paf      1054: 
1.182     paf      1055:        Table& source_table=GET_SELF(r, VTable).table();
                   1056:        Table& result_table=*new Table(source_table.columns());
1.148     paf      1057: 
                   1058:        int saved_current=source_table.current();
1.182     paf      1059:        int size=source_table.count();
1.148     paf      1060:        for(int row=0; row<size; row++) {
                   1061:                source_table.set_current(row);
                   1062: 
1.150     paf      1063:                bool condition=r.process_to_value(vcondition, 
1.148     paf      1064:                                false/*don't intercept string*/).as_bool();
                   1065: 
                   1066:                if(condition) // ...condition is true=
1.182     paf      1067:                        result_table+=source_table[row]; // =green light to go to result
1.148     paf      1068:        }
                   1069:        source_table.set_current(saved_current);
                   1070: 
1.182     paf      1071:        r.write_no_lang(*new VTable(&result_table));
1.148     paf      1072: }
                   1073: 
1.66      paf      1074: // constructor
                   1075: 
1.182     paf      1076: MTable::MTable(): Methoded("table") {
1.142     paf      1077:        // ^table::create{data}
                   1078:        // ^table::create[nameless]{data}
                   1079:        // ^table::create[table]
                   1080:        add_native_method("create", Method::CT_DYNAMIC, _create, 1, 2);
                   1081:        // old name for compatibility with <= v 1.141 2002/01/25 11:33:45 paf
                   1082:        add_native_method("set", Method::CT_DYNAMIC, _create, 1, 2); 
1.2       paf      1083: 
1.142     paf      1084:        // ^table::load[file]  
                   1085:        // ^table::load[nameless;file]
1.168     paf      1086:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 3);
1.22      paf      1087: 
                   1088:        // ^table.save[file]  
                   1089:        // ^table.save[nameless;file]
1.188     paf      1090:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 3);
1.6       paf      1091: 
                   1092:        // ^table.count[]
1.66      paf      1093:        add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6       paf      1094: 
                   1095:        // ^table.line[]
1.66      paf      1096:        add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6       paf      1097: 
1.10      paf      1098:        // ^table.offset[]  
1.133     paf      1099:        // ^table.offset(offset)
                   1100:        // ^table.offset[cur|set](offset)
                   1101:        add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 2);
1.7       paf      1102: 
1.10      paf      1103:        // ^table.menu{code}  
                   1104:        // ^table.menu{code}[delim]
1.66      paf      1105:        add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.74      paf      1106: 
1.79      paf      1107:        // ^table:hash[key field name]
1.123     parser   1108:        // ^table:hash[key field name][value field name(s) string/table]
1.163     paf      1109:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 3);
1.32      paf      1110: 
1.102     parser   1111:        // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc]
                   1112:        // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc]
1.66      paf      1113:        add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8       paf      1114: 
1.36      paf      1115:        // ^table.locate[field;value]
1.176     paf      1116:        add_native_method("locate", Method::CT_DYNAMIC, _locate, 1, 3);
1.39      paf      1117: 
                   1118:        // ^table.flip[]
1.66      paf      1119:        add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41      paf      1120: 
                   1121:        // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
1.66      paf      1122:        add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42      paf      1123: 
1.157     paf      1124:        // ^table.join[table][$.limit(10) $.offset(1) $.offset[cur] ]
                   1125:        add_native_method("join", Method::CT_DYNAMIC, _join, 1, 2);
1.49      paf      1126: 
                   1127: 
1.100     parser   1128:        // ^table:sql[query]
                   1129:        // ^table:sql[query][$.limit(1) $.offset(2)]
                   1130:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.88      parser   1131: 
                   1132:        // ^table:columns[]
                   1133:        add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 0);
1.148     paf      1134: 
                   1135:        // ^table.select(expression) = table
                   1136:        add_native_method("select", Method::CT_DYNAMIC, _select, 1, 1);
1.40      paf      1137: }

E-mail: