Annotation of parser3/src/classes/table.C, revision 1.240
1.20 paf 1: /** @file
1.47 paf 2: Parser: @b table parser class.
1.20 paf 3:
1.210 paf 4: Copyright (c) 2001-2005 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.221 paf 7:
1.240 ! misha 8: static const char * const IDENT_TABLE_C="$Date: 2008-02-14 09:10:24 $";
1.229 misha 9:
1.240 ! misha 10: #ifndef NO_STRINGSTREAM
1.229 misha 11: #include <sstream>
12: using namespace std;
1.240 ! misha 13: #endif
1.1 paf 14:
1.105 parser 15: #include "classes.h"
1.182 paf 16: #include "pa_vmethod_frame.h"
17:
1.16 paf 18: #include "pa_common.h"
1.1 paf 19: #include "pa_request.h"
20: #include "pa_vtable.h"
1.6 paf 21: #include "pa_vint.h"
1.51 paf 22: #include "pa_sql_connection.h"
1.72 paf 23: #include "pa_vbool.h"
1.1 paf 24:
1.66 paf 25: // class
26:
1.182 paf 27: class MTable: public Methoded {
1.66 paf 28: public: // VStateless_class
1.209 paf 29: Value* create_new_value(Pool&, HashStringValue&) { return new VTable(); }
1.66 paf 30:
31: public:
1.182 paf 32: MTable();
1.70 paf 33:
34: public: // Methoded
1.66 paf 35: bool used_directly() { return true; }
36: };
1.1 paf 37:
1.182 paf 38: // global variable
39:
40: DECLARE_CLASS_VAR(table, new MTable, 0);
41:
1.221 paf 42: // externs
43:
44: extern String cycle_data_name;
45:
1.182 paf 46: #define TABLE_REVERSE_NAME "reverse"
47:
48: // globals
49:
1.203 paf 50: String sql_bind_name(SQL_BIND_NAME);
1.214 paf 51: String sql_limit_name(PA_SQL_LIMIT_NAME);
52: String sql_offset_name(PA_SQL_OFFSET_NAME);
1.182 paf 53: String sql_default_name(SQL_DEFAULT_NAME);
54: String sql_distinct_name(SQL_DISTINCT_NAME);
1.227 misha 55: String sql_value_type_name(SQL_VALUE_TYPE_NAME);
1.182 paf 56: String table_reverse_name(TABLE_REVERSE_NAME);
57:
1.1 paf 58: // methods
1.66 paf 59:
1.182 paf 60: static Table::Action_options get_action_options(Request& r, MethodParams& params,
61: const Table& source) {
1.176 paf 62: Table::Action_options result;
1.182 paf 63: if(!params.count())
64: return result;
65:
66: Value& maybe_options=params.last();
67: /* can not do it:
68: want to enable ^table::create[$source;
69: # $.option[]
70: ]
71: but there is ^table.locate[name;value]
1.176 paf 72:
1.212 paf 73: ...if(voptions.is_defined() && !voptions.is_string()))
1.182 paf 74: if(maybe_options.is_string()) { // allow empty options
75: result.defined=true;
1.176 paf 76: return result;
1.182 paf 77: }
78: */
79: HashStringValue* options=maybe_options.get_hash();
1.176 paf 80: if(!options)
81: return result;
82:
83: result.defined=true;
84: bool defined_offset=false;
85:
86: int valid_options=0;
1.182 paf 87: if(Value* voffset=options->get(sql_offset_name)) {
1.176 paf 88: valid_options++;
89: defined_offset=true;
90: if(voffset->is_string()) {
1.182 paf 91: const String& soffset=*voffset->get_string();
1.176 paf 92: if(soffset == "cur")
93: result.offset=source.current();
94: else
1.226 misha 95: throw Exception(PARSER_RUNTIME,
1.176 paf 96: &soffset,
97: "must be 'cur' string or expression");
98: } else
99: result.offset=r.process_to_value(*voffset).as_int();
100: }
1.182 paf 101: if(Value* vlimit=options->get(sql_limit_name)) {
1.176 paf 102: valid_options++;
103: result.limit=r.process_to_value(*vlimit).as_int();
104: }
1.182 paf 105: if(Value *vreverse=(Value *)options->get(table_reverse_name)) {
106: valid_options++;
107: result.reverse=r.process_to_value(*vreverse).as_bool();
108: if(result.reverse && !defined_offset)
109: result.offset=source.count()-1;
110: }
111:
112: if(valid_options!=options->count())
1.226 misha 113: throw Exception(PARSER_RUNTIME,
1.182 paf 114: 0,
1.176 paf 115: "called with invalid option");
116:
117: return result;
118: }
1.180 paf 119: static void check_option_param(bool options_defined,
1.182 paf 120: MethodParams& params, size_t next_param_index,
1.176 paf 121: const char *msg) {
1.182 paf 122: if(next_param_index+(options_defined?1:0) != params.count())
1.226 misha 123: throw Exception(PARSER_RUNTIME,
1.182 paf 124: 0,
1.176 paf 125: "%s", msg);
1.157 paf 126: }
127:
1.236 misha 128: struct TableSeparators {
129: char column; const String* scolumn;
130: char encloser; const String* sencloser;
131:
132: TableSeparators():
133: column('\t'), scolumn(new String("\t", false)),
134: encloser(0), sencloser(0)
135: {}
136: int load( HashStringValue& options ) {
137: int result=0;
138: if(Value* vseparator=options.get(PA_COLUMN_SEPARATOR_NAME)) {
139: scolumn=&vseparator->as_string();
140: if(scolumn->length()!=1)
141: throw Exception(PARSER_RUNTIME,
142: scolumn,
143: "separator must be one character long");
144: column=scolumn->first_char();
145: result++;
146: }
147: if(Value* vencloser=options.get(PA_COLUMN_ENCLOSER_NAME)) {
148: sencloser=&vencloser->as_string();
149: if(sencloser->length()!=1)
150: throw Exception(PARSER_RUNTIME,
151: sencloser,
152: "encloser must be one character long");
153: encloser=sencloser->first_char();
154: result++;
155: }
156: return result;
157: }
158: };
159:
1.182 paf 160: static void _create(Request& r, MethodParams& params) {
1.157 paf 161: // clone/copy part?
1.182 paf 162: if(Table *source=params[0].get_table()) {
163: Table::Action_options o=get_action_options(r, params, *source);
164: check_option_param(o.defined, params, 1,
1.176 paf 165: "too many parameters");
1.182 paf 166: GET_SELF(r, VTable).set_table(*new Table(*source, o));
1.157 paf 167: return;
168: }
1.142 paf 169:
1.236 misha 170: size_t data_param_index=0;
171: bool nameless=false;
172:
173: if(params.count()>1) {
174: if(params[0].is_string()){ // can be nameless only
175: const String& snameless=params.as_string(0, "called with more then 1 param, first param may be only string 'nameless' or junction");
176: if(snameless!="nameless")
177: throw Exception(PARSER_RUNTIME,
178: &snameless,
179: "table::create called with more then 1 param, first param may be only 'nameless'");
180: nameless=true;
181: data_param_index++;
182: }
183: }
184:
185: HashStringValue *options=0;
186: TableSeparators separators;
187:
188: size_t options_param_index=data_param_index+1;
189: if(
190: options_param_index<params.count()
191: && (options=params.as_no_junction(options_param_index, "additional params must be hash").get_hash())
192: ) {
193: // cloning, so that we could change
194: options=new HashStringValue(*options);
195: separators.load(*options);
196: if(separators.encloser){
197: throw Exception(PARSER_RUNTIME,
198: 0,
199: "encloser not supported for table::create yet");
200: }
201: }
202:
203: // data
1.182 paf 204: Temp_lang temp_lang(r, String::L_PASS_APPENDED);
205: const String& data=
1.236 misha 206: r.process_to_string(params.as_junction(data_param_index, "body must be code"));
1.44 paf 207:
208: // parse columns
1.182 paf 209: size_t raw_pos_after=0;
210: Table::columns_type columns;
1.236 misha 211:
212: if(nameless){
1.182 paf 213: columns=Table::columns_type(0); // nameless
1.21 paf 214: } else {
1.182 paf 215: columns=Table::columns_type(new ArrayString);
1.44 paf 216:
1.182 paf 217: ArrayString head;
218: data.split(head, raw_pos_after, "\n", String::L_AS_IS, 1);
219: if(head.count()) {
220: size_t col_pos_after=0;
1.236 misha 221: head[0]->split(*columns, col_pos_after, *separators.scolumn, String::L_AS_IS);
1.182 paf 222: }
1.44 paf 223: }
224:
1.182 paf 225: Table& table=*new Table(columns);
1.44 paf 226: // parse cells
1.182 paf 227:
228: ArrayString rows;
229: data.split(rows, raw_pos_after, "\n", String::L_AS_IS);
230: Array_iterator<const String*> i(rows);
1.98 parser 231: while(i.has_next()) {
1.182 paf 232: Table::element_type row(new ArrayString);
233: const String& string=*i.next();
1.118 parser 234: // remove comment lines
1.182 paf 235: if(!string.length())
1.44 paf 236: continue;
237:
1.182 paf 238: size_t col_pos_after=0;
1.236 misha 239: string.split(*row, col_pos_after, *separators.scolumn, String::L_AS_IS);
1.182 paf 240: table+=row;
1.17 paf 241: }
1.1 paf 242:
1.44 paf 243: // replace any previous table value
1.182 paf 244: GET_SELF(r, VTable).set_table(table);
1.44 paf 245: }
246:
1.187 paf 247: struct lsplit_result {
248: char* piece;
249: char delim;
250:
251: operator bool() { return piece!=0; }
252: };
253:
254: inline lsplit_result lsplit(char* string, char delim1, char delim2) {
255: lsplit_result result;
256: if(string) {
257: char delims[]={delim1, delim2, 0};
258: if(char* v=strpbrk(string, delims)) {
259: result.delim=*v;
260: *v=0;
261: result.piece=v+1;
262: return result;
263: }
264: }
265: result.piece=0;
266: result.delim=0;
267: return result;
268: }
269:
270: inline lsplit_result lsplit(char* *string_ref, char delim1, char delim2) {
271: lsplit_result result;
272: result.piece=*string_ref;
273: lsplit_result next=lsplit(*string_ref, delim1, delim2);
274: result.delim=next.delim;
275: *string_ref=next.piece;
276: return result;
277: }
278:
279: static lsplit_result lsplit(char** string_ref, char delim1, char delim2, char encloser) {
280: lsplit_result result;
281:
282: if(char* string=*string_ref) {
283: if(encloser && *string==encloser) {
284: string++;
285:
286: char *read;
287: char *write;
288: write=read=string;
289: char c;
1.193 paf 290: while((c=*read++)) {
1.187 paf 291: if(c==encloser) {
292: char n=*read;
293: if(n==encloser) // double-encloser stands for encloser
294: read++;
295: else if(n==delim1 || n==delim2) {
296: result.delim=n;
297: read++;
298: break;
299: }
300: }
301:
302: *write++=c;
303: }
304: *write=0; // terminate
305: *string_ref=c? read: 0;
306: result.piece=string;
307: return result;
308: } else
309: return lsplit(string_ref, delim1, delim2);
310: }
311: result.piece=0;
312: return result;
313: }
314:
315: static void skip_empty_and_comment_lines( char** data_ref ) {
316: if(char *data=*data_ref) {
317: while( char c=*data ) {
318: if( c== '\n' || c == '#' ) {
319: /*nowhere=*/getrow(&data); // remove empty&comment lines
1.199 paf 320: if(!(*data_ref=data))
321: break;
1.187 paf 322: continue;
323: }
324: break;
325: }
326: }
327: }
328:
1.182 paf 329: static void _load(Request& r, MethodParams& params) {
1.232 misha 330: const String& first_param=params.as_string(0, FILE_NAME_MUST_BE_STRING);
1.168 paf 331: int filename_param_index=0;
332: bool nameless=first_param=="nameless";
333: if(nameless)
334: filename_param_index++;
1.182 paf 335: size_t options_param_index=filename_param_index+1;
1.186 paf 336:
337: HashStringValue *options=0;
1.187 paf 338: TableSeparators separators;
1.186 paf 339: if(options_param_index<params.count()
340: && (options=params.as_no_junction(options_param_index, "additional params must be hash").get_hash())) {
341: // cloning, so that we could change [to simplify checks on params inside file_read_text
342: options=new HashStringValue(*options);
1.187 paf 343: separators.load(*options);
1.186 paf 344: }
345:
1.44 paf 346: // loading text
1.182 paf 347: char *data=file_read_text(r.charsets,
1.232 misha 348: r.absolute(params.as_string(filename_param_index, FILE_NAME_MUST_BE_STRING)),
1.168 paf 349: true,
1.186 paf 350: options
1.168 paf 351: );
1.44 paf 352:
1.1 paf 353: // parse columns
1.182 paf 354: Table::columns_type columns;
1.168 paf 355: if(nameless) {
1.182 paf 356: columns=Table::columns_type(0); // nameless
1.1 paf 357: } else {
1.182 paf 358: columns=Table::columns_type(new ArrayString);
1.1 paf 359:
1.187 paf 360: skip_empty_and_comment_lines(&data);
361: while( lsplit_result sr=lsplit(&data, separators.column, '\n', separators.encloser) ) {
362: *columns+=new String(sr.piece, 0, true);
363: if(sr.delim=='\n')
364: break;
1.139 paf 365: }
1.1 paf 366: }
1.187 paf 367:
368: Table& table=*new Table(columns);
1.219 paf 369: int columns_count=columns? columns->count(): 0;
1.1 paf 370:
371: // parse cells
1.218 paf 372: Table::element_type row(new ArrayString(columns_count));
1.187 paf 373: skip_empty_and_comment_lines(&data);
374: while( lsplit_result sr=lsplit(&data, separators.column, '\n', separators.encloser) ) {
1.201 paf 375: if(!*sr.piece && !sr.delim && !row->count()) // append last empty column [if without \n]
376: break;
1.187 paf 377: *row+=new String(sr.piece, 0, true);
378: if(sr.delim=='\n') {
379: table+=row;
1.218 paf 380: row=new ArrayString(columns_count);
1.187 paf 381: skip_empty_and_comment_lines(&data);
1.1 paf 382: }
1.187 paf 383: }
384: // last line [if without \n]
385: if(row->count())
1.1 paf 386: table+=row;
1.187 paf 387:
1.1 paf 388: // replace any previous table value
1.182 paf 389: GET_SELF(r, VTable).set_table(table);
1.1 paf 390: }
1.2 paf 391:
1.224 misha 392: void maybe_enclose( String& to, const String& from, char encloser, const String* sencloser ) {
1.188 paf 393: if(encloser) {
394: to<<*sencloser;
395: // while we have 'encloser'...
1.189 paf 396: size_t pos_after=0;
1.220 paf 397: for( size_t pos_before; (pos_before=from.pos( encloser, pos_after ))!=STRING_NOT_FOUND; pos_after=pos_before) {
398: pos_before++; // including first encloser (and skipping it for next pos)
399: to<<from.mid(pos_after, pos_before);
400: to<<*sencloser; // doubling encloser
1.188 paf 401: }
402: // last piece
403: size_t from_length=from.length();
404: if(pos_after<from_length)
405: to<<from.mid(pos_after, from_length);
406:
407: to<<*sencloser;
408: } else
1.202 paf 409: to<<from;
1.188 paf 410: }
411:
1.224 misha 412: void maybe_enclose( ostringstream& to, const String& from, char encloser ) {
413: if(encloser) {
414: to<<encloser;
415: // while we have 'encloser'...
416: size_t pos_after=0;
417: for( size_t pos_before; (pos_before=from.pos( encloser, pos_after ))!=STRING_NOT_FOUND; pos_after=pos_before) {
418: pos_before++; // including first encloser (and skipping it for next pos)
419: to<<from.mid(pos_after, pos_before).cstr();
420: to<<encloser; // doubling encloser
421: }
422: // last piece
423: size_t from_length=from.length();
424: if(pos_after<from_length)
425: to<<from.mid(pos_after, from_length).cstr();
426:
427: to<<encloser;
428: } else
429: to<<from.cstr();
430: }
431:
1.236 misha 432: /*
1.224 misha 433: static void _save_old(Request& r, MethodParams& params) {
1.237 misha 434: const String& first_arg=params.as_string(0, FIRST_ARG_MUST_NOT_BE_CODE);
1.188 paf 435: size_t param_index=1;
436:
437: bool do_append=false;
438: bool output_column_names=true;
439:
440: // mode?
441: if(first_arg=="append")
442: do_append=true;
443: else if(first_arg=="nameless")
444: output_column_names=false;
445: else
446: --param_index;
447:
1.232 misha 448: const String& file_name=params.as_string(param_index++, FILE_NAME_MUST_NOT_BE_CODE);
1.188 paf 449:
450: TableSeparators separators;
451: if(param_index<params.count()) {
1.222 paf 452: Value& voptions=params.as_no_junction(param_index++, "additional params must be hash");
453: if( voptions.is_defined() && !voptions.is_string() ) {
454: if(HashStringValue* options=voptions.get_hash()) {
1.223 paf 455: int valid_options=separators.load(*options);
456: if(valid_options!=options->count())
1.226 misha 457: throw Exception(PARSER_RUNTIME,
1.223 paf 458: 0,
459: "invalid option passed");
460: } else {
1.226 misha 461: throw Exception(PARSER_RUNTIME,
1.188 paf 462: 0,
1.223 paf 463: "additional params must be hash (did you spell mode parameter correctly?)");
464: }
1.222 paf 465: }
1.188 paf 466: }
467: if(param_index<params.count())
1.226 misha 468: throw Exception(PARSER_RUNTIME,
1.188 paf 469: 0,
470: "bad mode (must be nameless or append)");
1.22 paf 471:
1.182 paf 472: Table& table=GET_SELF(r, VTable).table();
1.31 paf 473:
1.182 paf 474: String sdata;
1.188 paf 475: if(output_column_names) {
1.31 paf 476: if(table.columns()) { // named table
1.182 paf 477: for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
1.188 paf 478: maybe_enclose( sdata, *i.next(), separators.encloser, separators.sencloser );
1.98 parser 479: if(i.has_next())
1.206 paf 480: sdata<<*separators.scolumn;
1.31 paf 481: }
1.188 paf 482: } else { // nameless table [we were asked to output column names]
1.182 paf 483: if(int lsize=table.count()?table[0]->count():0)
1.31 paf 484: for(int column=0; column<lsize; column++) {
1.182 paf 485: char *cindex_tab=new(PointerFreeGC) char[MAX_NUMBER];
486: sdata.append_know_length(cindex_tab,
1.185 paf 487: snprintf(cindex_tab, MAX_NUMBER,
1.206 paf 488: column<lsize-1?"%d%c":"%d", column, separators.column),
1.185 paf 489: String::L_CLEAN);
1.31 paf 490: }
491: else
1.182 paf 492: sdata.append_help_length("empty nameless table", 0, String::L_CLEAN);
1.31 paf 493: }
1.182 paf 494: sdata.append_know_length("\n", 1, String::L_CLEAN);
1.188 paf 495: }
1.129 paf 496:
1.31 paf 497: // data lines
1.182 paf 498: Array_iterator<ArrayString*> i(table);
1.98 parser 499: while(i.has_next()) {
1.182 paf 500: for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
1.188 paf 501: maybe_enclose( sdata, *c.next(), separators.encloser, separators.sencloser );
1.98 parser 502: if(c.has_next())
1.206 paf 503: sdata<<*separators.scolumn;
1.31 paf 504: }
1.182 paf 505: sdata.append_know_length("\n", 1, String::L_CLEAN);
1.31 paf 506: }
507:
508: // write
1.217 paf 509: {
510: const char* data_cstr=sdata.cstr();
511: file_write(r.absolute(file_name),
512: data_cstr, sdata.length(), true, do_append);
513: if(*data_cstr) // not empty (when empty it's not heap memory)
514: pa_free((void*)data_cstr); // not needed anymore
515: }
1.22 paf 516: }
1.236 misha 517: */
1.22 paf 518:
1.224 misha 519: static void _save(Request& r, MethodParams& params) {
1.237 misha 520: const String& first_arg=params.as_string(0, FIRST_ARG_MUST_NOT_BE_CODE);
1.224 misha 521: size_t param_index=1;
522:
523: bool do_append=false;
524: bool output_column_names=true;
525:
526: // mode?
527: if(first_arg=="append")
528: do_append=true;
529: else if(first_arg=="nameless")
530: output_column_names=false;
531: else
532: --param_index;
533:
1.232 misha 534: const String& file_name=params.as_string(param_index++, FILE_NAME_MUST_NOT_BE_CODE);
1.224 misha 535:
536: TableSeparators separators;
537: if(param_index<params.count()) {
538: Value& voptions=params.as_no_junction(param_index++, "additional params must be hash");
539: if( voptions.is_defined() && !voptions.is_string() ) {
540: if(HashStringValue* options=voptions.get_hash()) {
541: int valid_options=separators.load(*options);
542: if(valid_options!=options->count())
1.226 misha 543: throw Exception(PARSER_RUNTIME,
1.224 misha 544: 0,
545: "invalid option passed");
546: } else {
1.226 misha 547: throw Exception(PARSER_RUNTIME,
1.224 misha 548: 0,
549: "additional params must be hash (did you spell mode parameter correctly?)");
550: }
551: }
552: }
553: if(param_index<params.count())
1.226 misha 554: throw Exception(PARSER_RUNTIME,
1.224 misha 555: 0,
556: "bad mode (must be nameless or append)");
557:
558: Table& table=GET_SELF(r, VTable).table();
559:
1.240 ! misha 560: #ifndef NO_STRINGSTREAM
! 561:
1.224 misha 562: ostringstream ost(stringstream::out);
563:
564: // process header
565: if(output_column_names) {
566: if(table.columns()) { // named table
567: for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
568: maybe_enclose( ost, *i.next(), separators.encloser );
569: if(i.has_next()){
570: ost<<separators.column;
571: }
572: }
573: } else { // nameless table [we were asked to output column names]
574: if(int lsize=table.count()?table[0]->count():0)
575: for(int column=0; column<lsize; column++) {
1.225 misha 576: if(separators.encloser) {
577: ost<<separators.encloser<<column<<separators.encloser;
578: } else {
1.239 misha 579: ost<<column;
1.225 misha 580: }
1.224 misha 581: if(column<lsize-1){
582: ost<<separators.column;
583: }
584: }
585: else
586: ost<<"empty nameless table";
587: }
588: ost<<'\n';
589: }
590:
591: // process data lines
592: Array_iterator<ArrayString*> i(table);
593: while(i.has_next()) {
594: for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
595: maybe_enclose( ost, *c.next(), separators.encloser );
596: if(c.has_next())
597: ost<<separators.column;
598: }
599: ost<<'\n';
600: }
601:
602: // write
603: {
604: string data=ost.str();
605: const char* data_cstr = data.c_str();
606:
607: file_write(r.absolute(file_name), data_cstr, data.length(), true /* as text */, do_append);
608: }
1.240 ! misha 609:
! 610: #else
! 611:
! 612: String sdata;
! 613: if(output_column_names) {
! 614: if(table.columns()) { // named table
! 615: for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) {
! 616: maybe_enclose( sdata, *i.next(), separators.encloser, separators.sencloser );
! 617: if(i.has_next())
! 618: sdata<<*separators.scolumn;
! 619: }
! 620: } else { // nameless table [we were asked to output column names]
! 621: if(int lsize=table.count()?table[0]->count():0)
! 622: for(int column=0; column<lsize; column++) {
! 623: char *cindex_tab=new(PointerFreeGC) char[MAX_NUMBER];
! 624: sdata.append_know_length(cindex_tab,
! 625: snprintf(cindex_tab, MAX_NUMBER,
! 626: column<lsize-1?"%d%c":"%d", column, separators.column),
! 627: String::L_CLEAN);
! 628: }
! 629: else
! 630: sdata.append_help_length("empty nameless table", 0, String::L_CLEAN);
! 631: }
! 632: sdata.append_know_length("\n", 1, String::L_CLEAN);
! 633: }
! 634:
! 635: // data lines
! 636: Array_iterator<ArrayString*> i(table);
! 637: while(i.has_next()) {
! 638: for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) {
! 639: maybe_enclose( sdata, *c.next(), separators.encloser, separators.sencloser );
! 640: if(c.has_next())
! 641: sdata<<*separators.scolumn;
! 642: }
! 643: sdata.append_know_length("\n", 1, String::L_CLEAN);
! 644: }
! 645:
! 646: // write
! 647: {
! 648: const char* data_cstr=sdata.cstr();
! 649: file_write(r.absolute(file_name),
! 650: data_cstr, sdata.length(), true, do_append);
! 651: if(*data_cstr) // not empty (when empty it's not heap memory)
! 652: pa_free((void*)data_cstr); // not needed anymore
! 653: }
! 654: #endif
1.224 misha 655: }
656:
1.182 paf 657: static void _count(Request& r, MethodParams&) {
658: int result=GET_SELF(r, VTable).table().count();
659: r.write_no_lang(*new VInt(result));
1.6 paf 660: }
661:
1.182 paf 662: static void _line(Request& r, MethodParams&) {
663: int result=1+GET_SELF(r, VTable).table().current();
664: r.write_no_lang(*new VInt(result));
1.6 paf 665: }
666:
1.182 paf 667: static void _offset(Request& r, MethodParams& params) {
668: Table& table=GET_SELF(r, VTable).table();
669: if(params.count()) {
1.133 paf 670: bool absolute=false;
1.182 paf 671: if(params.count()>1) {
672: const String& whence=params.as_string(0, "whence must be string");
1.133 paf 673: if(whence=="cur")
1.134 paf 674: absolute=false;
1.133 paf 675: else if(whence=="set")
1.134 paf 676: absolute=true;
1.133 paf 677: else
1.226 misha 678: throw Exception(PARSER_RUNTIME,
1.134 paf 679: &whence,
680: "is invalid whence, valid are 'cur' or 'set'");
1.157 paf 681: }
1.133 paf 682:
1.211 paf 683: int offset=params.as_int(params.count()-1, "offset must be expression", r);
684: table.offset(absolute, offset);
1.151 paf 685: } else
1.182 paf 686: r.write_no_lang(*new VInt(table.current()));
1.6 paf 687: }
688:
1.182 paf 689: static void _menu(Request& r, MethodParams& params) {
1.221 paf 690: Temp_hash_value<const String::Body, void*>
691: cycle_data_setter(r.classes_conf, cycle_data_name, /*any not null flag*/&r);
692:
1.182 paf 693: Value& body_code=params.as_junction(0, "body must be code");
1.7 paf 694:
1.182 paf 695: Value* delim_maybe_code=params.count()>1?¶ms[1]:0;
1.7 paf 696:
1.182 paf 697: Table& table=GET_SELF(r, VTable).table();
1.7 paf 698: bool need_delim=false;
1.99 parser 699: int saved_current=table.current();
1.182 paf 700: int size=table.count();
1.99 parser 701: for(int row=0; row<size; row++) {
1.22 paf 702: table.set_current(row);
1.7 paf 703:
1.161 paf 704: StringOrValue sv_processed=r.process(body_code);
1.221 paf 705: Request::Skip lskip=r.get_skip(); r.set_skip(Request::SKIP_NOTHING);
1.182 paf 706: const String* s_processed=sv_processed.get_string();
707: if(delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body
1.161 paf 708: if(need_delim) // need delim & iteration produced string?
1.150 paf 709: r.write_pass_lang(r.process(*delim_maybe_code));
1.7 paf 710: need_delim=true;
711: }
1.161 paf 712: r.write_pass_lang(sv_processed);
1.221 paf 713:
714: if(lskip==Request::SKIP_BREAK)
715: break;
1.7 paf 716: }
1.99 parser 717: table.set_current(saved_current);
1.7 paf 718: }
719:
1.110 parser 720: #ifndef DOXYGEN
1.74 paf 721: struct Row_info {
1.166 paf 722: Request *r;
1.74 paf 723: Table *table;
1.182 paf 724: Value* key_code;
725: size_t key_field;
726: Array<int>* value_fields;
727: HashStringValue* hash;
1.174 paf 728: Table2hash_distint distinct;
1.182 paf 729: size_t row;
1.227 misha 730: Table2hash_value_type value_type;
1.74 paf 731: };
1.110 parser 732: #endif
1.182 paf 733: static void table_row_to_hash(Table::element_type row, Row_info *info) {
734: const String* key;
735: if(info->key_code) {
736: info->table->set_current(info->row++); // change context row
737: StringOrValue sv_processed=info->r->process(*info->key_code);
1.166 paf 738: key=&sv_processed.as_string();
1.227 misha 739: } else {
1.182 paf 740: key=info->key_field<row->count()?row->get(info->key_field):0;
1.227 misha 741: }
1.166 paf 742:
743: if(!key)
744: return; // ignore rows without key [too-short-record_array if-indexed]
1.74 paf 745:
1.227 misha 746: bool exist = false;
747: switch (info->value_type) {
748: case C_STRING:
749: {
750: exist=info->hash->put_dont_replace(*key, new VString(*row->get(info->value_fields->get(0))));
751: }
752: break;
753: case C_HASH:
1.174 paf 754: {
1.182 paf 755: VHash* vhash=new VHash;
756: HashStringValue& hash=vhash->hash();
757: for(Array_iterator<int> i(*info->value_fields); i.has_next(); ) {
758: size_t value_field=i.next();
759: if(value_field<row->count())
1.174 paf 760: hash.put(
1.182 paf 761: *info->table->columns()->get(value_field),
762: new VString(*row->get(value_field)));
1.174 paf 763: }
764:
1.227 misha 765: exist=info->hash->put_dont_replace(*key, vhash);
1.174 paf 766: }
767: break;
1.227 misha 768: case C_TABLE:
1.174 paf 769: {
1.182 paf 770: VTable* vtable=(VTable*)info->hash->get(*key); // put. table existed?
1.227 misha 771: if( info->distinct==D_ILLEGAL ){
772: exist=true;
773: break;
774: }
775:
1.174 paf 776: Table* table;
1.227 misha 777: if(vtable) {
1.174 paf 778: table=vtable->get_table();
1.227 misha 779: } else {
1.174 paf 780: // no? creating table of same structure as source
1.179 paf 781: Table::Action_options table_options(0, 0);
1.182 paf 782: table=new Table(*info->table, table_options/*no rows, just structure*/);
783: info->hash->put(*key, new VTable(table));
1.174 paf 784: }
1.182 paf 785: *table+=row;
1.174 paf 786: }
787: break;
1.74 paf 788: }
1.227 misha 789: if(exist && info->distinct==D_ILLEGAL)
790: throw Exception(PARSER_RUNTIME,
791: key,
792: "duplicate key");
1.74 paf 793: }
1.235 misha 794:
795: Table2hash_value_type get_value_type(Value& vvalue_type){
796: if(vvalue_type.is_string()) {
797: const String& svalue_type=*vvalue_type.get_string();
798: if(svalue_type == "table"){
799: return C_TABLE;
800: } else if (svalue_type == "string") {
801: return C_STRING;
802: } else if (svalue_type == "hash") {
803: return C_HASH;
804: } else {
805: throw Exception(PARSER_RUNTIME,
806: &svalue_type,
807: "must be 'hash', 'table' or 'string'");
808: }
809: } else {
810: throw Exception(PARSER_RUNTIME,
811: 0,
812: "'type' must be hash");
813: }
814: }
815:
1.182 paf 816: static void _hash(Request& r, MethodParams& params) {
817: Table& self_table=GET_SELF(r, VTable).table();
818: VHash& result=*new VHash;
1.238 misha 819: if(Table::columns_type columns=self_table.columns()){
1.182 paf 820: if(columns->count()>0) {
1.174 paf 821: Table2hash_distint distinct=D_ILLEGAL;
1.227 misha 822: Table2hash_value_type value_type=C_HASH;
1.182 paf 823: int param_index=params.count()-1;
1.166 paf 824: if(param_index>0) {
1.238 misha 825: if(HashStringValue* options=params.as_no_junction(param_index, PARAM_MUST_NOT_BE_CODE).get_hash()){ // options where specified
1.166 paf 826: --param_index;
827: int valid_options=0;
1.238 misha 828: if(Value* vdistinct_code=options->get(sql_distinct_name)) { // $.distinct ?
1.166 paf 829: valid_options++;
1.174 paf 830: Value& vdistinct_value=r.process_to_value(*vdistinct_code);
831: if(vdistinct_value.is_string()) {
832: const String& sdistinct=*vdistinct_value.get_string();
1.227 misha 833: if(sdistinct=="tables") {
834: value_type=C_TABLE;
835: distinct=D_FIRST;
1.238 misha 836: } else {
1.226 misha 837: throw Exception(PARSER_RUNTIME,
1.174 paf 838: &sdistinct,
839: "must be 'tables' or true/false");
1.238 misha 840: }
841: } else {
1.174 paf 842: distinct=vdistinct_value.as_bool()?D_FIRST:D_ILLEGAL;
1.238 misha 843: }
1.166 paf 844: }
1.238 misha 845: if(Value* vvalue_type_code=options->get(sql_value_type_name)) { // $.type ?
846: if(value_type==C_TABLE) // $.distinct[tables] already was specified
1.227 misha 847: throw Exception(PARSER_RUNTIME,
848: 0,
1.238 misha 849: "you can't specify $.distinct[tables] and $.type[] together");
850:
851: valid_options++;
852: value_type=get_value_type(r.process_to_value(*vvalue_type_code));
853: }
1.226 misha 854:
1.182 paf 855: if(valid_options!=options->count())
1.226 misha 856: throw Exception(PARSER_RUNTIME,
1.182 paf 857: 0,
1.166 paf 858: "called with invalid option");
1.163 paf 859: }
860: }
1.238 misha 861:
862: if(param_index==2) // options was specified but not as hash
1.226 misha 863: throw Exception(PARSER_RUNTIME,
1.182 paf 864: 0,
1.163 paf 865: "options must be hash");
866:
1.182 paf 867: Array<int> value_fields;
1.238 misha 868: if(param_index==0){ // list of columns wasn't specified
869: if(value_type==C_STRING) // $.type[string]
870: throw Exception(PARSER_RUNTIME,
871: 0,
872: "you must specify one value field with option $.type[string]");
873:
874: for(size_t i=0; i<columns->count(); i++) // by all columns, including key
875: value_fields+=i;
876:
877: } else { // list of columns was specified
1.227 misha 878: if(value_type==C_TABLE)
879: throw Exception(PARSER_RUNTIME,
1.174 paf 880: 0,
1.238 misha 881: "you can't specify value field(s) with option $.distinct[tables] or $.type[tables]");
882:
1.182 paf 883: Value& value_fields_param=params.as_no_junction(param_index, "value field(s) must not be code");
1.238 misha 884: if(value_fields_param.is_string()) { // one column as string was specified
885: value_fields+=self_table.column_name2index(*value_fields_param.get_string(), true);
886: } else if(Table* value_fields_table=value_fields_param.get_table()) { // list of columns were specified in table
887: for(Array_iterator<Table::element_type> i(*value_fields_table); i.has_next(); ) {
888: const String& value_field_name =*i.next()->get(0);
889: value_fields +=self_table.column_name2index(value_field_name, true);
1.123 parser 890: }
891: } else
1.226 misha 892: throw Exception(PARSER_RUNTIME,
1.182 paf 893: 0,
1.226 misha 894: "value field(s) must be string or table");
1.239 misha 895: }
1.74 paf 896:
1.227 misha 897: if(value_type==C_STRING && value_fields.count()!=1)
898: throw Exception(PARSER_RUNTIME,
899: 0,
1.238 misha 900: "you can specify only one value field with option $.type[string]");
1.182 paf 901:
902: {
903: Value* key_param=¶ms[0];
1.193 paf 904: Row_info info={
905: &r,
906: &self_table,
907: /*key_code=*/key_param->get_junction()?key_param:0,
1.197 paf 908: /*key_field=*/0/*filled below*/,
1.193 paf 909: &value_fields,
910: &result.hash(),
911: distinct,
1.227 misha 912: /*row=*/0,
913: value_type
1.193 paf 914: };
1.195 paf 915: info.key_field=(info.key_code?-1
916: :self_table.column_name2index(key_param->as_string(), true));
1.182 paf 917:
918: int saved_current=self_table.current();
919: self_table.for_each(table_row_to_hash, &info);
920: self_table.set_current(saved_current);
1.207 paf 921:
922: result.extract_default();
1.182 paf 923: }
1.74 paf 924: }
1.238 misha 925: }
1.97 parser 926: r.write_no_lang(result);
1.74 paf 927: }
928:
1.110 parser 929: #ifndef DOXYGEN
1.78 paf 930: struct Table_seq_item {
1.182 paf 931: ArrayString* row;
1.34 paf 932: union {
1.182 paf 933: const char *c_str;
1.34 paf 934: double d;
935: } value;
1.32 paf 936: };
1.110 parser 937: #endif
1.34 paf 938: static int sort_cmp_string(const void *a, const void *b) {
939: return strcmp(
1.78 paf 940: static_cast<const Table_seq_item *>(a)->value.c_str,
941: static_cast<const Table_seq_item *>(b)->value.c_str
1.34 paf 942: );
943: }
944: static int sort_cmp_double(const void *a, const void *b) {
1.78 paf 945: double va=static_cast<const Table_seq_item *>(a)->value.d;
946: double vb=static_cast<const Table_seq_item *>(b)->value.d;
1.34 paf 947: if(va<vb)
948: return -1;
949: else if(va>vb)
950: return +1;
951: else
952: return 0;
953: }
1.182 paf 954: static void _sort(Request& r, MethodParams& params) {
955: Value& key_maker=params.as_junction(0, "key-maker must be code");
1.61 paf 956:
1.182 paf 957: bool reverse=params.count()>1/*..[desc|asc|]*/?
958: reverse=params.as_no_junction(1, "order must not be code").as_string()=="desc":
1.104 parser 959: false; // default=asc
1.32 paf 960:
1.182 paf 961: Table& old_table=GET_SELF(r, VTable).table();
962: Table& new_table=*new Table(old_table.columns());
1.34 paf 963:
1.182 paf 964: Table_seq_item* seq=new(PointerFreeGC) Table_seq_item[old_table.count()];
1.34 paf 965: int i;
966:
967: // calculate key values
968: bool key_values_are_strings=true;
1.182 paf 969: int old_count=old_table.count();
970: for(i=0; i<old_count; i++) {
1.101 parser 971: old_table.set_current(i);
1.32 paf 972: // calculate key value
1.182 paf 973: seq[i].row=old_table[i];
974: Value& value=r.process_to_value(key_maker).as_expr_result(true/*return string as-is*/);
1.34 paf 975: if(i==0) // determining key values type by first one
976: key_values_are_strings=value.is_string();
977:
978: if(key_values_are_strings)
979: seq[i].value.c_str=value.as_string().cstr();
980: else
981: seq[i].value.d=value.as_double();
1.32 paf 982: }
983: // sort keys
1.182 paf 984: _qsort(seq, old_count, sizeof(Table_seq_item),
1.34 paf 985: key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32 paf 986:
1.34 paf 987: // reorder table as they require in 'seq'
1.182 paf 988: for(i=0; i<old_count; i++)
989: new_table+=Table::element_type(seq[reverse?old_count-1-i:i].row);
990:
991: delete[] seq;
1.32 paf 992:
1.116 parser 993: // replace any previous table value
1.182 paf 994: GET_SELF(r, VTable).set_table(new_table);
1.32 paf 995: }
996:
1.176 paf 997: #ifndef DOXYGEN
1.182 paf 998: struct Expression_is_true_info {
1.176 paf 999: Request* r;
1000: Value* expression_code;
1001: };
1002: #endif
1.191 paf 1003: static bool expression_is_true(Table&, Expression_is_true_info* info) {
1004: return info->r->process_to_value(*info->expression_code).as_bool();
1.176 paf 1005: }
1006: static bool _locate_expression(Table& table, Table::Action_options o,
1.182 paf 1007: Request& r, MethodParams& params) {
1008: check_option_param(o.defined, params, 1,
1.176 paf 1009: "locate by expression only has parameters: expression and, maybe, options");
1.182 paf 1010: Value& expression_code=params.as_junction(0, "must be expression");
1.145 paf 1011:
1.182 paf 1012: Expression_is_true_info info={&r, &expression_code};
1013: return table.table_first_that(expression_is_true, &info, o);
1.145 paf 1014: }
1.176 paf 1015: static bool _locate_name_value(Table& table, Table::Action_options o,
1.191 paf 1016: Request&, MethodParams& params) {
1.182 paf 1017: check_option_param(o.defined, params, 2,
1.176 paf 1018: "locate by locate by name has parameters: name, value and, maybe, options");
1.182 paf 1019: const String& name=params.as_string(0, "column name must be string");
1.232 misha 1020: const String& value=params.as_string(1, VALUE_MUST_BE_STRING);
1.182 paf 1021: return table.locate(name, value, o);
1022: }
1023: static void _locate(Request& r, MethodParams& params) {
1024: Table& table=GET_SELF(r, VTable).table();
1.72 paf 1025:
1.182 paf 1026: Table::Action_options o=get_action_options(r, params, table);
1027:
1028: bool result=params[0].get_junction()?
1029: _locate_expression(table, o, r, params) :
1030: _locate_name_value(table, o, r, params);
1031: r.write_no_lang(*new VBool(result));
1.145 paf 1032: }
1.182 paf 1033:
1034:
1.191 paf 1035: static void _flip(Request& r, MethodParams&) {
1.182 paf 1036: Table& old_table=GET_SELF(r, VTable).table();
1.184 paf 1037: Table& new_table=*new Table(0);
1.182 paf 1038: if(size_t old_count=old_table.count())
1039: if(size_t old_cols=old_table[0]->count())
1040: for(size_t column=0; column<old_cols; column++) {
1041: Table::element_type new_row(new ArrayString(old_count));
1042: for(size_t i=0; i<old_count; i++) {
1043: Table::element_type old_row=old_table[i];
1044: *new_row+=column<old_row->count()?old_row->get(column):new String;
1.39 paf 1045: }
1.182 paf 1046: new_table+=new_row;
1.39 paf 1047: }
1048:
1.182 paf 1049: r.write_no_lang(*new VTable(&new_table));
1.39 paf 1050: }
1051:
1.182 paf 1052: static void _append(Request& r, MethodParams& params) {
1.134 paf 1053: // data
1.182 paf 1054: Temp_lang temp_lang(r, String::L_PASS_APPENDED);
1055: const String& string=r.process_to_string(params.as_junction(0, "body must be code"));
1.41 paf 1056:
1057: // parse cells
1.182 paf 1058: Table::element_type row=new ArrayString;
1059: size_t pos_after=0;
1060: string.split(*row, pos_after, "\t", String::L_AS_IS);
1.41 paf 1061:
1.182 paf 1062: GET_SELF(r, VTable).table()+=row;
1.41 paf 1063: }
1064:
1.182 paf 1065: static void join_named_row(Table& src, Table* dest) {
1066: Table::columns_type dest_columns=dest->columns();
1067: size_t dest_columns_count=dest_columns->count();
1068: Table::element_type dest_row(new ArrayString(dest_columns_count));
1069: for(size_t dest_column=0; dest_column<dest_columns_count; dest_column++) {
1070: const String* src_item=src.item(*dest_columns->get(dest_column));
1071: *dest_row+=src_item?src_item:new String;
1072: }
1073: *dest+=dest_row;
1074: }
1075: static void join_nameless_row(Table& src, Table* dest) {
1076: *dest+=src[src.current()];
1077: }
1078: static void _join(Request& r, MethodParams& params) {
1079: Table* maybe_src=params.as_no_junction(0, "table ref must not be code").get_table();
1.42 paf 1080: if(!maybe_src)
1.226 misha 1081: throw Exception(PARSER_RUNTIME,
1.182 paf 1082: 0,
1.42 paf 1083: "source is not a table");
1.176 paf 1084: Table& src=*maybe_src;
1085:
1.182 paf 1086: Table::Action_options o=get_action_options(r, params, src);
1087: check_option_param(o.defined, params, 1,
1.176 paf 1088: "invalid extra parameter");
1.42 paf 1089:
1.182 paf 1090: Table& dest=GET_SELF(r, VTable).table();
1.42 paf 1091: if(&src == &dest)
1.226 misha 1092: throw Exception(PARSER_RUNTIME,
1.182 paf 1093: 0,
1.42 paf 1094: "source and destination are same table");
1095:
1.193 paf 1096: if(dest.columns()) // dest is named
1.182 paf 1097: src.table_for_each(join_named_row, &dest, o);
1098: else // dest is nameless
1099: src.table_for_each(join_nameless_row, &dest, o);
1.42 paf 1100: }
1101:
1.94 parser 1102: #ifndef DOXYGEN
1.169 paf 1103: class Table_sql_event_handlers: public SQL_Driver_query_event_handlers {
1.182 paf 1104: ArrayString& columns;
1.218 paf 1105: int columns_count;
1.182 paf 1106: ArrayString* row;
1.94 parser 1107: public:
1.182 paf 1108: Table* table;
1109: public:
1110: Table_sql_event_handlers() :
1111: columns(*new ArrayString), row(0), table(0) {
1.94 parser 1112: }
1113:
1.182 paf 1114: bool add_column(SQL_Error& error, const char *str, size_t length) {
1.170 paf 1115: try {
1.182 paf 1116: columns+=new String(str, length, true);
1.170 paf 1117: return false;
1118: } catch(...) {
1119: error=SQL_Error("exception occured in Table_sql_event_handlers::add_column");
1120: return true;
1121: }
1122: }
1123: bool before_rows(SQL_Error& error) {
1124: try {
1.182 paf 1125: table=new Table(&columns);
1.218 paf 1126: columns_count=columns.count();
1.170 paf 1127: return false;
1128: } catch(...) {
1129: error=SQL_Error("exception occured in Table_sql_event_handlers::before_rows");
1130: return true;
1131: }
1132: }
1133: bool add_row(SQL_Error& error) {
1134: try {
1.218 paf 1135: *table+=row=new ArrayString(columns_count);
1.170 paf 1136: return false;
1137: } catch(...) {
1138: error=SQL_Error("exception occured in Table_sql_event_handlers::add_row");
1139: return true;
1140: }
1141: }
1.182 paf 1142: bool add_row_cell(SQL_Error& error, const char* str, size_t length) {
1.170 paf 1143: try {
1.182 paf 1144: String& cell=*new String;
1145: if(length)
1146: cell.append_know_length(str, length, String::L_TAINTED);
1147: *row+=&cell;
1.170 paf 1148: return false;
1149: } catch(...) {
1150: error=SQL_Error("exception occured in Table_sql_event_handlers::add_row_cell");
1151: return true;
1152: }
1.94 parser 1153: }
1154: };
1155: #endif
1.204 paf 1156:
1157: static void marshal_bind(
1158: HashStringValue::key_type aname,
1159: HashStringValue::value_type avalue,
1160: SQL_Driver::Placeholder** pptr)
1161: {
1162: SQL_Driver::Placeholder& ph=**pptr;
1163: ph.name=aname.cstr();
1.205 paf 1164: ph.value=avalue->as_string().cstr(String::L_UNSPECIFIED);
1.204 paf 1165: ph.is_null=avalue->get_class()==void_class;
1166: ph.were_updated=false;
1167:
1168: (*pptr)++;
1169: }
1170:
1171: // not static, used elsewhere
1172: int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders) {
1173: int hash_count=hash.count();
1174: placeholders=new(UseGC) SQL_Driver::Placeholder[hash_count];
1175: SQL_Driver::Placeholder* ptr=placeholders;
1.221 paf 1176: hash.for_each<SQL_Driver::Placeholder**>(marshal_bind, &ptr);
1.204 paf 1177: return hash_count;
1178: }
1179:
1180: // not static, used elsewhere
1181: void unmarshal_bind_updates(HashStringValue& hash, int placeholder_count, SQL_Driver::Placeholder* placeholders) {
1182: SQL_Driver::Placeholder* ph=placeholders;
1183: for(int i=0; i<placeholder_count; i++, ph++)
1184: if(ph->were_updated) {
1185: Value* value;
1186: if(ph->is_null)
1187: value=new VVoid();
1188: else
1189: if(ph->value)
1190: value=new VString(*new String(ph->value, 0, true/*tainted*/));
1191: else
1192: value=new VString(*new String());
1193: hash.put(ph->name, value);
1194: }
1195: }
1196:
1.182 paf 1197: static void _sql(Request& r, MethodParams& params) {
1198: Value& statement=params.as_junction(0, "statement must be code");
1.49 paf 1199:
1.204 paf 1200: HashStringValue* bind=0;
1.53 paf 1201: ulong limit=0;
1.100 parser 1202: ulong offset=0;
1.182 paf 1203: if(params.count()>1) {
1204: Value& voptions=params.as_no_junction(1, "options must be hash, not code");
1.212 paf 1205: if(voptions.is_defined() && !voptions.is_string())
1.182 paf 1206: if(HashStringValue* options=voptions.get_hash()) {
1.164 paf 1207: int valid_options=0;
1.204 paf 1208: if(Value* vbind=options->get(sql_bind_name)) {
1209: valid_options++;
1210: bind=vbind->get_hash();
1211: }
1.182 paf 1212: if(Value* vlimit=options->get(sql_limit_name)) {
1.164 paf 1213: valid_options++;
1.147 paf 1214: limit=(ulong)r.process_to_value(*vlimit).as_double();
1.164 paf 1215: }
1.182 paf 1216: if(Value* voffset=options->get(sql_offset_name)) {
1.164 paf 1217: valid_options++;
1.147 paf 1218: offset=(ulong)r.process_to_value(*voffset).as_double();
1.164 paf 1219: }
1.182 paf 1220: if(valid_options!=options->count())
1.226 misha 1221: throw Exception(PARSER_RUNTIME,
1.182 paf 1222: 0,
1.164 paf 1223: "called with invalid option");
1.109 parser 1224: } else
1.226 misha 1225: throw Exception(PARSER_RUNTIME,
1.182 paf 1226: 0,
1.109 parser 1227: "options must be hash");
1.49 paf 1228: }
1229:
1.204 paf 1230: SQL_Driver::Placeholder* placeholders=0;
1231: uint placeholders_count=0;
1232: if(bind)
1233: placeholders_count=marshal_binds(*bind, placeholders);
1234:
1.182 paf 1235: Temp_lang temp_lang(r, String::L_SQL);
1236: const String& statement_string=r.process_to_string(statement);
1237: const char* statement_cstr=
1238: statement_string.cstr(String::L_UNSPECIFIED, r.connection());
1239: Table_sql_event_handlers handlers;
1.135 paf 1240: #ifdef RESOURCES_DEBUG
1241: struct timeval mt[2];
1242: //measure:before
1243: gettimeofday(&mt[0],NULL);
1244: #endif
1.182 paf 1245: r.connection()->query(
1.203 paf 1246: statement_cstr,
1.208 paf 1247: placeholders_count, placeholders,
1.203 paf 1248: offset, limit,
1.160 paf 1249: handlers,
1250: statement_string);
1.135 paf 1251:
1252: #ifdef RESOURCES_DEBUG
1253: //measure:after connect
1254: gettimeofday(&mt[1],NULL);
1255:
1256: double t[2];
1257: for(int i=0;i<2;i++)
1258: t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
1259:
1260: r.sql_request_time+=t[1]-t[0];
1261: #endif
1.204 paf 1262:
1263: if(bind)
1264: unmarshal_bind_updates(*bind, placeholders_count, placeholders);
1.96 parser 1265:
1.182 paf 1266: Table& result=
1267: handlers.table?*handlers.table: // query resulted in table? return it
1268: *new Table(Table::columns_type(0)); // query returned no table, fake it
1.49 paf 1269:
1270: // replace any previous table value
1.182 paf 1271: GET_SELF(r, VTable).set_table(result);
1.49 paf 1272: }
1273:
1.233 misha 1274: static void _columns(Request& r, MethodParams& params) {
1275: const String* column_column_name;
1276: if(params.count()>0)
1277: column_column_name=¶ms.as_string(0, COLUMN_NAME_MUST_BE_STRING);
1278: else
1279: column_column_name=new String("column");
1.88 parser 1280:
1.182 paf 1281: Table::columns_type result_columns(new ArrayString);
1.233 misha 1282: *result_columns+=column_column_name;
1.182 paf 1283: Table& result_table=*new Table(result_columns);
1.88 parser 1284:
1.182 paf 1285: Table& source_table=GET_SELF(r, VTable).table();
1286: if(Table::columns_type source_columns=source_table.columns()) {
1287: for(Array_iterator<const String*> i(*source_columns); i.has_next(); ) {
1288: Table::element_type result_row(new ArrayString);
1289: *result_row+=i.next();
1290: result_table+=result_row;
1.88 parser 1291: }
1292: }
1293:
1.182 paf 1294: r.write_no_lang(*new VTable(&result_table));
1.88 parser 1295: }
1296:
1.182 paf 1297: static void _select(Request& r, MethodParams& params) {
1.231 misha 1298: Value& vcondition=params.as_expression(0, "condition must be number, bool or expression");
1.148 paf 1299:
1.182 paf 1300: Table& source_table=GET_SELF(r, VTable).table();
1301: Table& result_table=*new Table(source_table.columns());
1.148 paf 1302:
1303: int saved_current=source_table.current();
1.182 paf 1304: int size=source_table.count();
1.148 paf 1305: for(int row=0; row<size; row++) {
1306: source_table.set_current(row);
1307:
1.150 paf 1308: bool condition=r.process_to_value(vcondition,
1.148 paf 1309: false/*don't intercept string*/).as_bool();
1310:
1311: if(condition) // ...condition is true=
1.182 paf 1312: result_table+=source_table[row]; // =green light to go to result
1.148 paf 1313: }
1314: source_table.set_current(saved_current);
1315:
1.182 paf 1316: r.write_no_lang(*new VTable(&result_table));
1.148 paf 1317: }
1318:
1.66 paf 1319: // constructor
1320:
1.182 paf 1321: MTable::MTable(): Methoded("table") {
1.142 paf 1322: // ^table::create{data}
1323: // ^table::create[nameless]{data}
1324: // ^table::create[table]
1.236 misha 1325: add_native_method("create", Method::CT_DYNAMIC, _create, 1, 3);
1.142 paf 1326: // old name for compatibility with <= v 1.141 2002/01/25 11:33:45 paf
1.236 misha 1327: add_native_method("set", Method::CT_DYNAMIC, _create, 1, 3);
1.2 paf 1328:
1.142 paf 1329: // ^table::load[file]
1330: // ^table::load[nameless;file]
1.168 paf 1331: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 3);
1.22 paf 1332:
1333: // ^table.save[file]
1334: // ^table.save[nameless;file]
1.188 paf 1335: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 3);
1.6 paf 1336:
1.224 misha 1337: // add_native_method("save_old", Method::CT_DYNAMIC, _save_old, 1, 3);
1338:
1.6 paf 1339: // ^table.count[]
1.66 paf 1340: add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6 paf 1341:
1342: // ^table.line[]
1.66 paf 1343: add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6 paf 1344:
1.10 paf 1345: // ^table.offset[]
1.133 paf 1346: // ^table.offset(offset)
1347: // ^table.offset[cur|set](offset)
1348: add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 2);
1.7 paf 1349:
1.10 paf 1350: // ^table.menu{code}
1351: // ^table.menu{code}[delim]
1.66 paf 1352: add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.74 paf 1353:
1.239 misha 1354: // ^table.hash[key field name]
1355: // ^table.hash[key field name][value field name(s) string/table]
1.163 paf 1356: add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 3);
1.32 paf 1357:
1.102 parser 1358: // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc]
1359: // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc]
1.66 paf 1360: add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8 paf 1361:
1.36 paf 1362: // ^table.locate[field;value]
1.176 paf 1363: add_native_method("locate", Method::CT_DYNAMIC, _locate, 1, 3);
1.39 paf 1364:
1365: // ^table.flip[]
1.66 paf 1366: add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41 paf 1367:
1368: // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
1.66 paf 1369: add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42 paf 1370:
1.157 paf 1371: // ^table.join[table][$.limit(10) $.offset(1) $.offset[cur] ]
1372: add_native_method("join", Method::CT_DYNAMIC, _join, 1, 2);
1.49 paf 1373:
1374:
1.239 misha 1375: // ^table::sql[query]
1376: // ^table::sql[query][$.limit(1) $.offset(2)]
1.100 parser 1377: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.88 parser 1378:
1.239 misha 1379: // ^table.columns[[column name]]
1.233 misha 1380: add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 1);
1.148 paf 1381:
1382: // ^table.select(expression) = table
1383: add_native_method("select", Method::CT_DYNAMIC, _select, 1, 1);
1.40 paf 1384: }
E-mail: