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