--- parser3/src/classes/table.C 2002/10/23 10:04:54 1.167 +++ parser3/src/classes/table.C 2003/04/14 12:08:16 1.178 @@ -1,11 +1,11 @@ /** @file Parser: @b table parser class. - Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_TABLE_C="$Date: 2002/10/23 10:04:54 $"; +static const char* IDENT_TABLE_C="$Date: 2003/04/14 12:08:16 $"; #include "classes.h" #include "pa_common.h" @@ -30,57 +30,72 @@ public: // Methoded // methods -static void get_copy_options(Request& r, const String& method_name, MethodParams *params, int param_index, - const Table& source, - int& offset, - int& limit) { - offset=0; - limit=0; - if(params->size()<=param_index) - return; - - Value& voptions=params->as_no_junction(param_index, "options must be hash, not code"); - if(!voptions.is_string()) { - if(Hash *options=voptions.get_hash(&method_name)) { - int valid_options=0; - if(Value *voffset=(Value *)options->get(*sql_offset_name)) { - valid_options++; - if(voffset->is_string()) { - const String& soffset=*voffset->get_string(); - if(soffset == "cur") - offset=source.current(); - else - throw Exception("parser.runtime", - &soffset, - "must be 'cur' string or expression"); - } else - offset=r.process_to_value(*voffset).as_int(); - } - if(Value *vlimit=(Value *)options->get(*sql_limit_name)) { - valid_options++; - limit=r.process_to_value(*vlimit).as_int(); - } - if(valid_options!=options->size()) +static Table::Action_options get_action_options(Request& r, + const String& method_name, MethodParams *params, + const Table& source) { + Table::Action_options result; + + if(!params->size()) + return result; + + Hash* options=params->get(params->size()-1).get_hash(&method_name); + if(!options) + return result; + + result.defined=true; + bool defined_offset=false; + + int valid_options=0; + if(Value *voffset=(Value *)options->get(*sql_offset_name)) { + valid_options++; + defined_offset=true; + if(voffset->is_string()) { + const String& soffset=*voffset->get_string(); + if(soffset == "cur") + result.offset=source.current(); + else throw Exception("parser.runtime", - &method_name, - "called with invalid option"); - } else - throw Exception("parser.runtime", - &method_name, - "options must be hash"); + &soffset, + "must be 'cur' string or expression"); + } else + result.offset=r.process_to_value(*voffset).as_int(); + } + if(Value *vlimit=(Value *)options->get(*sql_limit_name)) { + valid_options++; + result.limit=r.process_to_value(*vlimit).as_int(); + } + if(Value *vreverse=(Value *)options->get(*table_reverse_name)) { + valid_options++; + result.reverse=r.process_to_value(*vreverse).as_bool(); + if(result.reverse && !defined_offset) + result.offset=source.size()-1; } - if(!limit) // zero limit = sould be 'nothing to copy', for methods zero means 'all' - limit=-1; // thus fixing + if(valid_options!=options->size()) + throw Exception("parser.runtime", + &method_name, + "called with invalid option"); + + return result; +} +static check_option_param(bool options_defined, + const String& method_name, MethodParams *params, + int next_param_index, + const char *msg) { + if(next_param_index+(options_defined?1:0) != params->size()) + throw Exception("parser.runtime", + &method_name, + "%s", msg); } static void _create(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); // clone/copy part? if(const Table *source=params->get(0).get_table()) { - int offset, limit; - get_copy_options(r, method_name, params, 1, *source, - offset, limit); - static_cast(r.get_self())->set_table(*new(pool) Table(pool, *source, offset, limit)); + Table::Action_options o=get_action_options(r, method_name, params, *source); + check_option_param(o.defined, method_name, params, 1, + "too many parameters"); + static_cast(r.get_self())-> + set_table(*new(pool) Table(pool, *source, o)); return; } @@ -125,12 +140,19 @@ static void _create(Request& r, const St static void _load(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - // filename is last parameter - Value& vfile_name=params->as_no_junction(params->size()-1, - "file name must not be code"); - + const String& first_param=params->as_string(0, "file name must be string"); + int filename_param_index=0; + bool nameless=first_param=="nameless"; + if(nameless) + filename_param_index++; + int options_param_index=filename_param_index+1; + // loading text - char *data=file_read_text(pool, r.absolute(vfile_name.as_string())); + char *data=file_read_text(pool, + r.absolute(params->as_string(filename_param_index, "file name must be string")), + true, + options_param_indexsize()?params->as_no_junction(options_param_index, "additional params must be hash").get_hash(&method_name):0 + ); // parse columns Array *columns; @@ -139,7 +161,7 @@ static void _load(Request& r, const Stri const char *file=origin.file; uint line=origin.line; #endif - if(params->size()==2) { + if(nameless) { columns=0; // nameless } else { columns=new(pool) Array(pool); @@ -303,6 +325,7 @@ static void _menu(Request& r, const Stri } #ifndef DOXYGEN +enum Table2hash_distint { D_ILLEGAL, D_FIRST, D_TABLES }; struct Row_info { Request *r; Table *table; @@ -310,7 +333,7 @@ struct Row_info { int key_field; Array *value_fields; Hash *hash; - bool distinct; + Table2hash_distint distinct; int row; }; #endif @@ -330,21 +353,47 @@ static void table_row_to_hash(Array::Ite if(!key) return; // ignore rows without key [too-short-record_array if-indexed] - VHash& result=*new(pool) VHash(pool); - Hash& hash=*result.get_hash(0); - for(int i=0; isize(); i++) { - int value_field=ri.value_fields->get_int(i); - if(value_fieldcolumns()->get_string(value_field), - new(pool) VString(*row.get_string(value_field))); + switch(ri.distinct) { + case D_ILLEGAL: case D_FIRST: + { + VHash& result=*new(pool) VHash(pool); + Hash& hash=*result.get_hash(0); + for(int i=0; isize(); i++) { + int value_field=ri.value_fields->get_int(i); + if(value_fieldcolumns()->get_string(value_field), + new(pool) VString(*row.get_string(value_field))); + } + + if(ri.hash->put_dont_replace(*key, &result)) // put. existed? + if(ri.distinct==D_ILLEGAL) + throw Exception("parser.runtime", + key, + "duplicate key"); + } + break; + case D_TABLES: + { + VTable* vtable=(VTable*)ri.hash->get(*key); // put. table existed? + Table* table; + if(vtable) + table=vtable->get_table(); + else { + // no? creating table of same structure as source + Table::Action_options table_options; + table=new(pool) Table(pool, *ri.table, table_options/*no rows, just structure*/); + ri.hash->put(*key, new(pool) VTable(pool, table)); + } + *table+=&row; + } + break; + default: + throw Exception(0, + 0, + "invalid distinct code (#%d)", ri.distinct); } - if(ri.hash->put_dont_replace(*key, &result)) // put. existed? - if(!ri.distinct) - throw Exception("parser.runtime", - key, - "duplicate key"); } static void _hash(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); @@ -352,16 +401,26 @@ static void _hash(Request& r, const Stri Value& result=*new(pool) VHash(pool); if(const Array *columns=self_table.columns()) if(columns->size()>0) { - bool distinct=false; + Table2hash_distint distinct=D_ILLEGAL; int param_index=params->size()-1; if(param_index>0) { if(Hash *options= params->as_no_junction(param_index, "param must not be code").get_hash(0)) { --param_index; int valid_options=0; - if(Value *vdistinct=(Value *)options->get(*sql_distinct_name)) { + if(Value *vdistinct_code=(Value *)options->get(*sql_distinct_name)) { valid_options++; - distinct=r.process_to_value(*vdistinct).as_bool(); + Value& vdistinct_value=r.process_to_value(*vdistinct_code); + if(vdistinct_value.is_string()) { + const String& sdistinct=*vdistinct_value.get_string(); + if(sdistinct=="tables") + distinct=D_TABLES; + else + throw Exception("parser.runtime", + &sdistinct, + "must be 'tables' or true/false"); + } else + distinct=vdistinct_value.as_bool()?D_FIRST:D_ILLEGAL; } if(valid_options!=options->size()) throw Exception("parser.runtime", @@ -376,6 +435,10 @@ static void _hash(Request& r, const Stri Array value_fields(pool); if(param_index>0) { + if(distinct!=D_ILLEGAL && distinct!=D_FIRST) + throw Exception("parser.runtime", + 0, + "in distinct[tables] mode you may not specify value field(s)"); Value& value_fields_param=params->as_no_junction(param_index, "value field(s) must not be code"); if(value_fields_param.is_string()) { value_fields+=self_table.column_name2index(value_fields_param.as_string(), true); @@ -391,8 +454,9 @@ static void _hash(Request& r, const Stri "value field(s) must be string or self_table" ); } else { // by all columns, including key - for(int i=0; isize(); i++) - value_fields+=i; + if(!(distinct!=D_ILLEGAL && distinct!=D_FIRST)) + for(int i=0; isize(); i++) + value_fields+=i; } Value& key_param=params->get(0); @@ -400,7 +464,9 @@ static void _hash(Request& r, const Stri int key_field=key_code?-1 :self_table.column_name2index(key_param.as_string(), true); - Row_info row_info={&r, &self_table, key_code, key_field, &value_fields, result.get_hash(0), distinct}; + Row_info row_info={&r, &self_table, + key_code, key_field, &value_fields, + result.get_hash(0), distinct}; int saved_current=self_table.current(); self_table.for_each(table_row_to_hash, &row_info); @@ -443,6 +509,9 @@ static void _sort(Request& r, const Stri false; // default=asc Table& old_table=static_cast(r.get_self())->table(&method_name); + if(old_table.size()==0) + return; + Table& new_table=*new(pool) Table(pool, &method_name, old_table.columns()); Table_seq_item *seq=(Table_seq_item *)pool.malloc(sizeof(Table_seq_item)*old_table.size()); @@ -450,8 +519,6 @@ static void _sort(Request& r, const Stri // calculate key values bool key_values_are_strings=true; - // save 'current' - int saved_current=old_table.current(); for(i=0; i(r.get_self())->set_table(new_table); } -static bool _locate_expression(Request& r, const String& method_name, MethodParams *params) { - if(params->size()>1) - throw Exception("parser.runtime", - &method_name, - "locate by expression has only one parameter - expression"); - +#ifndef DOXYGEN +struct Locate_expression_func_info { + Request* r; + Value* expression_code; +}; +#endif +bool locate_expression_func(Table& self, void* ainfo) { + Locate_expression_func_info& info=*static_cast(ainfo); + return info.r->process_to_value(*info.expression_code).as_bool(); +} +static bool _locate_expression(Table& table, Table::Action_options o, + Request& r, const String& method_name, MethodParams *params) { + check_option_param(o.defined, method_name, params, 1, + "locate by expression only has parameters: expression and, maybe, options"); Value& expression_code=params->as_junction(0, "must be expression"); - Table& table=static_cast(r.get_self())->table(&method_name); - int saved_current=table.current(); - int size=table.size(); - for(int row=0; rowsize()>2) - throw Exception("parser.runtime", - &method_name, - "locate by name and value has only two parameters - name and value"); +static bool _locate_name_value(Table& table, Table::Action_options o, + Request& r, const String& method_name, MethodParams *params) { + check_option_param(o.defined, method_name, params, 2, + "locate by locate by name has parameters: name, value and, maybe, options"); + const String& name=params->as_string(0, "column name must be string"); + const String& value=params->as_string(1, "value must be string"); - Table& table=static_cast(r.get_self())->table(&method_name); - return table.locate( - params->as_string(0, "column name must be string"), - params->as_string(1, "value must be string") - ); + return table.locate(name, value, o); } static void _locate(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); + Table& table=static_cast(r.get_self())->table(&method_name); + + Table::Action_options o=get_action_options(r, method_name, params, table); + bool result=params->get(0).get_junction()? - _locate_expression(r, method_name, params) : - _locate_name_value(r, method_name, params); + _locate_expression(table, o, r, method_name, params) : + _locate_name_value(table, o, r, method_name, params); r.write_no_lang(*new(pool) VBool(pool, result)); } @@ -554,30 +619,34 @@ static void _append(Request& r, const St static void _join(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - Table *maybe_src=params->as_no_junction(0, "table ref must not be code").get_table(); + Table* maybe_src=params->as_no_junction(0, "table ref must not be code").get_table(); if(!maybe_src) throw Exception("parser.runtime", &method_name, "source is not a table"); - Table& src=*maybe_src; + + Table::Action_options o=get_action_options(r, method_name, params, src); + check_option_param(o.defined, method_name, params, 1, + "invalid extra parameter"); + Table& dest=static_cast(r.get_self())->table(&method_name); if(&src == &dest) throw Exception("parser.runtime", &method_name, "source and destination are same table"); - int offset, limit; - get_copy_options(r, method_name, params, 1, src, - offset, limit); - + if(o.reverse) + throw Exception(0, + &method_name, + "not implemented in this version, please upgrade"); if(const Array *dest_columns=dest.columns()) { // dest is named int saved_src_current=src.current(); - int m=src.size()-offset; - if(!limit || limit>m) - limit=m; - int end=offset+limit; - for(int src_row=offset; src_rowm) + o.limit=m; + int end=o.offset+o.limit; + for(int src_row=o.offset; src_rowsize(); dest_column++) { @@ -594,7 +663,7 @@ static void _join(Request& r, const Stri } #ifndef DOXYGEN -class Table_sql_event_handlers : public SQL_Driver_query_event_handlers { +class Table_sql_event_handlers: public SQL_Driver_query_event_handlers { public: Table_sql_event_handlers(Pool& apool, const String& amethod_name, const String& astatement_string, const char *astatement_cstr) : @@ -608,26 +677,50 @@ public: { } - void add_column(void *ptr, size_t size) { - String *column=new(pool) String(pool); - column->APPEND_TAINTED( - (const char *)ptr, size, - statement_cstr, 0); - columns+=column; - } - void before_rows() { - table=new(pool) Table(pool, &method_name, &columns); - } - void add_row() { - (*table)+=(row=new(pool) Array(pool)); - } - void add_row_cell(void *ptr, size_t size) { - String *cell=new(pool) String(pool); - if(size) - cell->APPEND_TAINTED( + bool add_column(SQL_Error& error, void *ptr, size_t size) { + try { + String *column=new(pool) String(pool); + column->APPEND_TAINTED( (const char *)ptr, size, - statement_cstr, table->size()-1); - (*row)+=cell; + statement_cstr, 0); + columns+=column; + return false; + } catch(...) { + error=SQL_Error("exception occured in Table_sql_event_handlers::add_column"); + return true; + } + } + bool before_rows(SQL_Error& error) { + try { + table=new(pool) Table(pool, &method_name, &columns); + return false; + } catch(...) { + error=SQL_Error("exception occured in Table_sql_event_handlers::before_rows"); + return true; + } + } + bool add_row(SQL_Error& error) { + try { + (*table)+=(row=new(pool) Array(pool)); + return false; + } catch(...) { + error=SQL_Error("exception occured in Table_sql_event_handlers::add_row"); + return true; + } + } + bool add_row_cell(SQL_Error& error, void *ptr, size_t size) { + try { + String *cell=new(pool) String(pool); + if(size) + cell->APPEND_TAINTED( + (const char *)ptr, size, + statement_cstr, table->size()-1); + (*row)+=cell; + return false; + } catch(...) { + error=SQL_Error("exception occured in Table_sql_event_handlers::add_row_cell"); + return true; + } } private: @@ -765,7 +858,7 @@ MTable::MTable(Pool& apool) : Methoded(a // ^table::load[file] // ^table::load[nameless;file] - add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2); + add_native_method("load", Method::CT_DYNAMIC, _load, 1, 3); // ^table.save[file] // ^table.save[nameless;file] @@ -795,7 +888,7 @@ MTable::MTable(Pool& apool) : Methoded(a add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2); // ^table.locate[field;value] - add_native_method("locate", Method::CT_DYNAMIC, _locate, 1, 2); + add_native_method("locate", Method::CT_DYNAMIC, _locate, 1, 3); // ^table.flip[] add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);