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

1.20      paf         1: /** @file
1.47      paf         2:        Parser: @b table parser class.
1.20      paf         3: 
1.1       paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.20      paf         5: 
1.1       paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: */
1.106   ! parser      8: static const char *RCSId="$Id: table.C,v 1.105 2001/08/21 11:12:20 parser Exp $"; 
1.1       paf         9: 
1.105     parser     10: #include "classes.h"
1.24      paf        11: #include "pa_config_includes.h"
1.16      paf        12: #include "pa_common.h"
1.1       paf        13: #include "pa_request.h"
                     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.1       paf        18: 
1.66      paf        19: // defines
1.1       paf        20: 
1.66      paf        21: #define TABLE_CLASS_NAME "table"
                     22: 
                     23: // class
                     24: 
                     25: class MTable : public Methoded {
                     26: public: // VStateless_class
                     27:        Value *create_new_value(Pool& pool) { return new(pool) VTable(pool); }
                     28: 
                     29: public:
                     30:        MTable(Pool& pool);
1.70      paf        31: 
                     32: public: // Methoded
1.66      paf        33:        bool used_directly() { return true; }
                     34: };
1.1       paf        35: 
                     36: // methods
1.66      paf        37: 
1.61      paf        38: static void _set(Request& r, const String& method_name, MethodParams *params) {
1.1       paf        39:        Pool& pool=r.pool();
                     40:        // data is last parameter
1.90      parser     41:        Value& vdata=params->as_junction(params->size()-1, "body must be code");
1.44      paf        42: 
1.45      paf        43:        Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.61      paf        44:        const String& data=r.process(vdata).as_string();
1.44      paf        45: 
                     46:        size_t pos_after=0;
                     47:        // parse columns
                     48:        Array *columns;
                     49:        if(params->size()==2) {
                     50:                columns=0;
1.21      paf        51:        } else {
1.44      paf        52:                columns=new(pool) Array(pool);
                     53: 
                     54:                Array head(pool);
                     55:                data.split(head, &pos_after, "\n", 1, String::UL_CLEAN, 1);
                     56:                if(head.size())
                     57:                        head.get_string(0)->split(*columns, 0, "\t", 1, String::UL_CLEAN);
                     58:        }
                     59: 
                     60:        Table& table=*new(pool) Table(pool, &method_name, columns);
                     61:        // parse cells
                     62:        Array rows(pool);
                     63:        data.split(rows, &pos_after, "\n", 1, String::UL_CLEAN);
1.98      parser     64:        Array_iter i(rows);
                     65:        while(i.has_next()) {
1.44      paf        66:                Array& row=*new(pool) Array(pool);
1.98      parser     67:                const String& string=*i.next_string();
1.44      paf        68:                // remove empty lines
                     69:                if(!string.size())
                     70:                        continue;
                     71: 
                     72:                string.split(row, 0, "\t", 1, String::UL_CLEAN);
                     73:                table+=&row;
1.17      paf        74:        }
1.1       paf        75: 
1.44      paf        76:        // replace any previous table value
                     77:        static_cast<VTable *>(r.self)->set_table(table);
                     78: }
                     79: 
1.61      paf        80: static void _load(Request& r, const String& method_name, MethodParams *params) {
1.44      paf        81:        Pool& pool=r.pool();
                     82:        // filename is last parameter
1.90      parser     83:        Value& vfilename=params->as_no_junction(params->size()-1, 
1.61      paf        84:                "file name must not be code");
1.44      paf        85: 
                     86:        // loading text
1.61      paf        87:        char *data=file_read_text(pool, r.absolute(vfilename.as_string()));
1.44      paf        88: 
1.1       paf        89:        // parse columns
                     90:        Array *columns;
                     91: #ifndef NO_STRING_ORIGIN
                     92:        const Origin& origin=method_name.origin();
1.2       paf        93:        const char *file=origin.file;
1.1       paf        94:        uint line=origin.line;
                     95: #endif
                     96:        if(params->size()==2) {
                     97:                columns=0;
                     98:        } else {
                     99:                columns=new(pool) Array(pool);
                    100: 
                    101:                if(char *row_chars=getrow(&data)) 
                    102:                        do {
                    103:                                String *name=new(pool) String(pool);
1.45      paf       104:                                name->APPEND_TAINTED(lsplit(&row_chars, '\t'), 0, file, line++);
1.1       paf       105:                                *columns+=name;
                    106:                        } while(row_chars);
                    107:        }
                    108: 
                    109:        // parse cells
1.27      paf       110:        Table& table=*new(pool) Table(pool, &method_name, columns);
1.1       paf       111:        char *row_chars;
                    112:        while(row_chars=getrow(&data)) {
1.28      paf       113:                if(!*row_chars) // remove empty lines
                    114:                        continue;
1.1       paf       115:                Array *row=new(pool) Array(pool);
                    116:                while(char *cell_chars=lsplit(&row_chars, '\t')) {
                    117:                        String *cell=new(pool) String(pool);
1.44      paf       118:                        cell->APPEND_TAINTED(cell_chars, 0, file, line);
1.1       paf       119:                        *row+=cell;
                    120:                }
                    121:                line++;
                    122:                table+=row;
                    123:        };
                    124: 
                    125:        // replace any previous table value
1.18      paf       126:        static_cast<VTable *>(r.self)->set_table(table);
1.1       paf       127: }
1.2       paf       128: 
1.91      parser    129: static Table *fill_month_days(Request& r, 
                    130:                                                          const String& method_name, MethodParams *params, bool rus){
                    131:        Pool& pool=r.pool();
                    132:        Table *result=new(pool) Table(pool, &method_name, 0/*&columns*/);
                    133: 
                    134:     int year=params->as_int(1, r);
                    135:     int month=max(1, min(params->as_int(2, r), 12)) -1;
                    136: 
                    137:     tm tmIn={0, 0, 0, 1, month, year-1900};
                    138:     time_t t=mktime(&tmIn);
                    139:        if(t<0)
                    140:                PTHROW(0, 0, 
                    141:                        &method_name, 
                    142:                        "invalid date");
                    143:     tm *tmOut=localtime(&t);
                    144: 
                    145:     int weekDay1=tmOut->tm_wday;
                    146:        if(rus) 
                    147:                weekDay1=weekDay1?weekDay1-1:6; //sunday last
                    148:     int monthDays=getMonthDays(year, month);
                    149:     
                    150:     for(int _day=1-weekDay1; _day<=monthDays;) {
                    151:                Array& row=*new(pool) Array(pool, 7);
                    152:        for(int wday=0; wday<7; wday++, _day++) {
1.93      parser    153:                String *cell=new(pool) String(pool);
1.91      parser    154:                        if(_day>=1 && _day<=monthDays) {
1.106   ! parser    155:                                char *buf=(char *)pool.malloc(2+1); 
1.93      parser    156:                                cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day), 
                    157:                                        method_name.origin().file, method_name.origin().line);
                    158:             }
1.91      parser    159:                        row+=cell;            
                    160:         }
                    161:        *result+=&row;
                    162:     }
                    163:     
                    164:     return result;
                    165: }
                    166: 
                    167: static Table *fill_week_days(Request& r, 
                    168:                                                         const String& method_name, MethodParams *params, bool rus){
                    169:        Pool& pool=r.pool();
                    170:        Array& columns=*new(pool) Array(pool, 4);
                    171:        columns+=new(pool) String(pool, "year");
                    172:        columns+=new(pool) String(pool, "month");
                    173:        columns+=new(pool) String(pool, "day");
                    174:        columns+=new(pool) String(pool, "weekday");
                    175:        Table *result=new(pool) Table(pool, &method_name, &columns);
                    176: 
                    177:     int year=params->as_int(1, r);
                    178:     int month=max(1, min(params->as_int(2, r), 12)) -1;
                    179:     int day=params->as_int(3, r);
                    180:     
                    181:     tm tmIn={0, 0, 18, day, month, year-1900};
                    182:     time_t t=mktime(&tmIn);
                    183:        if(t<0)
                    184:                PTHROW(0, 0, 
                    185:                        &method_name, 
                    186:                        "invalid date");
                    187:     tm *tmOut=localtime(&t);
                    188:     
                    189:     int baseWeekDay=tmOut->tm_wday;
                    190:        if(rus) 
                    191:                baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
                    192: 
                    193:     t-=baseWeekDay*SECS_PER_DAY;
                    194: 
                    195:     for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) {
                    196:         tm *tmOut=localtime(&t);
                    197:                Array& row=*new(pool) Array(pool, 4);
1.93      parser    198: #define WDFILL(size, value) { \
1.106   ! parser    199:                char *buf=(char *)pool.malloc(size+1); \
1.93      parser    200:                String *cell=new(pool) String(pool); \
                    201:                cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \
                    202:                        method_name.origin().file, \
                    203:                        method_name.origin().line); \
                    204:                row+=cell; \
                    205:                }
                    206:                WDFILL(4, 1900+tmOut->tm_year);
                    207:                WDFILL(2, 1+tmOut->tm_mon);
                    208:                WDFILL(2, tmOut->tm_mday);
                    209:                WDFILL(2, tmOut->tm_wday);
1.91      parser    210:         *result+=&row;
                    211:     }
                    212:     
                    213:     return result;
                    214: }
                    215: 
                    216: static void _calendar(Request& r, const String& method_name, MethodParams *params) {
                    217:        Pool& pool=r.pool();
                    218: 
1.92      parser    219:        const String& what=params->as_string(0, "format must be strig");
1.91      parser    220:        bool rus=false;
1.92      parser    221:        if(what=="rus")
1.91      parser    222:                rus=true;
1.92      parser    223:        else if(what=="eng")
1.91      parser    224:                rus=false;
                    225:        else
                    226:                PTHROW(0, 0, 
                    227:                        &what, 
                    228:                        "must be rus|eng");
                    229: 
                    230:        Table *result=0;
                    231:        if(params->size()==1+2) 
                    232:                result=fill_month_days(r, method_name, params, rus);
                    233:        else // 1+3
                    234:                result=fill_week_days(r, method_name, params, rus);
                    235: 
                    236:        // replace any previous table value
                    237:        static_cast<VTable *>(r.self)->set_table(*result);
                    238: }
                    239: 
                    240: 
                    241: 
                    242: 
1.61      paf       243: static void _save(Request& r, const String& method_name, MethodParams *params) {
1.22      paf       244:        Pool& pool=r.pool();
1.90      parser    245:        Value& vtable_name=params->as_no_junction(params->size()-1, 
1.61      paf       246:                "file name must not be code");
1.22      paf       247: 
1.31      paf       248:        Table& table=static_cast<VTable *>(r.self)->table();
                    249: 
                    250:        String sdata(pool);
                    251:        if(params->size()==1) { // not nameless=named output
                    252:                // write out names line
                    253:                if(table.columns()) { // named table
1.98      parser    254:                        Array_iter i(*table.columns());
                    255:                        while(i.has_next()) {
                    256:                                sdata.append(*i.next_string(), //*static_cast<String *>(table.columns()->quick_get(column)), 
                    257:                                        String::UL_TABLE);
                    258:                                if(i.has_next())
1.31      paf       259:                                        sdata.APPEND_CONST("\t");
                    260:                        }
                    261:                } else { // nameless table
1.98      parser    262:                        if(int lsize=table.size()?static_cast<Array *>(table.get(0))->size():0)
1.31      paf       263:                                for(int column=0; column<lsize; column++) {
1.106   ! parser    264:                                        char *cindex_tab=(char *)pool.malloc(MAX_NUMBER);
1.31      paf       265:                                        snprintf(cindex_tab, MAX_NUMBER, "%d\t", column);
                    266:                                        sdata.APPEND_CONST(cindex_tab);
                    267:                                }
                    268:                        else
                    269:                                sdata.APPEND_CONST("empty nameless table");
                    270:                }
                    271:                sdata.APPEND_CONST("\n");
                    272:        }
                    273:        // data lines
1.98      parser    274:        Array_iter i(table);
                    275:        while(i.has_next()) {
                    276:                Array_iter c(*static_cast<Array *>(i.next()));
                    277:                while(c.has_next()) {
                    278:                        sdata.append(*c.next_string(), //*static_cast<String *>(row->quick_get(column)), 
                    279:                                String::UL_TABLE);
                    280:                        if(c.has_next())
1.31      paf       281:                                sdata.APPEND_CONST("\t");
                    282:                }
                    283:                sdata.APPEND_CONST("\n");
                    284:        }
                    285: 
                    286:        // write
1.61      paf       287:        file_write(pool, r.absolute(vtable_name.as_string()), 
1.48      paf       288:                sdata.cstr(), sdata.size(), true);
1.22      paf       289: }
                    290: 
1.93      parser    291: static void _count(Request& r, const String& method_name, MethodParams *) {
1.6       paf       292:        Pool& pool=r.pool();
1.18      paf       293:        Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size());
1.93      parser    294:        value.set_name(method_name);
1.15      paf       295:        r.write_no_lang(value);
1.6       paf       296: }
                    297: 
1.61      paf       298: static void _line(Request& r, const String& method_name, MethodParams *) {
1.6       paf       299:        Pool& pool=r.pool();
1.37      paf       300:        Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().current());
1.93      parser    301:        value.set_name(method_name);
1.15      paf       302:        r.write_no_lang(value);
1.6       paf       303: }
                    304: 
1.61      paf       305: static void _offset(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       306:        Pool& pool=r.pool();
1.18      paf       307:        Table& table=static_cast<VTable *>(r.self)->table();
1.74      paf       308:        if(params->size()) {
1.90      parser    309:                Value& offset_expr=params->as_junction(0, "offset must be expression");
1.80      parser    310:                table.shift(r.process(offset_expr).as_int());
1.74      paf       311:        } else {
1.37      paf       312:                Value& value=*new(pool) VInt(pool, table.current());
1.93      parser    313:                value.set_name(method_name);
1.15      paf       314:                r.write_no_lang(value);
1.6       paf       315:        }
                    316: }
                    317: 
1.61      paf       318: static void _menu(Request& r, const String& method_name, MethodParams *params) {
1.90      parser    319:        Value& body_code=params->as_junction(0, "body must be code");
1.7       paf       320:        
1.61      paf       321:        Value *delim_code=params->size()==2?&params->get(1):0;
1.7       paf       322: 
1.64      paf       323:        VTable& vtable=*static_cast<VTable *>(r.self);
                    324:        Table& table=vtable.table();
1.7       paf       325:        bool need_delim=false;
1.99      parser    326:        int saved_current=table.current();
                    327:        int size=table.size();
                    328:        for(int row=0; row<size; row++) {
1.22      paf       329:                table.set_current(row);
1.7       paf       330: 
1.12      paf       331:                Value& processed_body=r.process(body_code);
1.7       paf       332:                if(delim_code) { // delimiter set?
                    333:                        const String *string=processed_body.get_string();
                    334:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                    335:                                r.write_pass_lang(r.process(*delim_code));
                    336:                        need_delim=true;
                    337:                }
                    338:                r.write_pass_lang(processed_body);
                    339:        }
1.99      parser    340:        table.set_current(saved_current);
1.7       paf       341: }
                    342: 
1.61      paf       343: static void _empty(Request& r, const String& method_name, MethodParams *params) {
1.75      paf       344:        Pool& pool=r.pool();
1.18      paf       345:        Table& table=static_cast<VTable *>(r.self)->table();
1.75      paf       346:        
                    347:        r.write_no_lang(*new(pool) VBool(pool, table.size()==0));
1.8       paf       348: }
1.15      paf       349: 
1.74      paf       350: /// used by table: _hash / table_row_to_hash
                    351: struct Row_info {
                    352:        Table *table;
                    353:        int key_field;
                    354:        Array *value_fields;
                    355:        Hash *hash;
                    356: };
                    357: static void table_row_to_hash(Array::Item *value, void *info) {
                    358:        Array& row=*static_cast<Array *>(value);
                    359:        Row_info& ri=*static_cast<Row_info *>(info);
                    360:        Pool& pool=ri.table->pool();
                    361: 
1.76      paf       362:        if(ri.key_field<row.size()) {
                    363:                VHash& result=*new(pool) VHash(pool);
                    364:                Hash& hash=*result.get_hash();
1.74      paf       365:                for(int i=0; i<ri.value_fields->size(); i++) {
                    366:                        int value_field=ri.value_fields->get_int(i);
                    367:                        if(value_field<row.size())
                    368:                                hash.put(
1.91      parser    369:                                        *ri.table->columns()->get_string(value_field), 
1.74      paf       370:                                        new(pool) VString(*row.get_string(value_field)));
                    371:                }
                    372:                
1.77      paf       373:                ri.hash->put(*row.get_string(ri.key_field), &result);
1.74      paf       374:        }
                    375: }
                    376: static void _hash(Request& r, const String& method_name, MethodParams *params) {
1.97      parser    377:        Pool& pool=r.pool();
1.74      paf       378:        Table& table=static_cast<VTable *>(r.self)->table();
1.97      parser    379:        Value& result=*new(pool) VHash(pool);
1.74      paf       380:        if(const Array *columns=table.columns()) 
                    381:                if(columns->size()>1) {
                    382: 
1.90      parser    383:                        const String& key_field_name=params->as_no_junction(0, 
1.74      paf       384:                                "key field name must not be code").as_string();
                    385:                        int key_field=table.column_name2index(key_field_name, true);
                    386:                        int value_fields_count=params->size()-1;
                    387:                        bool value_fields_by_params=value_fields_count!=0;
                    388:                        if(!value_fields_by_params)
                    389:                                value_fields_count=columns->size()-1; // all columns except key
                    390:                        Array value_fields(pool, value_fields_count);
                    391:                        if(value_fields_by_params) {
                    392:                                for(int i=1; i<params->size(); i++) {
1.91      parser    393:                                        const String& value_field_name=params->as_no_junction(i, 
1.74      paf       394:                                                "value field name must not be code").as_string();
                    395:                                        value_fields+=table.column_name2index(value_field_name, true);
                    396:                                }
                    397:                        } else { // by all columns except key
                    398:                                for(int i=0; i<columns->size(); i++)
                    399:                                        if(i!=key_field)
                    400:                                                value_fields+=i;
                    401:                        }
                    402: 
                    403:                        // integers: key_field & value_fields
                    404:                        Row_info row_info={&table, key_field, &value_fields, result.get_hash()};
                    405:                        table.for_each(table_row_to_hash, &row_info);
                    406:                }
1.97      parser    407:        result.set_name(method_name);
                    408:        r.write_no_lang(result);
1.74      paf       409: }
                    410: 
                    411: /// used by table: _sort / sort_cmp_string|sort_cmp_double
1.78      paf       412: struct Table_seq_item {
1.34      paf       413:        Array *row;
                    414:        union {
                    415:                char *c_str;
                    416:                double d;
                    417:        } value;
1.32      paf       418: };
1.34      paf       419: static int sort_cmp_string(const void *a, const void *b) {
                    420:        return strcmp(
1.78      paf       421:                static_cast<const Table_seq_item *>(a)->value.c_str, 
                    422:                static_cast<const Table_seq_item *>(b)->value.c_str
1.34      paf       423:        );
                    424: }
                    425: static int sort_cmp_double(const void *a, const void *b) {
1.78      paf       426:        double va=static_cast<const Table_seq_item *>(a)->value.d;
                    427:        double vb=static_cast<const Table_seq_item *>(b)->value.d;
1.34      paf       428:        if(va<vb)
                    429:                return -1;
                    430:        else if(va>vb)
                    431:                return +1;
                    432:        else 
                    433:                return 0;
                    434: }
1.61      paf       435: static void _sort(Request& r, const String& method_name, MethodParams *params) {
1.101     parser    436:        Pool& pool=r.pool();
1.90      parser    437:        Value& key_maker=params->as_junction(0, "key-maker must be code");
1.61      paf       438: 
1.102     parser    439:        bool reverse=params->size()==2/*..[desc|asc|]*/?
1.103     parser    440:                reverse=params->as_no_junction(1, "order must not be code").as_string()=="desc":
1.104     parser    441:                false; // default=asc
1.32      paf       442: 
1.101     parser    443:        Table& old_table=static_cast<VTable *>(r.self)->table();
                    444:        Table& new_table=*new(pool) Table(pool, &method_name, old_table.columns());
1.34      paf       445: 
1.101     parser    446:        Table_seq_item *seq=(Table_seq_item *)pool.malloc(sizeof(Table_seq_item)*old_table.size());
1.34      paf       447:        int i;
                    448: 
                    449:        // calculate key values
                    450:        bool key_values_are_strings=true;
1.101     parser    451:        // save 'current'
                    452:        int saved_current=old_table.current();
                    453:        for(i=0; i<old_table.size(); i++) {
                    454:                old_table.set_current(i);
1.32      paf       455:                // calculate key value
1.101     parser    456:                seq[i].row=(MethodParams *)old_table.get(i);
1.34      paf       457:                Value& value=*r.process(key_maker).as_expr_result(true/*return string as-is*/);
                    458:                if(i==0) // determining key values type by first one
                    459:                        key_values_are_strings=value.is_string();
                    460: 
                    461:                if(key_values_are_strings)
                    462:                        seq[i].value.c_str=value.as_string().cstr();
                    463:                else
                    464:                        seq[i].value.d=value.as_double();
1.32      paf       465:        }
1.101     parser    466:        // restore 'current'
                    467:        old_table.set_current(saved_current);
1.32      paf       468:        // sort keys
1.101     parser    469:        _qsort(seq, old_table.size(), sizeof(Table_seq_item), 
1.34      paf       470:                key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32      paf       471: 
1.34      paf       472:        // reorder table as they require in 'seq'
1.101     parser    473:        for(i=0; i<old_table.size(); i++)
                    474:                new_table+=seq[reverse?old_table.size()-1-i:i].row;
1.32      paf       475: 
1.101     parser    476:        r.write_no_lang(*new(pool) VTable(pool, &new_table));
1.32      paf       477: }
                    478: 
1.61      paf       479: static void _locate(Request& r, const String& method_name, MethodParams *params) {
1.72      paf       480:        Pool& pool=r.pool();
                    481: 
1.36      paf       482:        VTable& vtable=*static_cast<VTable *>(r.self);
                    483:        Table& table=vtable.table();
1.72      paf       484:        Value& result=*new(pool) VBool(pool, table.locate(
1.91      parser    485:                params->get(0).as_string(), 
1.72      paf       486:                params->get(1).as_string()));
                    487:        result.set_name(method_name);
                    488:        r.write_no_lang(result);
1.37      paf       489: }
                    490: 
1.61      paf       491: static void _flip(Request& r, const String& method_name, MethodParams *params) {
1.39      paf       492:        Pool& pool=r.pool();
                    493:        VTable& vtable=*static_cast<VTable *>(r.self);
                    494: 
                    495:        Table& old_table=*vtable.get_table();
                    496:        Table& new_table=*new(pool) Table(pool, &method_name, 0/*nameless*/);
                    497:        if(old_table.size())
                    498:                if(int old_cols=old_table.at(0).size()) 
                    499:                        for(int column=0; column<old_cols; column++) {
                    500:                                Array& new_row=*new(pool) Array(pool, old_table.size());
                    501:                                for(int i=0; i<old_table.size(); i++) {
                    502:                                        const Array& old_row=old_table.at(i);
                    503:                                        new_row+=column<old_row.size()?old_row.get(column):empty_string;
                    504:                                }
                    505:                                new_table+=&new_row;
                    506:                        }
                    507: 
1.99      parser    508:        r.write_no_lang(*new(pool) VTable(pool, &new_table));
1.39      paf       509: }
                    510: 
1.61      paf       511: static void _append(Request& r, const String& method_name, MethodParams *params) {
1.41      paf       512:        Pool& pool=r.pool();
                    513:        // data is last parameter
1.61      paf       514:        const String& string=
1.90      parser    515:                r.process(params->as_junction(0, "body must be code")).as_string();
1.41      paf       516: 
                    517:        // parse cells
                    518:        Array& row=*new(pool) Array(pool);
1.46      paf       519:        string.split(row, 0, "\t", 1, String::UL_CLEAN);
1.41      paf       520: 
1.66      paf       521:        VTable& vtable=*static_cast<VTable *>(r.self);
                    522:        vtable.table()+=&row;
1.41      paf       523: }
                    524: 
1.61      paf       525: static void _join(Request& r, const String& method_name, MethodParams *params) {
1.42      paf       526:        Pool& pool=r.pool();
                    527: 
1.90      parser    528:        Table *maybe_src=params->as_no_junction(0, "table ref must not be code").get_table();
1.42      paf       529:        if(!maybe_src)
1.91      parser    530:                PTHROW(0, 0, 
                    531:                        &method_name, 
1.42      paf       532:                        "source is not a table");
                    533: 
                    534:        Table& src=*maybe_src;
                    535:        Table& dest=static_cast<VTable *>(r.self)->table();
                    536:        if(&src == &dest)
1.91      parser    537:                PTHROW(0, 0, 
                    538:                        &method_name, 
1.42      paf       539:                        "source and destination are same table");
                    540: 
                    541:        if(const Array *dest_columns=dest.columns()) { // dest is named
                    542:                int saved_src_current=src.current();
                    543:                for(int src_row=0; src_row<src.size(); src_row++) {
                    544:                        src.set_current(src_row);
                    545:                        Array& dest_row=*new(pool) Array(pool);
                    546:                        for(int dest_column=0; dest_column<dest_columns->size(); dest_column++) 
                    547:                                dest_row+=src.item(*dest_columns->get_string(dest_column));
                    548:                        dest+=&dest_row;
                    549:                }
                    550:                src.set_current(saved_src_current);
                    551:        } else { // dest is nameless
                    552:                for(int src_row=0; src_row<src.size(); src_row++)
                    553:                        dest+=&src.at(src_row);
                    554:        }
                    555: }
                    556: 
1.94      parser    557: #ifndef DOXYGEN
                    558: class Table_sql_event_handlers : public SQL_Driver_query_event_handlers {
                    559: public:
                    560:        Table_sql_event_handlers(Pool& apool, const String& amethod_name,
                    561:                const String& astatement_string, const char *astatement_cstr) :
                    562:                pool(apool), 
                    563:                method_name(amethod_name),
                    564:                statement_string(astatement_string),
                    565:                statement_cstr(astatement_cstr),
                    566:                columns(*new(pool) Array(pool)),
                    567:                row(0), row_index(0),
                    568:                table(0)
                    569:        {
                    570:        }
                    571: 
                    572:        void add_column(void *ptr, size_t size) {
                    573:                String *column=new(pool) String(pool);
                    574:                column->APPEND_TAINTED(
                    575:                        (const char *)ptr, size, 
                    576:                        statement_cstr, 0);
                    577:                columns+=column;
                    578:        }
                    579:        void before_rows() { 
                    580:                table=new(pool) Table(pool, &method_name, &columns);
                    581:        }
                    582:        void add_row() {
                    583:                (*table)+=(row=new(pool) Array(pool));
                    584:        }
                    585:        void add_row_cell(void *ptr, size_t size) {
                    586:                String *cell=new(pool) String(pool);
                    587:                if(size)
                    588:                        cell->APPEND_TAINTED(
                    589:                                (const char *)ptr, size, 
                    590:                                statement_cstr, row_index++);
                    591:                (*row)+=cell;
                    592:        }
                    593: 
                    594: private:
                    595:        Pool& pool;
                    596:        const String& method_name;
                    597:        const String& statement_string; const char *statement_cstr;
                    598:        Array& columns;
                    599:        Array *row;
                    600:        uint row_index;
                    601: public:
                    602:        Table *table;
                    603: };
                    604: #endif
1.61      paf       605: static void _sql(Request& r, const String& method_name, MethodParams *params) {
1.49      paf       606:        Pool& pool=r.pool();
                    607: 
                    608:        if(!r.connection)
1.91      parser    609:                PTHROW(0, 0, 
                    610:                        &method_name, 
1.49      paf       611:                        "without connect");
                    612: 
1.90      parser    613:        Value& statement=params->as_junction(0, "statement must be code");
1.49      paf       614: 
1.53      paf       615:        ulong limit=0;
1.100     parser    616:        ulong offset=0;
1.49      paf       617:        if(params->size()>1) {
1.100     parser    618:                Value& options_param=params->as_no_junction(1, "options must be hash, not code");
                    619:                if(Hash *options=options_param.get_hash()) {
                    620:                        if(Value *vlimit=(Value *)options->get(*sql_limit_name))
                    621:                                limit=(ulong)r.process(*vlimit).as_double();
                    622:                        if(Value *voffset=(Value *)options->get(*sql_offset_name))
                    623:                                offset=(ulong)r.process(*voffset).as_double();
                    624:                } else
                    625:                        PTHROW(0, 0,
                    626:                                &method_name,
                    627:                                "options must be hash");
1.49      paf       628:        }
                    629: 
1.54      paf       630:        Temp_lang temp_lang(r, String::UL_SQL);
                    631:        const String& statement_string=r.process(statement).as_string();
1.55      paf       632:        const char *statement_cstr=
                    633:                statement_string.cstr(String::UL_UNSPECIFIED, r.connection);
1.94      parser    634:        Table_sql_event_handlers handlers(pool, method_name,
                    635:                statement_string, statement_cstr);
1.52      paf       636:        bool need_rethrow=false; Exception rethrow_me;
                    637:        PTRY {
                    638:                r.connection->query(
1.91      parser    639:                        statement_cstr, offset, limit, 
1.94      parser    640:                        handlers);
1.52      paf       641:        }
1.82      parser    642:        PCATCH(e) { // query problem
1.52      paf       643:                rethrow_me=e;  need_rethrow=true;
                    644:        }
                    645:        PEND_CATCH
                    646:        if(need_rethrow)
1.91      parser    647:                PTHROW(rethrow_me.type(), rethrow_me.code(), 
1.53      paf       648:                        &statement_string, // setting more specific source [were url]
1.52      paf       649:                        rethrow_me.comment());
1.96      parser    650: 
                    651:        Table *result=
                    652:                handlers.table?handlers.table: // query resulted in table? return it
                    653:                new(pool) Table(pool, &method_name, 0); // query returned no table, fake it
1.49      paf       654: 
                    655:        // replace any previous table value
1.96      parser    656:        static_cast<VTable *>(r.self)->set_table(*result);
1.49      paf       657: }
                    658: 
1.88      parser    659: static void _columns(Request& r, const String& method_name, MethodParams *) {
                    660:        Pool& pool=r.pool();
                    661: 
                    662:        Array& result_columns=*new(pool) Array(pool);
1.89      parser    663:        result_columns+=new(pool) String(pool, "column");
1.88      parser    664:        Table& result_table=*new(pool) Table(pool, &method_name, &result_columns);
                    665: 
                    666:        Table& source_table=static_cast<VTable *>(r.self)->table();
                    667:        if(const Array *source_columns=source_table.columns()) {
1.98      parser    668:                Array_iter i(*source_columns);
                    669:                while(i.has_next()) {
1.88      parser    670:                        Array& result_row=*new(pool) Array(pool);
1.98      parser    671:                        result_row+=i.next();
1.88      parser    672:                        result_table+=&result_row;
                    673:                }
                    674:        }
                    675: 
                    676:        VTable& result=*new(pool) VTable(pool, &result_table);
                    677:        result.set_name(method_name);
                    678:        r.write_no_lang(result);
                    679: }
                    680: 
1.66      paf       681: // constructor
                    682: 
                    683: MTable::MTable(Pool& apool) : Methoded(apool) {
                    684:        set_name(*NEW String(pool(), TABLE_CLASS_NAME));
                    685: 
1.49      paf       686:        // ^table:set{data}
                    687:        // ^table:set[nameless]{data}
1.66      paf       688:        add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2);
1.2       paf       689: 
1.49      paf       690:        // ^table:load[file]  
                    691:        // ^table:load[nameless;file]
1.66      paf       692:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.91      parser    693: 
                    694:        // ^table:calendar[month|montheng;year;month]  
                    695:        // ^table:calendar[week|weekeng;year;month;day]
                    696:        add_native_method("calendar", Method::CT_DYNAMIC, _calendar, 3, 4);
1.22      paf       697: 
                    698:        // ^table.save[file]  
                    699:        // ^table.save[nameless;file]
1.66      paf       700:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.6       paf       701: 
                    702:        // ^table.count[]
1.66      paf       703:        add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6       paf       704: 
                    705:        // ^table.line[]
1.66      paf       706:        add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6       paf       707: 
1.10      paf       708:        // ^table.offset[]  
                    709:        // ^table.offset[offset]
1.66      paf       710:        add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1);
1.7       paf       711: 
1.10      paf       712:        // ^table.menu{code}  
                    713:        // ^table.menu{code}[delim]
1.66      paf       714:        add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.8       paf       715: 
1.75      paf       716:        // ^table.empty[]
                    717:        add_native_method("empty", Method::CT_DYNAMIC, _empty, 0, 0);
1.74      paf       718: 
1.79      paf       719:        // ^table:hash[key field name]
                    720:        // ^table:hash[key field name][value field name;...]
1.74      paf       721:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 1000);
1.32      paf       722: 
1.102     parser    723:        // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc]
                    724:        // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc]
1.66      paf       725:        add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8       paf       726: 
1.36      paf       727:        // ^table.locate[field;value]
1.66      paf       728:        add_native_method("locate", Method::CT_DYNAMIC, _locate, 2, 2);
1.39      paf       729: 
                    730:        // ^table.flip[]
1.66      paf       731:        add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41      paf       732: 
                    733:        // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
1.66      paf       734:        add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42      paf       735: 
                    736:        // ^table.join[table]
1.66      paf       737:        add_native_method("join", Method::CT_DYNAMIC, _join, 1, 1);
1.49      paf       738: 
                    739: 
1.100     parser    740:        // ^table:sql[query]
                    741:        // ^table:sql[query][$.limit(1) $.offset(2)]
                    742:        add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.88      parser    743: 
                    744:        // ^table:columns[]
                    745:        add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 0);
1.66      paf       746: }
                    747: 
                    748: // global variable
                    749: 
                    750: Methoded *table_class;
                    751: 
                    752: // creator
                    753: 
                    754: Methoded *MTable_create(Pool& pool) {
                    755:        return table_class=new(pool) MTable(pool);
1.40      paf       756: }

E-mail: