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

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

E-mail: