|
|
| version 1.172.2.14.2.13, 2003/03/24 11:45:28 | version 1.183, 2003/09/25 09:15:02 |
|---|---|
| Line 40 DECLARE_CLASS_VAR(table, new MTable, 0); | Line 40 DECLARE_CLASS_VAR(table, new MTable, 0); |
| #define SQL_OFFSET_NAME "offset" | #define SQL_OFFSET_NAME "offset" |
| #define SQL_DEFAULT_NAME "default" | #define SQL_DEFAULT_NAME "default" |
| #define SQL_DISTINCT_NAME "distinct" | #define SQL_DISTINCT_NAME "distinct" |
| #define TABLE_REVERSE_NAME "reverse" | |
| // globals | // globals |
| const String sql_limit_name(SQL_LIMIT_NAME); | String sql_limit_name(SQL_LIMIT_NAME); |
| const String sql_offset_name(SQL_OFFSET_NAME); | String sql_offset_name(SQL_OFFSET_NAME); |
| const String sql_default_name(SQL_DEFAULT_NAME); | String sql_default_name(SQL_DEFAULT_NAME); |
| const String sql_distinct_name(SQL_DISTINCT_NAME); | String sql_distinct_name(SQL_DISTINCT_NAME); |
| String table_reverse_name(TABLE_REVERSE_NAME); | |
| // methods | // methods |
| static void get_copy_options(Request& r, MethodParams* params, int param_index, | static Table::Action_options get_action_options(Request& r, MethodParams& params, |
| const Table& source, | const Table& source) { |
| int& offset, | Table::Action_options result; |
| int& limit) { | if(!params.count()) |
| return result; | |
| offset=0; | |
| limit=0; | Value& maybe_options=params.last(); |
| if(params->count()<=param_index) | /* can not do it: |
| return; | want to enable ^table::create[$source; |
| # $.option[] | |
| Value& voptions=params->as_no_junction(param_index, "options must be hash, not code"); | ] |
| if(!voptions.is_string()) { | but there is ^table.locate[name;value] |
| if(HashStringValue* options=voptions.get_hash()) { | |
| int valid_options=0; | if(maybe_options.is_string()) { // allow empty options |
| if(Value* voffset=options->get(sql_offset_name)) { | result.defined=true; |
| valid_options++; | return result; |
| if(voffset->is_string()) { | } |
| const String& soffset=*voffset->get_string(); | */ |
| if(soffset == "cur") | HashStringValue* options=maybe_options.get_hash(); |
| offset=source.current(); | if(!options) |
| else | return result; |
| throw Exception("parser.runtime", | |
| &soffset, | result.defined=true; |
| "must be 'cur' string or expression"); | bool defined_offset=false; |
| } else | |
| offset=r.process_to_value(*voffset).as_int(); | int valid_options=0; |
| } | if(Value* voffset=options->get(sql_offset_name)) { |
| if(Value* vlimit=options->get(sql_limit_name)) { | valid_options++; |
| valid_options++; | defined_offset=true; |
| limit=r.process_to_value(*vlimit).as_int(); | if(voffset->is_string()) { |
| if(!limit) // zero limit = should be 'nothing to copy', for methods zero means 'all' | const String& soffset=*voffset->get_string(); |
| limit=-1; // thus fixing | if(soffset == "cur") |
| } | result.offset=source.current(); |
| if(valid_options!=options->count()) | else |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| 0, | &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(); |
| 0, | } |
| "options must be hash"); | if(Value* vlimit=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.count()-1; | |
| } | |
| if(valid_options!=options->count()) | |
| throw Exception("parser.runtime", | |
| 0, | |
| "called with invalid option"); | |
| return result; | |
| } | |
| static void check_option_param(bool options_defined, | |
| MethodParams& params, size_t next_param_index, | |
| const char *msg) { | |
| if(next_param_index+(options_defined?1:0) != params.count()) | |
| throw Exception("parser.runtime", | |
| 0, | |
| "%s", msg); | |
| } | } |
| static void _create(Request& r, MethodParams* params) { | static void _create(Request& r, MethodParams& params) { |
| // clone/copy part? | // clone/copy part? |
| if(Table *source=(*params)[0]->get_table()) { | if(Table *source=params[0].get_table()) { |
| int offset, limit; | Table::Action_options o=get_action_options(r, params, *source); |
| get_copy_options(r, params, 1, *source, | check_option_param(o.defined, params, 1, |
| offset, limit); | "too many parameters"); |
| GET_SELF(r, VTable).set_table(*new Table(*source, offset, limit)); | GET_SELF(r, VTable).set_table(*new Table(*source, o)); |
| return; | return; |
| } | } |
| // data is last parameter | // data is last parameter |
| Temp_lang temp_lang(r, String::L_PASS_APPENDED); | Temp_lang temp_lang(r, String::L_PASS_APPENDED); |
| const String& data= | const String& data= |
| r.process_to_string(params->as_junction(params->count()-1, "body must be code")); | r.process_to_string(params.as_junction(params.count()-1, "body must be code")); |
| // parse columns | // parse columns |
| size_t raw_pos_after=0; | |
| Table::columns_type columns; | Table::columns_type columns; |
| if(params->count()==2) { | if(params.count()==2) { |
| columns=Table::columns_type(0); // nameless | columns=Table::columns_type(0); // nameless |
| } else { | } else { |
| columns=Table::columns_type(new ArrayString); | columns=Table::columns_type(new ArrayString); |
| ArrayString head; | ArrayString head; |
| size_t pos_after=0; | data.split(head, raw_pos_after, "\n", String::L_AS_IS, 1); |
| data.split(head, pos_after, "\n", String::L_AS_IS, 1); | |
| if(head.count()) { | if(head.count()) { |
| size_t pos_after=0; | size_t col_pos_after=0; |
| head[0]->split(*columns, pos_after, "\t", String::L_AS_IS); | head[0]->split(*columns, col_pos_after, "\t", String::L_AS_IS); |
| } | } |
| } | } |
| Line 129 static void _create(Request& r, MethodPa | Line 154 static void _create(Request& r, MethodPa |
| // parse cells | // parse cells |
| ArrayString rows; | ArrayString rows; |
| size_t pos_after=0; | data.split(rows, raw_pos_after, "\n", String::L_AS_IS); |
| data.split(rows, pos_after, "\n", String::L_AS_IS); | |
| Array_iterator<const String*> i(rows); | Array_iterator<const String*> i(rows); |
| while(i.has_next()) { | while(i.has_next()) { |
| Table::element_type row(new ArrayString); | Table::element_type row(new ArrayString); |
| Line 139 static void _create(Request& r, MethodPa | Line 163 static void _create(Request& r, MethodPa |
| if(!string.length()) | if(!string.length()) |
| continue; | continue; |
| size_t pos_after=0; | size_t col_pos_after=0; |
| string.split(*row, pos_after, "\t", String::L_AS_IS); | string.split(*row, col_pos_after, "\t", String::L_AS_IS); |
| table+=row; | table+=row; |
| } | } |
| Line 148 static void _create(Request& r, MethodPa | Line 172 static void _create(Request& r, MethodPa |
| GET_SELF(r, VTable).set_table(table); | GET_SELF(r, VTable).set_table(table); |
| } | } |
| static void _load(Request& r, MethodParams* params) { | static void _load(Request& r, MethodParams& params) { |
| const String& first_param=params->as_string(0, "file name must be string"); | const String& first_param=params.as_string(0, "file name must be string"); |
| int filename_param_index=0; | int filename_param_index=0; |
| bool nameless=first_param=="nameless"; | bool nameless=first_param=="nameless"; |
| if(nameless) | if(nameless) |
| filename_param_index++; | filename_param_index++; |
| int options_param_index=filename_param_index+1; | size_t options_param_index=filename_param_index+1; |
| // loading text | // loading text |
| char *data=file_read_text(r.charsets.source(), | char *data=file_read_text(r.charsets, |
| r.absolute(params->as_string(filename_param_index, "file name must be string")), | r.absolute(params.as_string(filename_param_index, "file name must be string")), |
| true, | true, |
| options_param_index<params->count()?params->as_no_junction(options_param_index, "additional params must be hash").get_hash():0 | options_param_index<params.count()?params.as_no_junction(options_param_index, "additional params must be hash").get_hash():0 |
| ); | ); |
| // parse columns | // parse columns |
| Line 183 static void _load(Request& r, MethodPara | Line 207 static void _load(Request& r, MethodPara |
| } | } |
| // parse cells | // parse cells |
| Table& table=*new Table(columns); | Table& table=*new Table(columns);//что-то очень плохое с realloc'ом: 1000 сильно помогает |
| char *row_chars; | char *row_chars; |
| int cells=0; | |
| while(row_chars=getrow(&data)) { | while(row_chars=getrow(&data)) { |
| // remove empty&comment lines | // remove empty&comment lines |
| if(!*row_chars || *row_chars == '#') | if(!*row_chars || *row_chars == '#') |
| Line 192 static void _load(Request& r, MethodPara | Line 217 static void _load(Request& r, MethodPara |
| Table::element_type row(new ArrayString); | Table::element_type row(new ArrayString); |
| while(char *cell_chars=lsplit(&row_chars, '\t')) { | while(char *cell_chars=lsplit(&row_chars, '\t')) { |
| *row+=new String(cell_chars, 0, true); | *row+=new String(cell_chars, 0, true); |
| cells++; | |
| } | } |
| table+=row; | table+=row; |
| }; | }; |
| Line 201 static void _load(Request& r, MethodPara | Line 227 static void _load(Request& r, MethodPara |
| } | } |
| /// @todo "x\nx" "xxx""xx" | /// @todo "x\nx" "xxx""xx" |
| static void _save(Request& r, MethodParams* params) { | static void _save(Request& r, MethodParams& params) { |
| Value& vfile_name=params->as_no_junction(params->count()-1, "file name must not be code"); | Value& vfile_name=params.as_no_junction(params.count()-1, "file name must not be code"); |
| Table& table=GET_SELF(r, VTable).table(); | Table& table=GET_SELF(r, VTable).table(); |
| bool do_append=false; | bool do_append=false; |
| String sdata; | String sdata; |
| if(params->count()==1) { // named output | if(params.count()==1) { // named output |
| // write out names line | // write out names line |
| if(table.columns()) { // named table | if(table.columns()) { // named table |
| Array_iterator<const String*> i(*table.columns()); | for(Array_iterator<const String*> i(*table.columns()); i.has_next(); ) { |
| while(i.has_next()) { | |
| sdata.append(*i.next(), String::L_TABLE); | sdata.append(*i.next(), String::L_TABLE); |
| if(i.has_next()) | if(i.has_next()) |
| sdata.append("\t", 1, String::L_CLEAN); | sdata.append_know_length("\t", 1, String::L_CLEAN); |
| } | } |
| } else { // nameless table | } else { // nameless table |
| if(int lsize=table.count()?table[0]->count():0) | if(int lsize=table.count()?table[0]->count():0) |
| for(int column=0; column<lsize; column++) { | for(int column=0; column<lsize; column++) { |
| char *cindex_tab=new char[MAX_NUMBER]; | char *cindex_tab=new(PointerFreeGC) char[MAX_NUMBER]; |
| sdata.append(cindex_tab, | sdata.append_know_length(cindex_tab, |
| snprintf(cindex_tab, MAX_NUMBER, "%d\t", column), | snprintf(cindex_tab, MAX_NUMBER, "%d\t", column), |
| String::L_CLEAN); | String::L_CLEAN); |
| } | } |
| else | else |
| sdata.append("empty nameless table", 0, String::L_CLEAN); | sdata.append_help_length("empty nameless table", 0, String::L_CLEAN); |
| } | } |
| sdata.append("\n", 1, String::L_CLEAN); | sdata.append_know_length("\n", 1, String::L_CLEAN); |
| } else { // mode specified | } else { // mode specified |
| const String& mode=params->as_string(0, "mode must be string"); | const String& mode=params.as_string(0, "mode must be string"); |
| if(mode=="append") | if(mode=="append") |
| do_append=true; | do_append=true; |
| else if(mode=="nameless") | else if(mode=="nameless") |
| Line 244 static void _save(Request& r, MethodPara | Line 269 static void _save(Request& r, MethodPara |
| // data lines | // data lines |
| Array_iterator<ArrayString*> i(table); | Array_iterator<ArrayString*> i(table); |
| while(i.has_next()) { | while(i.has_next()) { |
| Array_iterator<const String*> c(*i.next()); | for(Array_iterator<const String*> c(*i.next()); c.has_next(); ) { |
| while(c.has_next()) { | sdata.append(*c.next(), String::L_TABLE); |
| if(const String* s=c.next()) | |
| sdata.append(*s, String::L_TABLE); | |
| if(c.has_next()) | if(c.has_next()) |
| sdata.append("\t", 1, String::L_CLEAN); | sdata.append_know_length("\t", 1, String::L_CLEAN); |
| } | } |
| sdata.append("\n", 1, String::L_CLEAN); | sdata.append_know_length("\n", 1, String::L_CLEAN); |
| } | } |
| // write | // write |
| Line 259 static void _save(Request& r, MethodPara | Line 282 static void _save(Request& r, MethodPara |
| sdata.cstr(), sdata.length(), true, do_append); | sdata.cstr(), sdata.length(), true, do_append); |
| } | } |
| static void _count(Request& r, MethodParams* ) { | static void _count(Request& r, MethodParams&) { |
| int result=GET_SELF(r, VTable).table().count(); | int result=GET_SELF(r, VTable).table().count(); |
| r.write_no_lang(*new VInt(result)); | r.write_no_lang(*new VInt(result)); |
| } | } |
| static void _line(Request& r, MethodParams* ) { | static void _line(Request& r, MethodParams&) { |
| int result=1+GET_SELF(r, VTable).table().current(); | int result=1+GET_SELF(r, VTable).table().current(); |
| r.write_no_lang(*new VInt(result)); | r.write_no_lang(*new VInt(result)); |
| } | } |
| static void _offset(Request& r, MethodParams* params) { | static void _offset(Request& r, MethodParams& params) { |
| Table& table=GET_SELF(r, VTable).table(); | Table& table=GET_SELF(r, VTable).table(); |
| if(params->count()) { | if(params.count()) { |
| bool absolute=false; | bool absolute=false; |
| if(params->count()>1) { | if(params.count()>1) { |
| const String& whence=params->as_string(0, "whence must be string"); | const String& whence=params.as_string(0, "whence must be string"); |
| if(whence=="cur") | if(whence=="cur") |
| absolute=false; | absolute=false; |
| else if(whence=="set") | else if(whence=="set") |
| Line 285 static void _offset(Request& r, MethodPa | Line 308 static void _offset(Request& r, MethodPa |
| "is invalid whence, valid are 'cur' or 'set'"); | "is invalid whence, valid are 'cur' or 'set'"); |
| } | } |
| Value& offset_expr=params->as_junction(params->count()-1, "offset must be expression"); | Value& offset_expr=params.as_junction(params.count()-1, "offset must be expression"); |
| table.offset(absolute, r.process_to_value(offset_expr).as_int()); | table.offset(absolute, r.process_to_value(offset_expr).as_int()); |
| } else | } else |
| r.write_no_lang(*new VInt(table.current())); | r.write_no_lang(*new VInt(table.current())); |
| } | } |
| static void _menu(Request& r, MethodParams* params) { | static void _menu(Request& r, 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_maybe_code=params->count()>1?(*params)[1]:0; | Value* delim_maybe_code=params.count()>1?¶ms[1]:0; |
| Table& table=GET_SELF(r, VTable).table(); | Table& table=GET_SELF(r, VTable).table(); |
| bool need_delim=false; | bool need_delim=false; |
| Line 316 static void _menu(Request& r, MethodPara | Line 339 static void _menu(Request& r, MethodPara |
| } | } |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| enum Table2hash_distint { D_ILLEGAL, D_FIRST, D_TABLES }; | |
| struct Row_info { | struct Row_info { |
| Request *r; | Request *r; |
| Table *table; | Table *table; |
| Value* key_code; | Value* key_code; |
| int key_field; | size_t key_field; |
| Array<int>* value_fields; | Array<int>* value_fields; |
| HashStringValue* hash; | HashStringValue* hash; |
| bool distinct; | Table2hash_distint distinct; |
| int row; | size_t row; |
| }; | }; |
| #endif | #endif |
| static void table_row_to_hash(Table::element_type row, Row_info *info) { | static void table_row_to_hash(Table::element_type row, Row_info *info) { |
| Line 339 static void table_row_to_hash(Table::ele | Line 363 static void table_row_to_hash(Table::ele |
| if(!key) | if(!key) |
| return; // ignore rows without key [too-short-record_array if-indexed] | return; // ignore rows without key [too-short-record_array if-indexed] |
| VHash* vhash=new VHash; | switch(info->distinct) { |
| HashStringValue& hash=vhash->hash(); | case D_ILLEGAL: case D_FIRST: |
| for(int i=0; i<info->value_fields->count(); i++) { | { |
| int value_field=info->value_fields->get(i); | VHash* vhash=new VHash; |
| if(value_field<row->count()) | HashStringValue& hash=vhash->hash(); |
| hash.put( | for(Array_iterator<int> i(*info->value_fields); i.has_next(); ) { |
| *info->table->columns()->get(value_field), | size_t value_field=i.next(); |
| new VString(*row->get(value_field))); | if(value_field<row->count()) |
| } | hash.put( |
| *info->table->columns()->get(value_field), | |
| new VString(*row->get(value_field))); | |
| } | |
| if(info->hash->put_dont_replace(*key, vhash)) // put. existed? | if(info->hash->put_dont_replace(*key, vhash)) // put. existed? |
| if(!info->distinct) | if(info->distinct==D_ILLEGAL) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| key, | key, |
| "duplicate key"); | "duplicate key"); |
| } | |
| break; | |
| case D_TABLES: | |
| { | |
| VTable* vtable=(VTable*)info->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(0, 0); | |
| table=new Table(*info->table, table_options/*no rows, just structure*/); | |
| info->hash->put(*key, new VTable(table)); | |
| } | |
| *table+=row; | |
| } | |
| break; | |
| default: | |
| throw Exception(0, | |
| 0, | |
| "invalid distinct code (#%d)", info->distinct); | |
| } | |
| } | } |
| static void _hash(Request& r, MethodParams* params) { | static void _hash(Request& r, MethodParams& params) { |
| Table& self_table=GET_SELF(r, VTable).table(); | Table& self_table=GET_SELF(r, VTable).table(); |
| VHash& result=*new VHash; | VHash& result=*new VHash; |
| if(Table::columns_type columns=self_table.columns()) | if(Table::columns_type columns=self_table.columns()) |
| if(columns->count()>0) { | if(columns->count()>0) { |
| bool distinct=false; | Table2hash_distint distinct=D_ILLEGAL; |
| int param_index=params->count()-1; | int param_index=params.count()-1; |
| if(param_index>0) { | if(param_index>0) { |
| if(HashStringValue* options= | if(HashStringValue* options= |
| params->as_no_junction(param_index, "param must not be code").get_hash()) { | params.as_no_junction(param_index, "param must not be code").get_hash()) { |
| --param_index; | --param_index; |
| int valid_options=0; | int valid_options=0; |
| if(Value* vdistinct=options->get(sql_distinct_name)) { | if(Value* vdistinct_code=options->get(sql_distinct_name)) { |
| valid_options++; | 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->count()) | if(valid_options!=options->count()) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| Line 384 static void _hash(Request& r, MethodPara | Line 443 static void _hash(Request& r, MethodPara |
| Array<int> value_fields; | Array<int> value_fields; |
| if(param_index>0) { | if(param_index>0) { |
| Value& value_fields_param=params->as_no_junction(param_index, "value field(s) must not be code"); | 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()) { | if(value_fields_param.is_string()) { |
| value_fields+=self_table.column_name2index( | value_fields+=self_table.column_name2index( |
| *value_fields_param.get_string(), true); | *value_fields_param.get_string(), true); |
| } else if(Table* value_fields_table=value_fields_param.get_table()) { | } else if(Table* value_fields_table=value_fields_param.get_table()) { |
| for(int i=0; i<value_fields_table->count(); i++) { | for(Array_iterator<Table::element_type> i(*value_fields_table); |
| const String& value_field_name= | i.has_next(); ) { |
| *value_fields_table->get(i)->get(0); | const String& value_field_name |
| value_fields+=self_table.column_name2index(value_field_name, true); | =*i.next()->get(0); |
| value_fields | |
| +=self_table.column_name2index(value_field_name, true); | |
| } | } |
| } else | } else |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| Line 400 static void _hash(Request& r, MethodPara | Line 465 static void _hash(Request& r, MethodPara |
| "value field(s) must be string or self_table" | "value field(s) must be string or self_table" |
| ); | ); |
| } else { // by all columns, including key | } else { // by all columns, including key |
| for(int i=0; i<columns->count(); i++) | if(!(distinct!=D_ILLEGAL && distinct!=D_FIRST)) |
| value_fields+=i; | for(size_t i=0; i<columns->count(); i++) |
| value_fields+=i; | |
| } | } |
| { | { |
| Row_info info; | Row_info info={0}; |
| info.r=&r; | info.r=&r; |
| info.table=&self_table; | info.table=&self_table; |
| Value* key_param=(*params)[0]; | Value* key_param=¶ms[0]; |
| info.key_code=key_param->get_junction()?key_param:0; | info.key_code=key_param->get_junction()?key_param:0; |
| info.key_field=info.key_code?-1 | info.key_field=info.key_code?-1 |
| :self_table.column_name2index(key_param->as_string(), true); | :self_table.column_name2index(key_param->as_string(), true); |
| Line 451 static int sort_cmp_double(const void *a | Line 517 static int sort_cmp_double(const void *a |
| else | else |
| return 0; | return 0; |
| } | } |
| static void _sort(Request& r, MethodParams* params) { | static void _sort(Request& r, MethodParams& params) { |
| Value& key_maker=params->as_junction(0, "key-maker must be code"); | Value& key_maker=params.as_junction(0, "key-maker must be code"); |
| bool reverse=params->count()>1/*..[desc|asc|]*/? | bool reverse=params.count()>1/*..[desc|asc|]*/? |
| reverse=params->as_no_junction(1, "order must not be code").as_string()=="desc": | reverse=params.as_no_junction(1, "order must not be code").as_string()=="desc": |
| false; // default=asc | false; // default=asc |
| Table& old_table=GET_SELF(r, VTable).table(); | Table& old_table=GET_SELF(r, VTable).table(); |
| Table& new_table=*new Table(old_table.columns()); | Table& new_table=*new Table(old_table.columns()); |
| Table_seq_item* seq=new Table_seq_item[old_table.count()]; | Table_seq_item* seq=new(PointerFreeGC) Table_seq_item[old_table.count()]; |
| int i; | int i; |
| // calculate key values | // calculate key values |
| bool key_values_are_strings=true; | bool key_values_are_strings=true; |
| for(i=0; i<old_table.count(); i++) { | int old_count=old_table.count(); |
| for(i=0; i<old_count; i++) { | |
| old_table.set_current(i); | old_table.set_current(i); |
| // calculate key value | // calculate key value |
| seq[i].row=old_table[i]; | seq[i].row=old_table[i]; |
| Value* value=r.process_to_value(key_maker).as_expr_result(true/*return string as-is*/); | Value& value=r.process_to_value(key_maker).as_expr_result(true/*return string as-is*/); |
| if(i==0) // determining key values type by first one | if(i==0) // determining key values type by first one |
| key_values_are_strings=value->is_string(); | key_values_are_strings=value.is_string(); |
| if(key_values_are_strings) | if(key_values_are_strings) |
| seq[i].value.c_str=value->as_string().cstr(); | seq[i].value.c_str=value.as_string().cstr(); |
| else | else |
| seq[i].value.d=value->as_double(); | seq[i].value.d=value.as_double(); |
| } | } |
| // sort keys | // sort keys |
| _qsort(seq, old_table.count(), sizeof(Table_seq_item), | _qsort(seq, old_count, sizeof(Table_seq_item), |
| key_values_are_strings?sort_cmp_string:sort_cmp_double); | key_values_are_strings?sort_cmp_string:sort_cmp_double); |
| // reorder table as they require in 'seq' | // reorder table as they require in 'seq' |
| for(i=0; i<old_table.count(); i++) | for(i=0; i<old_count; i++) |
| new_table+=Table::element_type(seq[reverse?old_table.count()-1-i:i].row); | new_table+=Table::element_type(seq[reverse?old_count-1-i:i].row); |
| delete[] seq; | |
| // replace any previous table value | // replace any previous table value |
| GET_SELF(r, VTable).set_table(new_table); | GET_SELF(r, VTable).set_table(new_table); |
| } | } |
| static bool _locate_expression(Request& r, MethodParams* params) { | #ifndef DOXYGEN |
| if(params->count()>1) | struct Expression_is_true_info { |
| throw Exception("parser.runtime", | Request* r; |
| 0, | Value* expression_code; |
| "locate by expression has only one parameter - expression"); | }; |
| #endif | |
| Value& expression_code=params->as_junction(0, "must be expression"); | static bool expression_is_true(Table& self, void* ainfo) { |
| Expression_is_true_info& info=*static_cast<Expression_is_true_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, MethodParams& params) { | |
| check_option_param(o.defined, params, 1, | |
| "locate by expression only has parameters: expression and, maybe, options"); | |
| Value& expression_code=params.as_junction(0, "must be expression"); | |
| Expression_is_true_info info={&r, &expression_code}; | |
| return table.table_first_that(expression_is_true, &info, o); | |
| } | |
| static bool _locate_name_value(Table& table, Table::Action_options o, | |
| Request& r, MethodParams& params) { | |
| check_option_param(o.defined, 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"); | |
| return table.locate(name, value, o); | |
| } | |
| static void _locate(Request& r, MethodParams& params) { | |
| Table& table=GET_SELF(r, VTable).table(); | Table& table=GET_SELF(r, VTable).table(); |
| int saved_current=table.current(); | |
| int size=table.count(); | |
| for(int row=0; row<size; row++) { | |
| table.set_current(row); | |
| if(r.process_to_value(expression_code).as_bool()) | Table::Action_options o=get_action_options(r, params, table); |
| return true; | |
| } | |
| table.set_current(saved_current); | |
| return false; | |
| } | |
| static bool _locate_name_value(Request& r, MethodParams* params) { | |
| if(params->count()>2) | |
| throw Exception("parser.runtime", | |
| 0, | |
| "locate by name and value has only two parameters - name and value"); | |
| Table& table=GET_SELF(r, VTable).table(); | bool result=params[0].get_junction()? |
| return table.locate( | _locate_expression(table, o, r, params) : |
| params->as_string(0, "column name must be string"), | _locate_name_value(table, o, r, params); |
| params->as_string(1, "value must be string") | |
| ); | |
| } | |
| static void _locate(Request& r, MethodParams* params) { | |
| bool result=(*params)[0]->get_junction()? | |
| _locate_expression(r, params) : | |
| _locate_name_value(r, params); | |
| r.write_no_lang(*new VBool(result)); | r.write_no_lang(*new VBool(result)); |
| } | } |
| static void _flip(Request& r, MethodParams* params) { | |
| static void _flip(Request& r, MethodParams& params) { | |
| Table& old_table=GET_SELF(r, VTable).table(); | Table& old_table=GET_SELF(r, VTable).table(); |
| Table& new_table=*new Table(old_table.columns()); | Table& new_table=*new Table(old_table.columns()); |
| if(old_table.count()) | if(size_t old_count=old_table.count()) |
| if(int old_cols=old_table[0]->count()) | if(size_t old_cols=old_table[0]->count()) |
| for(int column=0; column<old_cols; column++) { | for(size_t column=0; column<old_cols; column++) { |
| Table::element_type new_row(new ArrayString(old_table.count())); | Table::element_type new_row(new ArrayString(old_count)); |
| for(int i=0; i<old_table.count(); i++) { | for(size_t i=0; i<old_count; i++) { |
| Table::element_type old_row=old_table[i]; | Table::element_type old_row=old_table[i]; |
| *new_row+=column<old_row->count()?old_row->get(column):new String; | *new_row+=column<old_row->count()?old_row->get(column):new String; |
| } | } |
| Line 547 static void _flip(Request& r, MethodPara | Line 616 static void _flip(Request& r, MethodPara |
| r.write_no_lang(*new VTable(&new_table)); | r.write_no_lang(*new VTable(&new_table)); |
| } | } |
| static void _append(Request& r, MethodParams* params) { | static void _append(Request& r, MethodParams& params) { |
| // data | // data |
| Temp_lang temp_lang(r, String::L_PASS_APPENDED); | Temp_lang temp_lang(r, String::L_PASS_APPENDED); |
| const String& string=r.process_to_string(params->as_junction(0, "body must be code")); | const String& string=r.process_to_string(params.as_junction(0, "body must be code")); |
| // parse cells | // parse cells |
| Table::element_type row=new ArrayString; | Table::element_type row=new ArrayString; |
| Line 560 static void _append(Request& r, MethodPa | Line 629 static void _append(Request& r, MethodPa |
| GET_SELF(r, VTable).table()+=row; | GET_SELF(r, VTable).table()+=row; |
| } | } |
| static void _join(Request& r, MethodParams* params) { | static void join_named_row(Table& src, Table* dest) { |
| Table* maybe_src=params->as_no_junction(0, "table ref must not be code").get_table(); | Table::columns_type dest_columns=dest->columns(); |
| size_t dest_columns_count=dest_columns->count(); | |
| Table::element_type dest_row(new ArrayString(dest_columns_count)); | |
| for(size_t dest_column=0; dest_column<dest_columns_count; dest_column++) { | |
| const String* src_item=src.item(*dest_columns->get(dest_column)); | |
| *dest_row+=src_item?src_item:new String; | |
| } | |
| *dest+=dest_row; | |
| } | |
| static void join_nameless_row(Table& src, Table* dest) { | |
| *dest+=src[src.current()]; | |
| } | |
| static void _join(Request& r, MethodParams& params) { | |
| 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", |
| 0, | 0, |
| "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, params, src); | |
| check_option_param(o.defined, params, 1, | |
| "invalid extra parameter"); | |
| Table& dest=GET_SELF(r, VTable).table(); | Table& dest=GET_SELF(r, VTable).table(); |
| if(&src == &dest) | if(&src == &dest) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| 0, | 0, |
| "source and destination are same table"); | "source and destination are same table"); |
| int offset, limit; | if(Table::columns_type dest_columns=dest.columns()) // dest is named |
| get_copy_options(r, params, 1, src, | src.table_for_each(join_named_row, &dest, o); |
| offset, limit); | else // dest is nameless |
| src.table_for_each(join_nameless_row, &dest, o); | |
| if(Table::columns_type dest_columns=dest.columns()) { // dest is named | |
| int saved_src_current=src.current(); | |
| int m=src.count()-offset; | |
| if(!limit || limit>m) | |
| limit=m; | |
| int end=offset+limit; | |
| for(int src_row=offset; src_row<end; src_row++) { | |
| src.set_current(src_row); | |
| Table::element_type dest_row(new ArrayString(src.count())); | |
| for(int dest_column=0; dest_column<dest_columns->count(); dest_column++) { | |
| const String* src_item=src.item(*dest_columns->get(dest_column)); | |
| *dest_row+=src_item?src_item:new String; | |
| } | |
| dest+=dest_row; | |
| } | |
| src.set_current(saved_src_current); | |
| } else { // dest is nameless | |
| for(int src_row=0; src_row<src.count(); src_row++) | |
| dest+=src[src_row]; | |
| } | |
| } | } |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| Line 611 public: | Line 677 public: |
| columns(*new ArrayString), row(0), table(0) { | columns(*new ArrayString), row(0), table(0) { |
| } | } |
| bool add_column(SQL_Error& error, void *ptr, size_t size) { | bool add_column(SQL_Error& error, const char *str, size_t length) { |
| try { | try { |
| columns+=new String((const char* )ptr, size, true); | columns+=new String(str, length, true); |
| return false; | return false; |
| } catch(...) { | } catch(...) { |
| error=SQL_Error("exception occured in Table_sql_event_handlers::add_column"); | error=SQL_Error("exception occured in Table_sql_event_handlers::add_column"); |
| Line 638 public: | Line 704 public: |
| return true; | return true; |
| } | } |
| } | } |
| bool add_row_cell(SQL_Error& error, void *ptr, size_t size) { | bool add_row_cell(SQL_Error& error, const char* str, size_t length) { |
| try { | try { |
| String& cell=*new String; | String& cell=*new String; |
| if(size) | if(length) |
| cell.append((const char* )ptr, size, String::L_TAINTED); | cell.append_know_length(str, length, String::L_TAINTED); |
| *row+=&cell; | *row+=&cell; |
| return false; | return false; |
| } catch(...) { | } catch(...) { |
| Line 652 public: | Line 718 public: |
| } | } |
| }; | }; |
| #endif | #endif |
| static void _sql(Request& r, MethodParams* params) { | static void _sql(Request& r, MethodParams& params) { |
| Value& statement=params->as_junction(0, "statement must be code"); | Value& statement=params.as_junction(0, "statement must be code"); |
| ulong limit=0; | ulong limit=0; |
| ulong offset=0; | ulong offset=0; |
| if(params->count()>1) { | if(params.count()>1) { |
| Value& voptions=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(!voptions.is_string()) | if(!voptions.is_string()) |
| if(HashStringValue* options=voptions.get_hash()) { | if(HashStringValue* options=voptions.get_hash()) { |
| int valid_options=0; | int valid_options=0; |
| Line 714 static void _sql(Request& r, MethodParam | Line 780 static void _sql(Request& r, MethodParam |
| GET_SELF(r, VTable).set_table(result); | GET_SELF(r, VTable).set_table(result); |
| } | } |
| static void _columns(Request& r, MethodParams* ) { | static void _columns(Request& r, MethodParams&) { |
| Table::columns_type result_columns(new ArrayString); | Table::columns_type result_columns(new ArrayString); |
| *result_columns+=new String("column"); | *result_columns+=new String("column"); |
| Line 722 static void _columns(Request& r, MethodP | Line 788 static void _columns(Request& r, MethodP |
| Table& source_table=GET_SELF(r, VTable).table(); | Table& source_table=GET_SELF(r, VTable).table(); |
| if(Table::columns_type source_columns=source_table.columns()) { | if(Table::columns_type source_columns=source_table.columns()) { |
| Array_iterator<const String*> i(*source_columns); | for(Array_iterator<const String*> i(*source_columns); i.has_next(); ) { |
| while(i.has_next()) { | |
| Table::element_type result_row(new ArrayString); | Table::element_type result_row(new ArrayString); |
| *result_row+=i.next(); | *result_row+=i.next(); |
| result_table+=result_row; | result_table+=result_row; |
| Line 733 static void _columns(Request& r, MethodP | Line 798 static void _columns(Request& r, MethodP |
| r.write_no_lang(*new VTable(&result_table)); | r.write_no_lang(*new VTable(&result_table)); |
| } | } |
| static void _select(Request& r, MethodParams* params) { | static void _select(Request& r, MethodParams& params) { |
| Value& vcondition=params->as_junction(0, "condition must be expression"); | Value& vcondition=params.as_junction(0, "condition must be expression"); |
| Table& source_table=GET_SELF(r, VTable).table(); | Table& source_table=GET_SELF(r, VTable).table(); |
| Table& result_table=*new Table(source_table.columns()); | Table& result_table=*new Table(source_table.columns()); |
| Line 798 MTable::MTable(): Methoded("table") { | Line 863 MTable::MTable(): Methoded("table") { |
| 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); |