Annotation of parser3/src/classes/table.C, revision 1.55
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.55 ! paf 8: $Id: table.C,v 1.54 2001/04/05 13:19:40 paf Exp $
1.1 paf 9: */
10:
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 "_table.h"
15: #include "pa_vtable.h"
1.6 paf 16: #include "pa_vint.h"
1.51 paf 17: #include "pa_sql_connection.h"
1.1 paf 18:
19: // global var
20:
1.14 paf 21: VStateless_class *table_class;
1.1 paf 22:
23: // methods
1.44 paf 24: static void _set(Request& r, const String& method_name, Array *params) {
1.1 paf 25: Pool& pool=r.pool();
26: // data is last parameter
1.44 paf 27: Value *vdata=static_cast<Value *>(params->get(params->size()-1));
28: // forcing {this body type}
29: r.fail_if_junction_(false, *vdata, method_name, "body must be junction");
30:
1.45 paf 31: Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.44 paf 32: const String& data=r.process(*vdata).as_string();
33:
34: size_t pos_after=0;
35: // parse columns
36: Array *columns;
37: if(params->size()==2) {
38: columns=0;
1.21 paf 39: } else {
1.44 paf 40: columns=new(pool) Array(pool);
41:
42: Array head(pool);
43: data.split(head, &pos_after, "\n", 1, String::UL_CLEAN, 1);
44: if(head.size())
45: head.get_string(0)->split(*columns, 0, "\t", 1, String::UL_CLEAN);
46: }
47:
48: Table& table=*new(pool) Table(pool, &method_name, columns);
49: // parse cells
50: Array rows(pool);
51: data.split(rows, &pos_after, "\n", 1, String::UL_CLEAN);
52: int size=rows.quick_size();
53: for(int i=0; i<size; i++) {
54: Array& row=*new(pool) Array(pool);
55: const String& string=*rows.quick_get_string(i);
56: // remove empty lines
57: if(!string.size())
58: continue;
59:
60: string.split(row, 0, "\t", 1, String::UL_CLEAN);
61: table+=&row;
1.17 paf 62: }
1.1 paf 63:
1.44 paf 64: // replace any previous table value
65: static_cast<VTable *>(r.self)->set_table(table);
66: }
67:
68: static void _load(Request& r, const String& method_name, Array *params) {
69: Pool& pool=r.pool();
70: // filename is last parameter
71: Value *vfilename=static_cast<Value *>(params->get(params->size()-1));
72: // forcing [this file name type]
73: r.fail_if_junction_(true, *vfilename,
74: method_name, "file name must not be junction");
75:
76: // loading text
1.48 paf 77: char *data=file_read_text(pool, r.absolute(vfilename->as_string()));
1.44 paf 78:
1.1 paf 79: // parse columns
80: Array *columns;
81: #ifndef NO_STRING_ORIGIN
82: const Origin& origin=method_name.origin();
1.2 paf 83: const char *file=origin.file;
1.1 paf 84: uint line=origin.line;
85: #endif
86: if(params->size()==2) {
87: columns=0;
88: } else {
89: columns=new(pool) Array(pool);
90:
91: if(char *row_chars=getrow(&data))
92: do {
93: String *name=new(pool) String(pool);
1.45 paf 94: name->APPEND_TAINTED(lsplit(&row_chars, '\t'), 0, file, line++);
1.1 paf 95: *columns+=name;
96: } while(row_chars);
97: }
98:
99: // parse cells
1.27 paf 100: Table& table=*new(pool) Table(pool, &method_name, columns);
1.1 paf 101: char *row_chars;
102: while(row_chars=getrow(&data)) {
1.28 paf 103: if(!*row_chars) // remove empty lines
104: continue;
1.1 paf 105: Array *row=new(pool) Array(pool);
106: while(char *cell_chars=lsplit(&row_chars, '\t')) {
107: String *cell=new(pool) String(pool);
1.44 paf 108: cell->APPEND_TAINTED(cell_chars, 0, file, line);
1.1 paf 109: *row+=cell;
110: }
111: line++;
112: table+=row;
113: };
114:
115: // replace any previous table value
1.18 paf 116: static_cast<VTable *>(r.self)->set_table(table);
1.1 paf 117: }
1.2 paf 118:
1.22 paf 119: static void _save(Request& r, const String& method_name, Array *params) {
120: Pool& pool=r.pool();
1.39 paf 121: Value *vtable_name=static_cast<Value *>(params->get(params->size()-1));
1.44 paf 122: // forcing this body type]
1.39 paf 123: r.fail_if_junction_(true, *vtable_name,
1.22 paf 124: method_name, "file name must not be junction");
125:
1.31 paf 126: Table& table=static_cast<VTable *>(r.self)->table();
127:
128: String sdata(pool);
129: if(params->size()==1) { // not nameless=named output
130: // write out names line
131: if(table.columns()) { // named table
132: for(int column=0; column<table.columns()->size(); column++) {
133: if(column)
134: sdata.APPEND_CONST("\t");
135: sdata.append(*static_cast<String *>(table.columns()->quick_get(column)),
136: String::UL_TABLE);
137: }
138: } else { // nameless table
139: int lsize=table.size()?static_cast<Array *>(table.get(0))->size():0;
140: if(lsize)
141: for(int column=0; column<lsize; column++) {
142: char *cindex_tab=(char *)malloc(MAX_NUMBER);
143: snprintf(cindex_tab, MAX_NUMBER, "%d\t", column);
144: sdata.APPEND_CONST(cindex_tab);
145: }
146: else
147: sdata.APPEND_CONST("empty nameless table");
148: }
149: sdata.APPEND_CONST("\n");
150: }
151: // data lines
152: for(int index=0; index<table.size(); index++) {
153: Array *row=static_cast<Array *>(table.quick_get(index));
154: for(int column=0; column<row->size(); column++) {
155: if(column)
156: sdata.APPEND_CONST("\t");
157: sdata.append(*static_cast<String *>(row->quick_get(column)),
158: String::UL_TABLE);
159: }
160: sdata.APPEND_CONST("\n");
161: }
162:
163: // write
1.48 paf 164: file_write(pool, r.absolute(vtable_name->as_string()),
165: sdata.cstr(), sdata.size(), true);
1.22 paf 166: }
167:
1.39 paf 168: static void _count(Request& r, const String&method_name, Array *) {
1.6 paf 169: Pool& pool=r.pool();
1.18 paf 170: Value& value=*new(pool) VInt(pool, static_cast<VTable *>(r.self)->table().size());
1.15 paf 171: r.write_no_lang(value);
1.6 paf 172: }
173:
1.39 paf 174: static void _line(Request& r, const String& method_name, Array *) {
1.6 paf 175: Pool& pool=r.pool();
1.37 paf 176: Value& value=*new(pool) VInt(pool, 1+static_cast<VTable *>(r.self)->table().current());
1.15 paf 177: r.write_no_lang(value);
1.6 paf 178: }
179:
1.39 paf 180: static void _offset(Request& r, const String& method_name, Array *params) {
1.6 paf 181: Pool& pool=r.pool();
1.18 paf 182: Table& table=static_cast<VTable *>(r.self)->table();
1.37 paf 183: if(params->size())
184: table.shift((int)r.process(*static_cast<Value *>(params->get(0))).as_double());
185: else {
186: Value& value=*new(pool) VInt(pool, table.current());
1.15 paf 187: r.write_no_lang(value);
1.6 paf 188: }
189: }
190:
1.54 paf 191: /// @test $a.menu{ $a[123] }
1.7 paf 192: static void _menu(Request& r, const String& method_name, Array *params) {
193: Value& body_code=*static_cast<Value *>(params->get(0));
194: // forcing ^menu{this param type}
195: r.fail_if_junction_(false, body_code,
196: method_name, "body must be junction");
197:
198: Value *delim_code=params->size()==2?static_cast<Value *>(params->get(1)):0;
199:
1.18 paf 200: Table& table=static_cast<VTable *>(r.self)->table();
1.7 paf 201: bool need_delim=false;
1.37 paf 202: int saved_current=table.current();
1.22 paf 203: for(int row=0; row<table.size(); row++) {
204: table.set_current(row);
1.7 paf 205:
1.12 paf 206: Value& processed_body=r.process(body_code);
1.7 paf 207: if(delim_code) { // delimiter set?
208: const String *string=processed_body.get_string();
209: if(need_delim && string && string->size()) // need delim & iteration produced string?
210: r.write_pass_lang(r.process(*delim_code));
211: need_delim=true;
212: }
213: r.write_pass_lang(processed_body);
214: }
1.37 paf 215: table.set_current(saved_current);
1.7 paf 216: }
217:
1.39 paf 218: static void _empty(Request& r, const String& method_name, Array *params) {
1.18 paf 219: Table& table=static_cast<VTable *>(r.self)->table();
1.8 paf 220: if(table.size()==0) {
221: Value& value=r.process(*static_cast<Value *>(params->get(0)));
222: r.write_pass_lang(value);
223: } else if(params->size()==2) {
224: Value& value=r.process(*static_cast<Value *>(params->get(1)));
225: r.write_pass_lang(value);
226: }
227: }
1.15 paf 228:
1.29 paf 229: struct Record_info {
230: Pool *pool;
231: Table *table;
232: Hash *hash;
233: };
234: static void store_column_item_to_hash(Array::Item *item, void *info) {
235: Record_info& ri=*static_cast<Record_info *>(info);
236: String& column_name=*static_cast<String *>(item);
237: const String *column_item=ri.table->item(column_name);
238: Value *value;
239: if(column_item)
240: value=new(*ri.pool) VString(*column_item);
241: else
242: value=new(*ri.pool) VUnknown(*ri.pool);
243: ri.hash->put(column_name, value);
244: }
1.39 paf 245: static void _record(Request& r, const String& method_name, Array *params) {
1.29 paf 246: Table& table=static_cast<VTable *>(r.self)->table();
247: if(const Array *columns=table.columns()) {
248: Pool& pool=r.pool();
249: Value& value=*new(pool) VHash(pool);
250: Record_info record_info={&pool, &table, value.get_hash()};
251: columns->for_each(store_column_item_to_hash, &record_info);
252:
253: r.write_no_lang(value);
254: }
255: }
256:
1.34 paf 257: struct Seq_item {
258: Array *row;
259: union {
260: char *c_str;
261: double d;
262: } value;
1.32 paf 263: };
1.34 paf 264: static int sort_cmp_string(const void *a, const void *b) {
265: return strcmp(
266: static_cast<const Seq_item *>(a)->value.c_str,
267: static_cast<const Seq_item *>(b)->value.c_str
268: );
269: }
270: static int sort_cmp_double(const void *a, const void *b) {
271: double va=static_cast<const Seq_item *>(a)->value.d;
272: double vb=static_cast<const Seq_item *>(b)->value.d;
273: if(va<vb)
274: return -1;
275: else if(va>vb)
276: return +1;
277: else
278: return 0;
279: }
1.32 paf 280: static void _sort(Request& r, const String& method_name, Array *params) {
281: Value& key_maker=*(Value *)params->get(0);
282: // forcing ^sort{this} ^sort(or this) param type
283: r.fail_if_junction_(false, key_maker, method_name, "key-maker must be junction");
284:
285: bool reverse;
286: if(params->size()==2) { // ..[asc|desc]
1.35 paf 287: Value& order=*(Value *)params->get(1);
1.32 paf 288: // forcing ..[this param-type]
1.35 paf 289: r.fail_if_junction_(true, order, method_name, "order must not be junction");
290: reverse=order.as_string()=="desc";
1.32 paf 291: } else
292: reverse=false;
293:
294: Table& table=static_cast<VTable *>(r.self)->table();
1.34 paf 295:
296: // anything to sort?
297: if(!table.size())
298: return;
299:
300: Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*table.size());
301: int i;
302:
303: // calculate key values
304: bool key_values_are_strings=true;
305: for(i=0; i<table.size(); i++) {
1.32 paf 306: table.set_current(i);
307: // calculate key value
1.34 paf 308: seq[i].row=(Array *)table.get(i);
309: Value& value=*r.process(key_maker).as_expr_result(true/*return string as-is*/);
310: if(i==0) // determining key values type by first one
311: key_values_are_strings=value.is_string();
312:
313: if(key_values_are_strings)
314: seq[i].value.c_str=value.as_string().cstr();
315: else
316: seq[i].value.d=value.as_double();
1.32 paf 317: }
318: // sort keys
1.34 paf 319: _qsort(seq, table.size(), sizeof(Seq_item),
320: key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32 paf 321:
1.34 paf 322: // reorder table as they require in 'seq'
323: for(i=0; i<table.size(); i++)
324: table.put(i, seq[reverse?table.size()-1-i:i].row);
1.32 paf 325:
1.34 paf 326: // reset 'current'
1.32 paf 327: table.set_current(0);
328: }
329:
1.39 paf 330: static void _locate(Request& r, const String& method_name, Array *params) {
1.36 paf 331: VTable& vtable=*static_cast<VTable *>(r.self);
332: Table& table=vtable.table();
333: vtable.last_locate_was_successful=table.locate(
334: static_cast<Value *>(params->get(0))->as_string(),
335: static_cast<Value *>(params->get(1))->as_string());
336: }
337:
1.37 paf 338: static void _found(Request& r, const String& method_name, Array *params) {
339: if(static_cast<VTable *>(r.self)->last_locate_was_successful) {
340: Value& then_code=*static_cast<Value *>(params->get(0));
341: // forcing ^found{this param type}
342: r.fail_if_junction_(false, then_code,
343: method_name, "found-parameter must be junction");
344: r.write_pass_lang(r.process(then_code));
345: } else if(params->size()==2) {
346: Value& else_code=*static_cast<Value *>(params->get(1));
347: // forcing ^found{this param type}
348: r.fail_if_junction_(false, else_code,
349: method_name, "not found-parameter must be junction");
350: r.write_pass_lang(r.process(else_code));
351: }
352: }
353:
1.39 paf 354: static void _flip(Request& r, const String& method_name, Array *params) {
355: Pool& pool=r.pool();
356: VTable& vtable=*static_cast<VTable *>(r.self);
357:
358: Table& old_table=*vtable.get_table();
359: Table& new_table=*new(pool) Table(pool, &method_name, 0/*nameless*/);
360: if(old_table.size())
361: if(int old_cols=old_table.at(0).size())
362: for(int column=0; column<old_cols; column++) {
363: Array& new_row=*new(pool) Array(pool, old_table.size());
364: for(int i=0; i<old_table.size(); i++) {
365: const Array& old_row=old_table.at(i);
366: new_row+=column<old_row.size()?old_row.get(column):empty_string;
367: }
368: new_table+=&new_row;
369: }
370:
371: vtable.set_table(new_table);
372: }
373:
1.41 paf 374: static void _append(Request& r, const String& method_name, Array *params) {
375: Pool& pool=r.pool();
376: // data is last parameter
377: Value *value=static_cast<Value *>(params->get(0));
1.46 paf 378: // forcing {this body type}
379: r.fail_if_junction_(false, *value, method_name, "body must be junction");
1.41 paf 380:
1.46 paf 381: const String& string=r.process(*value).as_string();
1.41 paf 382:
383: // parse cells
384: Array& row=*new(pool) Array(pool);
1.46 paf 385: string.split(row, 0, "\t", 1, String::UL_CLEAN);
1.41 paf 386:
387: static_cast<VTable *>(r.self)->table()+=&row;
388: }
389:
1.42 paf 390: static void _join(Request& r, const String& method_name, Array *params) {
391: Pool& pool=r.pool();
392:
393: Value *value=static_cast<Value *>(params->get(0));
394: // forcing [this table ref type]
395: r.fail_if_junction_(true, *value, method_name, "table ref must not be junction");
396:
397: Table *maybe_src=value->get_table();
398: if(!maybe_src)
1.49 paf 399: PTHROW(0, 0,
1.42 paf 400: &method_name,
401: "source is not a table");
402:
403: Table& src=*maybe_src;
404: Table& dest=static_cast<VTable *>(r.self)->table();
405: if(&src == &dest)
1.49 paf 406: PTHROW(0, 0,
1.42 paf 407: &method_name,
408: "source and destination are same table");
409:
410: if(const Array *dest_columns=dest.columns()) { // dest is named
411: int saved_src_current=src.current();
412: for(int src_row=0; src_row<src.size(); src_row++) {
413: src.set_current(src_row);
414: Array& dest_row=*new(pool) Array(pool);
415: for(int dest_column=0; dest_column<dest_columns->size(); dest_column++)
416: dest_row+=src.item(*dest_columns->get_string(dest_column));
417: dest+=&dest_row;
418: }
419: src.set_current(saved_src_current);
420: } else { // dest is nameless
421: for(int src_row=0; src_row<src.size(); src_row++)
422: dest+=&src.at(src_row);
423: }
424: }
425:
1.51 paf 426: /// ^table:sql{query}[(count[;offset])] @test limit/offset
1.49 paf 427: static void _sql(Request& r, const String& method_name, Array *params) {
428: Pool& pool=r.pool();
429:
430: if(!r.connection)
431: PTHROW(0, 0,
432: &method_name,
433: "without connect");
434:
1.51 paf 435: Value& statement=*static_cast<Value *>(params->get(0));
1.49 paf 436: // forcing {this query param type}
1.54 paf 437: r.fail_if_junction_(false, statement, method_name, "statement must be junction");
1.49 paf 438:
1.53 paf 439: ulong limit=0;
1.49 paf 440: if(params->size()>1) {
1.53 paf 441: Value& limit_code=*static_cast<Value *>(params->get(1));
442: // forcing (this limit param type)
443: r.fail_if_junction_(false, limit_code, method_name, "limit must be expression");
444: limit=(uint)r.process(limit_code).as_double();
1.49 paf 445: }
446:
1.51 paf 447: ulong offset=0;
1.49 paf 448: if(params->size()>2) {
1.51 paf 449: Value& offset_code=*static_cast<Value *>(params->get(2));
1.49 paf 450: // forcing (this limit param type)
1.51 paf 451: r.fail_if_junction_(false, offset_code, method_name, "offset must be expression");
452: offset=(ulong)r.process(offset_code).as_double();
1.49 paf 453: }
454:
1.54 paf 455: Temp_lang temp_lang(r, String::UL_SQL);
456: const String& statement_string=r.process(statement).as_string();
1.55 ! paf 457: const char *statement_cstr=
! 458: statement_string.cstr(String::UL_UNSPECIFIED, r.connection);
1.51 paf 459: unsigned int sql_column_count; SQL_Driver::Cell *sql_columns;
460: unsigned long sql_row_count; SQL_Driver::Cell **sql_rows;
1.52 paf 461: bool need_rethrow=false; Exception rethrow_me;
462: PTRY {
463: r.connection->query(
1.53 paf 464: statement_cstr, offset, limit,
1.52 paf 465: &sql_column_count, &sql_columns,
466: &sql_row_count, &sql_rows);
467: }
468: PCATCH(e) { // connect/process problem
469: rethrow_me=e; need_rethrow=true;
470: }
471: PEND_CATCH
472: if(need_rethrow)
473: PTHROW(rethrow_me.type(), rethrow_me.code(),
1.53 paf 474: &statement_string, // setting more specific source [were url]
1.52 paf 475: rethrow_me.comment());
1.51 paf 476:
477: Array& table_columns=*new(pool) Array(pool);
478: for(unsigned int i=0; i<sql_column_count; i++) {
479: String& table_column=*new(pool) String(pool);
480: table_column.APPEND_TAINTED(
481: (const char *)sql_columns[i].ptr, sql_columns[i].size,
482: statement_cstr, 0);
483: table_columns+=&table_column;
484: }
485:
486: Table& table=*new(pool) Table(pool, &method_name, &table_columns);
487:
488: {
489: for(unsigned long r=0; r<sql_row_count; r++) {
490: SQL_Driver::Cell *sql_cells=sql_rows[r];
491: Array& table_row=*new(pool) Array(pool);
492:
493: for(unsigned int i=0; i<sql_column_count; i++) {
494: String& table_cell=*new(pool) String(pool);
495: table_cell.APPEND_TAINTED(
496: (const char *)sql_cells[i].ptr, sql_cells[i].size,
497: statement_cstr, r);
498: table_row+=&table_cell;
499: }
500: table+=&table_row;
501: }
502: }
1.49 paf 503:
504: // replace any previous table value
505: static_cast<VTable *>(r.self)->set_table(table);
506: }
507:
1.15 paf 508: // initialize
1.8 paf 509:
1.14 paf 510: void initialize_table_class(Pool& pool, VStateless_class& vclass) {
1.49 paf 511: // ^table:set{data}
512: // ^table:set[nameless]{data}
1.40 paf 513: vclass.add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2);
1.2 paf 514:
1.49 paf 515: // ^table:load[file]
516: // ^table:load[nameless;file]
1.40 paf 517: vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.22 paf 518:
519: // ^table.save[file]
520: // ^table.save[nameless;file]
1.40 paf 521: vclass.add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.6 paf 522:
523: // ^table.count[]
1.40 paf 524: vclass.add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6 paf 525:
526: // ^table.line[]
1.40 paf 527: vclass.add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6 paf 528:
1.10 paf 529: // ^table.offset[]
530: // ^table.offset[offset]
1.40 paf 531: vclass.add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1);
1.7 paf 532:
1.10 paf 533: // ^table.menu{code}
534: // ^table.menu{code}[delim]
1.40 paf 535: vclass.add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.8 paf 536:
1.10 paf 537: // ^table.empty{code-when-empty}
538: // ^table.empty{code-when-empty}{code-when-not}
1.40 paf 539: vclass.add_native_method("empty", Method::CT_DYNAMIC, _empty, 1, 2);
1.29 paf 540:
541: // ^table.record[]
1.40 paf 542: vclass.add_native_method("record", Method::CT_DYNAMIC, _record, 0, 0);
1.32 paf 543:
544: // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc]
545: // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[asc|desc]
1.40 paf 546: vclass.add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8 paf 547:
1.36 paf 548: // ^table.locate[field;value]
1.40 paf 549: vclass.add_native_method("locate", Method::CT_DYNAMIC, _locate, 2, 2);
1.37 paf 550: // ^table.found{when-found}
551: // ^table.found{when-found}{when-not-found}
1.40 paf 552: vclass.add_native_method("found", Method::CT_DYNAMIC, _found, 1, 2);
1.39 paf 553:
554: // ^table.flip[]
1.40 paf 555: vclass.add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41 paf 556:
557: // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
558: vclass.add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42 paf 559:
560: // ^table.join[table]
561: vclass.add_native_method("join", Method::CT_DYNAMIC, _join, 1, 1);
1.49 paf 562:
563:
1.51 paf 564: // ^table:sql[query][(count[;offset])]
1.49 paf 565: vclass.add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3);
1.40 paf 566: }
E-mail: