|
|
| version 1.174, 2003/04/11 10:39:24 | version 1.178, 2003/04/14 12:08:16 |
|---|---|
| Line 30 public: // Methoded | Line 30 public: // Methoded |
| // methods | // methods |
| static void get_copy_options(Request& r, const String& method_name, MethodParams *params, int param_index, | static Table::Action_options get_action_options(Request& r, |
| const Table& source, | const String& method_name, MethodParams *params, |
| int& offset, | const Table& source) { |
| int& limit) { | Table::Action_options result; |
| offset=0; | |
| limit=0; | if(!params->size()) |
| if(params->size()<=param_index) | return result; |
| return; | |
| Hash* options=params->get(params->size()-1).get_hash(&method_name); | |
| Value& voptions=params->as_no_junction(param_index, "options must be hash, not code"); | if(!options) |
| if(!voptions.is_string()) { | return result; |
| if(Hash *options=voptions.get_hash(&method_name)) { | |
| int valid_options=0; | result.defined=true; |
| if(Value *voffset=(Value *)options->get(*sql_offset_name)) { | bool defined_offset=false; |
| valid_options++; | |
| if(voffset->is_string()) { | int valid_options=0; |
| const String& soffset=*voffset->get_string(); | if(Value *voffset=(Value *)options->get(*sql_offset_name)) { |
| if(soffset == "cur") | valid_options++; |
| offset=source.current(); | defined_offset=true; |
| else | if(voffset->is_string()) { |
| throw Exception("parser.runtime", | const String& soffset=*voffset->get_string(); |
| &soffset, | if(soffset == "cur") |
| "must be 'cur' string or expression"); | result.offset=source.current(); |
| } else | 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()) | |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| &method_name, | &soffset, |
| "called with invalid option"); | "must be 'cur' string or expression"); |
| } else | } else |
| throw Exception("parser.runtime", | result.offset=r.process_to_value(*voffset).as_int(); |
| &method_name, | } |
| "options must be hash"); | 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' | if(valid_options!=options->size()) |
| limit=-1; // thus fixing | 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) { | static void _create(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| // clone/copy part? | // clone/copy part? |
| if(const Table *source=params->get(0).get_table()) { | if(const Table *source=params->get(0).get_table()) { |
| int offset, limit; | Table::Action_options o=get_action_options(r, method_name, params, *source); |
| get_copy_options(r, method_name, params, 1, *source, | check_option_param(o.defined, method_name, params, 1, |
| offset, limit); | "too many parameters"); |
| static_cast<VTable *>(r.get_self())->set_table(*new(pool) Table(pool, *source, offset, limit)); | static_cast<VTable *>(r.get_self())-> |
| set_table(*new(pool) Table(pool, *source, o)); | |
| return; | return; |
| } | } |
| Line 366 static void table_row_to_hash(Array::Ite | Line 381 static void table_row_to_hash(Array::Ite |
| table=vtable->get_table(); | table=vtable->get_table(); |
| else { | else { |
| // no? creating table of same structure as source | // no? creating table of same structure as source |
| table=new(pool) Table(pool, *ri.table, 0, -1 /*no rows, just structure*/); | 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)); | ri.hash->put(*key, new(pool) VTable(pool, table)); |
| } | } |
| *table+=&row; | *table+=&row; |
| Line 375 static void table_row_to_hash(Array::Ite | Line 391 static void table_row_to_hash(Array::Ite |
| default: | default: |
| throw Exception(0, | throw Exception(0, |
| 0, | 0, |
| "invalid distinct code (#%d), ri.distinct"); | "invalid distinct code (#%d)", ri.distinct); |
| } | } |
| } | } |
| Line 528 static void _sort(Request& r, const Stri | Line 544 static void _sort(Request& r, const Stri |
| static_cast<VTable *>(r.get_self())->set_table(new_table); | static_cast<VTable *>(r.get_self())->set_table(new_table); |
| } | } |
| static bool _locate_expression(Request& r, const String& method_name, MethodParams *params) { | #ifndef DOXYGEN |
| if(params->size()>1) | struct Locate_expression_func_info { |
| throw Exception("parser.runtime", | Request* r; |
| &method_name, | Value* expression_code; |
| "locate by expression has only one parameter - expression"); | }; |
| #endif | |
| bool locate_expression_func(Table& self, void* ainfo) { | |
| Locate_expression_func_info& info=*static_cast<Locate_expression_func_info*>(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"); | Value& expression_code=params->as_junction(0, "must be expression"); |
| Table& table=static_cast<VTable *>(r.get_self())->table(&method_name); | Locate_expression_func_info info={&r, &expression_code}; |
| int saved_current=table.current(); | return table.locate(locate_expression_func, &info, o); |
| int size=table.size(); | |
| for(int row=0; row<size; row++) { | |
| table.set_current(row); | |
| if(r.process_to_value(expression_code).as_bool()) | |
| return true; | |
| } | |
| table.set_current(saved_current); | |
| return false; | |
| } | } |
| static bool _locate_name_value(Request& r, const String& method_name, MethodParams *params) { | static bool _locate_name_value(Table& table, Table::Action_options o, |
| if(params->size()>2) | Request& r, const String& method_name, MethodParams *params) { |
| throw Exception("parser.runtime", | check_option_param(o.defined, method_name, params, 2, |
| &method_name, | "locate by locate by name has parameters: name, value and, maybe, options"); |
| "locate by name and value has only two parameters - name and value"); | 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<VTable *>(r.get_self())->table(&method_name); | return table.locate(name, value, o); |
| return table.locate( | |
| params->as_string(0, "column name must be string"), | |
| params->as_string(1, "value must be string") | |
| ); | |
| } | } |
| static void _locate(Request& r, const String& method_name, MethodParams *params) { | static void _locate(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Table& table=static_cast<VTable *>(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()? | bool result=params->get(0).get_junction()? |
| _locate_expression(r, method_name, params) : | _locate_expression(table, o, r, method_name, params) : |
| _locate_name_value(r, method_name, params); | _locate_name_value(table, o, r, method_name, params); |
| r.write_no_lang(*new(pool) VBool(pool, result)); | r.write_no_lang(*new(pool) VBool(pool, result)); |
| } | } |
| Line 603 static void _append(Request& r, const St | Line 619 static void _append(Request& r, const St |
| static void _join(Request& r, const String& method_name, MethodParams *params) { | static void _join(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | 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) | if(!maybe_src) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| &method_name, | &method_name, |
| "source is not a table"); | "source is not a table"); |
| Table& src=*maybe_src; | 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<VTable *>(r.get_self())->table(&method_name); | Table& dest=static_cast<VTable *>(r.get_self())->table(&method_name); |
| if(&src == &dest) | if(&src == &dest) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| &method_name, | &method_name, |
| "source and destination are same table"); | "source and destination are same table"); |
| int offset, limit; | if(o.reverse) |
| get_copy_options(r, method_name, params, 1, src, | throw Exception(0, |
| offset, limit); | &method_name, |
| "not implemented in this version, please upgrade"); | |
| if(const Array *dest_columns=dest.columns()) { // dest is named | if(const Array *dest_columns=dest.columns()) { // dest is named |
| int saved_src_current=src.current(); | int saved_src_current=src.current(); |
| int m=src.size()-offset; | int m=src.size()-o.offset; |
| if(!limit || limit>m) | if(!o.limit || o.limit>m) |
| limit=m; | o.limit=m; |
| int end=offset+limit; | int end=o.offset+o.limit; |
| for(int src_row=offset; src_row<end; src_row++) { | for(int src_row=o.offset; src_row<end; src_row++) { |
| src.set_current(src_row); | src.set_current(src_row); |
| Array& dest_row=*new(pool) Array(pool); | Array& dest_row=*new(pool) Array(pool); |
| for(int dest_column=0; dest_column<dest_columns->size(); dest_column++) { | for(int dest_column=0; dest_column<dest_columns->size(); dest_column++) { |
| Line 868 MTable::MTable(Pool& apool) : Methoded(a | Line 888 MTable::MTable(Pool& apool) : Methoded(a |
| add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2); | add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2); |
| // ^table.locate[field;value] | // ^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[] | // ^table.flip[] |
| add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0); | add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0); |