|
|
| version 1.107, 2001/08/28 10:33:32 | version 1.121, 2001/10/09 08:13:04 |
|---|---|
| Line 2 | Line 2 |
| Parser: @b table parser class. | Parser: @b table parser class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) |
| $Id$ | |
| */ | */ |
| static const char *RCSId="$Id$"; | |
| #include "classes.h" | #include "classes.h" |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| Line 65 static void _set(Request& r, const Strin | Line 65 static void _set(Request& r, const Strin |
| while(i.has_next()) { | while(i.has_next()) { |
| Array& row=*new(pool) Array(pool); | Array& row=*new(pool) Array(pool); |
| const String& string=*i.next_string(); | const String& string=*i.next_string(); |
| // remove empty lines | // remove comment lines |
| if(!string.size()) | if(!string.size()) |
| continue; | continue; |
| Line 110 static void _load(Request& r, const Stri | Line 110 static void _load(Request& r, const Stri |
| Table& table=*new(pool) Table(pool, &method_name, columns); | Table& table=*new(pool) Table(pool, &method_name, columns); |
| char *row_chars; | char *row_chars; |
| while(row_chars=getrow(&data)) { | while(row_chars=getrow(&data)) { |
| if(!*row_chars) // remove empty lines | // remove empty&comment lines |
| if(!*row_chars || *row_chars == '#') | |
| continue; | continue; |
| Array *row=new(pool) Array(pool); | Array *row=new(pool) Array(pool); |
| while(char *cell_chars=lsplit(&row_chars, '\t')) { | while(char *cell_chars=lsplit(&row_chars, '\t')) { |
| Line 133 static Table *fill_month_days(Request& r | Line 134 static Table *fill_month_days(Request& r |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Table *result=new(pool) Table(pool, &method_name, 0/*&columns*/); | Table *result=new(pool) Table(pool, &method_name, 0/*&columns*/); |
| int year=params->as_int(1, r); | int year=params->as_int(1, "year must be int", r); |
| int month=max(1, min(params->as_int(2, r), 12)) -1; | int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1; |
| tm tmIn={0, 0, 0, 1, month, year-1900}; | tm tmIn={0, 0, 0, 1, month, year-1900}; |
| time_t t=mktime(&tmIn); | time_t t=mktime(&tmIn); |
| Line 176 static Table *fill_week_days(Request& r, | Line 177 static Table *fill_week_days(Request& r, |
| columns+=new(pool) String(pool, "weekday"); | columns+=new(pool) String(pool, "weekday"); |
| Table *result=new(pool) Table(pool, &method_name, &columns); | Table *result=new(pool) Table(pool, &method_name, &columns); |
| int year=params->as_int(1, r); | int year=params->as_int(1, "year must be int", r); |
| int month=max(1, min(params->as_int(2, r), 12)) -1; | int month=max(1, min(params->as_int(2, "month must be int", r), 12)) -1; |
| int day=params->as_int(3, r); | int day=params->as_int(3, "day must be int", r); |
| tm tmIn={0, 0, 18, day, month, year-1900}; | tm tmIn={0, 0, 18, day, month, year-1900}; |
| time_t t=mktime(&tmIn); | time_t t=mktime(&tmIn); |
| Line 320 static void _offset(Request& r, const St | Line 321 static void _offset(Request& r, const St |
| static void _menu(Request& r, const String& method_name, MethodParams *params) { | static void _menu(Request& r, const String& method_name, MethodParams *params) { |
| Value& body_code=params->as_junction(0, "body must be code"); | Value& body_code=params->as_junction(0, "body must be code"); |
| Value *delim_code=params->size()==2?¶ms->get(1):0; | Value *delim_maybe_code=params->size()==2?¶ms->get(1):0; |
| VTable& vtable=*static_cast<VTable *>(r.self); | VTable& vtable=*static_cast<VTable *>(r.self); |
| Table& table=vtable.table(); | Table& table=vtable.table(); |
| Line 331 static void _menu(Request& r, const Stri | Line 332 static void _menu(Request& r, const Stri |
| table.set_current(row); | table.set_current(row); |
| Value& processed_body=r.process(body_code); | Value& processed_body=r.process(body_code); |
| if(delim_code) { // delimiter set? | if(delim_maybe_code) { // delimiter set? |
| const String *string=processed_body.get_string(); | const String *string=processed_body.get_string(); |
| if(need_delim && string && string->size()) // need delim & iteration produced string? | if(need_delim && string && string->size()) // need delim & iteration produced string? |
| r.write_pass_lang(r.process(*delim_code)); | r.write_pass_lang(r.process(*delim_maybe_code)); |
| need_delim=true; | need_delim=true; |
| } | } |
| r.write_pass_lang(processed_body); | r.write_pass_lang(processed_body); |
| Line 342 static void _menu(Request& r, const Stri | Line 343 static void _menu(Request& r, const Stri |
| table.set_current(saved_current); | table.set_current(saved_current); |
| } | } |
| static void _empty(Request& r, const String& method_name, MethodParams *params) { | #ifndef DOXYGEN |
| Pool& pool=r.pool(); | |
| Table& table=static_cast<VTable *>(r.self)->table(); | |
| r.write_no_lang(*new(pool) VBool(pool, table.size()==0)); | |
| } | |
| /// used by table: _hash / table_row_to_hash | |
| struct Row_info { | struct Row_info { |
| Table *table; | Table *table; |
| int key_field; | int key_field; |
| Array *value_fields; | Array *value_fields; |
| Hash *hash; | Hash *hash; |
| }; | }; |
| #endif | |
| static void table_row_to_hash(Array::Item *value, void *info) { | static void table_row_to_hash(Array::Item *value, void *info) { |
| Array& row=*static_cast<Array *>(value); | Array& row=*static_cast<Array *>(value); |
| Row_info& ri=*static_cast<Row_info *>(info); | Row_info& ri=*static_cast<Row_info *>(info); |
| Line 410 static void _hash(Request& r, const Stri | Line 405 static void _hash(Request& r, const Stri |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| /// used by table: _sort / sort_cmp_string|sort_cmp_double | #ifndef DOXYGEN |
| struct Table_seq_item { | struct Table_seq_item { |
| Array *row; | Array *row; |
| union { | union { |
| Line 418 struct Table_seq_item { | Line 413 struct Table_seq_item { |
| double d; | double d; |
| } value; | } value; |
| }; | }; |
| #endif | |
| static int sort_cmp_string(const void *a, const void *b) { | static int sort_cmp_string(const void *a, const void *b) { |
| return strcmp( | return strcmp( |
| static_cast<const Table_seq_item *>(a)->value.c_str, | static_cast<const Table_seq_item *>(a)->value.c_str, |
| Line 475 static void _sort(Request& r, const Stri | Line 471 static void _sort(Request& r, const Stri |
| for(i=0; i<old_table.size(); i++) | for(i=0; i<old_table.size(); i++) |
| new_table+=seq[reverse?old_table.size()-1-i:i].row; | new_table+=seq[reverse?old_table.size()-1-i:i].row; |
| r.write_no_lang(*new(pool) VTable(pool, &new_table)); | // replace any previous table value |
| static_cast<VTable *>(r.self)->set_table(new_table); | |
| } | } |
| static void _locate(Request& r, const String& method_name, MethodParams *params) { | static void _locate(Request& r, const String& method_name, MethodParams *params) { |
| Line 484 static void _locate(Request& r, const St | Line 481 static void _locate(Request& r, const St |
| VTable& vtable=*static_cast<VTable *>(r.self); | VTable& vtable=*static_cast<VTable *>(r.self); |
| Table& table=vtable.table(); | Table& table=vtable.table(); |
| Value& result=*new(pool) VBool(pool, table.locate( | Value& result=*new(pool) VBool(pool, table.locate( |
| params->get(0).as_string(), | params->as_string(0, "column name must be string"), |
| params->get(1).as_string())); | params->as_string(1, "value must be string") |
| )); | |
| result.set_name(method_name); | result.set_name(method_name); |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| Line 617 static void _sql(Request& r, const Strin | Line 615 static void _sql(Request& r, const Strin |
| ulong limit=0; | ulong limit=0; |
| ulong offset=0; | ulong offset=0; |
| if(params->size()>1) { | if(params->size()>1) { |
| Value& options_param=params->as_no_junction(1, "options must be hash, not code"); | Value& voptions=params->as_no_junction(1, "options must be hash, not code"); |
| if(Hash *options=options_param.get_hash()) { | if(voptions.is_defined()) |
| if(Value *vlimit=(Value *)options->get(*sql_limit_name)) | if(Hash *options=voptions.get_hash()) { |
| limit=(ulong)r.process(*vlimit).as_double(); | if(Value *vlimit=(Value *)options->get(*sql_limit_name)) |
| if(Value *voffset=(Value *)options->get(*sql_offset_name)) | limit=(ulong)r.process(*vlimit).as_double(); |
| offset=(ulong)r.process(*voffset).as_double(); | if(Value *voffset=(Value *)options->get(*sql_offset_name)) |
| } else | offset=(ulong)r.process(*voffset).as_double(); |
| PTHROW(0, 0, | } else |
| &method_name, | PTHROW(0, 0, |
| "options must be hash"); | &method_name, |
| "options must be hash"); | |
| } | } |
| Temp_lang temp_lang(r, String::UL_SQL); | Temp_lang temp_lang(r, String::UL_SQL); |
| Line 636 static void _sql(Request& r, const Strin | Line 635 static void _sql(Request& r, const Strin |
| Table_sql_event_handlers handlers(pool, method_name, | Table_sql_event_handlers handlers(pool, method_name, |
| statement_string, statement_cstr); | statement_string, statement_cstr); |
| bool need_rethrow=false; Exception rethrow_me; | bool need_rethrow=false; Exception rethrow_me; |
| PTRY { | PTRY { |
| r.connection->query( | r.connection->query( |
| statement_cstr, offset, limit, | statement_cstr, offset, limit, |
| handlers); | handlers); |
| Line 715 MTable::MTable(Pool& apool) : Methoded(a | Line 714 MTable::MTable(Pool& apool) : Methoded(a |
| // ^table.menu{code}[delim] | // ^table.menu{code}[delim] |
| add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2); | add_native_method("menu", Method::CT_DYNAMIC, _menu, 1, 2); |
| // ^table.empty[] | |
| add_native_method("empty", Method::CT_DYNAMIC, _empty, 0, 0); | |
| // ^table:hash[key field name] | // ^table:hash[key field name] |
| // ^table:hash[key field name][value field name;...] | // ^table:hash[key field name][value field name;...] |
| add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 1000); | add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 1000); |