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