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