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

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

E-mail: