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