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