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