Annotation of parser3/src/classes/table.C, revision 1.110
1.20 paf 1: /** @file
1.47 paf 2: Parser: @b table parser class.
1.20 paf 3:
1.1 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.20 paf 5:
1.1 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7: */
1.110 ! parser 8: static const char *RCSId="$Id: table.C,v 1.109 2001/09/05 15:05:27 parser Exp $";
1.1 paf 9:
1.105 parser 10: #include "classes.h"
1.24 paf 11: #include "pa_config_includes.h"
1.16 paf 12: #include "pa_common.h"
1.1 paf 13: #include "pa_request.h"
14: #include "pa_vtable.h"
1.6 paf 15: #include "pa_vint.h"
1.51 paf 16: #include "pa_sql_connection.h"
1.72 paf 17: #include "pa_vbool.h"
1.1 paf 18:
1.66 paf 19: // defines
1.1 paf 20:
1.66 paf 21: #define TABLE_CLASS_NAME "table"
22:
23: // class
24:
25: class MTable : public Methoded {
26: public: // VStateless_class
27: Value *create_new_value(Pool& pool) { return new(pool) VTable(pool); }
28:
29: public:
30: MTable(Pool& pool);
1.70 paf 31:
32: public: // Methoded
1.66 paf 33: bool used_directly() { return true; }
34: };
1.1 paf 35:
36: // methods
1.66 paf 37:
1.61 paf 38: static void _set(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 39: Pool& pool=r.pool();
40: // data is last parameter
1.90 parser 41: Value& vdata=params->as_junction(params->size()-1, "body must be code");
1.44 paf 42:
1.45 paf 43: Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.61 paf 44: const String& data=r.process(vdata).as_string();
1.44 paf 45:
46: size_t pos_after=0;
47: // parse columns
48: Array *columns;
49: if(params->size()==2) {
50: columns=0;
1.21 paf 51: } else {
1.44 paf 52: columns=new(pool) Array(pool);
53:
54: Array head(pool);
55: data.split(head, &pos_after, "\n", 1, String::UL_CLEAN, 1);
56: if(head.size())
57: head.get_string(0)->split(*columns, 0, "\t", 1, String::UL_CLEAN);
58: }
59:
60: Table& table=*new(pool) Table(pool, &method_name, columns);
61: // parse cells
62: Array rows(pool);
63: data.split(rows, &pos_after, "\n", 1, String::UL_CLEAN);
1.98 parser 64: Array_iter i(rows);
65: while(i.has_next()) {
1.44 paf 66: Array& row=*new(pool) Array(pool);
1.98 parser 67: const String& string=*i.next_string();
1.44 paf 68: // remove empty lines
69: if(!string.size())
70: continue;
71:
72: string.split(row, 0, "\t", 1, String::UL_CLEAN);
73: table+=&row;
1.17 paf 74: }
1.1 paf 75:
1.44 paf 76: // replace any previous table value
77: static_cast<VTable *>(r.self)->set_table(table);
78: }
79:
1.61 paf 80: static void _load(Request& r, const String& method_name, MethodParams *params) {
1.44 paf 81: Pool& pool=r.pool();
82: // filename is last parameter
1.90 parser 83: Value& vfilename=params->as_no_junction(params->size()-1,
1.61 paf 84: "file name must not be code");
1.44 paf 85:
86: // loading text
1.61 paf 87: char *data=file_read_text(pool, r.absolute(vfilename.as_string()));
1.44 paf 88:
1.1 paf 89: // parse columns
90: Array *columns;
91: #ifndef NO_STRING_ORIGIN
92: const Origin& origin=method_name.origin();
1.2 paf 93: const char *file=origin.file;
1.1 paf 94: uint line=origin.line;
95: #endif
96: if(params->size()==2) {
97: columns=0;
98: } else {
99: columns=new(pool) Array(pool);
100:
101: if(char *row_chars=getrow(&data))
102: do {
103: String *name=new(pool) String(pool);
1.45 paf 104: name->APPEND_TAINTED(lsplit(&row_chars, '\t'), 0, file, line++);
1.1 paf 105: *columns+=name;
106: } while(row_chars);
107: }
108:
109: // parse cells
1.27 paf 110: Table& table=*new(pool) Table(pool, &method_name, columns);
1.1 paf 111: char *row_chars;
112: while(row_chars=getrow(&data)) {
1.28 paf 113: if(!*row_chars) // remove empty lines
114: continue;
1.1 paf 115: Array *row=new(pool) Array(pool);
116: while(char *cell_chars=lsplit(&row_chars, '\t')) {
117: String *cell=new(pool) String(pool);
1.44 paf 118: cell->APPEND_TAINTED(cell_chars, 0, file, line);
1.1 paf 119: *row+=cell;
120: }
1.107 parser 121: #ifndef NO_STRING_ORIGIN
1.1 paf 122: line++;
1.107 parser 123: #endif
1.1 paf 124: table+=row;
125: };
126:
127: // replace any previous table value
1.18 paf 128: static_cast<VTable *>(r.self)->set_table(table);
1.1 paf 129: }
1.2 paf 130:
1.91 parser 131: static Table *fill_month_days(Request& r,
132: const String& method_name, MethodParams *params, bool rus){
133: Pool& pool=r.pool();
134: Table *result=new(pool) Table(pool, &method_name, 0/*&columns*/);
135:
136: int year=params->as_int(1, r);
137: int month=max(1, min(params->as_int(2, r), 12)) -1;
138:
139: tm tmIn={0, 0, 0, 1, month, year-1900};
140: time_t t=mktime(&tmIn);
141: if(t<0)
142: PTHROW(0, 0,
143: &method_name,
144: "invalid date");
145: tm *tmOut=localtime(&t);
146:
147: int weekDay1=tmOut->tm_wday;
148: if(rus)
149: weekDay1=weekDay1?weekDay1-1:6; //sunday last
150: int monthDays=getMonthDays(year, month);
151:
152: for(int _day=1-weekDay1; _day<=monthDays;) {
153: Array& row=*new(pool) Array(pool, 7);
154: for(int wday=0; wday<7; wday++, _day++) {
1.93 parser 155: String *cell=new(pool) String(pool);
1.91 parser 156: if(_day>=1 && _day<=monthDays) {
1.106 parser 157: char *buf=(char *)pool.malloc(2+1);
1.93 parser 158: cell->APPEND_CLEAN(buf, sprintf(buf, "%02d", _day),
159: method_name.origin().file, method_name.origin().line);
160: }
1.91 parser 161: row+=cell;
162: }
163: *result+=&row;
164: }
165:
166: return result;
167: }
168:
169: static Table *fill_week_days(Request& r,
170: const String& method_name, MethodParams *params, bool rus){
171: Pool& pool=r.pool();
172: Array& columns=*new(pool) Array(pool, 4);
173: columns+=new(pool) String(pool, "year");
174: columns+=new(pool) String(pool, "month");
175: columns+=new(pool) String(pool, "day");
176: columns+=new(pool) String(pool, "weekday");
177: Table *result=new(pool) Table(pool, &method_name, &columns);
178:
179: int year=params->as_int(1, r);
180: int month=max(1, min(params->as_int(2, r), 12)) -1;
181: int day=params->as_int(3, r);
182:
183: tm tmIn={0, 0, 18, day, month, year-1900};
184: time_t t=mktime(&tmIn);
185: if(t<0)
186: PTHROW(0, 0,
187: &method_name,
188: "invalid date");
189: tm *tmOut=localtime(&t);
190:
191: int baseWeekDay=tmOut->tm_wday;
192: if(rus)
193: baseWeekDay=baseWeekDay?baseWeekDay-1:6; //sunday last
194:
195: t-=baseWeekDay*SECS_PER_DAY;
196:
197: for(int curWeekDay=0; curWeekDay<7; curWeekDay++, t+=SECS_PER_DAY) {
198: tm *tmOut=localtime(&t);
199: Array& row=*new(pool) Array(pool, 4);
1.93 parser 200: #define WDFILL(size, value) { \
1.106 parser 201: char *buf=(char *)pool.malloc(size+1); \
1.93 parser 202: String *cell=new(pool) String(pool); \
203: cell->APPEND_CLEAN(buf, sprintf(buf, "%0"#size"d", value), \
204: method_name.origin().file, \
205: method_name.origin().line); \
206: row+=cell; \
207: }
208: WDFILL(4, 1900+tmOut->tm_year);
209: WDFILL(2, 1+tmOut->tm_mon);
210: WDFILL(2, tmOut->tm_mday);
211: WDFILL(2, tmOut->tm_wday);
1.91 parser 212: *result+=&row;
213: }
214:
215: return result;
216: }
217:
218: static void _calendar(Request& r, const String& method_name, MethodParams *params) {
219: Pool& pool=r.pool();
220:
1.92 parser 221: const String& what=params->as_string(0, "format must be strig");
1.91 parser 222: bool rus=false;
1.92 parser 223: if(what=="rus")
1.91 parser 224: rus=true;
1.92 parser 225: else if(what=="eng")
1.91 parser 226: rus=false;
227: else
228: PTHROW(0, 0,
229: &what,
230: "must be rus|eng");
231:
232: Table *result=0;
233: if(params->size()==1+2)
234: result=fill_month_days(r, method_name, params, rus);
235: else // 1+3
236: result=fill_week_days(r, method_name, params, rus);
237:
238: // replace any previous table value
239: static_cast<VTable *>(r.self)->set_table(*result);
240: }
241:
242:
243:
244:
1.61 paf 245: static void _save(Request& r, const String& method_name, MethodParams *params) {
1.22 paf 246: Pool& pool=r.pool();
1.90 parser 247: Value& vtable_name=params->as_no_junction(params->size()-1,
1.61 paf 248: "file name must not be code");
1.22 paf 249:
1.31 paf 250: Table& table=static_cast<VTable *>(r.self)->table();
251:
252: String sdata(pool);
253: if(params->size()==1) { // not nameless=named output
254: // write out names line
255: if(table.columns()) { // named table
1.98 parser 256: Array_iter i(*table.columns());
257: while(i.has_next()) {
258: sdata.append(*i.next_string(), //*static_cast<String *>(table.columns()->quick_get(column)),
259: String::UL_TABLE);
260: if(i.has_next())
1.31 paf 261: sdata.APPEND_CONST("\t");
262: }
263: } else { // nameless table
1.98 parser 264: if(int lsize=table.size()?static_cast<Array *>(table.get(0))->size():0)
1.31 paf 265: for(int column=0; column<lsize; column++) {
1.106 parser 266: char *cindex_tab=(char *)pool.malloc(MAX_NUMBER);
1.31 paf 267: snprintf(cindex_tab, MAX_NUMBER, "%d\t", column);
268: sdata.APPEND_CONST(cindex_tab);
269: }
270: else
271: sdata.APPEND_CONST("empty nameless table");
272: }
273: sdata.APPEND_CONST("\n");
274: }
275: // data lines
1.98 parser 276: Array_iter i(table);
277: while(i.has_next()) {
278: Array_iter c(*static_cast<Array *>(i.next()));
279: while(c.has_next()) {
280: sdata.append(*c.next_string(), //*static_cast<String *>(row->quick_get(column)),
281: String::UL_TABLE);
282: if(c.has_next())
1.31 paf 283: sdata.APPEND_CONST("\t");
284: }
285: sdata.APPEND_CONST("\n");
286: }
287:
288: // write
1.61 paf 289: file_write(pool, r.absolute(vtable_name.as_string()),
1.48 paf 290: sdata.cstr(), sdata.size(), true);
1.22 paf 291: }
292:
1.93 parser 293: static void _count(Request& r, const String& method_name, MethodParams *) {
1.6 paf 294: Pool& pool=r.pool();
1.18 paf 295: Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size());
1.93 parser 296: value.set_name(method_name);
1.15 paf 297: r.write_no_lang(value);
1.6 paf 298: }
299:
1.61 paf 300: static void _line(Request& r, const String& method_name, MethodParams *) {
1.6 paf 301: Pool& pool=r.pool();
1.37 paf 302: Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().current());
1.93 parser 303: value.set_name(method_name);
1.15 paf 304: r.write_no_lang(value);
1.6 paf 305: }
306:
1.61 paf 307: static void _offset(Request& r, const String& method_name, MethodParams *params) {
1.6 paf 308: Pool& pool=r.pool();
1.18 paf 309: Table& table=static_cast<VTable *>(r.self)->table();
1.74 paf 310: if(params->size()) {
1.90 parser 311: Value& offset_expr=params->as_junction(0, "offset must be expression");
1.80 parser 312: table.shift(r.process(offset_expr).as_int());
1.74 paf 313: } else {
1.37 paf 314: Value& value=*new(pool) VInt(pool, table.current());
1.93 parser 315: value.set_name(method_name);
1.15 paf 316: r.write_no_lang(value);
1.6 paf 317: }
318: }
319:
1.61 paf 320: static void _menu(Request& r, const String& method_name, MethodParams *params) {
1.90 parser 321: Value& body_code=params->as_junction(0, "body must be code");
1.7 paf 322:
1.61 paf 323: Value *delim_code=params->size()==2?¶ms->get(1):0;
1.7 paf 324:
1.64 paf 325: VTable& vtable=*static_cast<VTable *>(r.self);
326: Table& table=vtable.table();
1.7 paf 327: bool need_delim=false;
1.99 parser 328: int saved_current=table.current();
329: int size=table.size();
330: for(int row=0; row<size; row++) {
1.22 paf 331: table.set_current(row);
1.7 paf 332:
1.12 paf 333: Value& processed_body=r.process(body_code);
1.7 paf 334: if(delim_code) { // delimiter set?
335: const String *string=processed_body.get_string();
336: if(need_delim && string && string->size()) // need delim & iteration produced string?
337: r.write_pass_lang(r.process(*delim_code));
338: need_delim=true;
339: }
340: r.write_pass_lang(processed_body);
341: }
1.99 parser 342: table.set_current(saved_current);
1.7 paf 343: }
344:
1.61 paf 345: static void _empty(Request& r, const String& method_name, MethodParams *params) {
1.75 paf 346: Pool& pool=r.pool();
1.18 paf 347: Table& table=static_cast<VTable *>(r.self)->table();
1.75 paf 348:
349: r.write_no_lang(*new(pool) VBool(pool, table.size()==0));
1.8 paf 350: }
1.15 paf 351:
1.110 ! parser 352: #ifndef DOXYGEN
1.74 paf 353: struct Row_info {
354: Table *table;
355: int key_field;
356: Array *value_fields;
357: Hash *hash;
358: };
1.110 ! parser 359: #endif
1.74 paf 360: static void table_row_to_hash(Array::Item *value, void *info) {
361: Array& row=*static_cast<Array *>(value);
362: Row_info& ri=*static_cast<Row_info *>(info);
363: Pool& pool=ri.table->pool();
364:
1.76 paf 365: if(ri.key_field<row.size()) {
366: VHash& result=*new(pool) VHash(pool);
367: Hash& hash=*result.get_hash();
1.74 paf 368: for(int i=0; i<ri.value_fields->size(); i++) {
369: int value_field=ri.value_fields->get_int(i);
370: if(value_field<row.size())
371: hash.put(
1.91 parser 372: *ri.table->columns()->get_string(value_field),
1.74 paf 373: new(pool) VString(*row.get_string(value_field)));
374: }
375:
1.77 paf 376: ri.hash->put(*row.get_string(ri.key_field), &result);
1.74 paf 377: }
378: }
379: static void _hash(Request& r, const String& method_name, MethodParams *params) {
1.97 parser 380: Pool& pool=r.pool();
1.74 paf 381: Table& table=static_cast<VTable *>(r.self)->table();
1.97 parser 382: Value& result=*new(pool) VHash(pool);
1.74 paf 383: if(const Array *columns=table.columns())
384: if(columns->size()>1) {
385:
1.90 parser 386: const String& key_field_name=params->as_no_junction(0,
1.74 paf 387: "key field name must not be code").as_string();
388: int key_field=table.column_name2index(key_field_name, true);
389: int value_fields_count=params->size()-1;
390: bool value_fields_by_params=value_fields_count!=0;
391: if(!value_fields_by_params)
392: value_fields_count=columns->size()-1; // all columns except key
393: Array value_fields(pool, value_fields_count);
394: if(value_fields_by_params) {
395: for(int i=1; i<params->size(); i++) {
1.91 parser 396: const String& value_field_name=params->as_no_junction(i,
1.74 paf 397: "value field name must not be code").as_string();
398: value_fields+=table.column_name2index(value_field_name, true);
399: }
400: } else { // by all columns except key
401: for(int i=0; i<columns->size(); i++)
402: if(i!=key_field)
403: value_fields+=i;
404: }
405:
406: // integers: key_field & value_fields
407: Row_info row_info={&table, key_field, &value_fields, result.get_hash()};
408: table.for_each(table_row_to_hash, &row_info);
409: }
1.97 parser 410: result.set_name(method_name);
411: r.write_no_lang(result);
1.74 paf 412: }
413:
1.110 ! parser 414: #ifndef DOXYGEN
1.78 paf 415: struct Table_seq_item {
1.34 paf 416: Array *row;
417: union {
418: char *c_str;
419: double d;
420: } value;
1.32 paf 421: };
1.110 ! parser 422: #endif
1.34 paf 423: static int sort_cmp_string(const void *a, const void *b) {
424: return strcmp(
1.78 paf 425: static_cast<const Table_seq_item *>(a)->value.c_str,
426: static_cast<const Table_seq_item *>(b)->value.c_str
1.34 paf 427: );
428: }
429: static int sort_cmp_double(const void *a, const void *b) {
1.78 paf 430: double va=static_cast<const Table_seq_item *>(a)->value.d;
431: double vb=static_cast<const Table_seq_item *>(b)->value.d;
1.34 paf 432: if(va<vb)
433: return -1;
434: else if(va>vb)
435: return +1;
436: else
437: return 0;
438: }
1.61 paf 439: static void _sort(Request& r, const String& method_name, MethodParams *params) {
1.101 parser 440: Pool& pool=r.pool();
1.90 parser 441: Value& key_maker=params->as_junction(0, "key-maker must be code");
1.61 paf 442:
1.102 parser 443: bool reverse=params->size()==2/*..[desc|asc|]*/?
1.103 parser 444: reverse=params->as_no_junction(1, "order must not be code").as_string()=="desc":
1.104 parser 445: false; // default=asc
1.32 paf 446:
1.101 parser 447: Table& old_table=static_cast<VTable *>(r.self)->table();
448: Table& new_table=*new(pool) Table(pool, &method_name, old_table.columns());
1.34 paf 449:
1.101 parser 450: Table_seq_item *seq=(Table_seq_item *)pool.malloc(sizeof(Table_seq_item)*old_table.size());
1.34 paf 451: int i;
452:
453: // calculate key values
454: bool key_values_are_strings=true;
1.101 parser 455: // save 'current'
456: int saved_current=old_table.current();
457: for(i=0; i<old_table.size(); i++) {
458: old_table.set_current(i);
1.32 paf 459: // calculate key value
1.101 parser 460: seq[i].row=(MethodParams *)old_table.get(i);
1.34 paf 461: Value& value=*r.process(key_maker).as_expr_result(true/*return string as-is*/);
462: if(i==0) // determining key values type by first one
463: key_values_are_strings=value.is_string();
464:
465: if(key_values_are_strings)
466: seq[i].value.c_str=value.as_string().cstr();
467: else
468: seq[i].value.d=value.as_double();
1.32 paf 469: }
1.101 parser 470: // restore 'current'
471: old_table.set_current(saved_current);
1.32 paf 472: // sort keys
1.101 parser 473: _qsort(seq, old_table.size(), sizeof(Table_seq_item),
1.34 paf 474: key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32 paf 475:
1.34 paf 476: // reorder table as they require in 'seq'
1.101 parser 477: for(i=0; i<old_table.size(); i++)
478: new_table+=seq[reverse?old_table.size()-1-i:i].row;
1.32 paf 479:
1.101 parser 480: r.write_no_lang(*new(pool) VTable(pool, &new_table));
1.32 paf 481: }
482:
1.61 paf 483: static void _locate(Request& r, const String& method_name, MethodParams *params) {
1.72 paf 484: Pool& pool=r.pool();
485:
1.36 paf 486: VTable& vtable=*static_cast<VTable *>(r.self);
487: Table& table=vtable.table();
1.72 paf 488: Value& result=*new(pool) VBool(pool, table.locate(
1.91 parser 489: params->get(0).as_string(),
1.72 paf 490: params->get(1).as_string()));
491: result.set_name(method_name);
492: r.write_no_lang(result);
1.37 paf 493: }
494:
1.61 paf 495: static void _flip(Request& r, const String& method_name, MethodParams *params) {
1.39 paf 496: Pool& pool=r.pool();
497: VTable& vtable=*static_cast<VTable *>(r.self);
498:
499: Table& old_table=*vtable.get_table();
500: Table& new_table=*new(pool) Table(pool, &method_name, 0/*nameless*/);
501: if(old_table.size())
502: if(int old_cols=old_table.at(0).size())
503: for(int column=0; column<old_cols; column++) {
504: Array& new_row=*new(pool) Array(pool, old_table.size());
505: for(int i=0; i<old_table.size(); i++) {
506: const Array& old_row=old_table.at(i);
507: new_row+=column<old_row.size()?old_row.get(column):empty_string;
508: }
509: new_table+=&new_row;
510: }
511:
1.99 parser 512: r.write_no_lang(*new(pool) VTable(pool, &new_table));
1.39 paf 513: }
514:
1.61 paf 515: static void _append(Request& r, const String& method_name, MethodParams *params) {
1.41 paf 516: Pool& pool=r.pool();
517: // data is last parameter
1.61 paf 518: const String& string=
1.90 parser 519: r.process(params->as_junction(0, "body must be code")).as_string();
1.41 paf 520:
521: // parse cells
522: Array& row=*new(pool) Array(pool);
1.46 paf 523: string.split(row, 0, "\t", 1, String::UL_CLEAN);
1.41 paf 524:
1.66 paf 525: VTable& vtable=*static_cast<VTable *>(r.self);
526: vtable.table()+=&row;
1.41 paf 527: }
528:
1.61 paf 529: static void _join(Request& r, const String& method_name, MethodParams *params) {
1.42 paf 530: Pool& pool=r.pool();
531:
1.90 parser 532: Table *maybe_src=params->as_no_junction(0, "table ref must not be code").get_table();
1.42 paf 533: if(!maybe_src)
1.91 parser 534: PTHROW(0, 0,
535: &method_name,
1.42 paf 536: "source is not a table");
537:
538: Table& src=*maybe_src;
539: Table& dest=static_cast<VTable *>(r.self)->table();
540: if(&src == &dest)
1.91 parser 541: PTHROW(0, 0,
542: &method_name,
1.42 paf 543: "source and destination are same table");
544:
545: if(const Array *dest_columns=dest.columns()) { // dest is named
546: int saved_src_current=src.current();
547: for(int src_row=0; src_row<src.size(); src_row++) {
548: src.set_current(src_row);
549: Array& dest_row=*new(pool) Array(pool);
550: for(int dest_column=0; dest_column<dest_columns->size(); dest_column++)
551: dest_row+=src.item(*dest_columns->get_string(dest_column));
552: dest+=&dest_row;
553: }
554: src.set_current(saved_src_current);
555: } else { // dest is nameless
556: for(int src_row=0; src_row<src.size(); src_row++)
557: dest+=&src.at(src_row);
558: }
559: }
560:
1.94 parser 561: #ifndef DOXYGEN
562: class Table_sql_event_handlers : public SQL_Driver_query_event_handlers {
563: public:
564: Table_sql_event_handlers(Pool& apool, const String& amethod_name,
565: const String& astatement_string, const char *astatement_cstr) :
566: pool(apool),
567: method_name(amethod_name),
568: statement_string(astatement_string),
569: statement_cstr(astatement_cstr),
570: columns(*new(pool) Array(pool)),
571: row(0), row_index(0),
572: table(0)
573: {
574: }
575:
576: void add_column(void *ptr, size_t size) {
577: String *column=new(pool) String(pool);
578: column->APPEND_TAINTED(
579: (const char *)ptr, size,
580: statement_cstr, 0);
581: columns+=column;
582: }
583: void before_rows() {
584: table=new(pool) Table(pool, &method_name, &columns);
585: }
586: void add_row() {
587: (*table)+=(row=new(pool) Array(pool));
588: }
589: void add_row_cell(void *ptr, size_t size) {
590: String *cell=new(pool) String(pool);
591: if(size)
592: cell->APPEND_TAINTED(
593: (const char *)ptr, size,
594: statement_cstr, row_index++);
595: (*row)+=cell;
596: }
597:
598: private:
599: Pool& pool;
600: const String& method_name;
601: const String& statement_string; const char *statement_cstr;
602: Array& columns;
603: Array *row;
604: uint row_index;
605: public:
606: Table *table;
607: };
608: #endif
1.61 paf 609: static void _sql(Request& r, const String& method_name, MethodParams *params) {
1.49 paf 610: Pool& pool=r.pool();
611:
612: if(!r.connection)
1.91 parser 613: PTHROW(0, 0,
614: &method_name,
1.49 paf 615: "without connect");
616:
1.90 parser 617: Value& statement=params->as_junction(0, "statement must be code");
1.49 paf 618:
1.53 paf 619: ulong limit=0;
1.100 parser 620: ulong offset=0;
1.49 paf 621: if(params->size()>1) {
1.109 parser 622: Value& voptions=params->as_no_junction(1, "options must be hash, not code");
623: if(voptions.is_defined())
624: if(Hash *options=voptions.get_hash()) {
625: if(Value *vlimit=(Value *)options->get(*sql_limit_name))
626: limit=(ulong)r.process(*vlimit).as_double();
627: if(Value *voffset=(Value *)options->get(*sql_offset_name))
628: offset=(ulong)r.process(*voffset).as_double();
629: } else
630: PTHROW(0, 0,
631: &method_name,
632: "options must be hash");
1.49 paf 633: }
634:
1.54 paf 635: Temp_lang temp_lang(r, String::UL_SQL);
636: const String& statement_string=r.process(statement).as_string();
1.55 paf 637: const char *statement_cstr=
638: statement_string.cstr(String::UL_UNSPECIFIED, r.connection);
1.94 parser 639: Table_sql_event_handlers handlers(pool, method_name,
640: statement_string, statement_cstr);
1.52 paf 641: bool need_rethrow=false; Exception rethrow_me;
1.108 parser 642: PTRY {
1.52 paf 643: r.connection->query(
1.91 parser 644: statement_cstr, offset, limit,
1.94 parser 645: handlers);
1.52 paf 646: }
1.82 parser 647: PCATCH(e) { // query problem
1.52 paf 648: rethrow_me=e; need_rethrow=true;
649: }
650: PEND_CATCH
651: if(need_rethrow)
1.91 parser 652: PTHROW(rethrow_me.type(), rethrow_me.code(),
1.53 paf 653: &statement_string, // setting more specific source [were url]
1.52 paf 654: rethrow_me.comment());
1.96 parser 655:
656: Table *result=
657: handlers.table?handlers.table: // query resulted in table? return it
658: new(pool) Table(pool, &method_name, 0); // query returned no table, fake it
1.49 paf 659:
660: // replace any previous table value
1.96 parser 661: static_cast<VTable *>(r.self)->set_table(*result);
1.49 paf 662: }
663:
1.88 parser 664: static void _columns(Request& r, const String& method_name, MethodParams *) {
665: Pool& pool=r.pool();
666:
667: Array& result_columns=*new(pool) Array(pool);
1.89 parser 668: result_columns+=new(pool) String(pool, "column");
1.88 parser 669: Table& result_table=*new(pool) Table(pool, &method_name, &result_columns);
670:
671: Table& source_table=static_cast<VTable *>(r.self)->table();
672: if(const Array *source_columns=source_table.columns()) {
1.98 parser 673: Array_iter i(*source_columns);
674: while(i.has_next()) {
1.88 parser 675: Array& result_row=*new(pool) Array(pool);
1.98 parser 676: result_row+=i.next();
1.88 parser 677: result_table+=&result_row;
678: }
679: }
680:
681: VTable& result=*new(pool) VTable(pool, &result_table);
682: result.set_name(method_name);
683: r.write_no_lang(result);
684: }
685:
1.66 paf 686: // constructor
687:
688: MTable::MTable(Pool& apool) : Methoded(apool) {
689: set_name(*NEW String(pool(), TABLE_CLASS_NAME));
690:
1.49 paf 691: // ^table:set{data}
692: // ^table:set[nameless]{data}
1.66 paf 693: add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2);
1.2 paf 694:
1.49 paf 695: // ^table:load[file]
696: // ^table:load[nameless;file]
1.66 paf 697: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.91 parser 698:
699: // ^table:calendar[month|montheng;year;month]
700: // ^table:calendar[week|weekeng;year;month;day]
701: add_native_method("calendar", Method::CT_DYNAMIC, _calendar, 3, 4);
1.22 paf 702:
703: // ^table.save[file]
704: // ^table.save[nameless;file]
1.66 paf 705: add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.6 paf 706:
707: // ^table.count[]
1.66 paf 708: add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6 paf 709:
710: // ^table.line[]
1.66 paf 711: add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6 paf 712:
1.10 paf 713: // ^table.offset[]
714: // ^table.offset[offset]
1.66 paf 715: add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1);
1.7 paf 716:
1.10 paf 717: // ^table.menu{code}
718: // ^table.menu{code}[delim]
1.66 paf 719: add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.8 paf 720:
1.75 paf 721: // ^table.empty[]
722: add_native_method("empty", Method::CT_DYNAMIC, _empty, 0, 0);
1.74 paf 723:
1.79 paf 724: // ^table:hash[key field name]
725: // ^table:hash[key field name][value field name;...]
1.74 paf 726: add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 1000);
1.32 paf 727:
1.102 parser 728: // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc]
729: // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc]
1.66 paf 730: add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8 paf 731:
1.36 paf 732: // ^table.locate[field;value]
1.66 paf 733: add_native_method("locate", Method::CT_DYNAMIC, _locate, 2, 2);
1.39 paf 734:
735: // ^table.flip[]
1.66 paf 736: add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41 paf 737:
738: // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
1.66 paf 739: add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42 paf 740:
741: // ^table.join[table]
1.66 paf 742: add_native_method("join", Method::CT_DYNAMIC, _join, 1, 1);
1.49 paf 743:
744:
1.100 parser 745: // ^table:sql[query]
746: // ^table:sql[query][$.limit(1) $.offset(2)]
747: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2);
1.88 parser 748:
749: // ^table:columns[]
750: add_native_method("columns", Method::CT_DYNAMIC, _columns, 0, 0);
1.66 paf 751: }
752:
753: // global variable
754:
755: Methoded *table_class;
756:
757: // creator
758:
759: Methoded *MTable_create(Pool& pool) {
760: return table_class=new(pool) MTable(pool);
1.40 paf 761: }
E-mail: