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