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

1.20      paf         1: /** @file
1.47      paf         2:        Parser: @b table parser class.
1.20      paf         3: 
1.312     moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.144     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.154     paf         6: */
1.221     paf         7: 
1.105     parser      8: #include "classes.h"
1.182     paf         9: #include "pa_vmethod_frame.h"
                     10: 
1.16      paf        11: #include "pa_common.h"
1.1       paf        12: #include "pa_request.h"
1.265     misha      13: #include "pa_charsets.h"
1.1       paf        14: #include "pa_vtable.h"
1.6       paf        15: #include "pa_vint.h"
1.51      paf        16: #include "pa_sql_connection.h"
1.72      paf        17: #include "pa_vbool.h"
1.277     moko       18: #include "pa_array.h"
1.1       paf        19: 
1.305     moko       20: #if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4))
                     21: #include <sstream>
                     22: #define USE_STRINGSTREAM
                     23: #endif
                     24: 
1.320   ! moko       25: volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.319 2016/09/06 22:19:47 moko Exp $";
1.286     moko       26: 
1.66      paf        27: // class
                     28: 
1.182     paf        29: class MTable: public Methoded {
1.66      paf        30: public: // VStateless_class
1.264     misha      31:        Value* create_new_value(Pool&) { return new VTable(); }
1.66      paf        32: public:
1.182     paf        33:        MTable();
1.66      paf        34: };
1.1       paf        35: 
1.182     paf        36: // global variable
                     37: 
1.313     moko       38: DECLARE_CLASS_VAR(table, new MTable);
1.182     paf        39: 
                     40: #define TABLE_REVERSE_NAME "reverse"
                     41: 
                     42: // globals
                     43: 
1.203     paf        44: String sql_bind_name(SQL_BIND_NAME);
1.214     paf        45: String sql_limit_name(PA_SQL_LIMIT_NAME);
                     46: String sql_offset_name(PA_SQL_OFFSET_NAME);
1.182     paf        47: String sql_default_name(SQL_DEFAULT_NAME);
                     48: String sql_distinct_name(SQL_DISTINCT_NAME);
1.227     misha      49: String sql_value_type_name(SQL_VALUE_TYPE_NAME);
1.182     paf        50: String table_reverse_name(TABLE_REVERSE_NAME);
                     51: 
1.1       paf        52: // methods
1.66      paf        53: 
1.319     moko       54: static Table::Action_options get_action_options(Request& r, MethodParams& params, size_t options_index, const Table& source) {
1.176     paf        55:        Table::Action_options result;
1.268     misha      56:        if(params.count() <= options_index)
1.182     paf        57:                return result;
                     58: 
1.288     misha      59:        HashStringValue* options=params.as_hash(options_index);
1.176     paf        60:        if(!options)
                     61:                return result;
                     62: 
                     63:        result.defined=true;
                     64:        bool defined_offset=false;
                     65: 
                     66:        int valid_options=0;
1.182     paf        67:        if(Value* voffset=options->get(sql_offset_name)) {
1.176     paf        68:                valid_options++;
                     69:                defined_offset=true;
                     70:                if(voffset->is_string()) {
1.182     paf        71:                        const String&  soffset=*voffset->get_string();
1.176     paf        72:                        if(soffset == "cur")
                     73:                                result.offset=source.current();
                     74:                        else
1.318     moko       75:                                throw Exception(PARSER_RUNTIME, &soffset, "must be 'cur' string or expression");
1.176     paf        76:                } else 
                     77:                        result.offset=r.process_to_value(*voffset).as_int();
                     78:        }
1.182     paf        79:        if(Value* vlimit=options->get(sql_limit_name)) {
1.176     paf        80:                valid_options++;
                     81:                result.limit=r.process_to_value(*vlimit).as_int();
                     82:        }
1.182     paf        83:        if(Value *vreverse=(Value *)options->get(table_reverse_name)) { 
                     84:                valid_options++; 
                     85:                result.reverse=r.process_to_value(*vreverse).as_bool(); 
                     86:                if(result.reverse && !defined_offset) 
                     87:                        result.offset=source.count()-1; 
                     88:        } 
                     89: 
                     90:        if(valid_options!=options->count())
1.272     misha      91:                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.176     paf        92: 
                     93:        return result;
                     94: }
1.157     paf        95: 
1.319     moko       96: struct TableControlChars {
                     97:        char separator;  const String* sseparator;
1.236     misha      98:        char encloser; const String* sencloser;
                     99: 
1.320   ! moko      100:        char separators[3];
        !           101: 
1.319     moko      102:        TableControlChars():
                    103:                separator('\t'), sseparator(new String("\t")),
1.236     misha     104:                encloser(0), sencloser(0)
1.320   ! moko      105:        {
        !           106:                strcpy(separators,"\t\n");
        !           107:        }
        !           108: 
1.236     misha     109:        int load( HashStringValue& options ) {
                    110:                int result=0;
                    111:                if(Value* vseparator=options.get(PA_COLUMN_SEPARATOR_NAME)) {
1.319     moko      112:                        sseparator=&vseparator->as_string();
                    113:                        if(sseparator->length()!=1)
                    114:                                throw Exception(PARSER_RUNTIME, sseparator, "separator must be one character long");
                    115:                        separator=sseparator->first_char();
1.320   ! moko      116:                        separators[0]=separator;
1.236     misha     117:                        result++;
                    118:                }
                    119:                if(Value* vencloser=options.get(PA_COLUMN_ENCLOSER_NAME)) {
                    120:                        sencloser=&vencloser->as_string();
1.297     moko      121:                        if(sencloser->is_empty()){
                    122:                                encloser=0;
                    123:                        } else {
1.236     misha     124:                        if(sencloser->length()!=1)
1.318     moko      125:                                throw Exception(PARSER_RUNTIME, sencloser, "encloser must be one character long");
1.236     misha     126:                        encloser=sencloser->first_char();
1.297     moko      127:                        }
1.236     misha     128:                        result++;
                    129:                }
                    130:                return result;
                    131:        }
                    132: };
                    133: 
1.319     moko      134: 
1.182     paf       135: static void _create(Request& r, MethodParams& params) {
1.157     paf       136:        // clone/copy part?
1.182     paf       137:        if(Table *source=params[0].get_table()) {
1.268     misha     138:                Table::Action_options o=get_action_options(r, params, 1, *source);
1.303     moko      139:                if(params.count()>2)
                    140:                        throw Exception(PARSER_RUNTIME, 0, "too many parameters");
1.182     paf       141:                GET_SELF(r, VTable).set_table(*new Table(*source, o));
1.157     paf       142:                return;
                    143:        }
1.142     paf       144: 
1.236     misha     145:        size_t data_param_index=0;
                    146:        bool nameless=false;
                    147: 
                    148:        if(params.count()>1) {
                    149:                if(params[0].is_string()){ // can be nameless only
                    150:                        const String& snameless=params.as_string(0, "called with more then 1 param, first param may be only string 'nameless' or junction");
                    151:                        if(snameless!="nameless")
1.318     moko      152:                                throw Exception(PARSER_RUNTIME, &snameless, "table::create called with more then 1 param, first param may be only 'nameless'");
1.236     misha     153:                        nameless=true;
                    154:                        data_param_index++;
                    155:                }
                    156:        }
                    157: 
                    158:        HashStringValue *options=0;
1.319     moko      159:        TableControlChars control_chars;
1.236     misha     160: 
                    161:        size_t options_param_index=data_param_index+1;
                    162:        if(
                    163:                options_param_index<params.count()
1.288     misha     164:                && (options=params.as_hash(options_param_index))
1.236     misha     165:        ) {
                    166:                // cloning, so that we could change
                    167:                options=new HashStringValue(*options);
1.319     moko      168:                control_chars.load(*options);
                    169:                if(control_chars.encloser){
1.318     moko      170:                        throw Exception(PARSER_RUNTIME, 0, "encloser not supported for table::create yet");
1.236     misha     171:                }
                    172:        }
                    173: 
                    174:        // data
1.182     paf       175:        Temp_lang temp_lang(r, String::L_PASS_APPENDED);
                    176:        const String&  data=
1.301     misha     177:                r.process_to_string(params.as_junction(data_param_index, "body must be table or code"));
1.44      paf       178: 
                    179:        // parse columns
1.182     paf       180:        size_t raw_pos_after=0;
                    181:        Table::columns_type columns;
1.236     misha     182: 
                    183:        if(nameless){
1.182     paf       184:                columns=Table::columns_type(0); // nameless
1.21      paf       185:        } else {
1.182     paf       186:                columns=Table::columns_type(new ArrayString);
1.44      paf       187: 
1.182     paf       188:                ArrayString head;
                    189:                data.split(head, raw_pos_after, "\n", String::L_AS_IS, 1);
                    190:                if(head.count()) {
                    191:                        size_t col_pos_after=0;
1.285     misha     192:                        if(head[0]->is_empty())
                    193:                                *columns += new String();
                    194:                        else
1.319     moko      195:                                head[0]->split(*columns, col_pos_after, *control_chars.sseparator, String::L_AS_IS);
1.182     paf       196:                }
1.44      paf       197:        }
                    198: 
1.182     paf       199:        Table& table=*new Table(columns);
1.44      paf       200:        // parse cells
1.182     paf       201: 
                    202:        ArrayString rows;
                    203:        data.split(rows, raw_pos_after, "\n", String::L_AS_IS);
                    204:        Array_iterator<const String*> i(rows);
1.98      parser    205:        while(i.has_next()) {
1.182     paf       206:                Table::element_type row(new ArrayString);
1.252     misha     207:                const String& string=*i.next();
1.118     parser    208:                // remove comment lines
1.252     misha     209:                if(string.is_empty())
1.44      paf       210:                        continue;
                    211: 
1.182     paf       212:                size_t col_pos_after=0;
1.319     moko      213:                string.split(*row, col_pos_after, *control_chars.sseparator, String::L_AS_IS);
1.182     paf       214:                table+=row;
1.17      paf       215:        }
1.1       paf       216: 
1.44      paf       217:        // replace any previous table value
1.182     paf       218:        GET_SELF(r, VTable).set_table(table);
1.44      paf       219: }
                    220: 
1.187     paf       221: struct lsplit_result {
                    222:        char* piece;
                    223:        char delim;
                    224: 
                    225:        operator bool() { return piece!=0; }
                    226: };
                    227: 
1.320   ! moko      228: inline lsplit_result lsplit(char* string, const char* delims) {
1.187     paf       229:        lsplit_result result;
1.273     misha     230:        if(string) {
1.187     paf       231:                if(char* v=strpbrk(string, delims)) {
                    232:                        result.delim=*v;
                    233:                        *v=0;
                    234:                        result.piece=v+1;
                    235:                        return result;
                    236:                }
1.273     misha     237:        }
1.187     paf       238:        result.piece=0;
                    239:        result.delim=0;
1.273     misha     240:        return result;
1.187     paf       241: }
                    242: 
1.320   ! moko      243: inline lsplit_result lsplit(char* *string_ref, const char* delims) {
1.273     misha     244:        lsplit_result result;
1.187     paf       245:        result.piece=*string_ref;
1.320   ! moko      246:        lsplit_result next=lsplit(*string_ref, delims);
1.187     paf       247:        result.delim=next.delim;
                    248:        *string_ref=next.piece;
1.273     misha     249:        return result;
1.187     paf       250: }
                    251: 
1.320   ! moko      252: static lsplit_result lsplit(char** string_ref, const char* delims, char encloser) {
1.187     paf       253:        lsplit_result result;
                    254: 
1.273     misha     255:        if(char* string=*string_ref) {
1.187     paf       256:                if(encloser && *string==encloser) {
1.273     misha     257:                        string++;
1.187     paf       258:                        
                    259:                        char *read;
                    260:                        char *write;
                    261:                        write=read=string;
                    262:                        char c;
1.290     moko      263:                        // we are enclosed, searching for second encloser
                    264:                        while(c=*read++) {
1.187     paf       265:                                if(c==encloser) {
1.290     moko      266:                                        if(*read==encloser) // double-encloser stands for encloser
1.187     paf       267:                                                read++;
1.290     moko      268:                                        else
                    269:                                                break; // note: skipping encloser
1.187     paf       270:                                }
                    271:                                *write++=c;
                    272:                        }
1.290     moko      273:                        // we are no longer enclosed, searching for delimiter, skipping extra enclosers
                    274:                        while(c=*read++) {
1.320   ! moko      275:                                if(c==delims[0] || c==delims[1]) {
1.290     moko      276:                                        result.delim=c;
                    277:                                        break;
                    278:                                } else  if(c!=encloser)
                    279:                                        *write++=c;
                    280:                        }
1.187     paf       281:                        *write=0; // terminate
                    282:                        *string_ref=c? read: 0;
                    283:                        result.piece=string;
                    284:                        return result;
                    285:                } else
1.320   ! moko      286:                        return lsplit(string_ref, delims);
1.273     misha     287:        }
1.187     paf       288:        result.piece=0;
1.273     misha     289:        return result;
1.187     paf       290: }
                    291: 
                    292: static void skip_empty_and_comment_lines( char** data_ref ) {
                    293:        if(char *data=*data_ref) {
                    294:                while( char c=*data ) {
                    295:                        if( c== '\n' || c == '#' ) {
                    296:                                /*nowhere=*/getrow(&data); // remove empty&comment lines
1.199     paf       297:                                if(!(*data_ref=data))
                    298:                                        break;
1.187     paf       299:                                continue;
                    300:                        }
                    301:                        break;
                    302:                }
                    303:        }
                    304: }
                    305: 
1.273     misha     306: static void skip_empty_lines( char** data_ref ) {
                    307:        if(char *data=*data_ref) {
                    308:                while( char c=*data ) {
                    309:                        if( c== '\n' ) {
                    310:                                /*nowhere=*/getrow(&data); // remove empty lines
                    311:                                if(!(*data_ref=data))
                    312:                                        break;
                    313:                                continue;
                    314:                        }
                    315:                        break;
                    316:                }
                    317:        }
                    318: }
                    319: 
                    320: typedef void (*Skip_lines_action)(char** data_ref);
                    321: 
1.182     paf       322: static void _load(Request& r, MethodParams& params) {
1.232     misha     323:        const String&  first_param=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.168     paf       324:        int filename_param_index=0;
                    325:        bool nameless=first_param=="nameless";
                    326:        if(nameless)
                    327:                filename_param_index++;
1.182     paf       328:        size_t options_param_index=filename_param_index+1;
1.186     paf       329: 
                    330:        HashStringValue *options=0;
1.319     moko      331:        TableControlChars control_chars;
1.186     paf       332:        if(options_param_index<params.count()
1.288     misha     333:                && (options=params.as_hash(options_param_index))) {
1.186     paf       334:                // cloning, so that we could change [to simplify checks on params inside file_read_text
                    335:                options=new HashStringValue(*options);
1.319     moko      336:                control_chars.load(*options);
1.186     paf       337:        }
                    338: 
1.44      paf       339:        // loading text
1.247     misha     340:        char *data=file_load_text(r,
1.232     misha     341:                r.absolute(params.as_string(filename_param_index, FILE_NAME_MUST_BE_STRING)),
1.168     paf       342:                true,
1.186     paf       343:                options
1.168     paf       344:        );
1.44      paf       345: 
1.319     moko      346:        Skip_lines_action skip_lines_action = (control_chars.separator=='#' || control_chars.encloser=='#') ? skip_empty_lines : skip_empty_and_comment_lines;
1.273     misha     347: 
1.1       paf       348:        // parse columns
1.182     paf       349:        Table::columns_type columns;
1.168     paf       350:        if(nameless) {
1.182     paf       351:                columns=Table::columns_type(0); // nameless
1.1       paf       352:        } else {
1.182     paf       353:                columns=Table::columns_type(new ArrayString);
1.1       paf       354: 
1.273     misha     355:                skip_lines_action(&data);
1.320   ! moko      356:                while( lsplit_result sr=lsplit(&data, control_chars.separators, control_chars.encloser) ) {
1.256     misha     357:                        *columns+=new String(sr.piece, String::L_TAINTED);
1.187     paf       358:                        if(sr.delim=='\n') 
                    359:                                break;
1.139     paf       360:                }
1.1       paf       361:        }
1.187     paf       362:        
                    363:        Table& table=*new Table(columns);
1.219     paf       364:        int columns_count=columns? columns->count(): 0;
1.1       paf       365: 
                    366:        // parse cells
1.218     paf       367:        Table::element_type row(new ArrayString(columns_count));
1.273     misha     368:        skip_lines_action(&data);
1.320   ! moko      369:        while( lsplit_result sr=lsplit(&data, control_chars.separators, control_chars.encloser) ) {
1.201     paf       370:                if(!*sr.piece && !sr.delim && !row->count()) // append last empty column [if without \n]
                    371:                        break;
1.256     misha     372:                *row+=new String(sr.piece, String::L_TAINTED);
1.187     paf       373:                if(sr.delim=='\n') {
                    374:                        table+=row;
1.218     paf       375:                        row=new ArrayString(columns_count);
1.273     misha     376:                        skip_lines_action(&data);
1.1       paf       377:                }
1.187     paf       378:        }
                    379:        // last line [if without \n]
                    380:        if(row->count())
1.1       paf       381:                table+=row;
1.187     paf       382:        
1.1       paf       383:        // replace any previous table value
1.182     paf       384:        GET_SELF(r, VTable).set_table(table);
1.1       paf       385: }
1.2       paf       386: 
1.297     moko      387: #ifdef USE_STRINGSTREAM
1.269     misha     388: #include "gc_allocator.h"
                    389: 
                    390: typedef std::basic_stringstream<char, std::char_traits<char>, gc_allocator<char> > pa_stringstream;
                    391: typedef std::basic_string<char, std::char_traits<char>, gc_allocator<char> > pa_string;
                    392: 
1.298     moko      393: static void enclose( pa_stringstream& to, const String* from, char encloser ) {
1.297     moko      394:        if(from){
1.257     misha     395:                to<<encloser;
1.188     paf       396:                // while we have 'encloser'...
1.189     paf       397:                size_t pos_after=0;
1.297     moko      398:                for( size_t pos_before; (pos_before=from->pos( encloser, pos_after ))!=STRING_NOT_FOUND; pos_after=pos_before) {
1.220     paf       399:                        pos_before++; // including first encloser (and skipping it for next pos)
1.297     moko      400:                        to<<from->mid(pos_after, pos_before).cstr();
1.257     misha     401:                        to<<encloser; // doubling encloser
1.188     paf       402:                }
                    403:                // last piece
1.297     moko      404:                size_t from_length=from->length();
1.188     paf       405:                if(pos_after<from_length)
1.297     moko      406:                        to<<from->mid(pos_after, from_length).cstr();
                    407:                to<<encloser;
                    408:        } else {
                    409:                to<<encloser<<encloser;
                    410:        }
                    411: }
                    412: 
1.319     moko      413: static void table_to_csv(pa_stringstream& result, Table& table, TableControlChars& control_chars, bool output_column_names) {
1.297     moko      414:        if(output_column_names) {
                    415:                if(table.columns()) { // named table
1.319     moko      416:                        if(control_chars.encloser){
1.297     moko      417:                                for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
1.319     moko      418:                                        enclose( result, i.next(), control_chars.encloser );
1.297     moko      419:                                        if(i.has_next())
1.319     moko      420:                                                result<<control_chars.separator;
1.297     moko      421:                                }
                    422:                        } else {
                    423:                                for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
                    424:                                        result<<i.next()->cstr();
                    425:                                        if(i.has_next())
1.319     moko      426:                                                result<<control_chars.separator;
1.297     moko      427:                                }
                    428:                        }
                    429:                } else { // nameless table [we were asked to output column names]
                    430:                        if(int lsize=table.count()?table[0]->count():0)
                    431:                                for(int column=0; column<lsize; column++) {
1.319     moko      432:                                        if(control_chars.encloser) {
                    433:                                                result<<control_chars.encloser<<column<<control_chars.encloser;
1.297     moko      434:                                        } else {
                    435:                                                result<<column;
                    436:                                        }
                    437:                                        if(column<lsize-1){
1.319     moko      438:                                                result<<control_chars.separator;
1.297     moko      439:                                        }
                    440:                                }
                    441:                        else
                    442:                                result<<"empty nameless table";
                    443:                }
                    444:                result<<'\n';
                    445:        }
1.188     paf       446: 
1.297     moko      447:        // process data lines
                    448:        Array_iterator<ArrayString*> i(table);
1.319     moko      449:        if(control_chars.encloser){
1.297     moko      450:                while(i.has_next()) {
                    451:                        for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
1.319     moko      452:                                enclose( result, c.next(), control_chars.encloser );
1.297     moko      453:                                if(c.has_next())
1.319     moko      454:                                        result<<control_chars.separator;
1.297     moko      455:                        }
                    456:                        result<<'\n';
                    457:                }
                    458:        } else {
                    459:                while(i.has_next()) {
                    460:                        for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
                    461:                                result<<c.next()->cstr();
                    462:                                if(c.has_next())
1.319     moko      463:                                        result<<control_chars.separator;
1.297     moko      464:                        }
                    465:                        result<<'\n';
                    466:                }
                    467:        }
1.188     paf       468: }
                    469: 
1.241     misha     470: #else
                    471: 
1.298     moko      472: void enclose( String& to, const String* from, char encloser, const String* sencloser ) {
1.297     moko      473:        if(from){
1.257     misha     474:                to<<*sencloser;
1.224     misha     475:                // while we have 'encloser'...
                    476:                size_t pos_after=0;
1.297     moko      477:                for( size_t pos_before; (pos_before=from->pos( encloser, pos_after ))!=STRING_NOT_FOUND; pos_after=pos_before) {
1.224     misha     478:                        pos_before++; // including first encloser (and skipping it for next pos)
1.297     moko      479:                        to<<from->mid(pos_after, pos_before);
1.257     misha     480:                        to<<*sencloser; // doubling encloser
1.224     misha     481:                }
                    482:                // last piece
1.297     moko      483:                size_t from_length=from->length();
1.224     misha     484:                if(pos_after<from_length)
1.297     moko      485:                        to<<from->mid(pos_after, from_length);
1.257     misha     486:                to<<*sencloser;
1.297     moko      487:        } else {
                    488:                to<<*sencloser<<*sencloser;
                    489:        }
1.224     misha     490: }
                    491: 
1.319     moko      492: static void table_to_csv(String& result, Table& table, TableControlChars& control_chars, bool output_column_names) {
1.297     moko      493:        if(output_column_names) {
                    494:                if(table.columns()) { // named table
1.319     moko      495:                        if(control_chars.encloser) {
1.297     moko      496:                                for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
1.319     moko      497:                                        enclose( result, i.next(), control_chars.encloser, control_chars.sencloser );
1.297     moko      498:                                        if(i.has_next())
1.319     moko      499:                                                result<<*control_chars.sseparator;
1.297     moko      500:                                }
                    501:                        } else {
                    502:                                for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
                    503:                                        result<<*i.next();
                    504:                                        if(i.has_next())
1.319     moko      505:                                                result<<*control_chars.sseparator;
1.297     moko      506:                                }
                    507:                        }
                    508:                } else { // nameless table [we were asked to output column names]
                    509:                        if(int lsize=table.count()?table[0]->count():0)
                    510:                                for(int column=0; column<lsize; column++) {
1.319     moko      511:                                        if(control_chars.encloser) {
                    512:                                                result<<*control_chars.sencloser<<String::Body::Format(column)<<*control_chars.sencloser;
1.298     moko      513:                                        } else {
                    514:                                                result<<String::Body::Format(column);
                    515:                                        }
                    516:                                        if(column<lsize-1){
1.319     moko      517:                                                result<<*control_chars.sseparator;
1.298     moko      518:                                        }
1.297     moko      519:                                }
                    520:                        else
                    521:                                result.append_help_length("empty nameless table", 0, String::L_CLEAN);
                    522:                }
                    523:                result.append_know_length("\n", 1, String::L_CLEAN);
                    524:        }
                    525: 
                    526:        // data lines
                    527:        Array_iterator<ArrayString*> i(table);
1.319     moko      528:        if(control_chars.encloser){
1.297     moko      529:                while(i.has_next()) {
                    530:                        for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
1.319     moko      531:                                enclose( result, c.next(), control_chars.encloser, control_chars.sencloser );
1.297     moko      532:                                if(c.has_next())
1.319     moko      533:                                        result<<*control_chars.sseparator;
1.297     moko      534:                        }
                    535:                        result.append_know_length("\n", 1, String::L_CLEAN);
                    536:                }
                    537:        } else {
                    538:                while(i.has_next()) {
                    539:                        for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
                    540:                                result<<*c.next();
                    541:                                if(c.has_next())
1.319     moko      542:                                        result<<*control_chars.sseparator;
1.297     moko      543:                        }
                    544:                        result.append_know_length("\n", 1, String::L_CLEAN);
                    545:                }
                    546:        }
                    547: }
1.257     misha     548: #endif // don't use stringstream
1.241     misha     549: 
1.297     moko      550: 
1.241     misha     551: static void _save(Request& r, MethodParams& params) {
1.237     misha     552:        const String& first_arg=params.as_string(0, FIRST_ARG_MUST_NOT_BE_CODE);
1.188     paf       553:        size_t param_index=1;
                    554: 
                    555:        bool do_append=false;
                    556:        bool output_column_names=true;
                    557: 
                    558:        // mode?
                    559:        if(first_arg=="append")
                    560:                do_append=true;
                    561:        else if(first_arg=="nameless")
                    562:                output_column_names=false;
                    563:        else
                    564:                --param_index;
                    565: 
1.232     misha     566:        const String& file_name=params.as_string(param_index++, FILE_NAME_MUST_NOT_BE_CODE);
1.246     misha     567:        String file_spec=r.absolute(file_name);
                    568: 
                    569:        if(do_append && file_exist(file_spec))
                    570:                output_column_names=false;
1.188     paf       571: 
1.319     moko      572:        TableControlChars control_chars;
1.281     misha     573:        if(param_index<params.count())
1.288     misha     574:                if(HashStringValue* options=params.as_hash(param_index++)) {
1.319     moko      575:                        int valid_options=control_chars.load(*options);
1.281     misha     576:                        if(valid_options!=options->count())
                    577:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.222     paf       578:                }
1.281     misha     579: 
1.188     paf       580:        if(param_index<params.count())
1.318     moko      581:                throw Exception(PARSER_RUNTIME, 0, "bad mode (must be nameless or append)");
1.22      paf       582: 
1.182     paf       583:        Table& table=GET_SELF(r, VTable).table();
1.31      paf       584: 
1.297     moko      585: #ifdef USE_STRINGSTREAM
1.269     misha     586:        pa_stringstream ost(std::stringstream::out);
1.257     misha     587: 
1.319     moko      588:        table_to_csv(ost, table, control_chars, output_column_names);
1.31      paf       589: 
                    590:        // write
1.269     misha     591:                pa_string data=ost.str();
1.257     misha     592:                const char* data_cstr=data.c_str();
1.267     misha     593:                file_write(r.charsets, file_spec, data_cstr, data.length(), true /* as text */, do_append);
1.241     misha     594: #else
1.257     misha     595:        String sdata;
1.224     misha     596: 
1.319     moko      597:        table_to_csv(sdata, table, control_chars, output_column_names);
1.224     misha     598: 
                    599:        // write
1.257     misha     600:                const char* data_cstr=sdata.cstr();
1.297     moko      601:        file_write(r.charsets, file_spec, data_cstr, sdata.length(), true /* as text */, do_append);
1.257     misha     602:                if(*data_cstr) // not empty (when empty it's not heap memory)
                    603:                        pa_free((void*)data_cstr); // not needed anymore
1.297     moko      604: #endif
                    605: }
                    606: 
                    607: static void _csv_string(Request& r, MethodParams& params) {
                    608:        bool output_column_names=true;
                    609:        size_t param_index=0;
                    610:        if(params.count()>0 && params[0].is_string()) {
                    611:                if(params.as_string(0, FIRST_ARG_MUST_NOT_BE_CODE)=="nameless") {
                    612:                        output_column_names=false;
                    613:                        param_index++;
                    614:                } else {
1.318     moko      615:                        throw Exception(PARSER_RUNTIME, 0, "bad mode (must be nameless)");
1.297     moko      616:                }
1.224     misha     617:        }
1.240     misha     618: 
1.319     moko      619:        TableControlChars control_chars;
1.297     moko      620:        if(param_index<params.count())
                    621:                if(HashStringValue* options=params.as_hash(param_index++)) {
1.319     moko      622:                        int valid_options=control_chars.load(*options);
1.297     moko      623:                        if(valid_options!=options->count())
                    624:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    625:                }
                    626: 
                    627:        Table& table=GET_SELF(r, VTable).table();
                    628: 
                    629: #ifdef USE_STRINGSTREAM
                    630:        pa_stringstream ost(std::stringstream::out);
                    631: 
1.319     moko      632:        table_to_csv(ost, table, control_chars, output_column_names);
1.297     moko      633: 
                    634:        r.write_no_lang(*new VString(*new String(pa_strdup(ost.str().c_str()), String::L_CLEAN)));
                    635: #else
                    636:        String sdata;
                    637: 
1.319     moko      638:        table_to_csv(sdata, table, control_chars, output_column_names);
1.297     moko      639: 
                    640:        r.write_no_lang(*new VString(*new String(sdata.cstr(), String::L_CLEAN)));
                    641: #endif
1.224     misha     642: }
                    643: 
1.280     misha     644: static void _count(Request& r, MethodParams& params) {
                    645:        Table& table=GET_SELF(r, VTable).table();
                    646:        size_t result=0;
                    647:        if(params.count()) {
                    648:                const String& param=params.as_string(0, PARAMETER_MUST_BE_STRING);
                    649:                if(param == "columns")
1.310     moko      650:                        result = table.columns() ? table.columns()->count() : table.max_cells();
1.280     misha     651:                else if(param == "cells")
                    652:                        result = table.count() ? table[table.current()]->count() : 0;
                    653:                else if(param == "rows") // synonim for ^table.count[]
                    654:                        result = table.count();
                    655:                else
                    656:                        throw Exception(PARSER_RUNTIME, &param, "parameter must be 'columns', 'cells' and 'rows' only");
                    657:        } else
                    658:                result = table.count();
                    659: 
1.182     paf       660:        r.write_no_lang(*new VInt(result));
1.6       paf       661: }
                    662: 
1.182     paf       663: static void _line(Request& r, MethodParams&) {
                    664:        int result=1+GET_SELF(r, VTable).table().current();
                    665:        r.write_no_lang(*new VInt(result));
1.6       paf       666: }
                    667: 
1.182     paf       668: static void _offset(Request& r, MethodParams& params) {
                    669:        Table& table=GET_SELF(r, VTable).table();
                    670:        if(params.count()) {
1.133     paf       671:                bool absolute=false;
1.182     paf       672:                if(params.count()>1) {
                    673:                    const String&  whence=params.as_string(0, "whence must be string");
1.133     paf       674:                    if(whence=="cur")
1.134     paf       675:                                absolute=false;
1.133     paf       676:                    else if(whence=="set")
1.134     paf       677:                                absolute=true;
1.133     paf       678:                    else
1.318     moko      679:                                throw Exception(PARSER_RUNTIME, &whence, "is invalid whence, valid are 'cur' or 'set'");
1.157     paf       680:                }
1.133     paf       681:                
1.211     paf       682:                int offset=params.as_int(params.count()-1, "offset must be expression", r);
                    683:                table.offset(absolute, offset);
1.151     paf       684:        } else
1.182     paf       685:                r.write_no_lang(*new VInt(table.current()));
1.6       paf       686: }
                    687: 
1.182     paf       688: static void _menu(Request& r, MethodParams& params) {
1.263     misha     689:        InCycle temp(r);
1.221     paf       690: 
1.182     paf       691:        Value& body_code=params.as_junction(0, "body must be code");
1.7       paf       692:        
1.182     paf       693:        Value* delim_maybe_code=params.count()>1?&params[1]:0;
1.7       paf       694: 
1.182     paf       695:        Table& table=GET_SELF(r, VTable).table();
1.293     misha     696:        size_t saved_current=table.current();
1.7       paf       697: 
1.251     misha     698:        if(delim_maybe_code) { // delimiter set
1.250     misha     699:                bool need_delim=false;
1.316     moko      700:                for(size_t row=0; row<table.count(); row++) {
1.250     misha     701:                        table.set_current(row);
                    702: 
                    703:                        StringOrValue sv_processed=r.process(body_code);
                    704:                        Request::Skip lskip=r.get_skip(); r.set_skip(Request::SKIP_NOTHING);
                    705: 
                    706:                        const String* s_processed=sv_processed.get_string();
1.251     misha     707:                        if(s_processed && !s_processed->is_empty()) { // we have body
1.250     misha     708:                                if(need_delim) // need delim & iteration produced string?
                    709:                                        r.write_pass_lang(r.process(*delim_maybe_code));
                    710:                                else
                    711:                                        need_delim=true;
                    712:                        }
                    713: 
                    714:                        r.write_pass_lang(sv_processed);
1.242     misha     715: 
1.250     misha     716:                        if(lskip==Request::SKIP_BREAK)
                    717:                                break;
                    718:                }
                    719:        } else {
1.316     moko      720:                for(size_t row=0; row<table.count(); row++) {
1.250     misha     721:                        table.set_current(row);
                    722:  
                    723:                        r.process_write(body_code);
                    724:                        Request::Skip lskip=r.get_skip(); r.set_skip(Request::SKIP_NOTHING);
                    725:  
                    726:                        if(lskip==Request::SKIP_BREAK)
                    727:                                break;
1.7       paf       728:                }
                    729:        }
1.99      parser    730:        table.set_current(saved_current);
1.7       paf       731: }
                    732: 
1.110     parser    733: #ifndef DOXYGEN
1.74      paf       734: struct Row_info {
1.166     paf       735:        Request *r;
1.74      paf       736:        Table *table;
1.182     paf       737:        Value* key_code;
                    738:        size_t key_field;
                    739:        Array<int>* value_fields;
1.315     moko      740:        Value* value_code;
1.182     paf       741:        HashStringValue* hash;
1.174     paf       742:        Table2hash_distint distinct;
1.182     paf       743:        size_t row;
1.227     misha     744:        Table2hash_value_type value_type;
1.74      paf       745: };
1.110     parser    746: #endif
1.182     paf       747: static void table_row_to_hash(Table::element_type row, Row_info *info) {
                    748:        const String* key;
                    749:        if(info->key_code) {
                    750:                info->table->set_current(info->row++); // change context row
                    751:                StringOrValue sv_processed=info->r->process(*info->key_code);
1.166     paf       752:                key=&sv_processed.as_string();
1.227     misha     753:        } else {
1.182     paf       754:                key=info->key_field<row->count()?row->get(info->key_field):0;
1.227     misha     755:        }
1.166     paf       756: 
                    757:        if(!key)
                    758:                return; // ignore rows without key [too-short-record_array if-indexed]
1.74      paf       759:                
1.243     misha     760:        bool exist=false;
                    761:        switch(info->value_type) {
                    762:                case C_STRING: {
1.317     moko      763:                        if(info->value_fields->count()){
                    764:                                size_t index=info->value_fields->get(0);
                    765:                                exist=info->hash->put_dont_replace(*key, (index < row->count()) ? new VString(*row->get(index)) : VString::empty());
                    766:                        } else {
                    767:                                exist=info->hash->put_dont_replace(*key, VString::empty());
                    768:                        }
1.243     misha     769:                        break;
1.227     misha     770:                }
1.243     misha     771:                case C_HASH: {
1.182     paf       772:                        VHash* vhash=new VHash;
                    773:                        HashStringValue& hash=vhash->hash();
                    774:                        for(Array_iterator<int> i(*info->value_fields); i.has_next(); ) {
                    775:                                size_t value_field=i.next();
                    776:                                if(value_field<row->count())
1.314     moko      777:                                        hash.put(*info->table->columns()->get(value_field), new VString(*row->get(value_field)));
1.174     paf       778:                        }
                    779: 
1.227     misha     780:                        exist=info->hash->put_dont_replace(*key, vhash);
1.243     misha     781:                        break;
1.174     paf       782:                }
1.243     misha     783:                case C_TABLE: {
                    784:                        VTable* vtable=(VTable*)info->hash->get(*key); // table exist?
1.174     paf       785:                        Table* table;
1.227     misha     786:                        if(vtable) {
1.243     misha     787:                                if(info->distinct==D_ILLEGAL) {
                    788:                                        exist=true;
                    789:                                        break;
                    790:                                }
1.174     paf       791:                                table=vtable->get_table();
1.227     misha     792:                        } else {
1.174     paf       793:                                // no? creating table of same structure as source
1.179     paf       794:                                Table::Action_options table_options(0, 0);
1.182     paf       795:                                table=new Table(*info->table, table_options/*no rows, just structure*/);
                    796:                                info->hash->put(*key, new VTable(table));
1.174     paf       797:                        }
1.308     moko      798:                        Table::element_type row_copy(new ArrayString(row->count()));
                    799:                        row_copy->append(*row);
                    800:                        *table+=row_copy;
1.243     misha     801:                        break;
1.174     paf       802:                }
1.315     moko      803:                case C_CODE: {
                    804:                        if(!info->key_code)
                    805:                                info->table->set_current(info->row++); // change context row
                    806:                        exist=info->hash->put_dont_replace(*key, &info->r->process(*info->value_code).as_value());
                    807:                        break;
                    808:                }
1.74      paf       809:        }
1.227     misha     810:        if(exist && info->distinct==D_ILLEGAL)
1.314     moko      811:                throw Exception(PARSER_RUNTIME, key, "duplicate key");
1.74      paf       812: }
1.235     misha     813: 
                    814: Table2hash_value_type get_value_type(Value& vvalue_type){
                    815:        if(vvalue_type.is_string()) {
                    816:                const String& svalue_type=*vvalue_type.get_string();
                    817:                if(svalue_type == "table"){
                    818:                        return C_TABLE;
                    819:                } else if (svalue_type == "string") {
                    820:                        return C_STRING;
                    821:                } else if (svalue_type == "hash") {
                    822:                        return C_HASH;
                    823:                } else {
1.314     moko      824:                        throw Exception(PARSER_RUNTIME, &svalue_type, "must be 'hash', 'table' or 'string'");
1.235     misha     825:                }
                    826:        } else {
1.314     moko      827:                throw Exception(PARSER_RUNTIME, 0, "'type' must be string");
1.235     misha     828:        }
                    829: }
                    830: 
1.315     moko      831: static Table2hash_distint get_distinct(Value& vdistinct, Table2hash_value_type& value_type){
                    832:        if(vdistinct.is_string()) {
                    833:                const String& sdistinct=*vdistinct.get_string();
                    834:                if(sdistinct!="tables")
                    835:                        throw Exception(PARSER_RUNTIME, &sdistinct, "must be 'tables' or true/false");
                    836:                value_type=C_TABLE;
                    837:                return D_FIRST;
                    838:        }
                    839:        return vdistinct.as_bool() ? D_FIRST : D_ILLEGAL;
                    840: }
                    841: 
1.182     paf       842: static void _hash(Request& r, MethodParams& params) {
                    843:        Table& self_table=GET_SELF(r, VTable).table();
                    844:        VHash& result=*new VHash;
1.238     misha     845:        if(Table::columns_type columns=self_table.columns()){
1.182     paf       846:                if(columns->count()>0) {
1.174     paf       847:                        Table2hash_distint distinct=D_ILLEGAL;
1.227     misha     848:                        Table2hash_value_type value_type=C_HASH;
1.182     paf       849:                        int param_index=params.count()-1;
1.166     paf       850:                        if(param_index>0) {
1.315     moko      851: 
                    852:                                if(params[1].get_junction())
                    853:                                        value_type=C_CODE;
                    854: 
                    855:                                if(HashStringValue* options=params[param_index].get_hash()){ // can't use .as_hash because the 2nd param could be table so .as_hash throws an error
1.166     paf       856:                                        --param_index;
                    857:                                        int valid_options=0;
1.238     misha     858:                                        if(Value* vdistinct_code=options->get(sql_distinct_name)) { // $.distinct ?
1.166     paf       859:                                                valid_options++;
1.315     moko      860:                                                distinct=get_distinct(r.process_to_value(*vdistinct_code), value_type);
1.166     paf       861:                                        }
1.238     misha     862:                                        if(Value* vvalue_type_code=options->get(sql_value_type_name)) { // $.type ?
                    863:                                                if(value_type==C_TABLE) // $.distinct[tables] already was specified
1.314     moko      864:                                                        throw Exception(PARSER_RUNTIME, 0, "you can't specify $.distinct[tables] and $.type[] together");
1.315     moko      865:                                                if(value_type==C_CODE)
                    866:                                                        throw Exception(PARSER_RUNTIME, 0, "you can't specify $.type[] if value is code");
1.238     misha     867:                                                valid_options++;
                    868:                                                value_type=get_value_type(r.process_to_value(*vvalue_type_code));
                    869:                                        }
1.226     misha     870: 
1.182     paf       871:                                        if(valid_options!=options->count())
1.272     misha     872:                                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.163     paf       873:                                }
                    874:                        }
1.238     misha     875: 
1.315     moko      876:                        if(param_index==2) // options were specified but not as hash
1.289     misha     877:                                throw Exception(PARSER_RUNTIME, 0, "options must be hash");
                    878: 
1.182     paf       879:                        Array<int> value_fields;
1.315     moko      880:                        Value* value_code=0;
                    881: 
1.238     misha     882:                        if(param_index==0){ // list of columns wasn't specified
                    883:                                if(value_type==C_STRING) // $.type[string]
1.314     moko      884:                                        throw Exception(PARSER_RUNTIME, 0, "you must specify one value field with option $.type[string]");
1.238     misha     885:                                
                    886:                                for(size_t i=0; i<columns->count(); i++) // by all columns, including key
                    887:                                        value_fields+=i;
                    888: 
1.315     moko      889:                        } else { // list of columns or code was specified
1.227     misha     890:                                if(value_type==C_TABLE)
1.314     moko      891:                                        throw Exception(PARSER_RUNTIME, 0, "you can't specify value field(s) with option $.distinct[tables] or $.type[tables]");
1.238     misha     892: 
1.315     moko      893:                                Value& value_fields_param=params[1];
1.317     moko      894:                                if(value_fields_param.get_junction()){ // code specified
1.315     moko      895:                                        value_code=&value_fields_param;
                    896:                                } else if(value_fields_param.is_string()) { // one column as string was specified
1.317     moko      897:                                        const String &field_name=*value_fields_param.get_string();
                    898:                                        if(!field_name.is_empty())
                    899:                                                value_fields+=self_table.column_name2index(field_name, true);
1.238     misha     900:                                } else if(Table* value_fields_table=value_fields_param.get_table()) { // list of columns were specified in table
                    901:                                        for(Array_iterator<Table::element_type> i(*value_fields_table); i.has_next(); ) {
                    902:                                                const String& value_field_name =*i.next()->get(0);
                    903:                                                value_fields +=self_table.column_name2index(value_field_name, true);
1.123     parser    904:                                        }
                    905:                                } else
1.315     moko      906:                                        throw Exception(PARSER_RUNTIME, 0, "value field(s) must be string or table or code");
1.239     misha     907:                        }
1.74      paf       908: 
1.227     misha     909:                        if(value_type==C_STRING && value_fields.count()!=1)
1.314     moko      910:                                throw Exception(PARSER_RUNTIME, 0, "you can specify only one value field with option $.type[string]");
1.182     paf       911: 
                    912:                        {
                    913:                                Value* key_param=&params[0];
1.193     paf       914:                                Row_info info={
                    915:                                        &r,
                    916:                                        &self_table,
1.315     moko      917:                                        /*key_code=*/key_param->get_junction() ? key_param : 0,
1.197     paf       918:                                        /*key_field=*/0/*filled below*/,
1.193     paf       919:                                        &value_fields,
1.315     moko      920:                                        value_code,
1.193     paf       921:                                        &result.hash(),
                    922:                                        distinct,
1.227     misha     923:                                        /*row=*/0,
                    924:                                        value_type
1.193     paf       925:                                };
1.314     moko      926:                                info.key_field=(info.key_code ? -1 : self_table.column_name2index(key_param->as_string(), true));
1.182     paf       927: 
                    928:                                int saved_current=self_table.current();
                    929:                                self_table.for_each(table_row_to_hash, &info);
                    930:                                self_table.set_current(saved_current);
1.207     paf       931: 
                    932:                                result.extract_default();
1.182     paf       933:                        }
1.74      paf       934:                }
1.238     misha     935:        }
1.97      parser    936:        r.write_no_lang(result);
1.74      paf       937: }
                    938: 
1.110     parser    939: #ifndef DOXYGEN
1.78      paf       940: struct Table_seq_item {
1.182     paf       941:        ArrayString* row;
1.34      paf       942:        union {
1.182     paf       943:                const char *c_str;
1.34      paf       944:                double d;
                    945:        } value;
1.32      paf       946: };
1.110     parser    947: #endif
1.34      paf       948: static int sort_cmp_string(const void *a, const void *b) {
                    949:        return strcmp(
1.78      paf       950:                static_cast<const Table_seq_item *>(a)->value.c_str, 
                    951:                static_cast<const Table_seq_item *>(b)->value.c_str
1.34      paf       952:        );
                    953: }
                    954: static int sort_cmp_double(const void *a, const void *b) {
1.78      paf       955:        double va=static_cast<const Table_seq_item *>(a)->value.d;
                    956:        double vb=static_cast<const Table_seq_item *>(b)->value.d;
1.34      paf       957:        if(va<vb)
                    958:                return -1;
                    959:        else if(va>vb)
                    960:                return +1;
                    961:        else 
                    962:                return 0;
                    963: }
1.182     paf       964: static void _sort(Request& r, MethodParams& params) {
                    965:        Value& key_maker=params.as_junction(0, "key-maker must be code");
1.61      paf       966: 
1.182     paf       967:        bool reverse=params.count()>1/*..[desc|asc|]*/?
                    968:                reverse=params.as_no_junction(1, "order must not be code").as_string()=="desc":
1.104     parser    969:                false; // default=asc
1.32      paf       970: 
1.182     paf       971:        Table& old_table=GET_SELF(r, VTable).table();
                    972:        Table& new_table=*new Table(old_table.columns());
1.34      paf       973: 
1.182     paf       974:        Table_seq_item* seq=new(PointerFreeGC) Table_seq_item[old_table.count()];
1.34      paf       975:        int i;
                    976: 
                    977:        // calculate key values
                    978:        bool key_values_are_strings=true;
1.182     paf       979:        int old_count=old_table.count();
                    980:        for(i=0; i<old_count; i++) {
1.101     parser    981:                old_table.set_current(i);
1.32      paf       982:                // calculate key value
1.182     paf       983:                seq[i].row=old_table[i];
1.287     moko      984:                Value& value=r.process_to_value(key_maker);
1.34      paf       985:                if(i==0) // determining key values type by first one
                    986:                        key_values_are_strings=value.is_string();
                    987: 
                    988:                if(key_values_are_strings)
                    989:                        seq[i].value.c_str=value.as_string().cstr();
                    990:                else
1.287     moko      991:                        seq[i].value.d=value.as_expr_result().as_double();
1.32      paf       992:        }
1.265     misha     993: 
                    994:        // @todo: handle this elsewhere
                    995:        if(r.charsets.source().NAME()=="KOI8-R" && key_values_are_strings) {
                    996:                for(i=0; i<old_count; i++)
                    997:                        if(*seq[i].value.c_str)
                    998:                                seq[i].value.c_str=Charset::transcode(seq[i].value.c_str, r.charsets.source(), UTF8_charset).cstr();
                    999:        }
                   1000: 
1.266     misha    1001:        // sort keys
1.295     moko     1002:        qsort(seq, old_count, sizeof(Table_seq_item), key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32      paf      1003: 
1.34      paf      1004:        // reorder table as they require in 'seq'
1.182     paf      1005:        for(i=0; i<old_count; i++)
                   1006:                new_table+=Table::element_type(seq[reverse?old_count-1-i:i].row);
                   1007: 
                   1008:        delete[] seq;
1.32      paf      1009: 
1.116     parser   1010:        // replace any previous table value
1.182     paf      1011:        GET_SELF(r, VTable).set_table(new_table);
1.32      paf      1012: }
                   1013: 
1.176     paf      1014: #ifndef DOXYGEN
1.182     paf      1015: struct Expression_is_true_info {
1.176     paf      1016:        Request* r;
                   1017:        Value* expression_code;
                   1018: };
                   1019: #endif
1.275     misha    1020: 
1.191     paf      1021: static bool expression_is_true(Table&, Expression_is_true_info* info) {
                   1022:        return info->r->process_to_value(*info->expression_code).as_bool();
1.176     paf      1023: }
1.275     misha    1024: 
                   1025: static bool _locate_expression(Table& table, Request& r, MethodParams& params) {
1.182     paf      1026:        Value& expression_code=params.as_junction(0, "must be expression");
1.303     moko     1027:        Table::Action_options o=get_action_options(r, params, 1, table);
                   1028:        if(params.count()>2)
                   1029:                throw Exception(PARSER_RUNTIME, 0, "locate by expression only has parameters: expression and, maybe, options");
1.182     paf      1030:        Expression_is_true_info info={&r, &expression_code};
                   1031:        return table.table_first_that(expression_is_true, &info, o);
1.145     paf      1032: }
1.275     misha    1033: 
                   1034: static bool _locate_name_value(Table& table, Request& r, MethodParams& params) {
1.182     paf      1035:        const String& name=params.as_string(0, "column name must be string");
1.232     misha    1036:        const String& value=params.as_string(1, VALUE_MUST_BE_STRING);
1.303     moko     1037:        Table::Action_options o=get_action_options(r, params, 2, table);
1.182     paf      1038:        return table.locate(name, value, o);
                   1039: }
1.275     misha    1040: 
1.182     paf      1041: static void _locate(Request& r, MethodParams& params) {
                   1042:        Table& table=GET_SELF(r, VTable).table();
1.72      paf      1043: 
1.291     moko     1044:        bool result=params[0].get_junction() || (params.count() == 1) ?
1.275     misha    1045:                _locate_expression(table, r, params) :
                   1046:                _locate_name_value(table, r, params);
1.249     misha    1047:        r.write_no_lang(VBool::get(result));
1.145     paf      1048: }
1.182     paf      1049: 
                   1050: 
1.191     paf      1051: static void _flip(Request& r, MethodParams&) {
1.182     paf      1052:        Table& old_table=GET_SELF(r, VTable).table();
1.184     paf      1053:        Table& new_table=*new Table(0);
1.182     paf      1054:        if(size_t old_count=old_table.count())
1.310     moko     1055:                if(size_t old_cols=old_table.columns()?old_table.columns()->count():old_table.max_cells())
1.182     paf      1056:                        for(size_t column=0; column<old_cols; column++) {
                   1057:                                Table::element_type new_row(new ArrayString(old_count));
                   1058:                                for(size_t i=0; i<old_count; i++) {
                   1059:                                        Table::element_type old_row=old_table[i];
                   1060:                                        *new_row+=column<old_row->count()?old_row->get(column):new String;
1.39      paf      1061:                                }
1.182     paf      1062:                                new_table+=new_row;
1.39      paf      1063:                        }
                   1064: 
1.182     paf      1065:        r.write_no_lang(*new VTable(&new_table));
1.39      paf      1066: }
                   1067: 
1.293     misha    1068: static void _foreach(Request& r, MethodParams& params) {
                   1069:        InCycle temp(r);
                   1070: 
                   1071:        const String& rownum_name=params.as_string(0, "rownum-var name must be string");
                   1072:        const String& value_name=params.as_string(1, "value-var name must be string");
                   1073: 
                   1074:        Value& body_code=params.as_junction(2, "body must be code");
                   1075:        
                   1076:        Value* delim_maybe_code=params.count()>3?&params[3]:0;
                   1077: 
                   1078:        Table& table=GET_SELF(r, VTable).table();
                   1079:        size_t saved_current=table.current();
                   1080: 
                   1081:        const String* rownum_var_name=rownum_name.is_empty()? 0 : &rownum_name;
                   1082:        const String* value_var_name=value_name.is_empty()? 0 : &value_name;
                   1083: 
                   1084:        Value* var_context=r.get_method_frame()->caller();
                   1085: 
                   1086:        if(delim_maybe_code) { // delimiter set
                   1087:                bool need_delim=false;
1.316     moko     1088:                for(size_t row=0; row<table.count(); row++) {
1.293     misha    1089:                        table.set_current(row);
                   1090: 
                   1091:                        if(rownum_var_name)
1.299     moko     1092:                                r.put_element(*var_context, *rownum_var_name, new VString(*new String(String::Body::Format(row), String::L_CLEAN)));
1.293     misha    1093:                        if(value_var_name)
1.299     moko     1094:                                r.put_element(*var_context, *value_var_name, new VTable(&table));
1.293     misha    1095: 
                   1096:                        StringOrValue sv_processed=r.process(body_code);
                   1097:                        Request::Skip lskip=r.get_skip(); r.set_skip(Request::SKIP_NOTHING);
                   1098: 
                   1099:                        const String* s_processed=sv_processed.get_string();
                   1100:                        if(s_processed && !s_processed->is_empty()) { // we have body
                   1101:                                if(need_delim) // need delim & iteration produced string?
                   1102:                                        r.write_pass_lang(r.process(*delim_maybe_code));
                   1103:                                else
                   1104:                                        need_delim=true;
                   1105:                        }
                   1106: 
                   1107:                        r.write_pass_lang(sv_processed);
                   1108: 
                   1109:                        if(lskip==Request::SKIP_BREAK)
                   1110:                                break;
                   1111:                }
                   1112:        } else {
1.316     moko     1113:                for(size_t row=0; row<table.count(); row++) {
1.293     misha    1114:                        table.set_current(row);
                   1115:  
                   1116:                        if(rownum_var_name)
1.300     moko     1117:                                r.put_element(*var_context, *rownum_var_name, new VString(*new String(String::Body::Format(row), String::L_CLEAN)));
1.293     misha    1118:                        if(value_var_name)
1.300     moko     1119:                                r.put_element(*var_context, *value_var_name, new VTable(&table));
1.293     misha    1120: 
                   1121:                        r.process_write(body_code);
                   1122:                        Request::Skip lskip=r.get_skip(); r.set_skip(Request::SKIP_NOTHING);
                   1123:  
                   1124:                        if(lskip==Request::SKIP_BREAK)
                   1125:                                break;
                   1126:                }
                   1127:        }
                   1128:        table.set_current(saved_current);
                   1129: }
                   1130: 
1.309     moko     1131: static void update_cell(HashStringValue::key_type aname, HashStringValue::value_type avalue, VTable *dest) {
                   1132:        dest->put_element(String(aname, String::L_CLEAN), avalue); // new not required
                   1133: }
                   1134: 
                   1135: inline Table::element_type row_from_string(Request& r, Value &param){
                   1136:        if(!param.is_string() && !param.get_junction())
                   1137:                throw Exception(PARSER_RUNTIME, 0, "row must be string, code or hash");
                   1138: 
1.182     paf      1139:        Temp_lang temp_lang(r, String::L_PASS_APPENDED);
1.309     moko     1140:        const String& string=r.process_to_string(param);
1.41      paf      1141: 
                   1142:        // parse cells
1.182     paf      1143:        Table::element_type row=new ArrayString;
                   1144:        size_t pos_after=0;
                   1145:        string.split(*row, pos_after, "\t", String::L_AS_IS);
1.41      paf      1146: 
1.309     moko     1147:        return row;
1.41      paf      1148: }
                   1149: 
1.309     moko     1150: static void _append(Request& r, MethodParams& params) {
                   1151:        VTable vtable=GET_SELF(r, VTable);
                   1152:        Table& table=vtable.table();
1.306     moko     1153: 
1.309     moko     1154:        HashStringValue* hash=params[0].get_hash();
                   1155:        if(hash){
                   1156:                table+=new ArrayString();
                   1157:                size_t saved_current=table.current();
                   1158:                table.set_current(table.count()-1);
                   1159:                hash->for_each<VTable*>(update_cell, &vtable);
                   1160:                table.set_current(saved_current);
                   1161:        } else {
                   1162:                table+=row_from_string(r, params[0]);
                   1163:        }
                   1164: }
1.306     moko     1165: 
1.309     moko     1166: static void _insert(Request& r, MethodParams& params) {
                   1167:        VTable vtable=GET_SELF(r, VTable);
                   1168:        Table& table=vtable.table();
                   1169:        HashStringValue* hash=params[0].get_hash();
                   1170:        if(hash){
                   1171:                table.insert(table.current(), new ArrayString());
                   1172:                hash->for_each<VTable*>(update_cell, &vtable);
                   1173:        } else {
                   1174:                table.insert(table.current(), row_from_string(r, params[0]));
                   1175:        }
1.306     moko     1176: }
                   1177: 
1.311     moko     1178: static void _delete(Request& r, MethodParams&) {
1.307     moko     1179:        Table& table=GET_SELF(r, VTable).table();
                   1180:        table.remove_current();
                   1181: }
                   1182: 
1.182     paf      1183: static void join_named_row(Table& src, Table* dest) {
                   1184:        Table::columns_type dest_columns=dest->columns();
                   1185:        size_t dest_columns_count=dest_columns->count();
                   1186:        Table::element_type dest_row(new ArrayString(dest_columns_count));
                   1187:        for(size_t dest_column=0; dest_column<dest_columns_count; dest_column++) {
                   1188:                const String* src_item=src.item(*dest_columns->get(dest_column));
                   1189:                *dest_row+=src_item?src_item:new String;
                   1190:        }
                   1191:        *dest+=dest_row;
                   1192: }
                   1193: static void join_nameless_row(Table& src, Table* dest) {
                   1194:        *dest+=src[src.current()];
                   1195: }
                   1196: static void _join(Request& r, MethodParams& params) {
1.288     misha    1197:        Table& src=*params.as_table(0, "source");
1.176     paf      1198: 
1.268     misha    1199:        Table::Action_options o=get_action_options(r, params, 1, src);
1.42      paf      1200: 
1.182     paf      1201:        Table& dest=GET_SELF(r, VTable).table();
1.42      paf      1202:        if(&src == &dest)
1.318     moko     1203:                throw Exception(PARSER_RUNTIME, 0, "source and destination are same table");
1.42      paf      1204: 
1.193     paf      1205:        if(dest.columns()) // dest is named
1.182     paf      1206:                src.table_for_each(join_named_row, &dest, o);
                   1207:        else // dest is nameless
                   1208:                src.table_for_each(join_nameless_row, &dest, o);
1.42      paf      1209: }
                   1210: 
1.94      parser   1211: #ifndef DOXYGEN
1.169     paf      1212: class Table_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.182     paf      1213:        ArrayString& columns;
1.218     paf      1214:        int columns_count;
1.182     paf      1215:        ArrayString* row;
1.94      parser   1216: public:
1.182     paf      1217:        Table* table;
                   1218: public:
                   1219:        Table_sql_event_handlers() :
                   1220:                columns(*new ArrayString), row(0), table(0) {
1.94      parser   1221:        }
                   1222: 
1.279     misha    1223:        bool add_column(SQL_Error& error, const char *str, size_t ) {
1.170     paf      1224:                try {
1.274     moko     1225:                        columns+=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */);
1.170     paf      1226:                        return false;
                   1227:                } catch(...) {
                   1228:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_column");
                   1229:                        return true;
                   1230:                }
                   1231:        }
                   1232:        bool before_rows(SQL_Error& error) { 
                   1233:                try {
1.182     paf      1234:                        table=new Table(&columns);
1.218     paf      1235:                        columns_count=columns.count();
1.170     paf      1236:                        return false;
                   1237:                } catch(...) {
                   1238:                        error=SQL_Error("exception occured in Table_sql_event_handlers::before_rows");
                   1239:                        return true;
                   1240:                }
                   1241:        }
                   1242:        bool add_row(SQL_Error& error) {
                   1243:                try {
1.218     paf      1244:                        *table+=row=new ArrayString(columns_count);
1.170     paf      1245:                        return false;
                   1246:                } catch(...) {
                   1247:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_row");
                   1248:                        return true;
                   1249:                }
                   1250:        }
1.279     misha    1251:        bool add_row_cell(SQL_Error& error, const char* str, size_t ) {
1.170     paf      1252:                try {
1.292     misha    1253:                        *row+=str?new String(str, String::L_TAINTED /* no length as 0x00 can be inside */):&String::Empty;
1.170     paf      1254:                        return false;
                   1255:                } catch(...) {
                   1256:                        error=SQL_Error("exception occured in Table_sql_event_handlers::add_row_cell");
                   1257:                        return true;
                   1258:                }
1.94      parser   1259:        }
                   1260: };
                   1261: #endif
1.204     paf      1262: 
                   1263: static void marshal_bind(
                   1264:                                                 HashStringValue::key_type aname, 
                   1265:                                                 HashStringValue::value_type avalue,
                   1266:                                                 SQL_Driver::Placeholder** pptr) 
                   1267: {
                   1268:        SQL_Driver::Placeholder& ph=**pptr;
                   1269:        ph.name=aname.cstr();
1.261     misha    1270:        ph.value=avalue->as_string().untaint_cstr(String::L_AS_IS);
1.204     paf      1271:        ph.is_null=avalue->get_class()==void_class;
                   1272:        ph.were_updated=false;
                   1273: 
                   1274:        (*pptr)++;
                   1275: }
                   1276: 
                   1277: // not static, used elsewhere
                   1278: int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders) {
                   1279:        int hash_count=hash.count();
1.302     moko     1280:        placeholders=new SQL_Driver::Placeholder[hash_count];
1.204     paf      1281:        SQL_Driver::Placeholder* ptr=placeholders;
1.221     paf      1282:        hash.for_each<SQL_Driver::Placeholder**>(marshal_bind, &ptr);
1.204     paf      1283:        return hash_count;
                   1284: }
                   1285: 
                   1286: // not static, used elsewhere
                   1287: void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders) {
                   1288:        SQL_Driver::Placeholder* ph=placeholders;
                   1289:        for(int i=0; i<placeholder_count; i++, ph++)
                   1290:                if(ph->were_updated) {
                   1291:                        Value* value;
                   1292:                        if(ph->is_null)
1.248     misha    1293:                                value=VVoid::get();
1.204     paf      1294:                        else
1.256     misha    1295:                                value=new VString(*new String(ph->value, String::L_TAINTED));
1.204     paf      1296:                        hash.put(ph->name, value);
                   1297:                }
                   1298: }
                   1299: 
1.182     paf      1300: static void _sql(Request& r, MethodParams& params) {
                   1301:        Value& statement=params.as_junction(0, "statement must be code");
1.49      paf      1302: 
1.204     paf      1303:        HashStringValue* bind=0;
1.244     misha    1304:        ulong limit=SQL_NO_LIMIT;
1.100     parser   1305:        ulong offset=0;
1.281     misha    1306:        if(params.count()>1)
1.288     misha    1307:                if(HashStringValue* options=params.as_hash(1, "sql options")) {
1.281     misha    1308:                        int valid_options=0;
                   1309:                        if(Value* vbind=options->get(sql_bind_name)) {
                   1310:                                valid_options++;
                   1311:                                bind=vbind->get_hash();
                   1312:                        }
                   1313:                        if(Value* vlimit=options->get(sql_limit_name)) {
                   1314:                                valid_options++;
                   1315:                                limit=(ulong)r.process_to_value(*vlimit).as_double();
                   1316:                        }
                   1317:                        if(Value* voffset=options->get(sql_offset_name)) {
                   1318:                                valid_options++;
                   1319:                                offset=(ulong)r.process_to_value(*voffset).as_double();
                   1320:                        }
                   1321:                        if(valid_options!=options->count())
                   1322:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                   1323:                }
1.49      paf      1324: 
1.204     paf      1325:        SQL_Driver::Placeholder* placeholders=0;
                   1326:        uint placeholders_count=0;
                   1327:        if(bind)
                   1328:                placeholders_count=marshal_binds(*bind, placeholders);
                   1329: 
1.182     paf      1330:        Temp_lang temp_lang(r, String::L_SQL);
                   1331:        const String&  statement_string=r.process_to_string(statement);
1.262     misha    1332:        const char* statement_cstr=statement_string.untaint_cstr(r.flang, r.connection());
1.260     misha    1333: 
1.182     paf      1334:        Table_sql_event_handlers handlers;
1.294     moko     1335: 
1.182     paf      1336:        r.connection()->query(
1.203     paf      1337:                statement_cstr, 
1.208     paf      1338:                placeholders_count, placeholders,
1.203     paf      1339:                offset, limit, 
1.160     paf      1340:                handlers,
                   1341:                statement_string);
1.135     paf      1342:        
1.204     paf      1343:        if(bind)
                   1344:                unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.96      parser   1345: 
1.182     paf      1346:        Table& result=
                   1347:                handlers.table?*handlers.table: // query resulted in table? return it
                   1348:                *new Table(Table::columns_type(0)); // query returned no table, fake it
1.49      paf      1349: 
                   1350:        // replace any previous table value
1.182     paf      1351:        GET_SELF(r, VTable).set_table(result);
1.49      paf      1352: }
                   1353: 
1.233     misha    1354: static void _columns(Request& r, MethodParams& params) {
                   1355:        const String* column_column_name;
                   1356:        if(params.count()>0)
                   1357:                column_column_name=&params.as_string(0, COLUMN_NAME_MUST_BE_STRING);
                   1358:        else 
                   1359:                column_column_name=new String("column");
1.88      parser   1360: 
1.182     paf      1361:        Table::columns_type result_columns(new ArrayString);
1.233     misha    1362:        *result_columns+=column_column_name;
1.182     paf      1363:        Table& result_table=*new Table(result_columns);
1.88      parser   1364: 
1.182     paf      1365:        Table& source_table=GET_SELF(r, VTable).table();
                   1366:        if(Table::columns_type source_columns=source_table.columns()) {
                   1367:                for(Array_iterator<const String*> i(*source_columns); i.has_next(); ) {
                   1368:                        Table::element_type result_row(new ArrayString);
                   1369:                        *result_row+=i.next();
                   1370:                        result_table+=result_row;
1.88      parser   1371:                }
                   1372:        }
                   1373: 
1.182     paf      1374:        r.write_no_lang(*new VTable(&result_table));
1.88      parser   1375: }
                   1376: 
1.182     paf      1377: static void _select(Request& r, MethodParams& params) {
1.231     misha    1378:        Value& vcondition=params.as_expression(0, "condition must be number, bool or expression");
1.148     paf      1379: 
1.182     paf      1380:        Table& source_table=GET_SELF(r, VTable).table();
1.148     paf      1381: 
1.277     moko     1382:        int limit=source_table.count();
1.282     misha    1383:        int offset=0;
                   1384:        bool reverse=false;
1.278     moko     1385:        
1.281     misha    1386:        if(params.count()>1)
                   1387:                if(HashStringValue* options=params.as_hash(1)) {
                   1388:                        int valid_options=0;
                   1389:                        if(Value* vlimit=options->get(sql_limit_name)) {
                   1390:                                valid_options++;
                   1391:                                limit=r.process_to_value(*vlimit).as_int();
                   1392:                        }
                   1393:                        if(Value* voffset=options->get(sql_offset_name)) {
                   1394:                                valid_options++;
                   1395:                                offset=r.process_to_value(*voffset).as_int();
                   1396:                        }
                   1397:                        if(Value* vreverse=options->get(table_reverse_name)) {
                   1398:                                valid_options++;
                   1399:                                reverse=r.process_to_value(*vreverse).as_bool();
                   1400:                        }
                   1401:                        if(valid_options!=options->count())
1.288     misha    1402:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.281     misha    1403:                }
                   1404: 
                   1405:        Table& result_table=*new Table(source_table.columns());
1.277     moko     1406: 
1.283     misha    1407:        size_t size=source_table.count();
                   1408:        if(offset<0)
                   1409:                offset+=size;
1.284     misha    1410:        if(size && limit>0 && offset>=0 && (size_t)offset<size){
1.282     misha    1411:                size_t saved_current=source_table.current();
                   1412:                size_t appended=0;
                   1413: 
                   1414:                if(reverse){
                   1415:                        for(size_t row=size-1; result_table.count() < (size_t)limit; row--) {
                   1416:                                source_table.set_current(row);
                   1417: 
                   1418:                                bool condition=r.process_to_value(vcondition, false/*don't intercept string*/).as_bool();
                   1419: 
                   1420:                                if(condition && ++appended > (size_t)offset) // ...condition is true, adding to the result
                   1421:                                        result_table+=source_table[row];
                   1422:                                if(row==0) break;
                   1423:                        }
                   1424:                } else {
                   1425:                        for(size_t row=0; row < size && result_table.count() < (size_t)limit; row++) {
                   1426:                                source_table.set_current(row);
1.148     paf      1427: 
1.282     misha    1428:                                bool condition=r.process_to_value(vcondition, false/*don't intercept string*/).as_bool();
1.277     moko     1429: 
1.282     misha    1430:                                if(condition && ++appended > (size_t)offset) // ...condition is true, adding to the result
                   1431:                                        result_table+=source_table[row];
                   1432:                        }
1.277     moko     1433:                }
1.282     misha    1434:                source_table.set_current(saved_current);
1.148     paf      1435:        }
                   1436: 
1.182     paf      1437:        r.write_no_lang(*new VTable(&result_table));
1.148     paf      1438: }
                   1439: 
1.66      paf      1440: // constructor
                   1441: 
1.182     paf      1442: MTable::MTable(): Methoded("table") {
1.142     paf      1443:        // ^table::create{data}
                   1444:        // ^table::create[nameless]{data}
                   1445:        // ^table::create[table]
1.236     misha    1446:        add_native_method("create", Method::CT_DYNAMIC, _create, 1, 3);
1.142     paf      1447:        // old name for compatibility with <= v 1.141 2002/01/25 11:33:45 paf
1.236     misha    1448:        add_native_method("set", Method::CT_DYNAMIC, _create, 1, 3); 
1.2       paf      1449: 
1.142     paf      1450:        // ^table::load[file]  
                   1451:        // ^table::load[nameless;file]
1.168     paf      1452:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 3);
1.22      paf      1453: 
                   1454:        // ^table.save[file]  
                   1455:        // ^table.save[nameless;file]
1.188     paf      1456:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 3);
1.6       paf      1457: 
1.224     misha    1458:        // add_native_method("save_old", Method::CT_DYNAMIC, _save_old, 1, 3);
                   1459: 
1.297     moko     1460:        // ^table.csv-string[]
                   1461:        // ^table.csv-string[nameless]
                   1462:        // ^table.csv-string[nameless;$.encloser["] $.separator[,]]
                   1463:        add_native_method("csv-string", Method::CT_DYNAMIC, _csv_string, 0, 2);
                   1464: 
1.6       paf      1465:        // ^table.count[]
1.280     misha    1466:        // ^table.count[rows]
                   1467:        // ^table.count[columns]
                   1468:        // ^table.count[cells]
                   1469:        add_native_method("count", Method::CT_DYNAMIC, _count, 0, 1);
1.6       paf      1470: 
                   1471:        // ^table.line[]
1.66      paf      1472:        add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6       paf      1473: 
1.10      paf      1474:        // ^table.offset[]  
1.133     paf      1475:        // ^table.offset(offset)
                   1476:        // ^table.offset[cur|set](offset)
                   1477:        add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 2);
1.7       paf      1478: 
1.10      paf      1479:        // ^table.menu{code}  
                   1480:        // ^table.menu{code}[delim]
1.66      paf      1481:        add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.74      paf      1482: 
1.239     misha    1483:        // ^table.hash[key field name]
                   1484:        // ^table.hash[key field name][value field name(s) string/table]
1.163     paf      1485:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 3);
1.32      paf      1486: 
1.102     parser   1487:        // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc]
                   1488:        // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc]
1.66      paf      1489:        add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8       paf      1490: 
1.36      paf      1491:        // ^table.locate[field;value]
1.176     paf      1492:        add_native_method("locate", Method::CT_DYNAMIC, _locate, 1, 3);
1.39      paf      1493: 
                   1494:        // ^table.flip[]
1.66      paf      1495:        add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41      paf      1496: 
1.293     misha    1497:        // ^table.foreach[row-num;value]{code}
                   1498:        // ^table.foreach[row-num;value]{code}[delim]
                   1499:        add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 3, 4);
                   1500: 
1.306     moko     1501:        // ^table.append{row{tab}data}
1.66      paf      1502:        add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42      paf      1503: 
1.306     moko     1504:        // ^table.insert{row{tab}data} before current row
                   1505:        add_native_method("insert", Method::CT_DYNAMIC, _insert, 1, 1);
                   1506: 
1.307     moko     1507:        // ^table.delete[] current row
                   1508:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 0);
                   1509: 
1.157     paf      1510:        // ^table.join[table][$.limit(10) $.offset(1) $.offset[cur] ]
                   1511:        add_native_method("join", Method::CT_DYNAMIC, _join, 1, 2);
1.49      paf      1512: 
                   1513: 
1.239     misha    1514:        // ^table::sql[query]
                   1515:        // ^table::sql[query][$.limit(1) $.offset(2)]
1.100     parser   1516:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.88      parser   1517: 
1.239     misha    1518:        // ^table.columns[[column name]]
1.233     misha    1519:        add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 1);
1.148     paf      1520: 
                   1521:        // ^table.select(expression) = table
1.277     moko     1522:        add_native_method("select", Method::CT_DYNAMIC, _select, 1, 2);
1.40      paf      1523: }

E-mail: