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