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

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

E-mail: