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

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.349   ! moko       25: volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.348 2019/09/11 15:26:09 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.349   ! moko      409:        if(options_param_index<params.count() && (options=params.as_hash(options_param_index)))
1.319     moko      410:                control_chars.load(*options);
1.186     paf       411: 
1.44      paf       412:        // loading text
1.321     moko      413:        char *data=file_load_text(r, r.absolute(params.as_string(filename_param_index, FILE_NAME_MUST_BE_STRING)), true, options);
1.44      paf       414: 
1.319     moko      415:        Skip_lines_action skip_lines_action = (control_chars.separator=='#' || control_chars.encloser=='#') ? skip_empty_lines : skip_empty_and_comment_lines;
1.273     misha     416: 
1.1       paf       417:        // parse columns
1.182     paf       418:        Table::columns_type columns;
1.168     paf       419:        if(nameless) {
1.322     moko      420:                columns=0; // nameless
1.1       paf       421:        } else {
1.322     moko      422:                columns=new ArrayString;
1.1       paf       423: 
1.273     misha     424:                skip_lines_action(&data);
1.320     moko      425:                while( lsplit_result sr=lsplit(&data, control_chars.separators, control_chars.encloser) ) {
1.256     misha     426:                        *columns+=new String(sr.piece, String::L_TAINTED);
1.187     paf       427:                        if(sr.delim=='\n') 
                    428:                                break;
1.139     paf       429:                }
1.1       paf       430:        }
1.187     paf       431:        
                    432:        Table& table=*new Table(columns);
1.322     moko      433:        int columns_count=columns ? columns->count(): 0;
1.1       paf       434: 
                    435:        // parse cells
1.218     paf       436:        Table::element_type row(new ArrayString(columns_count));
1.273     misha     437:        skip_lines_action(&data);
1.320     moko      438:        while( lsplit_result sr=lsplit(&data, control_chars.separators, control_chars.encloser) ) {
1.201     paf       439:                if(!*sr.piece && !sr.delim && !row->count()) // append last empty column [if without \n]
                    440:                        break;
1.256     misha     441:                *row+=new String(sr.piece, String::L_TAINTED);
1.187     paf       442:                if(sr.delim=='\n') {
                    443:                        table+=row;
1.218     paf       444:                        row=new ArrayString(columns_count);
1.273     misha     445:                        skip_lines_action(&data);
1.1       paf       446:                }
1.187     paf       447:        }
                    448:        // last line [if without \n]
                    449:        if(row->count())
1.1       paf       450:                table+=row;
1.187     paf       451:        
1.1       paf       452:        // replace any previous table value
1.182     paf       453:        GET_SELF(r, VTable).set_table(table);
1.1       paf       454: }
1.2       paf       455: 
1.297     moko      456: #ifdef USE_STRINGSTREAM
1.346     moko      457: #include "../lib/gc/include/gc_allocator.h"
1.269     misha     458: 
                    459: typedef std::basic_stringstream<char, std::char_traits<char>, gc_allocator<char> > pa_stringstream;
                    460: typedef std::basic_string<char, std::char_traits<char>, gc_allocator<char> > pa_string;
                    461: 
1.298     moko      462: static void enclose( pa_stringstream& to, const String* from, char encloser ) {
1.297     moko      463:        if(from){
1.257     misha     464:                to<<encloser;
1.188     paf       465:                // while we have 'encloser'...
1.189     paf       466:                size_t pos_after=0;
1.297     moko      467:                for( size_t pos_before; (pos_before=from->pos( encloser, pos_after ))!=STRING_NOT_FOUND; pos_after=pos_before) {
1.220     paf       468:                        pos_before++; // including first encloser (and skipping it for next pos)
1.297     moko      469:                        to<<from->mid(pos_after, pos_before).cstr();
1.257     misha     470:                        to<<encloser; // doubling encloser
1.188     paf       471:                }
                    472:                // last piece
1.297     moko      473:                size_t from_length=from->length();
1.188     paf       474:                if(pos_after<from_length)
1.297     moko      475:                        to<<from->mid(pos_after, from_length).cstr();
                    476:                to<<encloser;
                    477:        } else {
                    478:                to<<encloser<<encloser;
                    479:        }
                    480: }
                    481: 
1.319     moko      482: static void table_to_csv(pa_stringstream& result, Table& table, TableControlChars& control_chars, bool output_column_names) {
1.297     moko      483:        if(output_column_names) {
                    484:                if(table.columns()) { // named table
1.319     moko      485:                        if(control_chars.encloser){
1.297     moko      486:                                for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
1.319     moko      487:                                        enclose( result, i.next(), control_chars.encloser );
1.297     moko      488:                                        if(i.has_next())
1.319     moko      489:                                                result<<control_chars.separator;
1.297     moko      490:                                }
                    491:                        } else {
                    492:                                for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
                    493:                                        result<<i.next()->cstr();
                    494:                                        if(i.has_next())
1.319     moko      495:                                                result<<control_chars.separator;
1.297     moko      496:                                }
                    497:                        }
                    498:                } else { // nameless table [we were asked to output column names]
                    499:                        if(int lsize=table.count()?table[0]->count():0)
                    500:                                for(int column=0; column<lsize; column++) {
1.319     moko      501:                                        if(control_chars.encloser) {
                    502:                                                result<<control_chars.encloser<<column<<control_chars.encloser;
1.297     moko      503:                                        } else {
                    504:                                                result<<column;
                    505:                                        }
                    506:                                        if(column<lsize-1){
1.319     moko      507:                                                result<<control_chars.separator;
1.297     moko      508:                                        }
                    509:                                }
                    510:                        else
                    511:                                result<<"empty nameless table";
                    512:                }
                    513:                result<<'\n';
                    514:        }
1.188     paf       515: 
1.297     moko      516:        // process data lines
                    517:        Array_iterator<ArrayString*> i(table);
1.319     moko      518:        if(control_chars.encloser){
1.297     moko      519:                while(i.has_next()) {
                    520:                        for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
1.319     moko      521:                                enclose( result, c.next(), control_chars.encloser );
1.297     moko      522:                                if(c.has_next())
1.319     moko      523:                                        result<<control_chars.separator;
1.297     moko      524:                        }
                    525:                        result<<'\n';
                    526:                }
                    527:        } else {
                    528:                while(i.has_next()) {
                    529:                        for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
                    530:                                result<<c.next()->cstr();
                    531:                                if(c.has_next())
1.319     moko      532:                                        result<<control_chars.separator;
1.297     moko      533:                        }
                    534:                        result<<'\n';
                    535:                }
                    536:        }
1.188     paf       537: }
                    538: 
1.241     misha     539: #else
                    540: 
1.298     moko      541: void enclose( String& to, const String* from, char encloser, const String* sencloser ) {
1.297     moko      542:        if(from){
1.257     misha     543:                to<<*sencloser;
1.224     misha     544:                // while we have 'encloser'...
                    545:                size_t pos_after=0;
1.297     moko      546:                for( size_t pos_before; (pos_before=from->pos( encloser, pos_after ))!=STRING_NOT_FOUND; pos_after=pos_before) {
1.224     misha     547:                        pos_before++; // including first encloser (and skipping it for next pos)
1.297     moko      548:                        to<<from->mid(pos_after, pos_before);
1.257     misha     549:                        to<<*sencloser; // doubling encloser
1.224     misha     550:                }
                    551:                // last piece
1.297     moko      552:                size_t from_length=from->length();
1.224     misha     553:                if(pos_after<from_length)
1.297     moko      554:                        to<<from->mid(pos_after, from_length);
1.257     misha     555:                to<<*sencloser;
1.297     moko      556:        } else {
                    557:                to<<*sencloser<<*sencloser;
                    558:        }
1.224     misha     559: }
                    560: 
1.319     moko      561: static void table_to_csv(String& result, Table& table, TableControlChars& control_chars, bool output_column_names) {
1.297     moko      562:        if(output_column_names) {
                    563:                if(table.columns()) { // named table
1.319     moko      564:                        if(control_chars.encloser) {
1.297     moko      565:                                for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
1.319     moko      566:                                        enclose( result, i.next(), control_chars.encloser, control_chars.sencloser );
1.297     moko      567:                                        if(i.has_next())
1.319     moko      568:                                                result<<*control_chars.sseparator;
1.297     moko      569:                                }
                    570:                        } else {
                    571:                                for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
                    572:                                        result<<*i.next();
                    573:                                        if(i.has_next())
1.319     moko      574:                                                result<<*control_chars.sseparator;
1.297     moko      575:                                }
                    576:                        }
                    577:                } else { // nameless table [we were asked to output column names]
                    578:                        if(int lsize=table.count()?table[0]->count():0)
                    579:                                for(int column=0; column<lsize; column++) {
1.319     moko      580:                                        if(control_chars.encloser) {
                    581:                                                result<<*control_chars.sencloser<<String::Body::Format(column)<<*control_chars.sencloser;
1.298     moko      582:                                        } else {
                    583:                                                result<<String::Body::Format(column);
                    584:                                        }
                    585:                                        if(column<lsize-1){
1.319     moko      586:                                                result<<*control_chars.sseparator;
1.298     moko      587:                                        }
1.297     moko      588:                                }
                    589:                        else
                    590:                                result.append_help_length("empty nameless table", 0, String::L_CLEAN);
                    591:                }
                    592:                result.append_know_length("\n", 1, String::L_CLEAN);
                    593:        }
                    594: 
                    595:        // data lines
                    596:        Array_iterator<ArrayString*> i(table);
1.319     moko      597:        if(control_chars.encloser){
1.297     moko      598:                while(i.has_next()) {
                    599:                        for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
1.319     moko      600:                                enclose( result, c.next(), control_chars.encloser, control_chars.sencloser );
1.297     moko      601:                                if(c.has_next())
1.319     moko      602:                                        result<<*control_chars.sseparator;
1.297     moko      603:                        }
                    604:                        result.append_know_length("\n", 1, String::L_CLEAN);
                    605:                }
                    606:        } else {
                    607:                while(i.has_next()) {
                    608:                        for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
                    609:                                result<<*c.next();
                    610:                                if(c.has_next())
1.319     moko      611:                                        result<<*control_chars.sseparator;
1.297     moko      612:                        }
                    613:                        result.append_know_length("\n", 1, String::L_CLEAN);
                    614:                }
                    615:        }
                    616: }
1.257     misha     617: #endif // don't use stringstream
1.241     misha     618: 
1.297     moko      619: 
1.241     misha     620: static void _save(Request& r, MethodParams& params) {
1.237     misha     621:        const String& first_arg=params.as_string(0, FIRST_ARG_MUST_NOT_BE_CODE);
1.188     paf       622:        size_t param_index=1;
                    623: 
                    624:        bool do_append=false;
                    625:        bool output_column_names=true;
                    626: 
                    627:        // mode?
                    628:        if(first_arg=="append")
                    629:                do_append=true;
                    630:        else if(first_arg=="nameless")
                    631:                output_column_names=false;
                    632:        else
                    633:                --param_index;
                    634: 
1.232     misha     635:        const String& file_name=params.as_string(param_index++, FILE_NAME_MUST_NOT_BE_CODE);
1.246     misha     636:        String file_spec=r.absolute(file_name);
                    637: 
                    638:        if(do_append && file_exist(file_spec))
                    639:                output_column_names=false;
1.188     paf       640: 
1.319     moko      641:        TableControlChars control_chars;
1.281     misha     642:        if(param_index<params.count())
1.288     misha     643:                if(HashStringValue* options=params.as_hash(param_index++)) {
1.319     moko      644:                        int valid_options=control_chars.load(*options);
1.281     misha     645:                        if(valid_options!=options->count())
                    646:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.222     paf       647:                }
1.281     misha     648: 
1.188     paf       649:        if(param_index<params.count())
1.318     moko      650:                throw Exception(PARSER_RUNTIME, 0, "bad mode (must be nameless or append)");
1.22      paf       651: 
1.182     paf       652:        Table& table=GET_SELF(r, VTable).table();
1.31      paf       653: 
1.297     moko      654: #ifdef USE_STRINGSTREAM
1.269     misha     655:        pa_stringstream ost(std::stringstream::out);
1.257     misha     656: 
1.319     moko      657:        table_to_csv(ost, table, control_chars, output_column_names);
1.31      paf       658: 
                    659:        // write
1.269     misha     660:                pa_string data=ost.str();
1.257     misha     661:                const char* data_cstr=data.c_str();
1.267     misha     662:                file_write(r.charsets, file_spec, data_cstr, data.length(), true /* as text */, do_append);
1.241     misha     663: #else
1.257     misha     664:        String sdata;
1.224     misha     665: 
1.319     moko      666:        table_to_csv(sdata, table, control_chars, output_column_names);
1.224     misha     667: 
                    668:        // write
1.257     misha     669:                const char* data_cstr=sdata.cstr();
1.297     moko      670:        file_write(r.charsets, file_spec, data_cstr, sdata.length(), true /* as text */, do_append);
1.257     misha     671:                if(*data_cstr) // not empty (when empty it's not heap memory)
                    672:                        pa_free((void*)data_cstr); // not needed anymore
1.297     moko      673: #endif
                    674: }
                    675: 
                    676: static void _csv_string(Request& r, MethodParams& params) {
                    677:        bool output_column_names=true;
                    678:        size_t param_index=0;
                    679:        if(params.count()>0 && params[0].is_string()) {
                    680:                if(params.as_string(0, FIRST_ARG_MUST_NOT_BE_CODE)=="nameless") {
                    681:                        output_column_names=false;
                    682:                        param_index++;
                    683:                } else {
1.318     moko      684:                        throw Exception(PARSER_RUNTIME, 0, "bad mode (must be nameless)");
1.297     moko      685:                }
1.224     misha     686:        }
1.240     misha     687: 
1.319     moko      688:        TableControlChars control_chars;
1.297     moko      689:        if(param_index<params.count())
                    690:                if(HashStringValue* options=params.as_hash(param_index++)) {
1.319     moko      691:                        int valid_options=control_chars.load(*options);
1.297     moko      692:                        if(valid_options!=options->count())
                    693:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    694:                }
                    695: 
                    696:        Table& table=GET_SELF(r, VTable).table();
                    697: 
                    698: #ifdef USE_STRINGSTREAM
                    699:        pa_stringstream ost(std::stringstream::out);
                    700: 
1.319     moko      701:        table_to_csv(ost, table, control_chars, output_column_names);
1.297     moko      702: 
1.338     moko      703:        r.write(*new VString(*new String(pa_strdup(ost.str().c_str()), String::L_CLEAN)));
1.297     moko      704: #else
                    705:        String sdata;
                    706: 
1.319     moko      707:        table_to_csv(sdata, table, control_chars, output_column_names);
1.297     moko      708: 
1.338     moko      709:        r.write(*new VString(*new String(sdata.cstr(), String::L_CLEAN)));
1.297     moko      710: #endif
1.224     misha     711: }
                    712: 
1.280     misha     713: static void _count(Request& r, MethodParams& params) {
                    714:        Table& table=GET_SELF(r, VTable).table();
                    715:        size_t result=0;
                    716:        if(params.count()) {
                    717:                const String& param=params.as_string(0, PARAMETER_MUST_BE_STRING);
                    718:                if(param == "columns")
1.310     moko      719:                        result = table.columns() ? table.columns()->count() : table.max_cells();
1.280     misha     720:                else if(param == "cells")
                    721:                        result = table.count() ? table[table.current()]->count() : 0;
                    722:                else if(param == "rows") // synonim for ^table.count[]
                    723:                        result = table.count();
                    724:                else
                    725:                        throw Exception(PARSER_RUNTIME, &param, "parameter must be 'columns', 'cells' and 'rows' only");
                    726:        } else
                    727:                result = table.count();
                    728: 
1.338     moko      729:        r.write(*new VInt(result));
1.6       paf       730: }
                    731: 
1.182     paf       732: static void _line(Request& r, MethodParams&) {
                    733:        int result=1+GET_SELF(r, VTable).table().current();
1.338     moko      734:        r.write(*new VInt(result));
1.6       paf       735: }
                    736: 
1.182     paf       737: static void _offset(Request& r, MethodParams& params) {
                    738:        Table& table=GET_SELF(r, VTable).table();
                    739:        if(params.count()) {
1.133     paf       740:                bool absolute=false;
1.182     paf       741:                if(params.count()>1) {
                    742:                    const String&  whence=params.as_string(0, "whence must be string");
1.133     paf       743:                    if(whence=="cur")
1.134     paf       744:                                absolute=false;
1.133     paf       745:                    else if(whence=="set")
1.134     paf       746:                                absolute=true;
1.133     paf       747:                    else
1.318     moko      748:                                throw Exception(PARSER_RUNTIME, &whence, "is invalid whence, valid are 'cur' or 'set'");
1.157     paf       749:                }
1.133     paf       750:                
1.211     paf       751:                int offset=params.as_int(params.count()-1, "offset must be expression", r);
                    752:                table.offset(absolute, offset);
1.151     paf       753:        } else
1.338     moko      754:                r.write(*new VInt(table.current()));
1.6       paf       755: }
                    756: 
1.182     paf       757: static void _menu(Request& r, MethodParams& params) {
1.263     misha     758:        InCycle temp(r);
1.221     paf       759: 
1.182     paf       760:        Value& body_code=params.as_junction(0, "body must be code");
1.7       paf       761:        
1.182     paf       762:        Value* delim_maybe_code=params.count()>1?&params[1]:0;
1.7       paf       763: 
1.182     paf       764:        Table& table=GET_SELF(r, VTable).table();
1.293     misha     765:        size_t saved_current=table.current();
1.7       paf       766: 
1.251     misha     767:        if(delim_maybe_code) { // delimiter set
1.250     misha     768:                bool need_delim=false;
1.316     moko      769:                for(size_t row=0; row<table.count(); row++) {
1.250     misha     770:                        table.set_current(row);
                    771: 
1.334     moko      772:                        Value& sv_processed=r.process(body_code);
1.341     moko      773:                        TempSkip4Delimiter skip(r);
1.250     misha     774: 
                    775:                        const String* s_processed=sv_processed.get_string();
1.251     misha     776:                        if(s_processed && !s_processed->is_empty()) { // we have body
1.250     misha     777:                                if(need_delim) // need delim & iteration produced string?
1.338     moko      778:                                        r.write(r.process(*delim_maybe_code));
1.250     misha     779:                                else
                    780:                                        need_delim=true;
                    781:                        }
                    782: 
1.338     moko      783:                        r.write(sv_processed);
1.242     misha     784: 
1.341     moko      785:                        if(skip.check_break())
1.250     misha     786:                                break;
                    787:                }
                    788:        } else {
1.316     moko      789:                for(size_t row=0; row<table.count(); row++) {
1.250     misha     790:                        table.set_current(row);
                    791:  
                    792:                        r.process_write(body_code);
                    793:  
1.341     moko      794:                        if(r.check_skip_break())
1.250     misha     795:                                break;
1.7       paf       796:                }
                    797:        }
1.99      parser    798:        table.set_current(saved_current);
1.7       paf       799: }
                    800: 
1.110     parser    801: #ifndef DOXYGEN
1.74      paf       802: struct Row_info {
1.166     paf       803:        Request *r;
1.74      paf       804:        Table *table;
1.182     paf       805:        Value* key_code;
                    806:        size_t key_field;
                    807:        Array<int>* value_fields;
1.315     moko      808:        Value* value_code;
1.182     paf       809:        HashStringValue* hash;
1.174     paf       810:        Table2hash_distint distinct;
1.182     paf       811:        size_t row;
1.227     misha     812:        Table2hash_value_type value_type;
1.74      paf       813: };
1.110     parser    814: #endif
1.182     paf       815: static void table_row_to_hash(Table::element_type row, Row_info *info) {
                    816:        const String* key;
                    817:        if(info->key_code) {
                    818:                info->table->set_current(info->row++); // change context row
1.334     moko      819:                Value& sv_processed=info->r->process(*info->key_code);
1.166     paf       820:                key=&sv_processed.as_string();
1.227     misha     821:        } else {
1.182     paf       822:                key=info->key_field<row->count()?row->get(info->key_field):0;
1.227     misha     823:        }
1.166     paf       824: 
                    825:        if(!key)
                    826:                return; // ignore rows without key [too-short-record_array if-indexed]
1.74      paf       827:                
1.243     misha     828:        bool exist=false;
                    829:        switch(info->value_type) {
                    830:                case C_STRING: {
1.317     moko      831:                        if(info->value_fields->count()){
                    832:                                size_t index=info->value_fields->get(0);
                    833:                                exist=info->hash->put_dont_replace(*key, (index < row->count()) ? new VString(*row->get(index)) : VString::empty());
                    834:                        } else {
                    835:                                exist=info->hash->put_dont_replace(*key, VString::empty());
                    836:                        }
1.243     misha     837:                        break;
1.227     misha     838:                }
1.243     misha     839:                case C_HASH: {
1.182     paf       840:                        VHash* vhash=new VHash;
                    841:                        HashStringValue& hash=vhash->hash();
                    842:                        for(Array_iterator<int> i(*info->value_fields); i.has_next(); ) {
                    843:                                size_t value_field=i.next();
                    844:                                if(value_field<row->count())
1.314     moko      845:                                        hash.put(*info->table->columns()->get(value_field), new VString(*row->get(value_field)));
1.174     paf       846:                        }
                    847: 
1.227     misha     848:                        exist=info->hash->put_dont_replace(*key, vhash);
1.243     misha     849:                        break;
1.174     paf       850:                }
1.243     misha     851:                case C_TABLE: {
                    852:                        VTable* vtable=(VTable*)info->hash->get(*key); // table exist?
1.174     paf       853:                        Table* table;
1.227     misha     854:                        if(vtable) {
1.243     misha     855:                                if(info->distinct==D_ILLEGAL) {
                    856:                                        exist=true;
                    857:                                        break;
                    858:                                }
1.174     paf       859:                                table=vtable->get_table();
1.227     misha     860:                        } else {
1.174     paf       861:                                // no? creating table of same structure as source
1.179     paf       862:                                Table::Action_options table_options(0, 0);
1.182     paf       863:                                table=new Table(*info->table, table_options/*no rows, just structure*/);
                    864:                                info->hash->put(*key, new VTable(table));
1.174     paf       865:                        }
1.308     moko      866:                        Table::element_type row_copy(new ArrayString(row->count()));
                    867:                        row_copy->append(*row);
                    868:                        *table+=row_copy;
1.243     misha     869:                        break;
1.174     paf       870:                }
1.315     moko      871:                case C_CODE: {
                    872:                        if(!info->key_code)
                    873:                                info->table->set_current(info->row++); // change context row
1.334     moko      874:                        exist=info->hash->put_dont_replace(*key, &info->r->process(*info->value_code));
1.315     moko      875:                        break;
                    876:                }
1.74      paf       877:        }
1.227     misha     878:        if(exist && info->distinct==D_ILLEGAL)
1.314     moko      879:                throw Exception(PARSER_RUNTIME, key, "duplicate key");
1.74      paf       880: }
1.235     misha     881: 
                    882: Table2hash_value_type get_value_type(Value& vvalue_type){
                    883:        if(vvalue_type.is_string()) {
                    884:                const String& svalue_type=*vvalue_type.get_string();
                    885:                if(svalue_type == "table"){
                    886:                        return C_TABLE;
                    887:                } else if (svalue_type == "string") {
                    888:                        return C_STRING;
                    889:                } else if (svalue_type == "hash") {
                    890:                        return C_HASH;
                    891:                } else {
1.314     moko      892:                        throw Exception(PARSER_RUNTIME, &svalue_type, "must be 'hash', 'table' or 'string'");
1.235     misha     893:                }
                    894:        } else {
1.314     moko      895:                throw Exception(PARSER_RUNTIME, 0, "'type' must be string");
1.235     misha     896:        }
                    897: }
                    898: 
1.315     moko      899: static Table2hash_distint get_distinct(Value& vdistinct, Table2hash_value_type& value_type){
                    900:        if(vdistinct.is_string()) {
                    901:                const String& sdistinct=*vdistinct.get_string();
                    902:                if(sdistinct!="tables")
                    903:                        throw Exception(PARSER_RUNTIME, &sdistinct, "must be 'tables' or true/false");
                    904:                value_type=C_TABLE;
                    905:                return D_FIRST;
                    906:        }
                    907:        return vdistinct.as_bool() ? D_FIRST : D_ILLEGAL;
                    908: }
                    909: 
1.182     paf       910: static void _hash(Request& r, MethodParams& params) {
                    911:        Table& self_table=GET_SELF(r, VTable).table();
                    912:        VHash& result=*new VHash;
1.238     misha     913:        if(Table::columns_type columns=self_table.columns()){
1.182     paf       914:                if(columns->count()>0) {
1.174     paf       915:                        Table2hash_distint distinct=D_ILLEGAL;
1.227     misha     916:                        Table2hash_value_type value_type=C_HASH;
1.182     paf       917:                        int param_index=params.count()-1;
1.166     paf       918:                        if(param_index>0) {
1.315     moko      919: 
                    920:                                if(params[1].get_junction())
                    921:                                        value_type=C_CODE;
                    922: 
                    923:                                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       924:                                        --param_index;
                    925:                                        int valid_options=0;
1.238     misha     926:                                        if(Value* vdistinct_code=options->get(sql_distinct_name)) { // $.distinct ?
1.166     paf       927:                                                valid_options++;
1.335     moko      928:                                                distinct=get_distinct(r.process(*vdistinct_code), value_type);
1.166     paf       929:                                        }
1.238     misha     930:                                        if(Value* vvalue_type_code=options->get(sql_value_type_name)) { // $.type ?
                    931:                                                if(value_type==C_TABLE) // $.distinct[tables] already was specified
1.314     moko      932:                                                        throw Exception(PARSER_RUNTIME, 0, "you can't specify $.distinct[tables] and $.type[] together");
1.315     moko      933:                                                if(value_type==C_CODE)
                    934:                                                        throw Exception(PARSER_RUNTIME, 0, "you can't specify $.type[] if value is code");
1.238     misha     935:                                                valid_options++;
1.335     moko      936:                                                value_type=get_value_type(r.process(*vvalue_type_code));
1.238     misha     937:                                        }
1.226     misha     938: 
1.182     paf       939:                                        if(valid_options!=options->count())
1.272     misha     940:                                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.163     paf       941:                                }
                    942:                        }
1.238     misha     943: 
1.315     moko      944:                        if(param_index==2) // options were specified but not as hash
1.289     misha     945:                                throw Exception(PARSER_RUNTIME, 0, "options must be hash");
                    946: 
1.182     paf       947:                        Array<int> value_fields;
1.315     moko      948:                        Value* value_code=0;
                    949: 
1.238     misha     950:                        if(param_index==0){ // list of columns wasn't specified
                    951:                                if(value_type==C_STRING) // $.type[string]
1.314     moko      952:                                        throw Exception(PARSER_RUNTIME, 0, "you must specify one value field with option $.type[string]");
1.238     misha     953:                                
                    954:                                for(size_t i=0; i<columns->count(); i++) // by all columns, including key
                    955:                                        value_fields+=i;
                    956: 
1.315     moko      957:                        } else { // list of columns or code was specified
1.227     misha     958:                                if(value_type==C_TABLE)
1.314     moko      959:                                        throw Exception(PARSER_RUNTIME, 0, "you can't specify value field(s) with option $.distinct[tables] or $.type[tables]");
1.238     misha     960: 
1.315     moko      961:                                Value& value_fields_param=params[1];
1.317     moko      962:                                if(value_fields_param.get_junction()){ // code specified
1.315     moko      963:                                        value_code=&value_fields_param;
                    964:                                } else if(value_fields_param.is_string()) { // one column as string was specified
1.317     moko      965:                                        const String &field_name=*value_fields_param.get_string();
                    966:                                        if(!field_name.is_empty())
                    967:                                                value_fields+=self_table.column_name2index(field_name, true);
1.238     misha     968:                                } else if(Table* value_fields_table=value_fields_param.get_table()) { // list of columns were specified in table
                    969:                                        for(Array_iterator<Table::element_type> i(*value_fields_table); i.has_next(); ) {
                    970:                                                const String& value_field_name =*i.next()->get(0);
                    971:                                                value_fields +=self_table.column_name2index(value_field_name, true);
1.123     parser    972:                                        }
                    973:                                } else
1.315     moko      974:                                        throw Exception(PARSER_RUNTIME, 0, "value field(s) must be string or table or code");
1.239     misha     975:                        }
1.74      paf       976: 
1.340     moko      977:                        if(value_type==C_STRING && value_fields.count()>1)
                    978:                                throw Exception(PARSER_RUNTIME, 0, "you can't specify more then one value field with option $.type[string]");
1.182     paf       979: 
                    980:                        {
                    981:                                Value* key_param=&params[0];
1.193     paf       982:                                Row_info info={
                    983:                                        &r,
                    984:                                        &self_table,
1.315     moko      985:                                        /*key_code=*/key_param->get_junction() ? key_param : 0,
1.197     paf       986:                                        /*key_field=*/0/*filled below*/,
1.193     paf       987:                                        &value_fields,
1.315     moko      988:                                        value_code,
1.193     paf       989:                                        &result.hash(),
                    990:                                        distinct,
1.227     misha     991:                                        /*row=*/0,
                    992:                                        value_type
1.193     paf       993:                                };
1.314     moko      994:                                info.key_field=(info.key_code ? -1 : self_table.column_name2index(key_param->as_string(), true));
1.182     paf       995: 
                    996:                                int saved_current=self_table.current();
                    997:                                self_table.for_each(table_row_to_hash, &info);
                    998:                                self_table.set_current(saved_current);
1.207     paf       999: 
                   1000:                                result.extract_default();
1.182     paf      1001:                        }
1.74      paf      1002:                }
1.238     misha    1003:        }
1.338     moko     1004:        r.write(result);
1.74      paf      1005: }
                   1006: 
1.110     parser   1007: #ifndef DOXYGEN
1.344     moko     1008: struct Table_seq_item : public PA_Allocated {
1.182     paf      1009:        ArrayString* row;
1.34      paf      1010:        union {
1.182     paf      1011:                const char *c_str;
1.34      paf      1012:                double d;
                   1013:        } value;
1.32      paf      1014: };
1.110     parser   1015: #endif
1.34      paf      1016: static int sort_cmp_string(const void *a, const void *b) {
                   1017:        return strcmp(
1.78      paf      1018:                static_cast<const Table_seq_item *>(a)->value.c_str, 
                   1019:                static_cast<const Table_seq_item *>(b)->value.c_str
1.34      paf      1020:        );
                   1021: }
                   1022: static int sort_cmp_double(const void *a, const void *b) {
1.78      paf      1023:        double va=static_cast<const Table_seq_item *>(a)->value.d;
                   1024:        double vb=static_cast<const Table_seq_item *>(b)->value.d;
1.34      paf      1025:        if(va<vb)
                   1026:                return -1;
                   1027:        else if(va>vb)
                   1028:                return +1;
                   1029:        else 
                   1030:                return 0;
                   1031: }
1.182     paf      1032: static void _sort(Request& r, MethodParams& params) {
                   1033:        Value& key_maker=params.as_junction(0, "key-maker must be code");
1.61      paf      1034: 
1.339     moko     1035:        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      1036: 
1.182     paf      1037:        Table& old_table=GET_SELF(r, VTable).table();
                   1038:        Table& new_table=*new Table(old_table.columns());
1.34      paf      1039: 
1.344     moko     1040:        Table_seq_item* seq=new Table_seq_item[old_table.count()];
1.34      paf      1041:        int i;
                   1042: 
                   1043:        // calculate key values
                   1044:        bool key_values_are_strings=true;
1.182     paf      1045:        int old_count=old_table.count();
                   1046:        for(i=0; i<old_count; i++) {
1.101     parser   1047:                old_table.set_current(i);
1.32      paf      1048:                // calculate key value
1.182     paf      1049:                seq[i].row=old_table[i];
1.335     moko     1050:                Value& value=r.process(key_maker);
1.34      paf      1051:                if(i==0) // determining key values type by first one
                   1052:                        key_values_are_strings=value.is_string();
                   1053: 
                   1054:                if(key_values_are_strings)
                   1055:                        seq[i].value.c_str=value.as_string().cstr();
                   1056:                else
1.287     moko     1057:                        seq[i].value.d=value.as_expr_result().as_double();
1.32      paf      1058:        }
1.265     misha    1059: 
                   1060:        // @todo: handle this elsewhere
                   1061:        if(r.charsets.source().NAME()=="KOI8-R" && key_values_are_strings) {
                   1062:                for(i=0; i<old_count; i++)
                   1063:                        if(*seq[i].value.c_str)
1.332     moko     1064:                                seq[i].value.c_str=Charset::transcode(seq[i].value.c_str, r.charsets.source(), pa_UTF8_charset).cstr();
1.265     misha    1065:        }
                   1066: 
1.266     misha    1067:        // sort keys
1.295     moko     1068:        qsort(seq, old_count, sizeof(Table_seq_item), key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32      paf      1069: 
1.34      paf      1070:        // reorder table as they require in 'seq'
1.182     paf      1071:        for(i=0; i<old_count; i++)
                   1072:                new_table+=Table::element_type(seq[reverse?old_count-1-i:i].row);
                   1073: 
                   1074:        delete[] seq;
1.32      paf      1075: 
1.116     parser   1076:        // replace any previous table value
1.182     paf      1077:        GET_SELF(r, VTable).set_table(new_table);
1.32      paf      1078: }
                   1079: 
1.176     paf      1080: #ifndef DOXYGEN
1.182     paf      1081: struct Expression_is_true_info {
1.176     paf      1082:        Request* r;
                   1083:        Value* expression_code;
                   1084: };
                   1085: #endif
1.275     misha    1086: 
1.191     paf      1087: static bool expression_is_true(Table&, Expression_is_true_info* info) {
1.335     moko     1088:        return info->r->process(*info->expression_code).as_bool();
1.176     paf      1089: }
1.275     misha    1090: 
                   1091: static bool _locate_expression(Table& table, Request& r, MethodParams& params) {
1.182     paf      1092:        Value& expression_code=params.as_junction(0, "must be expression");
1.303     moko     1093:        Table::Action_options o=get_action_options(r, params, 1, table);
                   1094:        if(params.count()>2)
                   1095:                throw Exception(PARSER_RUNTIME, 0, "locate by expression only has parameters: expression and, maybe, options");
1.182     paf      1096:        Expression_is_true_info info={&r, &expression_code};
                   1097:        return table.table_first_that(expression_is_true, &info, o);
1.145     paf      1098: }
1.275     misha    1099: 
                   1100: static bool _locate_name_value(Table& table, Request& r, MethodParams& params) {
1.182     paf      1101:        const String& name=params.as_string(0, "column name must be string");
1.232     misha    1102:        const String& value=params.as_string(1, VALUE_MUST_BE_STRING);
1.303     moko     1103:        Table::Action_options o=get_action_options(r, params, 2, table);
1.182     paf      1104:        return table.locate(name, value, o);
                   1105: }
1.275     misha    1106: 
1.182     paf      1107: static void _locate(Request& r, MethodParams& params) {
                   1108:        Table& table=GET_SELF(r, VTable).table();
1.72      paf      1109: 
1.291     moko     1110:        bool result=params[0].get_junction() || (params.count() == 1) ?
1.275     misha    1111:                _locate_expression(table, r, params) :
                   1112:                _locate_name_value(table, r, params);
1.338     moko     1113:        r.write(VBool::get(result));
1.145     paf      1114: }
1.182     paf      1115: 
                   1116: 
1.191     paf      1117: static void _flip(Request& r, MethodParams&) {
1.182     paf      1118:        Table& old_table=GET_SELF(r, VTable).table();
1.184     paf      1119:        Table& new_table=*new Table(0);
1.182     paf      1120:        if(size_t old_count=old_table.count())
1.310     moko     1121:                if(size_t old_cols=old_table.columns()?old_table.columns()->count():old_table.max_cells())
1.182     paf      1122:                        for(size_t column=0; column<old_cols; column++) {
                   1123:                                Table::element_type new_row(new ArrayString(old_count));
                   1124:                                for(size_t i=0; i<old_count; i++) {
                   1125:                                        Table::element_type old_row=old_table[i];
                   1126:                                        *new_row+=column<old_row->count()?old_row->get(column):new String;
1.39      paf      1127:                                }
1.182     paf      1128:                                new_table+=new_row;
1.39      paf      1129:                        }
                   1130: 
1.338     moko     1131:        r.write(*new VTable(&new_table));
1.39      paf      1132: }
                   1133: 
1.293     misha    1134: static void _foreach(Request& r, MethodParams& params) {
                   1135:        InCycle temp(r);
                   1136: 
1.331     moko     1137:        const String* rownum_var_name=&params.as_string(0, "rownum-var name must be string");
                   1138:        const String* value_var_name=&params.as_string(1, "value-var name must be string");
1.293     misha    1139: 
                   1140:        Value& body_code=params.as_junction(2, "body must be code");
                   1141:        
                   1142:        Value* delim_maybe_code=params.count()>3?&params[3]:0;
                   1143: 
                   1144:        Table& table=GET_SELF(r, VTable).table();
                   1145:        size_t saved_current=table.current();
                   1146: 
1.331     moko     1147:        rownum_var_name=rownum_var_name->is_empty()? 0 : rownum_var_name;
                   1148:        value_var_name=value_var_name->is_empty()? 0 : value_var_name;
1.293     misha    1149: 
                   1150:        Value* var_context=r.get_method_frame()->caller();
                   1151: 
                   1152:        if(delim_maybe_code) { // delimiter set
                   1153:                bool need_delim=false;
1.316     moko     1154:                for(size_t row=0; row<table.count(); row++) {
1.293     misha    1155:                        table.set_current(row);
                   1156: 
                   1157:                        if(rownum_var_name)
1.299     moko     1158:                                r.put_element(*var_context, *rownum_var_name, new VString(*new String(String::Body::Format(row), String::L_CLEAN)));
1.293     misha    1159:                        if(value_var_name)
1.299     moko     1160:                                r.put_element(*var_context, *value_var_name, new VTable(&table));
1.293     misha    1161: 
1.334     moko     1162:                        Value& sv_processed=r.process(body_code);
1.341     moko     1163:                        TempSkip4Delimiter skip(r);
1.293     misha    1164: 
                   1165:                        const String* s_processed=sv_processed.get_string();
                   1166:                        if(s_processed && !s_processed->is_empty()) { // we have body
                   1167:                                if(need_delim) // need delim & iteration produced string?
1.338     moko     1168:                                        r.write(r.process(*delim_maybe_code));
1.293     misha    1169:                                else
                   1170:                                        need_delim=true;
                   1171:                        }
                   1172: 
1.338     moko     1173:                        r.write(sv_processed);
1.293     misha    1174: 
1.341     moko     1175:                        if(skip.check_break())
1.293     misha    1176:                                break;
                   1177:                }
                   1178:        } else {
1.316     moko     1179:                for(size_t row=0; row<table.count(); row++) {
1.293     misha    1180:                        table.set_current(row);
                   1181:  
                   1182:                        if(rownum_var_name)
1.300     moko     1183:                                r.put_element(*var_context, *rownum_var_name, new VString(*new String(String::Body::Format(row), String::L_CLEAN)));
1.293     misha    1184:                        if(value_var_name)
1.300     moko     1185:                                r.put_element(*var_context, *value_var_name, new VTable(&table));
1.293     misha    1186: 
                   1187:                        r.process_write(body_code);
                   1188:  
1.341     moko     1189:                        if(r.check_skip_break())
1.293     misha    1190:                                break;
                   1191:                }
                   1192:        }
                   1193:        table.set_current(saved_current);
                   1194: }
                   1195: 
1.309     moko     1196: static void update_cell(HashStringValue::key_type aname, HashStringValue::value_type avalue, VTable *dest) {
                   1197:        dest->put_element(String(aname, String::L_CLEAN), avalue); // new not required
                   1198: }
                   1199: 
                   1200: inline Table::element_type row_from_string(Request& r, Value &param){
                   1201:        if(!param.is_string() && !param.get_junction())
                   1202:                throw Exception(PARSER_RUNTIME, 0, "row must be string, code or hash");
                   1203: 
                   1204:        const String& string=r.process_to_string(param);
1.41      paf      1205: 
                   1206:        // parse cells
1.182     paf      1207:        Table::element_type row=new ArrayString;
1.326     moko     1208:        string.split(*row, 0, "\t", String::L_AS_IS);
1.41      paf      1209: 
1.309     moko     1210:        return row;
1.41      paf      1211: }
                   1212: 
1.309     moko     1213: static void _append(Request& r, MethodParams& params) {
                   1214:        VTable vtable=GET_SELF(r, VTable);
                   1215:        Table& table=vtable.table();
1.306     moko     1216: 
1.309     moko     1217:        HashStringValue* hash=params[0].get_hash();
                   1218:        if(hash){
                   1219:                table+=new ArrayString();
                   1220:                size_t saved_current=table.current();
                   1221:                table.set_current(table.count()-1);
                   1222:                hash->for_each<VTable*>(update_cell, &vtable);
                   1223:                table.set_current(saved_current);
                   1224:        } else {
                   1225:                table+=row_from_string(r, params[0]);
                   1226:        }
                   1227: }
1.306     moko     1228: 
1.309     moko     1229: static void _insert(Request& r, MethodParams& params) {
                   1230:        VTable vtable=GET_SELF(r, VTable);
                   1231:        Table& table=vtable.table();
                   1232:        HashStringValue* hash=params[0].get_hash();
                   1233:        if(hash){
                   1234:                table.insert(table.current(), new ArrayString());
                   1235:                hash->for_each<VTable*>(update_cell, &vtable);
                   1236:        } else {
                   1237:                table.insert(table.current(), row_from_string(r, params[0]));
                   1238:        }
1.306     moko     1239: }
                   1240: 
1.311     moko     1241: static void _delete(Request& r, MethodParams&) {
1.307     moko     1242:        Table& table=GET_SELF(r, VTable).table();
                   1243:        table.remove_current();
                   1244: }
                   1245: 
1.182     paf      1246: static void join_named_row(Table& src, Table* dest) {
                   1247:        Table::columns_type dest_columns=dest->columns();
                   1248:        size_t dest_columns_count=dest_columns->count();
                   1249:        Table::element_type dest_row(new ArrayString(dest_columns_count));
                   1250:        for(size_t dest_column=0; dest_column<dest_columns_count; dest_column++) {
                   1251:                const String* src_item=src.item(*dest_columns->get(dest_column));
                   1252:                *dest_row+=src_item?src_item:new String;
                   1253:        }
                   1254:        *dest+=dest_row;
                   1255: }
                   1256: static void join_nameless_row(Table& src, Table* dest) {
                   1257:        *dest+=src[src.current()];
                   1258: }
                   1259: static void _join(Request& r, MethodParams& params) {
1.288     misha    1260:        Table& src=*params.as_table(0, "source");
1.176     paf      1261: 
1.268     misha    1262:        Table::Action_options o=get_action_options(r, params, 1, src);
1.42      paf      1263: 
1.182     paf      1264:        Table& dest=GET_SELF(r, VTable).table();
1.42      paf      1265:        if(&src == &dest)
1.318     moko     1266:                throw Exception(PARSER_RUNTIME, 0, "source and destination are same table");
1.42      paf      1267: 
1.193     paf      1268:        if(dest.columns()) // dest is named
1.182     paf      1269:                src.table_for_each(join_named_row, &dest, o);
                   1270:        else // dest is nameless
                   1271:                src.table_for_each(join_nameless_row, &dest, o);
1.42      paf      1272: }
                   1273: 
1.94      parser   1274: #ifndef DOXYGEN
1.169     paf      1275: class Table_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.182     paf      1276:        ArrayString& columns;
1.218     paf      1277:        int columns_count;
1.182     paf      1278:        ArrayString* row;
1.94      parser   1279: public:
1.182     paf      1280:        Table* table;
                   1281: public:
                   1282:        Table_sql_event_handlers() :
                   1283:                columns(*new ArrayString), row(0), table(0) {
1.94      parser   1284:        }
                   1285: 
1.279     misha    1286:        bool add_column(SQL_Error& error, const char *str, size_t ) {
1.170     paf      1287:                try {
1.274     moko     1288:                        columns+=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */);
1.170     paf      1289:                        return false;
                   1290:                } catch(...) {
1.343     moko     1291:                        error=SQL_Error("exception occurred in Table_sql_event_handlers::add_column");
1.170     paf      1292:                        return true;
                   1293:                }
                   1294:        }
1.348     moko     1295:        bool before_rows(SQL_Error& error) {
                   1296:                if(table) {
                   1297:                        error=SQL_Error("result must contain exactly one table");
                   1298:                        return true;
                   1299:                }
1.170     paf      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: