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