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