Annotation of parser3/src/classes/table.C, revision 1.53
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.53 ! paf 8: $Id: table.C,v 1.52 2001/04/05 11:36:51 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.7 paf 191: static void _menu(Request& r, const String& method_name, Array *params) {
192: Value& body_code=*static_cast<Value *>(params->get(0));
193: // forcing ^menu{this param type}
194: r.fail_if_junction_(false, body_code,
195: method_name, "body must be junction");
196:
197: Value *delim_code=params->size()==2?static_cast<Value *>(params->get(1)):0;
198:
1.18 paf 199: Table& table=static_cast<VTable *>(r.self)->table();
1.7 paf 200: bool need_delim=false;
1.37 paf 201: int saved_current=table.current();
1.22 paf 202: for(int row=0; row<table.size(); row++) {
203: table.set_current(row);
1.7 paf 204:
1.12 paf 205: Value& processed_body=r.process(body_code);
1.7 paf 206: if(delim_code) { // delimiter set?
207: const String *string=processed_body.get_string();
208: if(need_delim && string && string->size()) // need delim & iteration produced string?
209: r.write_pass_lang(r.process(*delim_code));
210: need_delim=true;
211: }
212: r.write_pass_lang(processed_body);
213: }
1.37 paf 214: table.set_current(saved_current);
1.7 paf 215: }
216:
1.39 paf 217: static void _empty(Request& r, const String& method_name, Array *params) {
1.18 paf 218: Table& table=static_cast<VTable *>(r.self)->table();
1.8 paf 219: if(table.size()==0) {
220: Value& value=r.process(*static_cast<Value *>(params->get(0)));
221: r.write_pass_lang(value);
222: } else if(params->size()==2) {
223: Value& value=r.process(*static_cast<Value *>(params->get(1)));
224: r.write_pass_lang(value);
225: }
226: }
1.15 paf 227:
1.29 paf 228: struct Record_info {
229: Pool *pool;
230: Table *table;
231: Hash *hash;
232: };
233: static void store_column_item_to_hash(Array::Item *item, void *info) {
234: Record_info& ri=*static_cast<Record_info *>(info);
235: String& column_name=*static_cast<String *>(item);
236: const String *column_item=ri.table->item(column_name);
237: Value *value;
238: if(column_item)
239: value=new(*ri.pool) VString(*column_item);
240: else
241: value=new(*ri.pool) VUnknown(*ri.pool);
242: ri.hash->put(column_name, value);
243: }
1.39 paf 244: static void _record(Request& r, const String& method_name, Array *params) {
1.29 paf 245: Table& table=static_cast<VTable *>(r.self)->table();
246: if(const Array *columns=table.columns()) {
247: Pool& pool=r.pool();
248: Value& value=*new(pool) VHash(pool);
249: Record_info record_info={&pool, &table, value.get_hash()};
250: columns->for_each(store_column_item_to_hash, &record_info);
251:
252: r.write_no_lang(value);
253: }
254: }
255:
1.34 paf 256: struct Seq_item {
257: Array *row;
258: union {
259: char *c_str;
260: double d;
261: } value;
1.32 paf 262: };
1.34 paf 263: static int sort_cmp_string(const void *a, const void *b) {
264: return strcmp(
265: static_cast<const Seq_item *>(a)->value.c_str,
266: static_cast<const Seq_item *>(b)->value.c_str
267: );
268: }
269: static int sort_cmp_double(const void *a, const void *b) {
270: double va=static_cast<const Seq_item *>(a)->value.d;
271: double vb=static_cast<const Seq_item *>(b)->value.d;
272: if(va<vb)
273: return -1;
274: else if(va>vb)
275: return +1;
276: else
277: return 0;
278: }
1.32 paf 279: static void _sort(Request& r, const String& method_name, Array *params) {
280: Value& key_maker=*(Value *)params->get(0);
281: // forcing ^sort{this} ^sort(or this) param type
282: r.fail_if_junction_(false, key_maker, method_name, "key-maker must be junction");
283:
284: bool reverse;
285: if(params->size()==2) { // ..[asc|desc]
1.35 paf 286: Value& order=*(Value *)params->get(1);
1.32 paf 287: // forcing ..[this param-type]
1.35 paf 288: r.fail_if_junction_(true, order, method_name, "order must not be junction");
289: reverse=order.as_string()=="desc";
1.32 paf 290: } else
291: reverse=false;
292:
293: Table& table=static_cast<VTable *>(r.self)->table();
1.34 paf 294:
295: // anything to sort?
296: if(!table.size())
297: return;
298:
299: Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*table.size());
300: int i;
301:
302: // calculate key values
303: bool key_values_are_strings=true;
304: for(i=0; i<table.size(); i++) {
1.32 paf 305: table.set_current(i);
306: // calculate key value
1.34 paf 307: seq[i].row=(Array *)table.get(i);
308: Value& value=*r.process(key_maker).as_expr_result(true/*return string as-is*/);
309: if(i==0) // determining key values type by first one
310: key_values_are_strings=value.is_string();
311:
312: if(key_values_are_strings)
313: seq[i].value.c_str=value.as_string().cstr();
314: else
315: seq[i].value.d=value.as_double();
1.32 paf 316: }
317: // sort keys
1.34 paf 318: _qsort(seq, table.size(), sizeof(Seq_item),
319: key_values_are_strings?sort_cmp_string:sort_cmp_double);
1.32 paf 320:
1.34 paf 321: // reorder table as they require in 'seq'
322: for(i=0; i<table.size(); i++)
323: table.put(i, seq[reverse?table.size()-1-i:i].row);
1.32 paf 324:
1.34 paf 325: // reset 'current'
1.32 paf 326: table.set_current(0);
327: }
328:
1.39 paf 329: static void _locate(Request& r, const String& method_name, Array *params) {
1.36 paf 330: VTable& vtable=*static_cast<VTable *>(r.self);
331: Table& table=vtable.table();
332: vtable.last_locate_was_successful=table.locate(
333: static_cast<Value *>(params->get(0))->as_string(),
334: static_cast<Value *>(params->get(1))->as_string());
335: }
336:
1.37 paf 337: static void _found(Request& r, const String& method_name, Array *params) {
338: if(static_cast<VTable *>(r.self)->last_locate_was_successful) {
339: Value& then_code=*static_cast<Value *>(params->get(0));
340: // forcing ^found{this param type}
341: r.fail_if_junction_(false, then_code,
342: method_name, "found-parameter must be junction");
343: r.write_pass_lang(r.process(then_code));
344: } else if(params->size()==2) {
345: Value& else_code=*static_cast<Value *>(params->get(1));
346: // forcing ^found{this param type}
347: r.fail_if_junction_(false, else_code,
348: method_name, "not found-parameter must be junction");
349: r.write_pass_lang(r.process(else_code));
350: }
351: }
352:
1.39 paf 353: static void _flip(Request& r, const String& method_name, Array *params) {
354: Pool& pool=r.pool();
355: VTable& vtable=*static_cast<VTable *>(r.self);
356:
357: Table& old_table=*vtable.get_table();
358: Table& new_table=*new(pool) Table(pool, &method_name, 0/*nameless*/);
359: if(old_table.size())
360: if(int old_cols=old_table.at(0).size())
361: for(int column=0; column<old_cols; column++) {
362: Array& new_row=*new(pool) Array(pool, old_table.size());
363: for(int i=0; i<old_table.size(); i++) {
364: const Array& old_row=old_table.at(i);
365: new_row+=column<old_row.size()?old_row.get(column):empty_string;
366: }
367: new_table+=&new_row;
368: }
369:
370: vtable.set_table(new_table);
371: }
372:
1.41 paf 373: static void _append(Request& r, const String& method_name, Array *params) {
374: Pool& pool=r.pool();
375: // data is last parameter
376: Value *value=static_cast<Value *>(params->get(0));
1.46 paf 377: // forcing {this body type}
378: r.fail_if_junction_(false, *value, method_name, "body must be junction");
1.41 paf 379:
1.46 paf 380: const String& string=r.process(*value).as_string();
1.41 paf 381:
382: // parse cells
383: Array& row=*new(pool) Array(pool);
1.46 paf 384: string.split(row, 0, "\t", 1, String::UL_CLEAN);
1.41 paf 385:
386: static_cast<VTable *>(r.self)->table()+=&row;
387: }
388:
1.42 paf 389: static void _join(Request& r, const String& method_name, Array *params) {
390: Pool& pool=r.pool();
391:
392: Value *value=static_cast<Value *>(params->get(0));
393: // forcing [this table ref type]
394: r.fail_if_junction_(true, *value, method_name, "table ref must not be junction");
395:
396: Table *maybe_src=value->get_table();
397: if(!maybe_src)
1.49 paf 398: PTHROW(0, 0,
1.42 paf 399: &method_name,
400: "source is not a table");
401:
402: Table& src=*maybe_src;
403: Table& dest=static_cast<VTable *>(r.self)->table();
404: if(&src == &dest)
1.49 paf 405: PTHROW(0, 0,
1.42 paf 406: &method_name,
407: "source and destination are same table");
408:
409: if(const Array *dest_columns=dest.columns()) { // dest is named
410: int saved_src_current=src.current();
411: for(int src_row=0; src_row<src.size(); src_row++) {
412: src.set_current(src_row);
413: Array& dest_row=*new(pool) Array(pool);
414: for(int dest_column=0; dest_column<dest_columns->size(); dest_column++)
415: dest_row+=src.item(*dest_columns->get_string(dest_column));
416: dest+=&dest_row;
417: }
418: src.set_current(saved_src_current);
419: } else { // dest is nameless
420: for(int src_row=0; src_row<src.size(); src_row++)
421: dest+=&src.at(src_row);
422: }
423: }
424:
1.51 paf 425: /// ^table:sql{query}[(count[;offset])] @test limit/offset
1.49 paf 426: static void _sql(Request& r, const String& method_name, Array *params) {
427: Pool& pool=r.pool();
428:
429: if(!r.connection)
430: PTHROW(0, 0,
431: &method_name,
432: "without connect");
433:
1.51 paf 434: Value& statement=*static_cast<Value *>(params->get(0));
1.49 paf 435: // forcing {this query param type}
1.51 paf 436: r.fail_if_junction_(true, statement, method_name, "statement must not be junction");
1.49 paf 437:
1.53 ! paf 438: ulong limit=0;
1.49 paf 439: if(params->size()>1) {
1.53 ! paf 440: Value& limit_code=*static_cast<Value *>(params->get(1));
! 441: // forcing (this limit param type)
! 442: r.fail_if_junction_(false, limit_code, method_name, "limit must be expression");
! 443: limit=(uint)r.process(limit_code).as_double();
1.49 paf 444: }
445:
1.51 paf 446: ulong offset=0;
1.49 paf 447: if(params->size()>2) {
1.51 paf 448: Value& offset_code=*static_cast<Value *>(params->get(2));
1.49 paf 449: // forcing (this limit param type)
1.51 paf 450: r.fail_if_junction_(false, offset_code, method_name, "offset must be expression");
451: offset=(ulong)r.process(offset_code).as_double();
1.49 paf 452: }
453:
1.52 paf 454: const String& statement_string=statement.as_string();
455: const char *statement_cstr=statement_string.cstr(String::UL_SQL);
1.51 paf 456: unsigned int sql_column_count; SQL_Driver::Cell *sql_columns;
457: unsigned long sql_row_count; SQL_Driver::Cell **sql_rows;
1.52 paf 458: bool need_rethrow=false; Exception rethrow_me;
459: PTRY {
460: r.connection->query(
1.53 ! paf 461: statement_cstr, offset, limit,
1.52 paf 462: &sql_column_count, &sql_columns,
463: &sql_row_count, &sql_rows);
464: }
465: PCATCH(e) { // connect/process problem
466: rethrow_me=e; need_rethrow=true;
467: }
468: PEND_CATCH
469: if(need_rethrow)
470: PTHROW(rethrow_me.type(), rethrow_me.code(),
1.53 ! paf 471: &statement_string, // setting more specific source [were url]
1.52 paf 472: rethrow_me.comment());
1.51 paf 473:
474: Array& table_columns=*new(pool) Array(pool);
475: for(unsigned int i=0; i<sql_column_count; i++) {
476: String& table_column=*new(pool) String(pool);
477: table_column.APPEND_TAINTED(
478: (const char *)sql_columns[i].ptr, sql_columns[i].size,
479: statement_cstr, 0);
480: table_columns+=&table_column;
481: }
482:
483: Table& table=*new(pool) Table(pool, &method_name, &table_columns);
484:
485: {
486: for(unsigned long r=0; r<sql_row_count; r++) {
487: SQL_Driver::Cell *sql_cells=sql_rows[r];
488: Array& table_row=*new(pool) Array(pool);
489:
490: for(unsigned int i=0; i<sql_column_count; i++) {
491: String& table_cell=*new(pool) String(pool);
492: table_cell.APPEND_TAINTED(
493: (const char *)sql_cells[i].ptr, sql_cells[i].size,
494: statement_cstr, r);
495: table_row+=&table_cell;
496: }
497: table+=&table_row;
498: }
499: }
1.49 paf 500:
501: // replace any previous table value
502: static_cast<VTable *>(r.self)->set_table(table);
503: }
504:
1.15 paf 505: // initialize
1.8 paf 506:
1.14 paf 507: void initialize_table_class(Pool& pool, VStateless_class& vclass) {
1.49 paf 508: // ^table:set{data}
509: // ^table:set[nameless]{data}
1.40 paf 510: vclass.add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2);
1.2 paf 511:
1.49 paf 512: // ^table:load[file]
513: // ^table:load[nameless;file]
1.40 paf 514: vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
1.22 paf 515:
516: // ^table.save[file]
517: // ^table.save[nameless;file]
1.40 paf 518: vclass.add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
1.6 paf 519:
520: // ^table.count[]
1.40 paf 521: vclass.add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);
1.6 paf 522:
523: // ^table.line[]
1.40 paf 524: vclass.add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
1.6 paf 525:
1.10 paf 526: // ^table.offset[]
527: // ^table.offset[offset]
1.40 paf 528: vclass.add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1);
1.7 paf 529:
1.10 paf 530: // ^table.menu{code}
531: // ^table.menu{code}[delim]
1.40 paf 532: vclass.add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2);
1.8 paf 533:
1.10 paf 534: // ^table.empty{code-when-empty}
535: // ^table.empty{code-when-empty}{code-when-not}
1.40 paf 536: vclass.add_native_method("empty", Method::CT_DYNAMIC, _empty, 1, 2);
1.29 paf 537:
538: // ^table.record[]
1.40 paf 539: vclass.add_native_method("record", Method::CT_DYNAMIC, _record, 0, 0);
1.32 paf 540:
541: // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[asc|desc]
542: // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[asc|desc]
1.40 paf 543: vclass.add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);
1.8 paf 544:
1.36 paf 545: // ^table.locate[field;value]
1.40 paf 546: vclass.add_native_method("locate", Method::CT_DYNAMIC, _locate, 2, 2);
1.37 paf 547: // ^table.found{when-found}
548: // ^table.found{when-found}{when-not-found}
1.40 paf 549: vclass.add_native_method("found", Method::CT_DYNAMIC, _found, 1, 2);
1.39 paf 550:
551: // ^table.flip[]
1.40 paf 552: vclass.add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);
1.41 paf 553:
554: // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d}
555: vclass.add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1);
1.42 paf 556:
557: // ^table.join[table]
558: vclass.add_native_method("join", Method::CT_DYNAMIC, _join, 1, 1);
1.49 paf 559:
560:
1.51 paf 561: // ^table:sql[query][(count[;offset])]
1.49 paf 562: vclass.add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3);
1.40 paf 563: }
E-mail: