Annotation of parser3/src/classes/table.C, revision 1.172.2.14.2.28
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. 8(paf 8:3): static const char* IDENT_TABLE_C="$Date: 2003/04/11 10:39:24 $";
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. 4(paf 53:3): static void get_copy_options(Request& r, MethodParams& params, size_t 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.14.2. 4(paf 60:3): if(params.count()<=param_index)
1.157 paf 61: return;
62:
1.172.2.14.2. 4(paf 63:3): Value& voptions=params.as_no_junction(param_index, "options must be hash, not code");
3(paf 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. 4(paf 97:3): static void _create(Request& r, MethodParams& params) {
1.157 paf 98: // clone/copy part?
1.172.2.14.2. 5(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=
4(paf 110:3): 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.14.2. 4(paf 115:3): 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. 4(paf 150:3): static void _load(Request& r, MethodParams& params) {
151:3): 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++;
1.172.2.14.2. 8(paf 156:3): size_t options_param_index=filename_param_index+1;
1.168 paf 157:
1.44 paf 158: // loading text
1.172.2.14.2. 7(paf 159:3): char *data=file_read_text(r.charsets,
4(paf 160:3): r.absolute(params.as_string(filename_param_index, "file name must be string")),
1.168 paf 161: true,
1.172.2.14.2. 4(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. 4(paf 203:3): static void _save(Request& r, MethodParams& params) {
204:3): 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.14.2. 4(paf 210:3): 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. 9(paf 213:3): for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
(paf 214:): sdata.append(*i.next(), String::L_TABLE);
1.98 parser 215: if(i.has_next())
1.172.2.14.2. 6(paf 216:3): sdata.append_know_length("\t", 1, String::L_CLEAN);
1.31 paf 217: }
218: } else { // nameless table
1.172.2.6 paf 219: if(int lsize=table.count()?table[0]->count():0)
1.31 paf 220: for(int column=0; column<lsize; column++) {
1.172.2.14.2. 5(paf 221:3): char *cindex_tab=new(PointerFreeGC) char[MAX_NUMBER];
6(paf 222:3): sdata.append_know_length(cindex_tab,
(paf 223:): snprintf(cindex_tab, MAX_NUMBER, "%d\t", column),
224:): String::L_CLEAN);
1.31 paf 225: }
226: else
1.172.2.14.2. 0(paf 227:3): sdata.append_help_length("empty nameless table", 0, String::L_CLEAN);
1.31 paf 228: }
1.172.2.14.2. 6(paf 229:3): sdata.append_know_length("\n", 1, String::L_CLEAN);
1.129 paf 230: } else { // mode specified
1.172.2.14.2. 4(paf 231:3): const String& mode=params.as_string(0, "mode must be string");
3(paf 232:3): if(mode=="append")
1.129 paf 233: do_append=true;
1.172.2.14.2. 3(paf 234:3): else if(mode=="nameless")
1.129 paf 235: /*ok, already skipped names output*/;
236: else
1.146 paf 237: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 238:3): &mode,
1.129 paf 239: "unknown mode, must be 'append'");
240:
1.31 paf 241: }
242: // data lines
1.172.2.14.2. (paf 243:): Array_iterator<ArrayString*> i(table);
1.98 parser 244: while(i.has_next()) {
1.172.2.14.2. 9(paf 245:3): for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
246:3): sdata.append(*c.next(), String::L_TABLE);
1.98 parser 247: if(c.has_next())
1.172.2.14.2. 6(paf 248:3): sdata.append_know_length("\t", 1, String::L_CLEAN);
1.31 paf 249: }
1.172.2.14.2. 6(paf 250:3): sdata.append_know_length("\n", 1, String::L_CLEAN);
1.31 paf 251: }
252:
253: // write
1.172.2.14.2. (paf 254:): file_write(r.absolute(vfile_name.as_string()),
255:): sdata.cstr(), sdata.length(), true, do_append);
1.22 paf 256: }
257:
1.172.2.14.2. 4(paf 258:3): static void _count(Request& r, MethodParams&) {
(paf 259:): int result=GET_SELF(r, VTable).table().count();
3(paf 260:3): r.write_no_lang(*new VInt(result));
1.6 paf 261: }
262:
1.172.2.14.2. 4(paf 263:3): static void _line(Request& r, MethodParams&) {
(paf 264:): int result=1+GET_SELF(r, VTable).table().current();
3(paf 265:3): r.write_no_lang(*new VInt(result));
1.6 paf 266: }
267:
1.172.2.14.2. 4(paf 268:3): static void _offset(Request& r, MethodParams& params) {
(paf 269:): Table& table=GET_SELF(r, VTable).table();
4(paf 270:3): if(params.count()) {
1.133 paf 271: bool absolute=false;
1.172.2.14.2. 4(paf 272:3): if(params.count()>1) {
273:3): const String& whence=params.as_string(0, "whence must be string");
3(paf 274:3): if(whence=="cur")
1.134 paf 275: absolute=false;
1.172.2.14.2. 3(paf 276:3): else if(whence=="set")
1.134 paf 277: absolute=true;
1.133 paf 278: else
1.146 paf 279: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 280:3): &whence,
1.134 paf 281: "is invalid whence, valid are 'cur' or 'set'");
1.157 paf 282: }
1.133 paf 283:
1.172.2.14.2. 4(paf 284:3): Value& offset_expr=params.as_junction(params.count()-1, "offset must be expression");
0(paf 285:3): table.offset(absolute, r.process_to_value(offset_expr).as_int());
1.151 paf 286: } else
1.172.2.14.2. 0(paf 287:3): r.write_no_lang(*new VInt(table.current()));
1.6 paf 288: }
289:
1.172.2.14.2. 4(paf 290:3): static void _menu(Request& r, MethodParams& params) {
291:3): Value& body_code=params.as_junction(0, "body must be code");
1.7 paf 292:
1.172.2.14.2. 5(paf 293:3): Value* delim_maybe_code=params.count()>1?¶ms[1]:0;
1.7 paf 294:
1.172.2.14.2. (paf 295:): Table& table=GET_SELF(r, VTable).table();
1.7 paf 296: bool need_delim=false;
1.99 parser 297: int saved_current=table.current();
1.172.2.6 paf 298: int size=table.count();
1.99 parser 299: for(int row=0; row<size; row++) {
1.22 paf 300: table.set_current(row);
1.7 paf 301:
1.161 paf 302: StringOrValue sv_processed=r.process(body_code);
1.172.2.14.2. 3(paf 303:3): const String* s_processed=sv_processed.get_string();
(paf 304:): if(delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body
1.161 paf 305: if(need_delim) // need delim & iteration produced string?
1.172.2.14.2. 3(paf 306:3): r.write_pass_lang(r.process(*delim_maybe_code));
1.7 paf 307: need_delim=true;
308: }
1.161 paf 309: r.write_pass_lang(sv_processed);
1.7 paf 310: }
1.99 parser 311: table.set_current(saved_current);
1.7 paf 312: }
313:
1.110 parser 314: #ifndef DOXYGEN
1.172.2.14.2. 8(paf 315:3): enum Table2hash_distint { D_ILLEGAL, D_FIRST, D_TABLES };
1.74 paf 316: struct Row_info {
1.166 paf 317: Request *r;
1.74 paf 318: Table *table;
1.172.2.14.2. (paf 319:): Value* key_code;
8(paf 320:3): size_t key_field;
1.172.2.6 paf 321: Array<int>* value_fields;
322: HashStringValue* hash;
1.172.2.14.2. 8(paf 323:3): Table2hash_distint distinct;
324:3): size_t row;
1.74 paf 325: };
1.110 parser 326: #endif
1.172.2.6 paf 327: static void table_row_to_hash(Table::element_type row, Row_info *info) {
1.172.2.14.2. 3(paf 328:3): const String* key;
1.172.2.6 paf 329: if(info->key_code) {
330: info->table->set_current(info->row++); // change context row
1.172.2.14.2. 3(paf 331:3): StringOrValue sv_processed=info->r->process(*info->key_code);
332:3): key=&sv_processed.as_string();
1.166 paf 333: } else
1.172.2.14.2. (paf 334:): key=info->key_field<row->count()?row->get(info->key_field):0;
1.166 paf 335:
336: if(!key)
337: return; // ignore rows without key [too-short-record_array if-indexed]
1.74 paf 338:
1.172.2.14.2. 8(paf 339:3): switch(info->distinct) {
340:3): case D_ILLEGAL: case D_FIRST:
341:3): {
342:3): VHash* vhash=new VHash;
343:3): HashStringValue& hash=vhash->hash();
344:3): for(Array_iterator<int> i(*info->value_fields); i.has_next(); ) {
345:3): size_t value_field=i.next();
346:3): if(value_field<row->count())
347:3): hash.put(
348:3): *info->table->columns()->get(value_field),
349:3): new VString(*row->get(value_field)));
350:3): }
1.166 paf 351:
1.172.2.14.2. 8(paf 352:3): if(info->hash->put_dont_replace(*key, vhash)) // put. existed?
353:3): if(info->distinct==D_ILLEGAL)
354:3): throw Exception("parser.runtime",
355:3): key,
356:3): "duplicate key");
357:3): }
358:3): break;
359:3): case D_TABLES:
360:3): {
361:3): VTable* vtable=(VTable*)info->hash->get(*key); // put. table existed?
362:3): Table* table;
363:3): if(vtable)
364:3): table=vtable->get_table();
365:3): else {
366:3): // no? creating table of same structure as source
367:3): table=new Table(*info->table, 0, -1 /*no rows, just structure*/);
368:3): info->hash->put(*key, new VTable(table));
369:3): }
370:3): *table+=row;
371:3): }
372:3): break;
373:3): default:
374:3): throw Exception(0,
375:3): 0,
376:3): "invalid distinct code (#%d), ri.distinct");
377:3): }
1.74 paf 378: }
1.172.2.14.2. 4(paf 379:3): static void _hash(Request& r, MethodParams& params) {
(paf 380:): Table& self_table=GET_SELF(r, VTable).table();
3(paf 381:3): VHash& result=*new VHash;
1.172.2.6 paf 382: if(Table::columns_type columns=self_table.columns())
383: if(columns->count()>0) {
1.172.2.14.2. 8(paf 384:3): Table2hash_distint distinct=D_ILLEGAL;
4(paf 385:3): int param_index=params.count()-1;
1.166 paf 386: if(param_index>0) {
1.172.2.6 paf 387: if(HashStringValue* options=
1.172.2.14.2. 4(paf 388:3): params.as_no_junction(param_index, "param must not be code").get_hash()) {
1.166 paf 389: --param_index;
390: int valid_options=0;
1.172.2.14.2. 8(paf 391:3): if(Value* vdistinct_code=options->get(sql_distinct_name)) {
1.166 paf 392: valid_options++;
1.172.2.14.2. 8(paf 393:3): Value& vdistinct_value=r.process_to_value(*vdistinct_code);
394:3): if(vdistinct_value.is_string()) {
395:3): const String& sdistinct=*vdistinct_value.get_string();
396:3): if(sdistinct=="tables")
397:3): distinct=D_TABLES;
398:3): else
399:3): throw Exception("parser.runtime",
400:3): &sdistinct,
401:3): "must be 'tables' or true/false");
402:3): } else
403:3): distinct=vdistinct_value.as_bool()?D_FIRST:D_ILLEGAL;
1.166 paf 404: }
1.172.2.6 paf 405: if(valid_options!=options->count())
1.166 paf 406: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 407:3): 0,
1.166 paf 408: "called with invalid option");
1.163 paf 409: }
410: }
411: if(param_index==2) // bad options param type
412: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 413:3): 0,
1.163 paf 414: "options must be hash");
415:
1.172.2.6 paf 416: Array<int> value_fields;
1.163 paf 417: if(param_index>0) {
1.172.2.14.2. 8(paf 418:3): if(distinct!=D_ILLEGAL && distinct!=D_FIRST)
419:3): throw Exception("parser.runtime",
420:3): 0,
421:3): "in distinct[tables] mode you may not specify value field(s)");
4(paf 422:3): Value& value_fields_param=params.as_no_junction(param_index, "value field(s) must not be code");
3(paf 423:3): if(value_fields_param.is_string()) {
1.172.2.6 paf 424: value_fields+=self_table.column_name2index(
1.172.2.14.2. 3(paf 425:3): *value_fields_param.get_string(), true);
426:3): } else if(Table* value_fields_table=value_fields_param.get_table()) {
1(paf 427:3): for(Array_iterator<Table::element_type> i(*value_fields_table);
428:3): i.has_next(); ) {
429:3): const String& value_field_name
430:3): =*i.next()->get(0);
431:3): value_fields
432:3): +=self_table.column_name2index(value_field_name, true);
1.123 parser 433: }
434: } else
1.146 paf 435: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 436:3): 0,
1.123 parser 437: "value field(s) must be string or self_table"
438: );
439: } else { // by all columns, including key
1.172.2.14.2. 8(paf 440:3): if(!(distinct!=D_ILLEGAL && distinct!=D_FIRST))
441:3): for(size_t i=0; i<columns->count(); i++)
442:3): value_fields+=i;
1.74 paf 443: }
444:
1.172.2.6 paf 445:
446: {
447: Row_info info;
448: info.r=&r;
449: info.table=&self_table;
1.172.2.14.2. 5(paf 450:3): Value* key_param=¶ms[0];
(paf 451:): info.key_code=key_param->get_junction()?key_param:0;
1.172.2.6 paf 452: info.key_field=info.key_code?-1
1.172.2.14.2. (paf 453:): :self_table.column_name2index(key_param->as_string(), true);
1.172.2.6 paf 454: info.value_fields=&value_fields;
1.172.2.14.2. 2(paf 455:3): info.hash=&result.hash();
1.172.2.6 paf 456: info.distinct=distinct;
1.172.2.12 paf 457: info.row=0;
1.172.2.6 paf 458:
459: int saved_current=self_table.current();
460: self_table.for_each(table_row_to_hash, &info);
461: self_table.set_current(saved_current);
462: }
1.74 paf 463: }
1.97 parser 464: r.write_no_lang(result);
1.74 paf 465: }
466:
1.110 parser 467: #ifndef DOXYGEN
1.78 paf 468: struct Table_seq_item {
1.172.2.13 paf 469: ArrayString* row;
1.34 paf 470: union {
1.172.2.14.2. 3(paf 471:3): const char *c_str;
1.34 paf 472: double d;
473: } value;
1.32 paf 474: };
1.110 parser 475: #endif
1.34 paf 476: static int sort_cmp_string(const void *a, const void *b) {
477: return strcmp(
1.78 paf 478: static_cast<const Table_seq_item *>(a)->value.c_str,
479: static_cast<const Table_seq_item *>(b)->value.c_str
1.34 paf 480: );
481: }
482: static int sort_cmp_double(const void *a, const void *b) {
1.78 paf 483: double va=static_cast<const Table_seq_item *>(a)->value.d;
484: double vb=static_cast<const Table_seq_item *>(b)->value.d;
1.34 paf 485: if(va<vb)
486: return -1;
487: else if(va>vb)
488: return +1;
489: else
490: return 0;
491: }
1.172.2.14.2. 4(paf 492:3): static void _sort(Request& r, MethodParams& params) {
493:3): Value& key_maker=params.as_junction(0, "key-maker must be code");
1.61 paf 494:
1.172.2.14.2. 4(paf 495:3): bool reverse=params.count()>1/*..[desc|asc|]*/?
496:3): reverse=params.as_no_junction(1, "order must not be code").as_string()=="desc":
1.104 parser 497: false; // default=asc
1.32 paf 498:
1.172.2.14.2. (paf 499:): Table& old_table=GET_SELF(r, VTable).table();
3(paf 500:3): Table& new_table=*new Table(old_table.columns());
1.34 paf 501:
1.172.2.14.2. 6(paf 502:3): Table_seq_item* seq=new(PointerFreeGC) Table_seq_item[old_table.count()];
1.34 paf 503: int i;
504:
505: // calculate key values
506: bool key_values_are_strings=true;
1.172.2.14.2. 8(paf 507:3): int old_count=old_table.count();
508:3): for(i=0; i<old_count; i++) {
1.101 parser 509: old_table.set_current(i);
1.32 paf 510: // calculate key value
1.172.2.14.2. (paf 511:): seq[i].row=old_table[i];
2(paf 512:3): Value& value=r.process_to_value(key_maker).as_expr_result(true/*return string as-is*/);
1.34 paf 513: if(i==0) // determining key values type by first one
1.172.2.14.2. 2(paf 514:3): key_values_are_strings=value.is_string();
1.34 paf 515:
516: if(key_values_are_strings)
1.172.2.14.2. 2(paf 517:3): seq[i].value.c_str=value.as_string().cstr();
1.34 paf 518: else
1.172.2.14.2. 2(paf 519:3): seq[i].value.d=value.as_double();
1.32 paf 520: }
521: // sort keys
1.172.2.14.2. 8(paf 522:3): _qsort(seq, old_count, sizeof(Table_seq_item),
1.34 paf 523: key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32 paf 524:
1.34 paf 525: // reorder table as they require in 'seq'
1.172.2.14.2. 8(paf 526:3): for(i=0; i<old_count; i++)
527:3): new_table+=Table::element_type(seq[reverse?old_count-1-i:i].row);
6(paf 528:3):
529:3): delete[] seq;
1.32 paf 530:
1.116 parser 531: // replace any previous table value
1.172.2.6 paf 532: GET_SELF(r, VTable).set_table(new_table);
1.32 paf 533: }
534:
1.172.2.14.2. 4(paf 535:3): static bool _locate_expression(Request& r, MethodParams& params) {
536:3): if(params.count()>1)
1.146 paf 537: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 538:3): 0,
1.145 paf 539: "locate by expression has only one parameter - expression");
540:
1.172.2.14.2. 4(paf 541:3): Value& expression_code=params.as_junction(0, "must be expression");
1.145 paf 542:
1.172.2.14.2. (paf 543:): Table& table=GET_SELF(r, VTable).table();
1.145 paf 544: int saved_current=table.current();
1.172.2.6 paf 545: int size=table.count();
1.145 paf 546: for(int row=0; row<size; row++) {
547: table.set_current(row);
548:
1.172.2.14.2. 3(paf 549:3): if(r.process_to_value(expression_code).as_bool())
1.145 paf 550: return true;
551: }
552: table.set_current(saved_current);
553: return false;
554: }
1.172.2.14.2. 4(paf 555:3): static bool _locate_name_value(Request& r, MethodParams& params) {
556:3): if(params.count()>2)
1.146 paf 557: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 558:3): 0,
1.145 paf 559: "locate by name and value has only two parameters - name and value");
1.72 paf 560:
1.172.2.14.2. (paf 561:): Table& table=GET_SELF(r, VTable).table();
1.145 paf 562: return table.locate(
1.172.2.14.2. 4(paf 563:3): params.as_string(0, "column name must be string"),
564:3): params.as_string(1, "value must be string")
1.145 paf 565: );
566: }
1.172.2.14.2. 4(paf 567:3): static void _locate(Request& r, MethodParams& params) {
5(paf 568:3): bool result=params[0].get_junction()?
3(paf 569:3): _locate_expression(r, params) :
570:3): _locate_name_value(r, params);
571:3): r.write_no_lang(*new VBool(result));
1.37 paf 572: }
573:
1.172.2.14.2. 4(paf 574:3): static void _flip(Request& r, MethodParams& params) {
(paf 575:): Table& old_table=GET_SELF(r, VTable).table();
3(paf 576:3): Table& new_table=*new Table(old_table.columns());
8(paf 577:3): if(size_t old_count=old_table.count())
578:3): if(size_t old_cols=old_table[0]->count())
579:3): for(size_t column=0; column<old_cols; column++) {
580:3): Table::element_type new_row(new ArrayString(old_count));
581:3): for(size_t i=0; i<old_count; i++) {
1.172.2.6 paf 582: Table::element_type old_row=old_table[i];
1.172.2.14.2. 3(paf 583:3): *new_row+=column<old_row->count()?old_row->get(column):new String;
1.39 paf 584: }
1.172.2.14.2. 3(paf 585:3): new_table+=new_row;
1.39 paf 586: }
587:
1.172.2.14.2. 3(paf 588:3): r.write_no_lang(*new VTable(&new_table));
1.39 paf 589: }
590:
1.172.2.14.2. 4(paf 591:3): static void _append(Request& r, MethodParams& params) {
1.134 paf 592: // data
1.172.2.14.2. (paf 593:): Temp_lang temp_lang(r, String::L_PASS_APPENDED);
4(paf 594:3): const String& string=r.process_to_string(params.as_junction(0, "body must be code"));
1.41 paf 595:
596: // parse cells
1.172.2.14.2. 3(paf 597:3): Table::element_type row=new ArrayString;
598:3): size_t pos_after=0;
599:3): string.split(*row, pos_after, "\t", String::L_AS_IS);
1.41 paf 600:
1.172.2.14.2. (paf 601:): GET_SELF(r, VTable).table()+=row;
1.41 paf 602: }
603:
1.172.2.14.2. 4(paf 604:3): static void _join(Request& r, MethodParams& params) {
605:3): Table* maybe_src=params.as_no_junction(0, "table ref must not be code").get_table();
1.42 paf 606: if(!maybe_src)
1.146 paf 607: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 608:3): 0,
1.42 paf 609: "source is not a table");
610:
611: Table& src=*maybe_src;
1.172.2.14.2. (paf 612:): Table& dest=GET_SELF(r, VTable).table();
1.42 paf 613: if(&src == &dest)
1.146 paf 614: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 615:3): 0,
1.42 paf 616: "source and destination are same table");
617:
1.157 paf 618: int offset, limit;
1.172.2.14.2. 3(paf 619:3): get_copy_options(r, params, 1, src,
1.157 paf 620: offset, limit);
621:
1.172.2.6 paf 622: if(Table::columns_type dest_columns=dest.columns()) { // dest is named
1.42 paf 623: int saved_src_current=src.current();
1.172.2.6 paf 624: int m=src.count()-offset;
1.157 paf 625: if(!limit || limit>m)
626: limit=m;
627: int end=offset+limit;
628: for(int src_row=offset; src_row<end; src_row++) {
1.42 paf 629: src.set_current(src_row);
1.172.2.6 paf 630: Table::element_type dest_row(new ArrayString(src.count()));
1.172.2.14.2. 8(paf 631:3): for(size_t dest_column=0; dest_column<dest_columns->count(); dest_column++) {
3(paf 632:3): const String* src_item=src.item(*dest_columns->get(dest_column));
633:3): *dest_row+=src_item?src_item:new String;
1.140 paf 634: }
1.172.2.6 paf 635: dest+=dest_row;
1.42 paf 636: }
637: src.set_current(saved_src_current);
638: } else { // dest is nameless
1.172.2.14.2. 8(paf 639:3): size_t src_count=src.count();
640:3): for(size_t src_row=0; src_row<src_count; src_row++)
1.172.2.6 paf 641: dest+=src[src_row];
1.42 paf 642: }
643: }
644:
1.94 parser 645: #ifndef DOXYGEN
1.169 paf 646: class Table_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.172.2.14.2. (paf 647:): ArrayString& columns;
1.172.2.6 paf 648: ArrayString* row;
649: public:
1.172.2.14.2. (paf 650:): Table* table;
1.94 parser 651: public:
1.172.2.14.2. 3(paf 652:3): Table_sql_event_handlers() :
653:3): columns(*new ArrayString), row(0), table(0) {
1.172.2.14 paf 654: }
1.94 parser 655:
1.172.2.14.2. 3(paf 656:3): bool add_column(SQL_Error& error, const char *str, size_t length) {
1.170 paf 657: try {
1.172.2.14.2. 3(paf 658:3): columns+=new String(str, length, true);
1.170 paf 659: return false;
660: } catch(...) {
661: error=SQL_Error("exception occured in Table_sql_event_handlers::add_column");
662: return true;
663: }
664: }
665: bool before_rows(SQL_Error& error) {
666: try {
1.172.2.14.2. 3(paf 667:3): table=new Table(&columns);
1.170 paf 668: return false;
669: } catch(...) {
670: error=SQL_Error("exception occured in Table_sql_event_handlers::before_rows");
671: return true;
672: }
673: }
674: bool add_row(SQL_Error& error) {
675: try {
1.172.2.14.2. 3(paf 676:3): *table+=row=new ArrayString;
1.170 paf 677: return false;
678: } catch(...) {
679: error=SQL_Error("exception occured in Table_sql_event_handlers::add_row");
680: return true;
681: }
682: }
1.172.2.14.2. 3(paf 683:3): bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.170 paf 684: try {
1.172.2.14.2. 3(paf 685:3): String& cell=*new String;
686:3): if(length)
6(paf 687:3): cell.append_know_length(str, length, String::L_TAINTED);
3(paf 688:3): *row+=&cell;
1.170 paf 689: return false;
690: } catch(...) {
691: error=SQL_Error("exception occured in Table_sql_event_handlers::add_row_cell");
692: return true;
693: }
1.94 parser 694: }
695: };
696: #endif
1.172.2.14.2. 4(paf 697:3): static void _sql(Request& r, MethodParams& params) {
698:3): Value& statement=params.as_junction(0, "statement must be code");
1.49 paf 699:
1.53 paf 700: ulong limit=0;
1.100 parser 701: ulong offset=0;
1.172.2.14.2. 4(paf 702:3): if(params.count()>1) {
703:3): Value& voptions=params.as_no_junction(1, "options must be hash, not code");
3(paf 704:3): if(!voptions.is_string())
705:3): if(HashStringValue* options=voptions.get_hash()) {
1.164 paf 706: int valid_options=0;
1.172.2.14.2. (paf 707:): if(Value* vlimit=options->get(sql_limit_name)) {
1.164 paf 708: valid_options++;
1.172.2.14.2. 3(paf 709:3): limit=(ulong)r.process_to_value(*vlimit).as_double();
1.164 paf 710: }
1.172.2.14.2. (paf 711:): if(Value* voffset=options->get(sql_offset_name)) {
1.164 paf 712: valid_options++;
1.172.2.14.2. 3(paf 713:3): offset=(ulong)r.process_to_value(*voffset).as_double();
1.164 paf 714: }
1.172.2.6 paf 715: if(valid_options!=options->count())
1.164 paf 716: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 717:3): 0,
1.164 paf 718: "called with invalid option");
1.109 parser 719: } else
1.146 paf 720: throw Exception("parser.runtime",
1.172.2.14.2. 3(paf 721:3): 0,
1.109 parser 722: "options must be hash");
1.49 paf 723: }
724:
1.172.2.14.2. (paf 725:): Temp_lang temp_lang(r, String::L_SQL);
726:): const String& statement_string=r.process_to_string(statement);
1.172.2.2 paf 727: const char* statement_cstr=
1.172.2.14.2. 3(paf 728:3): statement_string.cstr(String::L_UNSPECIFIED, r.connection());
729:3): Table_sql_event_handlers handlers;
1.135 paf 730: #ifdef RESOURCES_DEBUG
731: struct timeval mt[2];
732: //measure:before
733: gettimeofday(&mt[0],NULL);
734: #endif
1.172.2.14.2. (paf 735:): r.connection()->query(
1.160 paf 736: statement_cstr, offset, limit,
737: handlers,
738: statement_string);
1.135 paf 739:
740: #ifdef RESOURCES_DEBUG
741: //measure:after connect
742: gettimeofday(&mt[1],NULL);
743:
744: double t[2];
745: for(int i=0;i<2;i++)
746: t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
747:
748: r.sql_request_time+=t[1]-t[0];
749: #endif
1.96 parser 750:
1.172.2.14.2. 3(paf 751:3): Table& result=
752:3): handlers.table?*handlers.table: // query resulted in table? return it
753:3): *new Table(Table::columns_type(0)); // query returned no table, fake it
1.49 paf 754:
755: // replace any previous table value
1.172.2.6 paf 756: GET_SELF(r, VTable).set_table(result);
1.49 paf 757: }
758:
1.172.2.14.2. 4(paf 759:3): static void _columns(Request& r, MethodParams&) {
1.88 parser 760:
1.172.2.6 paf 761: Table::columns_type result_columns(new ArrayString);
1.172.2.14.2. 3(paf 762:3): *result_columns+=new String("column");
763:3): Table& result_table=*new Table(result_columns);
1.172.2.6 paf 764:
1.172.2.14.2. (paf 765:): Table& source_table=GET_SELF(r, VTable).table();
1.172.2.6 paf 766: if(Table::columns_type source_columns=source_table.columns()) {
1.172.2.14.2. 9(paf 767:3): for(Array_iterator<const String*> i(*source_columns); i.has_next(); ) {
1.172.2.6 paf 768: Table::element_type result_row(new ArrayString);
769: *result_row+=i.next();
1.172.2.14.2. 3(paf 770:3): result_table+=result_row;
1.88 parser 771: }
772: }
773:
1.172.2.14.2. 3(paf 774:3): r.write_no_lang(*new VTable(&result_table));
1.88 parser 775: }
776:
1.172.2.14.2. 4(paf 777:3): static void _select(Request& r, MethodParams& params) {
778:3): Value& vcondition=params.as_junction(0, "condition must be expression");
1.148 paf 779:
1.172.2.14.2. (paf 780:): Table& source_table=GET_SELF(r, VTable).table();
3(paf 781:3): Table& result_table=*new Table(source_table.columns());
1.148 paf 782:
783: int saved_current=source_table.current();
1.172.2.6 paf 784: int size=source_table.count();
1.148 paf 785: for(int row=0; row<size; row++) {
786: source_table.set_current(row);
787:
1.150 paf 788: bool condition=r.process_to_value(vcondition,
1.149 paf 789: /*0/*no name* /,*/
1.172.2.14.2. 3(paf 790:3): false/*don't intercept string*/).as_bool();
1.148 paf 791:
792: if(condition) // ...condition is true=
1.172.2.14.2. 3(paf 793:3): result_table+=source_table[row]; // =green light to go to result
1.148 paf 794: }
795: source_table.set_current(saved_current);
796:
1.172.2.14.2. 3(paf 797:3): r.write_no_lang(*new VTable(&result_table));
1.148 paf 798: }
799:
1.66 paf 800: // constructor
801:
1.172.2.6 paf 802: MTable::MTable(): Methoded("table") {
1.142 paf 803: // ^table::create{data}
804: // ^table::create[nameless]{data}
805: // ^table::create[table]
806: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 2);
807: // old name for compatibility with <= v 1.141 2002/01/25 11:33:45 paf
808: add_native_method("set", Method::CT_DYNAMIC, _create, 1, 2);
1.2 paf 809:
1.142 paf 810: // ^table::load[file]
811: // ^table::load[nameless;file]
1.168 paf 812: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 3);
1.22 paf 813:
814: // ^table.save[file]
815: // ^table.save[nameless;file]
1.66 paf 816: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.6 paf 817:
818: // ^table.count[]
1.66 paf 819: add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6 paf 820:
821: // ^table.line[]
1.66 paf 822: add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6 paf 823:
1.10 paf 824: // ^table.offset[]
1.133 paf 825: // ^table.offset(offset)
826: // ^table.offset[cur|set](offset)
827: add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 2);
1.7 paf 828:
1.10 paf 829: // ^table.menu{code}
830: // ^table.menu{code}[delim]
1.66 paf 831: add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.74 paf 832:
1.79 paf 833: // ^table:hash[key field name]
1.123 parser 834: // ^table:hash[key field name][value field name(s) string/table]
1.163 paf 835: add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 3);
1.32 paf 836:
1.102 parser 837: // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc]
838: // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc]
1.66 paf 839: add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8 paf 840:
1.36 paf 841: // ^table.locate[field;value]
1.145 paf 842: add_native_method("locate", Method::CT_DYNAMIC, _locate, 1, 2);
1.39 paf 843:
844: // ^table.flip[]
1.66 paf 845: add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41 paf 846:
847: // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
1.66 paf 848: add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42 paf 849:
1.157 paf 850: // ^table.join[table][$.limit(10) $.offset(1) $.offset[cur] ]
851: add_native_method("join", Method::CT_DYNAMIC, _join, 1, 2);
1.49 paf 852:
853:
1.100 parser 854: // ^table:sql[query]
855: // ^table:sql[query][$.limit(1) $.offset(2)]
856: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.88 parser 857:
858: // ^table:columns[]
859: add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 0);
1.148 paf 860:
861: // ^table.select(expression) = table
862: add_native_method("select", Method::CT_DYNAMIC, _select, 1, 1);
1.40 paf 863: }
E-mail: