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

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.20      paf         7: 
1.212   ! paf         8: static const char * const IDENT_TABLE_C="$Date: 2005/08/26 11:12:45 $";
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: 
                     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.212   ! paf        76:        ...if(voptions.is_defined() && !voptions.is_string()))
1.182     paf        77:        if(maybe_options.is_string()) { // allow empty options
                     78:                result.defined=true;
1.176     paf        79:                return result;
1.182     paf        80:        }
                     81: */
                     82:        HashStringValue* options=maybe_options.get_hash();
1.176     paf        83:        if(!options)
                     84:                return result;
                     85: 
                     86:        result.defined=true;
                     87:        bool defined_offset=false;
                     88: 
                     89:        int valid_options=0;
1.182     paf        90:        if(Value* voffset=options->get(sql_offset_name)) {
1.176     paf        91:                valid_options++;
                     92:                defined_offset=true;
                     93:                if(voffset->is_string()) {
1.182     paf        94:                        const String&  soffset=*voffset->get_string();
1.176     paf        95:                        if(soffset == "cur")
                     96:                                result.offset=source.current();
                     97:                        else
1.164     paf        98:                                throw Exception("parser.runtime",
1.176     paf        99:                                        &soffset,
                    100:                                        "must be 'cur' string or expression");
                    101:                } else 
                    102:                        result.offset=r.process_to_value(*voffset).as_int();
                    103:        }
1.182     paf       104:        if(Value* vlimit=options->get(sql_limit_name)) {
1.176     paf       105:                valid_options++;
                    106:                result.limit=r.process_to_value(*vlimit).as_int();
                    107:        }
1.182     paf       108:        if(Value *vreverse=(Value *)options->get(table_reverse_name)) { 
                    109:                valid_options++; 
                    110:                result.reverse=r.process_to_value(*vreverse).as_bool(); 
                    111:                if(result.reverse && !defined_offset) 
                    112:                        result.offset=source.count()-1; 
                    113:        } 
                    114: 
                    115:        if(valid_options!=options->count())
1.176     paf       116:                throw Exception("parser.runtime",
1.182     paf       117:                        0,
1.176     paf       118:                        "called with invalid option");
                    119: 
                    120:        return result;
                    121: }
1.180     paf       122: static void check_option_param(bool options_defined, 
1.182     paf       123:                          MethodParams& params, size_t next_param_index,
1.176     paf       124:                          const char *msg) {
1.182     paf       125:        if(next_param_index+(options_defined?1:0) != params.count())
1.176     paf       126:                throw Exception("parser.runtime",
1.182     paf       127:                        0,
1.176     paf       128:                        "%s", msg);
1.157     paf       129: }
                    130: 
1.182     paf       131: static void _create(Request& r, MethodParams& params) {
1.157     paf       132:        // clone/copy part?
1.182     paf       133:        if(Table *source=params[0].get_table()) {
                    134:                Table::Action_options o=get_action_options(r, params, *source);
                    135:                check_option_param(o.defined, params, 1, 
1.176     paf       136:                        "too many parameters");
1.182     paf       137:                GET_SELF(r, VTable).set_table(*new Table(*source, o));
1.157     paf       138:                return;
                    139:        }
1.142     paf       140: 
1.1       paf       141:        // data is last parameter
1.182     paf       142:        Temp_lang temp_lang(r, String::L_PASS_APPENDED);
                    143:        const String&  data=
                    144:                r.process_to_string(params.as_junction(params.count()-1, "body must be code"));
1.44      paf       145: 
                    146:        // parse columns
1.182     paf       147:        size_t raw_pos_after=0;
                    148:        Table::columns_type columns;
                    149:        if(params.count()==2) {
                    150:                columns=Table::columns_type(0); // nameless
1.21      paf       151:        } else {
1.182     paf       152:                columns=Table::columns_type(new ArrayString);
1.44      paf       153: 
1.182     paf       154:                ArrayString head;
                    155:                data.split(head, raw_pos_after, "\n", String::L_AS_IS, 1);
                    156:                if(head.count()) {
                    157:                        size_t col_pos_after=0;
                    158:                        head[0]->split(*columns, col_pos_after, "\t", String::L_AS_IS);
                    159:                }
1.44      paf       160:        }
                    161: 
1.182     paf       162:        Table& table=*new Table(columns);
1.44      paf       163:        // parse cells
1.182     paf       164: 
                    165:        ArrayString rows;
                    166:        data.split(rows, raw_pos_after, "\n", String::L_AS_IS);
                    167:        Array_iterator<const String*> i(rows);
1.98      parser    168:        while(i.has_next()) {
1.182     paf       169:                Table::element_type row(new ArrayString);
                    170:                const String&  string=*i.next();
1.118     parser    171:                // remove comment lines
1.182     paf       172:                if(!string.length())
1.44      paf       173:                        continue;
                    174: 
1.182     paf       175:                size_t col_pos_after=0;
                    176:                string.split(*row, col_pos_after, "\t", String::L_AS_IS);
                    177:                table+=row;
1.17      paf       178:        }
1.1       paf       179: 
1.44      paf       180:        // replace any previous table value
1.182     paf       181:        GET_SELF(r, VTable).set_table(table);
1.44      paf       182: }
                    183: 
1.187     paf       184: struct lsplit_result {
                    185:        char* piece;
                    186:        char delim;
                    187: 
                    188:        operator bool() { return piece!=0; }
                    189: };
                    190: 
                    191: inline lsplit_result lsplit(char* string, char delim1, char delim2) {
                    192:        lsplit_result result;
                    193:     if(string) {
                    194:                char delims[]={delim1, delim2, 0};
                    195:                if(char* v=strpbrk(string, delims)) {
                    196:                        result.delim=*v;
                    197:                        *v=0;
                    198:                        result.piece=v+1;
                    199:                        return result;
                    200:                }
                    201:     }
                    202:        result.piece=0;
                    203:        result.delim=0;
                    204:     return result;
                    205: }
                    206: 
                    207: inline lsplit_result lsplit(char* *string_ref, char delim1, char delim2) {
                    208:     lsplit_result result;
                    209:        result.piece=*string_ref;
                    210:        lsplit_result next=lsplit(*string_ref, delim1, delim2);
                    211:        result.delim=next.delim;
                    212:        *string_ref=next.piece;
                    213:     return result;
                    214: }
                    215: 
                    216: static lsplit_result lsplit(char** string_ref, char delim1, char delim2, char encloser) {
                    217:        lsplit_result result;
                    218: 
                    219:     if(char* string=*string_ref) {
                    220:                if(encloser && *string==encloser) {
                    221:             string++;
                    222:                        
                    223:                        char *read;
                    224:                        char *write;
                    225:                        write=read=string;
                    226:                        char c;
1.193     paf       227:                        while((c=*read++)) {
1.187     paf       228:                                if(c==encloser) {
                    229:                                        char n=*read;
                    230:                                        if(n==encloser) // double-encloser stands for encloser
                    231:                                                read++;
                    232:                                        else if(n==delim1 || n==delim2) {
                    233:                                                result.delim=n;
                    234:                                                read++;
                    235:                                                break;
                    236:                                        }
                    237:                                }
                    238: 
                    239:                                *write++=c;
                    240:                        }
                    241:                        *write=0; // terminate
                    242:                        *string_ref=c? read: 0;
                    243:                        result.piece=string;
                    244:                        return result;
                    245:                } else
                    246:                        return lsplit(string_ref, delim1, delim2);
                    247:     }
                    248:        result.piece=0;
                    249:     return result;
                    250: }
                    251: 
                    252: static void skip_empty_and_comment_lines( char** data_ref ) {
                    253:        if(char *data=*data_ref) {
                    254:                while( char c=*data ) {
                    255:                        if( c== '\n' || c == '#' ) {
                    256:                                /*nowhere=*/getrow(&data); // remove empty&comment lines
1.199     paf       257:                                if(!(*data_ref=data))
                    258:                                        break;
1.187     paf       259:                                continue;
                    260:                        }
                    261:                        break;
                    262:                }
                    263:        }
                    264: }
                    265: 
                    266: struct TableSeparators {
1.206     paf       267:        char column;  const String* scolumn;
                    268:        char encloser; const String* sencloser;
                    269: 
                    270:        TableSeparators():
                    271:                column('\t'), scolumn(new String("\t", false)),
                    272:                encloser(0), sencloser(0)
                    273:        {}
1.187     paf       274:        void load( HashStringValue& options ) {
                    275:                if(Value* vseparator=options.get(COLUMN_SEPARATOR_NAME)) {
                    276:                        options.remove(COLUMN_SEPARATOR_NAME);
1.206     paf       277:                        scolumn=&vseparator->as_string();
                    278:                        if(scolumn->length()!=1)
1.187     paf       279:                                throw Exception("parser.runtime",
1.206     paf       280:                                        scolumn,
1.187     paf       281:                                        "separator must be one character long");
1.206     paf       282:                        column=scolumn->first_char();
1.187     paf       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.206     paf       422:                                        sdata<<*separators.scolumn;
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, 
1.206     paf       430:                                                        column<lsize-1?"%d%c":"%d", column, separators.column),
1.185     paf       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.206     paf       445:                                sdata<<*separators.scolumn;
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.211     paf       481:                int offset=params.as_int(params.count()-1, "offset must be expression", r);
                    482:                table.offset(absolute, offset);
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);
1.207     paf       662: 
                    663:                                result.extract_default();
1.182     paf       664:                        }
1.74      paf       665:                }
1.97      parser    666:        r.write_no_lang(result);
1.74      paf       667: }
                    668: 
1.110     parser    669: #ifndef DOXYGEN
1.78      paf       670: struct Table_seq_item {
1.182     paf       671:        ArrayString* row;
1.34      paf       672:        union {
1.182     paf       673:                const char *c_str;
1.34      paf       674:                double d;
                    675:        } value;
1.32      paf       676: };
1.110     parser    677: #endif
1.34      paf       678: static int sort_cmp_string(const void *a, const void *b) {
                    679:        return strcmp(
1.78      paf       680:                static_cast<const Table_seq_item *>(a)->value.c_str, 
                    681:                static_cast<const Table_seq_item *>(b)->value.c_str
1.34      paf       682:        );
                    683: }
                    684: static int sort_cmp_double(const void *a, const void *b) {
1.78      paf       685:        double va=static_cast<const Table_seq_item *>(a)->value.d;
                    686:        double vb=static_cast<const Table_seq_item *>(b)->value.d;
1.34      paf       687:        if(va<vb)
                    688:                return -1;
                    689:        else if(va>vb)
                    690:                return +1;
                    691:        else 
                    692:                return 0;
                    693: }
1.182     paf       694: static void _sort(Request& r, MethodParams& params) {
                    695:        Value& key_maker=params.as_junction(0, "key-maker must be code");
1.61      paf       696: 
1.182     paf       697:        bool reverse=params.count()>1/*..[desc|asc|]*/?
                    698:                reverse=params.as_no_junction(1, "order must not be code").as_string()=="desc":
1.104     parser    699:                false; // default=asc
1.32      paf       700: 
1.182     paf       701:        Table& old_table=GET_SELF(r, VTable).table();
                    702:        Table& new_table=*new Table(old_table.columns());
1.34      paf       703: 
1.182     paf       704:        Table_seq_item* seq=new(PointerFreeGC) Table_seq_item[old_table.count()];
1.34      paf       705:        int i;
                    706: 
                    707:        // calculate key values
                    708:        bool key_values_are_strings=true;
1.182     paf       709:        int old_count=old_table.count();
                    710:        for(i=0; i<old_count; i++) {
1.101     parser    711:                old_table.set_current(i);
1.32      paf       712:                // calculate key value
1.182     paf       713:                seq[i].row=old_table[i];
                    714:                Value& value=r.process_to_value(key_maker).as_expr_result(true/*return string as-is*/);
1.34      paf       715:                if(i==0) // determining key values type by first one
                    716:                        key_values_are_strings=value.is_string();
                    717: 
                    718:                if(key_values_are_strings)
                    719:                        seq[i].value.c_str=value.as_string().cstr();
                    720:                else
                    721:                        seq[i].value.d=value.as_double();
1.32      paf       722:        }
                    723:        // sort keys
1.182     paf       724:        _qsort(seq, old_count, sizeof(Table_seq_item), 
1.34      paf       725:                key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32      paf       726: 
1.34      paf       727:        // reorder table as they require in 'seq'
1.182     paf       728:        for(i=0; i<old_count; i++)
                    729:                new_table+=Table::element_type(seq[reverse?old_count-1-i:i].row);
                    730: 
                    731:        delete[] seq;
1.32      paf       732: 
1.116     parser    733:        // replace any previous table value
1.182     paf       734:        GET_SELF(r, VTable).set_table(new_table);
1.32      paf       735: }
                    736: 
1.176     paf       737: #ifndef DOXYGEN
1.182     paf       738: struct Expression_is_true_info {
1.176     paf       739:        Request* r;
                    740:        Value* expression_code;
                    741: };
                    742: #endif
1.191     paf       743: static bool expression_is_true(Table&, Expression_is_true_info* info) {
                    744:        return info->r->process_to_value(*info->expression_code).as_bool();
1.176     paf       745: }
                    746: static bool _locate_expression(Table& table, Table::Action_options o,
1.182     paf       747:                               Request& r, MethodParams& params) {
                    748:        check_option_param(o.defined, params, 1,
1.176     paf       749:                "locate by expression only has parameters: expression and, maybe, options");
1.182     paf       750:        Value& expression_code=params.as_junction(0, "must be expression");
1.145     paf       751: 
1.182     paf       752:        Expression_is_true_info info={&r, &expression_code};
                    753:        return table.table_first_that(expression_is_true, &info, o);
1.145     paf       754: }
1.176     paf       755: static bool _locate_name_value(Table& table, Table::Action_options o,
1.191     paf       756:                               Request&, MethodParams& params) {
1.182     paf       757:        check_option_param(o.defined, params, 2,
1.176     paf       758:                "locate by locate by name has parameters: name, value and, maybe, options");
1.182     paf       759:        const String& name=params.as_string(0, "column name must be string");
                    760:        const String& value=params.as_string(1, "value must be string");
                    761:        return table.locate(name, value, o);
                    762: }
                    763: static void _locate(Request& r, MethodParams& params) {
                    764:        Table& table=GET_SELF(r, VTable).table();
1.72      paf       765: 
1.182     paf       766:        Table::Action_options o=get_action_options(r, params, table);
                    767: 
                    768:        bool result=params[0].get_junction()?
                    769:                _locate_expression(table, o, r, params) :
                    770:                _locate_name_value(table, o, r, params);
                    771:        r.write_no_lang(*new VBool(result));
1.145     paf       772: }
1.182     paf       773: 
                    774: 
1.191     paf       775: static void _flip(Request& r, MethodParams&) {
1.182     paf       776:        Table& old_table=GET_SELF(r, VTable).table();
1.184     paf       777:        Table& new_table=*new Table(0);
1.182     paf       778:        if(size_t old_count=old_table.count())
                    779:                if(size_t old_cols=old_table[0]->count()) 
                    780:                        for(size_t column=0; column<old_cols; column++) {
                    781:                                Table::element_type new_row(new ArrayString(old_count));
                    782:                                for(size_t i=0; i<old_count; i++) {
                    783:                                        Table::element_type old_row=old_table[i];
                    784:                                        *new_row+=column<old_row->count()?old_row->get(column):new String;
1.39      paf       785:                                }
1.182     paf       786:                                new_table+=new_row;
1.39      paf       787:                        }
                    788: 
1.182     paf       789:        r.write_no_lang(*new VTable(&new_table));
1.39      paf       790: }
                    791: 
1.182     paf       792: static void _append(Request& r, MethodParams& params) {
1.134     paf       793:        // data
1.182     paf       794:        Temp_lang temp_lang(r, String::L_PASS_APPENDED);
                    795:        const String& string=r.process_to_string(params.as_junction(0, "body must be code"));
1.41      paf       796: 
                    797:        // parse cells
1.182     paf       798:        Table::element_type row=new ArrayString;
                    799:        size_t pos_after=0;
                    800:        string.split(*row, pos_after, "\t", String::L_AS_IS);
1.41      paf       801: 
1.182     paf       802:        GET_SELF(r, VTable).table()+=row;
1.41      paf       803: }
                    804: 
1.182     paf       805: static void join_named_row(Table& src, Table* dest) {
                    806:        Table::columns_type dest_columns=dest->columns();
                    807:        size_t dest_columns_count=dest_columns->count();
                    808:        Table::element_type dest_row(new ArrayString(dest_columns_count));
                    809:        for(size_t dest_column=0; dest_column<dest_columns_count; dest_column++) {
                    810:                const String* src_item=src.item(*dest_columns->get(dest_column));
                    811:                *dest_row+=src_item?src_item:new String;
                    812:        }
                    813:        *dest+=dest_row;
                    814: }
                    815: static void join_nameless_row(Table& src, Table* dest) {
                    816:        *dest+=src[src.current()];
                    817: }
                    818: static void _join(Request& r, MethodParams& params) {
                    819:        Table* maybe_src=params.as_no_junction(0, "table ref must not be code").get_table();
1.42      paf       820:        if(!maybe_src)
1.146     paf       821:                throw Exception("parser.runtime", 
1.182     paf       822:                        0, 
1.42      paf       823:                        "source is not a table");
1.176     paf       824:        Table& src=*maybe_src;
                    825: 
1.182     paf       826:        Table::Action_options o=get_action_options(r, params, src);
                    827:        check_option_param(o.defined, params, 1,
1.176     paf       828:                "invalid extra parameter");
1.42      paf       829: 
1.182     paf       830:        Table& dest=GET_SELF(r, VTable).table();
1.42      paf       831:        if(&src == &dest)
1.146     paf       832:                throw Exception("parser.runtime", 
1.182     paf       833:                        0, 
1.42      paf       834:                        "source and destination are same table");
                    835: 
1.193     paf       836:        if(dest.columns()) // dest is named
1.182     paf       837:                src.table_for_each(join_named_row, &dest, o);
                    838:        else // dest is nameless
                    839:                src.table_for_each(join_nameless_row, &dest, o);
1.42      paf       840: }
                    841: 
1.94      parser    842: #ifndef DOXYGEN
1.169     paf       843: class Table_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.182     paf       844:        ArrayString& columns;
                    845:        ArrayString* row;
1.94      parser    846: public:
1.182     paf       847:        Table* table;
                    848: public:
                    849:        Table_sql_event_handlers() :
                    850:                columns(*new ArrayString), row(0), table(0) {
1.94      parser    851:        }
                    852: 
1.182     paf       853:        bool add_column(SQL_Error& error, const char *str, size_t length) {
1.170     paf       854:                try {
1.182     paf       855:                        columns+=new String(str, length, true);
1.170     paf       856:                        return false;
                    857:                } catch(...) {
                    858:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_column");
                    859:                        return true;
                    860:                }
                    861:        }
                    862:        bool before_rows(SQL_Error& error) { 
                    863:                try {
1.182     paf       864:                        table=new Table(&columns);
1.170     paf       865:                        return false;
                    866:                } catch(...) {
                    867:                        error=SQL_Error("exception occured in Table_sql_event_handlers::before_rows");
                    868:                        return true;
                    869:                }
                    870:        }
                    871:        bool add_row(SQL_Error& error) {
                    872:                try {
1.182     paf       873:                        *table+=row=new ArrayString;
1.170     paf       874:                        return false;
                    875:                } catch(...) {
                    876:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_row");
                    877:                        return true;
                    878:                }
                    879:        }
1.182     paf       880:        bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.170     paf       881:                try {
1.182     paf       882:                        String& cell=*new String;
                    883:                        if(length)
                    884:                                cell.append_know_length(str, length, String::L_TAINTED);
                    885:                        *row+=&cell;
1.170     paf       886:                        return false;
                    887:                } catch(...) {
                    888:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_row_cell");
                    889:                        return true;
                    890:                }
1.94      parser    891:        }
                    892: };
                    893: #endif
1.204     paf       894: 
                    895: static void marshal_bind(
                    896:                                                 HashStringValue::key_type aname, 
                    897:                                                 HashStringValue::value_type avalue,
                    898:                                                 SQL_Driver::Placeholder** pptr) 
                    899: {
                    900:        SQL_Driver::Placeholder& ph=**pptr;
                    901:        ph.name=aname.cstr();
1.205     paf       902:        ph.value=avalue->as_string().cstr(String::L_UNSPECIFIED);
1.204     paf       903:        ph.is_null=avalue->get_class()==void_class;
                    904:        ph.were_updated=false;
                    905: 
                    906:        (*pptr)++;
                    907: }
                    908: 
                    909: // not static, used elsewhere
                    910: int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders) {
                    911:        int hash_count=hash.count();
                    912:        placeholders=new(UseGC) SQL_Driver::Placeholder[hash_count];
                    913:        SQL_Driver::Placeholder* ptr=placeholders;
                    914:        hash.for_each(marshal_bind, &ptr);
                    915:        return hash_count;
                    916: }
                    917: 
                    918: // not static, used elsewhere
                    919: void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders) {
                    920:        SQL_Driver::Placeholder* ph=placeholders;
                    921:        for(int i=0; i<placeholder_count; i++, ph++)
                    922:                if(ph->were_updated) {
                    923:                        Value* value;
                    924:                        if(ph->is_null)
                    925:                                value=new VVoid();
                    926:                        else
                    927:                                if(ph->value)
                    928:                                        value=new VString(*new String(ph->value, 0, true/*tainted*/));
                    929:                                else
                    930:                                        value=new VString(*new String());                                       
                    931:                        hash.put(ph->name, value);
                    932:                }
                    933: }
                    934: 
1.182     paf       935: static void _sql(Request& r, MethodParams& params) {
                    936:        Value& statement=params.as_junction(0, "statement must be code");
1.49      paf       937: 
1.204     paf       938:        HashStringValue* bind=0;
1.53      paf       939:        ulong limit=0;
1.100     parser    940:        ulong offset=0;
1.182     paf       941:        if(params.count()>1) {
                    942:                Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.212   ! paf       943:                if(voptions.is_defined() && !voptions.is_string())
1.182     paf       944:                        if(HashStringValue* options=voptions.get_hash()) {
1.164     paf       945:                                int valid_options=0;
1.204     paf       946:                                if(Value* vbind=options->get(sql_bind_name)) {
                    947:                                        valid_options++;
                    948:                                        bind=vbind->get_hash();
                    949:                                }
1.182     paf       950:                                if(Value* vlimit=options->get(sql_limit_name)) {
1.164     paf       951:                                        valid_options++;
1.147     paf       952:                                        limit=(ulong)r.process_to_value(*vlimit).as_double();
1.164     paf       953:                                }
1.182     paf       954:                                if(Value* voffset=options->get(sql_offset_name)) {
1.164     paf       955:                                        valid_options++;
1.147     paf       956:                                        offset=(ulong)r.process_to_value(*voffset).as_double();
1.164     paf       957:                                }
1.182     paf       958:                                if(valid_options!=options->count())
1.164     paf       959:                                        throw Exception("parser.runtime",
1.182     paf       960:                                                0,
1.164     paf       961:                                                "called with invalid option");
1.109     parser    962:                        } else
1.146     paf       963:                                throw Exception("parser.runtime",
1.182     paf       964:                                        0,
1.109     parser    965:                                        "options must be hash");
1.49      paf       966:        }
                    967: 
1.204     paf       968:        SQL_Driver::Placeholder* placeholders=0;
                    969:        uint placeholders_count=0;
                    970:        if(bind)
                    971:                placeholders_count=marshal_binds(*bind, placeholders);
                    972: 
1.182     paf       973:        Temp_lang temp_lang(r, String::L_SQL);
                    974:        const String&  statement_string=r.process_to_string(statement);
                    975:        const char* statement_cstr=
                    976:                statement_string.cstr(String::L_UNSPECIFIED, r.connection());
                    977:        Table_sql_event_handlers handlers;
1.135     paf       978: #ifdef RESOURCES_DEBUG
                    979:        struct timeval mt[2];
                    980:        //measure:before
                    981:        gettimeofday(&mt[0],NULL);
                    982: #endif 
1.182     paf       983:        r.connection()->query(
1.203     paf       984:                statement_cstr, 
1.208     paf       985:                placeholders_count, placeholders,
1.203     paf       986:                offset, limit, 
1.160     paf       987:                handlers,
                    988:                statement_string);
1.135     paf       989:        
                    990: #ifdef RESOURCES_DEBUG
                    991:                //measure:after connect
                    992:        gettimeofday(&mt[1],NULL);
                    993:        
                    994:        double t[2];
                    995:        for(int i=0;i<2;i++)
                    996:            t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
                    997:            
                    998:        r.sql_request_time+=t[1]-t[0];
                    999: #endif                         
1.204     paf      1000: 
                   1001:        if(bind)
                   1002:                unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.96      parser   1003: 
1.182     paf      1004:        Table& result=
                   1005:                handlers.table?*handlers.table: // query resulted in table? return it
                   1006:                *new Table(Table::columns_type(0)); // query returned no table, fake it
1.49      paf      1007: 
                   1008:        // replace any previous table value
1.182     paf      1009:        GET_SELF(r, VTable).set_table(result);
1.49      paf      1010: }
                   1011: 
1.182     paf      1012: static void _columns(Request& r, MethodParams&) {
1.88      parser   1013: 
1.182     paf      1014:        Table::columns_type result_columns(new ArrayString);
                   1015:        *result_columns+=new String("column");
                   1016:        Table& result_table=*new Table(result_columns);
1.88      parser   1017: 
1.182     paf      1018:        Table& source_table=GET_SELF(r, VTable).table();
                   1019:        if(Table::columns_type source_columns=source_table.columns()) {
                   1020:                for(Array_iterator<const String*> i(*source_columns); i.has_next(); ) {
                   1021:                        Table::element_type result_row(new ArrayString);
                   1022:                        *result_row+=i.next();
                   1023:                        result_table+=result_row;
1.88      parser   1024:                }
                   1025:        }
                   1026: 
1.182     paf      1027:        r.write_no_lang(*new VTable(&result_table));
1.88      parser   1028: }
                   1029: 
1.182     paf      1030: static void _select(Request& r, MethodParams& params) {
                   1031:        Value& vcondition=params.as_junction(0, "condition must be expression");
1.148     paf      1032: 
1.182     paf      1033:        Table& source_table=GET_SELF(r, VTable).table();
                   1034:        Table& result_table=*new Table(source_table.columns());
1.148     paf      1035: 
                   1036:        int saved_current=source_table.current();
1.182     paf      1037:        int size=source_table.count();
1.148     paf      1038:        for(int row=0; row<size; row++) {
                   1039:                source_table.set_current(row);
                   1040: 
1.150     paf      1041:                bool condition=r.process_to_value(vcondition, 
1.148     paf      1042:                                false/*don't intercept string*/).as_bool();
                   1043: 
                   1044:                if(condition) // ...condition is true=
1.182     paf      1045:                        result_table+=source_table[row]; // =green light to go to result
1.148     paf      1046:        }
                   1047:        source_table.set_current(saved_current);
                   1048: 
1.182     paf      1049:        r.write_no_lang(*new VTable(&result_table));
1.148     paf      1050: }
                   1051: 
1.66      paf      1052: // constructor
                   1053: 
1.182     paf      1054: MTable::MTable(): Methoded("table") {
1.142     paf      1055:        // ^table::create{data}
                   1056:        // ^table::create[nameless]{data}
                   1057:        // ^table::create[table]
                   1058:        add_native_method("create", Method::CT_DYNAMIC, _create, 1, 2);
                   1059:        // old name for compatibility with <= v 1.141 2002/01/25 11:33:45 paf
                   1060:        add_native_method("set", Method::CT_DYNAMIC, _create, 1, 2); 
1.2       paf      1061: 
1.142     paf      1062:        // ^table::load[file]  
                   1063:        // ^table::load[nameless;file]
1.168     paf      1064:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 3);
1.22      paf      1065: 
                   1066:        // ^table.save[file]  
                   1067:        // ^table.save[nameless;file]
1.188     paf      1068:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 3);
1.6       paf      1069: 
                   1070:        // ^table.count[]
1.66      paf      1071:        add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6       paf      1072: 
                   1073:        // ^table.line[]
1.66      paf      1074:        add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6       paf      1075: 
1.10      paf      1076:        // ^table.offset[]  
1.133     paf      1077:        // ^table.offset(offset)
                   1078:        // ^table.offset[cur|set](offset)
                   1079:        add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 2);
1.7       paf      1080: 
1.10      paf      1081:        // ^table.menu{code}  
                   1082:        // ^table.menu{code}[delim]
1.66      paf      1083:        add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.74      paf      1084: 
1.79      paf      1085:        // ^table:hash[key field name]
1.123     parser   1086:        // ^table:hash[key field name][value field name(s) string/table]
1.163     paf      1087:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 3);
1.32      paf      1088: 
1.102     parser   1089:        // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc]
                   1090:        // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc]
1.66      paf      1091:        add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8       paf      1092: 
1.36      paf      1093:        // ^table.locate[field;value]
1.176     paf      1094:        add_native_method("locate", Method::CT_DYNAMIC, _locate, 1, 3);
1.39      paf      1095: 
                   1096:        // ^table.flip[]
1.66      paf      1097:        add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41      paf      1098: 
                   1099:        // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
1.66      paf      1100:        add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42      paf      1101: 
1.157     paf      1102:        // ^table.join[table][$.limit(10) $.offset(1) $.offset[cur] ]
                   1103:        add_native_method("join", Method::CT_DYNAMIC, _join, 1, 2);
1.49      paf      1104: 
                   1105: 
1.100     parser   1106:        // ^table:sql[query]
                   1107:        // ^table:sql[query][$.limit(1) $.offset(2)]
                   1108:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.88      parser   1109: 
                   1110:        // ^table:columns[]
                   1111:        add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 0);
1.148     paf      1112: 
                   1113:        // ^table.select(expression) = table
                   1114:        add_native_method("select", Method::CT_DYNAMIC, _select, 1, 1);
1.40      paf      1115: }

E-mail: