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

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

E-mail: