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