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