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