Annotation of parser3/src/classes/table.C, revision 1.172.2.14.2.32
1.20 paf 1: /** @file
1.47 paf 2: Parser: @b table parser class.
1.20 paf 3:
1.172.2.2 paf 4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.144 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.154 paf 6: */
1.20 paf 7:
1.172.2.14.2. 2(paf 8:3): static const char* IDENT_TABLE_C="$Date: 2003/04/14 14:03:11 $";
1.1 paf 9:
1.105 parser 10: #include "classes.h"
1.172.2.4 paf 11: #include "pa_vmethod_frame.h"
12:
1.16 paf 13: #include "pa_common.h"
1.1 paf 14: #include "pa_request.h"
15: #include "pa_vtable.h"
1.6 paf 16: #include "pa_vint.h"
1.51 paf 17: #include "pa_sql_connection.h"
1.72 paf 18: #include "pa_vbool.h"
1.1 paf 19:
1.66 paf 20: // class
21:
1.172.2.9 paf 22: class MTable: public Methoded {
1.66 paf 23: public: // VStateless_class
1.172.2.14.2. (paf 24:): Value* create_new_value() { return new VTable(); }
1.66 paf 25:
26: public:
1.172.2.6 paf 27: MTable();
1.70 paf 28:
29: public: // Methoded
1.66 paf 30: bool used_directly() { return true; }
31: };
1.1 paf 32:
1.172.2.6 paf 33: // global variable
34:
1.172.2.11 paf 35: DECLARE_CLASS_VAR(table, new MTable, 0);
1.172.2.7 paf 36:
37: // defines for globals
38:
39: #define SQL_LIMIT_NAME "limit"
40: #define SQL_OFFSET_NAME "offset"
41: #define SQL_DEFAULT_NAME "default"
42: #define SQL_DISTINCT_NAME "distinct"
1.172.2.14.2. 0(paf 43:3): #define TABLE_REVERSE_NAME "reverse"
1.172.2.7 paf 44:
45: // globals
46:
1.172.2.14.2. 4(paf 47:3): String sql_limit_name(SQL_LIMIT_NAME);
48:3): String sql_offset_name(SQL_OFFSET_NAME);
49:3): String sql_default_name(SQL_DEFAULT_NAME);
50:3): String sql_distinct_name(SQL_DISTINCT_NAME);
0(paf 51:3): String table_reverse_name(TABLE_REVERSE_NAME);
1.172.2.6 paf 52:
1.1 paf 53: // methods
1.66 paf 54:
1.172.2.14.2. 0(paf 55:3): static Table::Action_options get_action_options(Request& r, MethodParams& params,
56:3): const Table& source) {
57:3): Table::Action_options result;
58:3): if(!params.count())
59:3): return result;
60:3):
1(paf 61:3): Value& maybe_options=params.last();
62:3): /* can not do it:
63:3): want to enable ^table::create[$source;
64:3): # $.option[]
65:3): ]
66:3): but there is ^table.locate[name;value]
0(paf 67:3):
1(paf 68:3): if(maybe_options.is_string()) { // allow empty options
69:3): result.defined=true;
70:3): return result;
71:3): }
72:3): */
73:3): HashStringValue* options=maybe_options.get_hash();
0(paf 74:3): if(!options)
75:3): return result;
76:3):
77:3): result.defined=true;
78:3): bool defined_offset=false;
79:3):
80:3): int valid_options=0;
81:3): if(Value* voffset=options->get(sql_offset_name)) {
82:3): valid_options++;
83:3): defined_offset=true;
84:3): if(voffset->is_string()) {
85:3): const String& soffset=*voffset->get_string();
86:3): if(soffset == "cur")
87:3): result.offset=source.current();
88:3): else
1.164 paf 89: throw Exception("parser.runtime",
1.172.2.14.2. 0(paf 90:3): &soffset,
91:3): "must be 'cur' string or expression");
92:3): } else
93:3): result.offset=r.process_to_value(*voffset).as_int();
94:3): }
95:3): if(Value* vlimit=options->get(sql_limit_name)) {
96:3): valid_options++;
97:3): result.limit=r.process_to_value(*vlimit).as_int();
98:3): }
99:3): if(Value *vreverse=(Value *)options->get(table_reverse_name)) {
100:3): valid_options++;
101:3): result.reverse=r.process_to_value(*vreverse).as_bool();
102:3): if(result.reverse && !defined_offset)
103:3): result.offset=source.count()-1;
104:3): }
105:3):
106:3): if(valid_options!=options->count())
107:3): throw Exception("parser.runtime",
108:3): 0,
109:3): "called with invalid option");
110:3):
111:3): return result;
112:3): }
2(paf 113:3): static void check_option_param(bool options_defined,
0(paf 114:3): MethodParams& params, size_t next_param_index,
115:3): const char *msg) {
116:3): if(next_param_index+(options_defined?1:0) != params.count())
117:3): throw Exception("parser.runtime",
118:3): 0,
119:3): "%s", msg);
1.157 paf 120: }
121:
1.172.2.14.2. 4(paf 122:3): static void _create(Request& r, MethodParams& params) {
1.157 paf 123: // clone/copy part?
1.172.2.14.2. 5(paf 124:3): if(Table *source=params[0].get_table()) {
0(paf 125:3): Table::Action_options o=get_action_options(r, params, *source);
126:3): check_option_param(o.defined, params, 1,
127:3): "too many parameters");
128:3): GET_SELF(r, VTable).set_table(*new Table(*source, o));
1.157 paf 129: return;
130: }
1.142 paf 131:
1.1 paf 132: // data is last parameter
1.172.2.14.2. (paf 133:): Temp_lang temp_lang(r, String::L_PASS_APPENDED);
134:): const String& data=
4(paf 135:3): r.process_to_string(params.as_junction(params.count()-1, "body must be code"));
1.44 paf 136:
137: // parse columns
1.172.2.14.2. 7(paf 138:3): size_t raw_pos_after=0;
1.172.2.6 paf 139: Table::columns_type columns;
1.172.2.14.2. 4(paf 140:3): if(params.count()==2) {
1.172.2.6 paf 141: columns=Table::columns_type(0); // nameless
1.21 paf 142: } else {
1.172.2.6 paf 143: columns=Table::columns_type(new ArrayString);
1.44 paf 144:
1.172.2.6 paf 145: ArrayString head;
1.172.2.14.2. 7(paf 146:3): data.split(head, raw_pos_after, "\n", String::L_AS_IS, 1);
3(paf 147:3): if(head.count()) {
7(paf 148:3): size_t col_pos_after=0;
149:3): head[0]->split(*columns, col_pos_after, "\t", String::L_AS_IS);
3(paf 150:3): }
1.44 paf 151: }
152:
1.172.2.14.2. 3(paf 153:3): Table& table=*new Table(columns);
1.44 paf 154: // parse cells
1.172.2.6 paf 155:
156: ArrayString rows;
1.172.2.14.2. 7(paf 157:3): data.split(rows, raw_pos_after, "\n", String::L_AS_IS);
3(paf 158:3): Array_iterator<const String*> i(rows);
1.98 parser 159: while(i.has_next()) {
1.172.2.6 paf 160: Table::element_type row(new ArrayString);
1.172.2.14.2. 3(paf 161:3): const String& string=*i.next();
1.118 parser 162: // remove comment lines
1.172.2.14.2. 3(paf 163:3): if(!string.length())
1.44 paf 164: continue;
165:
1.172.2.14.2. 7(paf 166:3): size_t col_pos_after=0;
167:3): string.split(*row, col_pos_after, "\t", String::L_AS_IS);
3(paf 168:3): table+=row;
1.17 paf 169: }
1.1 paf 170:
1.44 paf 171: // replace any previous table value
1.172.2.6 paf 172: GET_SELF(r, VTable).set_table(table);
1.44 paf 173: }
174:
1.172.2.14.2. 4(paf 175:3): static void _load(Request& r, MethodParams& params) {
176:3): const String& first_param=params.as_string(0, "file name must be string");
1.168 paf 177: int filename_param_index=0;
1.172.2.14.2. 3(paf 178:3): bool nameless=first_param=="nameless";
1.168 paf 179: if(nameless)
180: filename_param_index++;
1.172.2.14.2. 8(paf 181:3): size_t options_param_index=filename_param_index+1;
1.168 paf 182:
1.44 paf 183: // loading text
1.172.2.14.2. 7(paf 184:3): char *data=file_read_text(r.charsets,
4(paf 185:3): r.absolute(params.as_string(filename_param_index, "file name must be string")),
1.168 paf 186: true,
1.172.2.14.2. 4(paf 187:3): options_param_index<params.count()?params.as_no_junction(options_param_index, "additional params must be hash").get_hash():0
1.168 paf 188: );
1.44 paf 189:
1.1 paf 190: // parse columns
1.172.2.6 paf 191: Table::columns_type columns;
1.168 paf 192: if(nameless) {
1.172.2.6 paf 193: columns=Table::columns_type(0); // nameless
1.1 paf 194: } else {
1.172.2.6 paf 195: columns=Table::columns_type(new ArrayString);
1.1 paf 196:
1.139 paf 197: while(char *row_chars=getrow(&data)) {
198: // remove empty&comment lines
199: if(!*row_chars || *row_chars == '#')
200: continue;
1.1 paf 201: do {
1.172.2.14.2. 3(paf 202:3): *columns+=new String(lsplit(&row_chars, '\t'), 0, true);
1.1 paf 203: } while(row_chars);
1.139 paf 204:
205: break;
206: }
1.1 paf 207: }
208:
209: // parse cells
1.172.2.14.2. (paf 210:): Table& table=*new Table(columns);
1.1 paf 211: char *row_chars;
212: while(row_chars=getrow(&data)) {
1.118 parser 213: // remove empty&comment lines
214: if(!*row_chars || *row_chars == '#')
1.28 paf 215: continue;
1.172.2.6 paf 216: Table::element_type row(new ArrayString);
1.1 paf 217: while(char *cell_chars=lsplit(&row_chars, '\t')) {
1.172.2.14.2. (paf 218:): *row+=new String(cell_chars, 0, true);
1.1 paf 219: }
1.172.2.14.2. (paf 220:): table+=row;
1.1 paf 221: };
222:
223: // replace any previous table value
1.172.2.6 paf 224: GET_SELF(r, VTable).set_table(table);
1.1 paf 225: }
1.2 paf 226:
1.146 paf 227: /// @todo "x\nx" "xxx""xx"
1.172.2.14.2. 4(paf 228:3): static void _save(Request& r, MethodParams& params) {
229:3): Value& vfile_name=params.as_no_junction(params.count()-1, "file name must not be code");
1.22 paf 230:
1.172.2.14.2. (paf 231:): Table& table=GET_SELF(r, VTable).table();
1.31 paf 232:
1.129 paf 233: bool do_append=false;
1.172.2.6 paf 234: String sdata;
1.172.2.14.2. 4(paf 235:3): if(params.count()==1) { // named output
1.31 paf 236: // write out names line
237: if(table.columns()) { // named table
1.172.2.14.2. 9(paf 238:3): for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
(paf 239:): sdata.append(*i.next(), String::L_TABLE);
1.98 parser 240: if(i.has_next())
1.172.2.14.2. 6(paf 241:3): sdata.append_know_length("\t", 1, String::L_CLEAN);
1.31 paf 242: }
243: } else { // nameless table
1.172.2.6 paf 244: if(int lsize=table.count()?table[0]->count():0)
1.31 paf 245: for(int column=0; column<lsize; column++) {
1.172.2.14.2. 5(paf 246:3): char *cindex_tab=new(PointerFreeGC) char[MAX_NUMBER];
6(paf 247:3): sdata.append_know_length(cindex_tab,
(paf 248:): snprintf(cindex_tab, MAX_NUMBER, "%d\t", column),
249:): String::L_CLEAN);
1.31 paf 250: }
251: else
1.172.2.14.2. 0(paf 252:3): sdata.append_help_length("empty nameless table", 0, String::L_CLEAN);
1.31 paf 253: }
1.172.2.14.2. 6(paf 254:3): sdata.append_know_length("\n", 1, String::L_CLEAN);
1.129 paf 255: } else { // mode specified
1.172.2.14.2. 4(paf 256:3): const String& mode=params.as_string(0, "mode must be string");
3(paf 257:3): if(mode=="append")
1.129 paf 258: do_append=true;
1.172.2.14.2. 3(paf 259:3): else if(mode=="nameless")
1.129 paf 260: /*ok, already skipped names output*/;
261: else
1.146 paf 262: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 263:3): &mode,
1.129 paf 264: "unknown mode, must be 'append'");
265:
1.31 paf 266: }
267: // data lines
1.172.2.14.2. (paf 268:): Array_iterator<ArrayString*> i(table);
1.98 parser 269: while(i.has_next()) {
1.172.2.14.2. 9(paf 270:3): for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
271:3): sdata.append(*c.next(), String::L_TABLE);
1.98 parser 272: if(c.has_next())
1.172.2.14.2. 6(paf 273:3): sdata.append_know_length("\t", 1, String::L_CLEAN);
1.31 paf 274: }
1.172.2.14.2. 6(paf 275:3): sdata.append_know_length("\n", 1, String::L_CLEAN);
1.31 paf 276: }
277:
278: // write
1.172.2.14.2. (paf 279:): file_write(r.absolute(vfile_name.as_string()),
280:): sdata.cstr(), sdata.length(), true, do_append);
1.22 paf 281: }
282:
1.172.2.14.2. 4(paf 283:3): static void _count(Request& r, MethodParams&) {
(paf 284:): int result=GET_SELF(r, VTable).table().count();
3(paf 285:3): r.write_no_lang(*new VInt(result));
1.6 paf 286: }
287:
1.172.2.14.2. 4(paf 288:3): static void _line(Request& r, MethodParams&) {
(paf 289:): int result=1+GET_SELF(r, VTable).table().current();
3(paf 290:3): r.write_no_lang(*new VInt(result));
1.6 paf 291: }
292:
1.172.2.14.2. 4(paf 293:3): static void _offset(Request& r, MethodParams& params) {
(paf 294:): Table& table=GET_SELF(r, VTable).table();
4(paf 295:3): if(params.count()) {
1.133 paf 296: bool absolute=false;
1.172.2.14.2. 4(paf 297:3): if(params.count()>1) {
298:3): const String& whence=params.as_string(0, "whence must be string");
3(paf 299:3): if(whence=="cur")
1.134 paf 300: absolute=false;
1.172.2.14.2. 3(paf 301:3): else if(whence=="set")
1.134 paf 302: absolute=true;
1.133 paf 303: else
1.146 paf 304: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 305:3): &whence,
1.134 paf 306: "is invalid whence, valid are 'cur' or 'set'");
1.157 paf 307: }
1.133 paf 308:
1.172.2.14.2. 4(paf 309:3): Value& offset_expr=params.as_junction(params.count()-1, "offset must be expression");
0(paf 310:3): table.offset(absolute, r.process_to_value(offset_expr).as_int());
1.151 paf 311: } else
1.172.2.14.2. 0(paf 312:3): r.write_no_lang(*new VInt(table.current()));
1.6 paf 313: }
314:
1.172.2.14.2. 4(paf 315:3): static void _menu(Request& r, MethodParams& params) {
316:3): Value& body_code=params.as_junction(0, "body must be code");
1.7 paf 317:
1.172.2.14.2. 5(paf 318:3): Value* delim_maybe_code=params.count()>1?¶ms[1]:0;
1.7 paf 319:
1.172.2.14.2. (paf 320:): Table& table=GET_SELF(r, VTable).table();
1.7 paf 321: bool need_delim=false;
1.99 parser 322: int saved_current=table.current();
1.172.2.6 paf 323: int size=table.count();
1.99 parser 324: for(int row=0; row<size; row++) {
1.22 paf 325: table.set_current(row);
1.7 paf 326:
1.161 paf 327: StringOrValue sv_processed=r.process(body_code);
1.172.2.14.2. 3(paf 328:3): const String* s_processed=sv_processed.get_string();
(paf 329:): if(delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body
1.161 paf 330: if(need_delim) // need delim & iteration produced string?
1.172.2.14.2. 3(paf 331:3): r.write_pass_lang(r.process(*delim_maybe_code));
1.7 paf 332: need_delim=true;
333: }
1.161 paf 334: r.write_pass_lang(sv_processed);
1.7 paf 335: }
1.99 parser 336: table.set_current(saved_current);
1.7 paf 337: }
338:
1.110 parser 339: #ifndef DOXYGEN
1.172.2.14.2. 8(paf 340:3): enum Table2hash_distint { D_ILLEGAL, D_FIRST, D_TABLES };
1.74 paf 341: struct Row_info {
1.166 paf 342: Request *r;
1.74 paf 343: Table *table;
1.172.2.14.2. (paf 344:): Value* key_code;
8(paf 345:3): size_t key_field;
1.172.2.6 paf 346: Array<int>* value_fields;
347: HashStringValue* hash;
1.172.2.14.2. 8(paf 348:3): Table2hash_distint distinct;
349:3): size_t row;
1.74 paf 350: };
1.110 parser 351: #endif
1.172.2.6 paf 352: static void table_row_to_hash(Table::element_type row, Row_info *info) {
1.172.2.14.2. 3(paf 353:3): const String* key;
1.172.2.6 paf 354: if(info->key_code) {
355: info->table->set_current(info->row++); // change context row
1.172.2.14.2. 3(paf 356:3): StringOrValue sv_processed=info->r->process(*info->key_code);
357:3): key=&sv_processed.as_string();
1.166 paf 358: } else
1.172.2.14.2. (paf 359:): key=info->key_field<row->count()?row->get(info->key_field):0;
1.166 paf 360:
361: if(!key)
362: return; // ignore rows without key [too-short-record_array if-indexed]
1.74 paf 363:
1.172.2.14.2. 8(paf 364:3): switch(info->distinct) {
365:3): case D_ILLEGAL: case D_FIRST:
366:3): {
367:3): VHash* vhash=new VHash;
368:3): HashStringValue& hash=vhash->hash();
369:3): for(Array_iterator<int> i(*info->value_fields); i.has_next(); ) {
370:3): size_t value_field=i.next();
371:3): if(value_field<row->count())
372:3): hash.put(
373:3): *info->table->columns()->get(value_field),
374:3): new VString(*row->get(value_field)));
375:3): }
1.166 paf 376:
1.172.2.14.2. 8(paf 377:3): if(info->hash->put_dont_replace(*key, vhash)) // put. existed?
378:3): if(info->distinct==D_ILLEGAL)
379:3): throw Exception("parser.runtime",
380:3): key,
381:3): "duplicate key");
382:3): }
383:3): break;
384:3): case D_TABLES:
385:3): {
386:3): VTable* vtable=(VTable*)info->hash->get(*key); // put. table existed?
387:3): Table* table;
388:3): if(vtable)
389:3): table=vtable->get_table();
390:3): else {
391:3): // no? creating table of same structure as source
1(paf 392:3): Table::Action_options table_options(0, 0);
0(paf 393:3): table=new Table(*info->table, table_options/*no rows, just structure*/);
8(paf 394:3): info->hash->put(*key, new VTable(table));
395:3): }
396:3): *table+=row;
397:3): }
398:3): break;
399:3): default:
400:3): throw Exception(0,
401:3): 0,
9(paf 402:3): "invalid distinct code (#%d)", info->distinct);
8(paf 403:3): }
1.74 paf 404: }
1.172.2.14.2. 4(paf 405:3): static void _hash(Request& r, MethodParams& params) {
(paf 406:): Table& self_table=GET_SELF(r, VTable).table();
3(paf 407:3): VHash& result=*new VHash;
1.172.2.6 paf 408: if(Table::columns_type columns=self_table.columns())
409: if(columns->count()>0) {
1.172.2.14.2. 8(paf 410:3): Table2hash_distint distinct=D_ILLEGAL;
4(paf 411:3): int param_index=params.count()-1;
1.166 paf 412: if(param_index>0) {
1.172.2.6 paf 413: if(HashStringValue* options=
1.172.2.14.2. 4(paf 414:3): params.as_no_junction(param_index, "param must not be code").get_hash()) {
1.166 paf 415: --param_index;
416: int valid_options=0;
1.172.2.14.2. 8(paf 417:3): if(Value* vdistinct_code=options->get(sql_distinct_name)) {
1.166 paf 418: valid_options++;
1.172.2.14.2. 8(paf 419:3): Value& vdistinct_value=r.process_to_value(*vdistinct_code);
420:3): if(vdistinct_value.is_string()) {
421:3): const String& sdistinct=*vdistinct_value.get_string();
422:3): if(sdistinct=="tables")
423:3): distinct=D_TABLES;
424:3): else
425:3): throw Exception("parser.runtime",
426:3): &sdistinct,
427:3): "must be 'tables' or true/false");
428:3): } else
429:3): distinct=vdistinct_value.as_bool()?D_FIRST:D_ILLEGAL;
1.166 paf 430: }
1.172.2.6 paf 431: if(valid_options!=options->count())
1.166 paf 432: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 433:3): 0,
1.166 paf 434: "called with invalid option");
1.163 paf 435: }
436: }
437: if(param_index==2) // bad options param type
438: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 439:3): 0,
1.163 paf 440: "options must be hash");
441:
1.172.2.6 paf 442: Array<int> value_fields;
1.163 paf 443: if(param_index>0) {
1.172.2.14.2. 8(paf 444:3): if(distinct!=D_ILLEGAL && distinct!=D_FIRST)
445:3): throw Exception("parser.runtime",
446:3): 0,
447:3): "in distinct[tables] mode you may not specify value field(s)");
4(paf 448:3): Value& value_fields_param=params.as_no_junction(param_index, "value field(s) must not be code");
3(paf 449:3): if(value_fields_param.is_string()) {
1.172.2.6 paf 450: value_fields+=self_table.column_name2index(
1.172.2.14.2. 3(paf 451:3): *value_fields_param.get_string(), true);
452:3): } else if(Table* value_fields_table=value_fields_param.get_table()) {
1(paf 453:3): for(Array_iterator<Table::element_type> i(*value_fields_table);
454:3): i.has_next(); ) {
455:3): const String& value_field_name
456:3): =*i.next()->get(0);
457:3): value_fields
458:3): +=self_table.column_name2index(value_field_name, true);
1.123 parser 459: }
460: } else
1.146 paf 461: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 462:3): 0,
1.123 parser 463: "value field(s) must be string or self_table"
464: );
465: } else { // by all columns, including key
1.172.2.14.2. 8(paf 466:3): if(!(distinct!=D_ILLEGAL && distinct!=D_FIRST))
467:3): for(size_t i=0; i<columns->count(); i++)
468:3): value_fields+=i;
1.74 paf 469: }
470:
1.172.2.6 paf 471:
472: {
473: Row_info info;
474: info.r=&r;
475: info.table=&self_table;
1.172.2.14.2. 5(paf 476:3): Value* key_param=¶ms[0];
(paf 477:): info.key_code=key_param->get_junction()?key_param:0;
1.172.2.6 paf 478: info.key_field=info.key_code?-1
1.172.2.14.2. (paf 479:): :self_table.column_name2index(key_param->as_string(), true);
1.172.2.6 paf 480: info.value_fields=&value_fields;
1.172.2.14.2. 2(paf 481:3): info.hash=&result.hash();
1.172.2.6 paf 482: info.distinct=distinct;
1.172.2.12 paf 483: info.row=0;
1.172.2.6 paf 484:
485: int saved_current=self_table.current();
486: self_table.for_each(table_row_to_hash, &info);
487: self_table.set_current(saved_current);
488: }
1.74 paf 489: }
1.97 parser 490: r.write_no_lang(result);
1.74 paf 491: }
492:
1.110 parser 493: #ifndef DOXYGEN
1.78 paf 494: struct Table_seq_item {
1.172.2.13 paf 495: ArrayString* row;
1.34 paf 496: union {
1.172.2.14.2. 3(paf 497:3): const char *c_str;
1.34 paf 498: double d;
499: } value;
1.32 paf 500: };
1.110 parser 501: #endif
1.34 paf 502: static int sort_cmp_string(const void *a, const void *b) {
503: return strcmp(
1.78 paf 504: static_cast<const Table_seq_item *>(a)->value.c_str,
505: static_cast<const Table_seq_item *>(b)->value.c_str
1.34 paf 506: );
507: }
508: static int sort_cmp_double(const void *a, const void *b) {
1.78 paf 509: double va=static_cast<const Table_seq_item *>(a)->value.d;
510: double vb=static_cast<const Table_seq_item *>(b)->value.d;
1.34 paf 511: if(va<vb)
512: return -1;
513: else if(va>vb)
514: return +1;
515: else
516: return 0;
517: }
1.172.2.14.2. 4(paf 518:3): static void _sort(Request& r, MethodParams& params) {
519:3): Value& key_maker=params.as_junction(0, "key-maker must be code");
1.61 paf 520:
1.172.2.14.2. 4(paf 521:3): bool reverse=params.count()>1/*..[desc|asc|]*/?
522:3): reverse=params.as_no_junction(1, "order must not be code").as_string()=="desc":
1.104 parser 523: false; // default=asc
1.32 paf 524:
1.172.2.14.2. (paf 525:): Table& old_table=GET_SELF(r, VTable).table();
3(paf 526:3): Table& new_table=*new Table(old_table.columns());
1.34 paf 527:
1.172.2.14.2. 6(paf 528:3): Table_seq_item* seq=new(PointerFreeGC) Table_seq_item[old_table.count()];
1.34 paf 529: int i;
530:
531: // calculate key values
532: bool key_values_are_strings=true;
1.172.2.14.2. 8(paf 533:3): int old_count=old_table.count();
534:3): for(i=0; i<old_count; i++) {
1.101 parser 535: old_table.set_current(i);
1.32 paf 536: // calculate key value
1.172.2.14.2. (paf 537:): seq[i].row=old_table[i];
2(paf 538:3): Value& value=r.process_to_value(key_maker).as_expr_result(true/*return string as-is*/);
1.34 paf 539: if(i==0) // determining key values type by first one
1.172.2.14.2. 2(paf 540:3): key_values_are_strings=value.is_string();
1.34 paf 541:
542: if(key_values_are_strings)
1.172.2.14.2. 2(paf 543:3): seq[i].value.c_str=value.as_string().cstr();
1.34 paf 544: else
1.172.2.14.2. 2(paf 545:3): seq[i].value.d=value.as_double();
1.32 paf 546: }
547: // sort keys
1.172.2.14.2. 8(paf 548:3): _qsort(seq, old_count, sizeof(Table_seq_item),
1.34 paf 549: key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32 paf 550:
1.34 paf 551: // reorder table as they require in 'seq'
1.172.2.14.2. 8(paf 552:3): for(i=0; i<old_count; i++)
553:3): new_table+=Table::element_type(seq[reverse?old_count-1-i:i].row);
6(paf 554:3):
555:3): delete[] seq;
1.32 paf 556:
1.116 parser 557: // replace any previous table value
1.172.2.6 paf 558: GET_SELF(r, VTable).set_table(new_table);
1.32 paf 559: }
560:
1.172.2.14.2. 0(paf 561:3): #ifndef DOXYGEN
1(paf 562:3): struct Expression_is_true_info {
0(paf 563:3): Request* r;
564:3): Value* expression_code;
565:3): };
566:3): #endif
1(paf 567:3): static bool expression_is_true(Table& self, void* ainfo) {
568:3): Expression_is_true_info& info=*static_cast<Expression_is_true_info*>(ainfo);
0(paf 569:3): return info.r->process_to_value(*info.expression_code).as_bool();
570:3): }
571:3): static bool _locate_expression(Table& table, Table::Action_options o,
572:3): Request& r, MethodParams& params) {
573:3): check_option_param(o.defined, params, 1,
574:3): "locate by expression only has parameters: expression and, maybe, options");
4(paf 575:3): Value& expression_code=params.as_junction(0, "must be expression");
1.145 paf 576:
1.172.2.14.2. 1(paf 577:3): Expression_is_true_info info={&r, &expression_code};
578:3): return table.table_first_that(expression_is_true, &info, o);
1.145 paf 579: }
1.172.2.14.2. 0(paf 580:3): static bool _locate_name_value(Table& table, Table::Action_options o,
581:3): Request& r, MethodParams& params) {
582:3): check_option_param(o.defined, params, 2,
583:3): "locate by locate by name has parameters: name, value and, maybe, options");
584:3): const String& name=params.as_string(0, "column name must be string");
585:3): const String& value=params.as_string(1, "value must be string");
586:3): return table.locate(name, value, o);
1.145 paf 587: }
1.172.2.14.2. 4(paf 588:3): static void _locate(Request& r, MethodParams& params) {
0(paf 589:3): Table& table=GET_SELF(r, VTable).table();
590:3):
591:3): Table::Action_options o=get_action_options(r, params, table);
592:3):
5(paf 593:3): bool result=params[0].get_junction()?
0(paf 594:3): _locate_expression(table, o, r, params) :
595:3): _locate_name_value(table, o, r, params);
3(paf 596:3): r.write_no_lang(*new VBool(result));
1.37 paf 597: }
598:
1.172.2.14.2. 0(paf 599:3):
4(paf 600:3): static void _flip(Request& r, MethodParams& params) {
(paf 601:): Table& old_table=GET_SELF(r, VTable).table();
3(paf 602:3): Table& new_table=*new Table(old_table.columns());
8(paf 603:3): if(size_t old_count=old_table.count())
604:3): if(size_t old_cols=old_table[0]->count())
605:3): for(size_t column=0; column<old_cols; column++) {
606:3): Table::element_type new_row(new ArrayString(old_count));
607:3): for(size_t i=0; i<old_count; i++) {
1.172.2.6 paf 608: Table::element_type old_row=old_table[i];
1.172.2.14.2. 3(paf 609:3): *new_row+=column<old_row->count()?old_row->get(column):new String;
1.39 paf 610: }
1.172.2.14.2. 3(paf 611:3): new_table+=new_row;
1.39 paf 612: }
613:
1.172.2.14.2. 3(paf 614:3): r.write_no_lang(*new VTable(&new_table));
1.39 paf 615: }
616:
1.172.2.14.2. 4(paf 617:3): static void _append(Request& r, MethodParams& params) {
1.134 paf 618: // data
1.172.2.14.2. (paf 619:): Temp_lang temp_lang(r, String::L_PASS_APPENDED);
4(paf 620:3): const String& string=r.process_to_string(params.as_junction(0, "body must be code"));
1.41 paf 621:
622: // parse cells
1.172.2.14.2. 3(paf 623:3): Table::element_type row=new ArrayString;
624:3): size_t pos_after=0;
625:3): string.split(*row, pos_after, "\t", String::L_AS_IS);
1.41 paf 626:
1.172.2.14.2. (paf 627:): GET_SELF(r, VTable).table()+=row;
1.41 paf 628: }
629:
1.172.2.14.2. 1(paf 630:3): static void join_named_row(Table& src, Table* dest) {
631:3): Table::columns_type dest_columns=dest->columns();
632:3): size_t dest_columns_count=dest_columns->count();
633:3): Table::element_type dest_row(new ArrayString(dest_columns_count));
634:3): for(size_t dest_column=0; dest_column<dest_columns_count; dest_column++) {
635:3): const String* src_item=src.item(*dest_columns->get(dest_column));
636:3): *dest_row+=src_item?src_item:new String;
637:3): }
638:3): *dest+=dest_row;
639:3): }
640:3): static void join_nameless_row(Table& src, Table* dest) {
641:3): *dest+=src[src.current()];
642:3): }
4(paf 643:3): static void _join(Request& r, MethodParams& params) {
644:3): Table* maybe_src=params.as_no_junction(0, "table ref must not be code").get_table();
1.42 paf 645: if(!maybe_src)
1.146 paf 646: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 647:3): 0,
1.42 paf 648: "source is not a table");
649: Table& src=*maybe_src;
1.172.2.14.2. 0(paf 650:3):
651:3): Table::Action_options o=get_action_options(r, params, src);
652:3): check_option_param(o.defined, params, 1,
653:3): "invalid extra parameter");
654:3):
(paf 655:): Table& dest=GET_SELF(r, VTable).table();
1.42 paf 656: if(&src == &dest)
1.146 paf 657: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 658:3): 0,
1.42 paf 659: "source and destination are same table");
660:
1.172.2.14.2. 1(paf 661:3): if(Table::columns_type dest_columns=dest.columns()) // dest is named
662:3): src.table_for_each(join_named_row, &dest, o);
663:3): else // dest is nameless
664:3): src.table_for_each(join_nameless_row, &dest, o);
1.42 paf 665: }
666:
1.94 parser 667: #ifndef DOXYGEN
1.169 paf 668: class Table_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.172.2.14.2. (paf 669:): ArrayString& columns;
1.172.2.6 paf 670: ArrayString* row;
671: public:
1.172.2.14.2. (paf 672:): Table* table;
1.94 parser 673: public:
1.172.2.14.2. 3(paf 674:3): Table_sql_event_handlers() :
675:3): columns(*new ArrayString), row(0), table(0) {
1.172.2.14 paf 676: }
1.94 parser 677:
1.172.2.14.2. 3(paf 678:3): bool add_column(SQL_Error& error, const char *str, size_t length) {
1.170 paf 679: try {
1.172.2.14.2. 3(paf 680:3): columns+=new String(str, length, true);
1.170 paf 681: return false;
682: } catch(...) {
683: error=SQL_Error("exception occured in Table_sql_event_handlers::add_column");
684: return true;
685: }
686: }
687: bool before_rows(SQL_Error& error) {
688: try {
1.172.2.14.2. 3(paf 689:3): table=new Table(&columns);
1.170 paf 690: return false;
691: } catch(...) {
692: error=SQL_Error("exception occured in Table_sql_event_handlers::before_rows");
693: return true;
694: }
695: }
696: bool add_row(SQL_Error& error) {
697: try {
1.172.2.14.2. 3(paf 698:3): *table+=row=new ArrayString;
1.170 paf 699: return false;
700: } catch(...) {
701: error=SQL_Error("exception occured in Table_sql_event_handlers::add_row");
702: return true;
703: }
704: }
1.172.2.14.2. 3(paf 705:3): bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.170 paf 706: try {
1.172.2.14.2. 3(paf 707:3): String& cell=*new String;
708:3): if(length)
6(paf 709:3): cell.append_know_length(str, length, String::L_TAINTED);
3(paf 710:3): *row+=&cell;
1.170 paf 711: return false;
712: } catch(...) {
713: error=SQL_Error("exception occured in Table_sql_event_handlers::add_row_cell");
714: return true;
715: }
1.94 parser 716: }
717: };
718: #endif
1.172.2.14.2. 4(paf 719:3): static void _sql(Request& r, MethodParams& params) {
720:3): Value& statement=params.as_junction(0, "statement must be code");
1.49 paf 721:
1.53 paf 722: ulong limit=0;
1.100 parser 723: ulong offset=0;
1.172.2.14.2. 4(paf 724:3): if(params.count()>1) {
725:3): Value& voptions=params.as_no_junction(1, "options must be hash, not code");
3(paf 726:3): if(!voptions.is_string())
727:3): if(HashStringValue* options=voptions.get_hash()) {
1.164 paf 728: int valid_options=0;
1.172.2.14.2. (paf 729:): if(Value* vlimit=options->get(sql_limit_name)) {
1.164 paf 730: valid_options++;
1.172.2.14.2. 3(paf 731:3): limit=(ulong)r.process_to_value(*vlimit).as_double();
1.164 paf 732: }
1.172.2.14.2. (paf 733:): if(Value* voffset=options->get(sql_offset_name)) {
1.164 paf 734: valid_options++;
1.172.2.14.2. 3(paf 735:3): offset=(ulong)r.process_to_value(*voffset).as_double();
1.164 paf 736: }
1.172.2.6 paf 737: if(valid_options!=options->count())
1.164 paf 738: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 739:3): 0,
1.164 paf 740: "called with invalid option");
1.109 parser 741: } else
1.146 paf 742: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 743:3): 0,
1.109 parser 744: "options must be hash");
1.49 paf 745: }
746:
1.172.2.14.2. (paf 747:): Temp_lang temp_lang(r, String::L_SQL);
748:): const String& statement_string=r.process_to_string(statement);
1.172.2.2 paf 749: const char* statement_cstr=
1.172.2.14.2. 3(paf 750:3): statement_string.cstr(String::L_UNSPECIFIED, r.connection());
751:3): Table_sql_event_handlers handlers;
1.135 paf 752: #ifdef RESOURCES_DEBUG
753: struct timeval mt[2];
754: //measure:before
755: gettimeofday(&mt[0],NULL);
756: #endif
1.172.2.14.2. (paf 757:): r.connection()->query(
1.160 paf 758: statement_cstr, offset, limit,
759: handlers,
760: statement_string);
1.135 paf 761:
762: #ifdef RESOURCES_DEBUG
763: //measure:after connect
764: gettimeofday(&mt[1],NULL);
765:
766: double t[2];
767: for(int i=0;i<2;i++)
768: t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
769:
770: r.sql_request_time+=t[1]-t[0];
771: #endif
1.96 parser 772:
1.172.2.14.2. 3(paf 773:3): Table& result=
774:3): handlers.table?*handlers.table: // query resulted in table? return it
775:3): *new Table(Table::columns_type(0)); // query returned no table, fake it
1.49 paf 776:
777: // replace any previous table value
1.172.2.6 paf 778: GET_SELF(r, VTable).set_table(result);
1.49 paf 779: }
780:
1.172.2.14.2. 4(paf 781:3): static void _columns(Request& r, MethodParams&) {
1.88 parser 782:
1.172.2.6 paf 783: Table::columns_type result_columns(new ArrayString);
1.172.2.14.2. 3(paf 784:3): *result_columns+=new String("column");
785:3): Table& result_table=*new Table(result_columns);
1.172.2.6 paf 786:
1.172.2.14.2. (paf 787:): Table& source_table=GET_SELF(r, VTable).table();
1.172.2.6 paf 788: if(Table::columns_type source_columns=source_table.columns()) {
1.172.2.14.2. 9(paf 789:3): for(Array_iterator<const String*> i(*source_columns); i.has_next(); ) {
1.172.2.6 paf 790: Table::element_type result_row(new ArrayString);
791: *result_row+=i.next();
1.172.2.14.2. 3(paf 792:3): result_table+=result_row;
1.88 parser 793: }
794: }
795:
1.172.2.14.2. 3(paf 796:3): r.write_no_lang(*new VTable(&result_table));
1.88 parser 797: }
798:
1.172.2.14.2. 4(paf 799:3): static void _select(Request& r, MethodParams& params) {
800:3): Value& vcondition=params.as_junction(0, "condition must be expression");
1.148 paf 801:
1.172.2.14.2. (paf 802:): Table& source_table=GET_SELF(r, VTable).table();
3(paf 803:3): Table& result_table=*new Table(source_table.columns());
1.148 paf 804:
805: int saved_current=source_table.current();
1.172.2.6 paf 806: int size=source_table.count();
1.148 paf 807: for(int row=0; row<size; row++) {
808: source_table.set_current(row);
809:
1.150 paf 810: bool condition=r.process_to_value(vcondition,
1.149 paf 811: /*0/*no name* /,*/
1.172.2.14.2. 3(paf 812:3): false/*don't intercept string*/).as_bool();
1.148 paf 813:
814: if(condition) // ...condition is true=
1.172.2.14.2. 3(paf 815:3): result_table+=source_table[row]; // =green light to go to result
1.148 paf 816: }
817: source_table.set_current(saved_current);
818:
1.172.2.14.2. 3(paf 819:3): r.write_no_lang(*new VTable(&result_table));
1.148 paf 820: }
821:
1.66 paf 822: // constructor
823:
1.172.2.6 paf 824: MTable::MTable(): Methoded("table") {
1.142 paf 825: // ^table::create{data}
826: // ^table::create[nameless]{data}
827: // ^table::create[table]
828: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 2);
829: // old name for compatibility with <= v 1.141 2002/01/25 11:33:45 paf
830: add_native_method("set", Method::CT_DYNAMIC, _create, 1, 2);
1.2 paf 831:
1.142 paf 832: // ^table::load[file]
833: // ^table::load[nameless;file]
1.168 paf 834: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 3);
1.22 paf 835:
836: // ^table.save[file]
837: // ^table.save[nameless;file]
1.66 paf 838: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.6 paf 839:
840: // ^table.count[]
1.66 paf 841: add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6 paf 842:
843: // ^table.line[]
1.66 paf 844: add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6 paf 845:
1.10 paf 846: // ^table.offset[]
1.133 paf 847: // ^table.offset(offset)
848: // ^table.offset[cur|set](offset)
849: add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 2);
1.7 paf 850:
1.10 paf 851: // ^table.menu{code}
852: // ^table.menu{code}[delim]
1.66 paf 853: add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.74 paf 854:
1.79 paf 855: // ^table:hash[key field name]
1.123 parser 856: // ^table:hash[key field name][value field name(s) string/table]
1.163 paf 857: add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 3);
1.32 paf 858:
1.102 parser 859: // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc]
860: // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc]
1.66 paf 861: add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8 paf 862:
1.36 paf 863: // ^table.locate[field;value]
1.172.2.14.2. 0(paf 864:3): add_native_method("locate", Method::CT_DYNAMIC, _locate, 1, 3);
1.39 paf 865:
866: // ^table.flip[]
1.66 paf 867: add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41 paf 868:
869: // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
1.66 paf 870: add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42 paf 871:
1.157 paf 872: // ^table.join[table][$.limit(10) $.offset(1) $.offset[cur] ]
873: add_native_method("join", Method::CT_DYNAMIC, _join, 1, 2);
1.49 paf 874:
875:
1.100 parser 876: // ^table:sql[query]
877: // ^table:sql[query][$.limit(1) $.offset(2)]
878: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.88 parser 879:
880: // ^table:columns[]
881: add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 0);
1.148 paf 882:
883: // ^table.select(expression) = table
884: add_native_method("select", Method::CT_DYNAMIC, _select, 1, 1);
1.40 paf 885: }
E-mail: