|
|
| version 1.172.2.14.2.11, 2003/03/24 10:31:17 | version 1.172.2.14.2.17, 2003/03/24 15:56:58 |
|---|---|
| Line 43 DECLARE_CLASS_VAR(table, new MTable, 0); | Line 43 DECLARE_CLASS_VAR(table, new MTable, 0); |
| // 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); |
| // methods | // methods |
| Line 60 static void get_copy_options(Request& r, | Line 60 static void get_copy_options(Request& r, |
| if(params->count()<=param_index) | if(params->count()<=param_index) |
| return; | return; |
| Value* voptions=params->as_no_junction(param_index, "options must be hash, not code"); | Value& voptions=params->as_no_junction(param_index, "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; |
| if(Value* voffset=options->get(sql_offset_name)) { | if(Value* voffset=options->get(sql_offset_name)) { |
| valid_options++; | valid_options++; |
| if(voffset->is_string()) { | if(voffset->is_string()) { |
| const String& soffset=voffset->get_string(); | const String& soffset=*voffset->get_string(); |
| if(*soffset == "cur") | if(soffset == "cur") |
| offset=source.current(); | offset=source.current(); |
| else | else |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| Line 79 static void get_copy_options(Request& r, | Line 79 static void get_copy_options(Request& r, |
| } | } |
| if(Value* vlimit=options->get(sql_limit_name)) { | if(Value* vlimit=options->get(sql_limit_name)) { |
| valid_options++; | valid_options++; |
| limit=r.process_to_value(vlimit).as_int(); | limit=r.process_to_value(*vlimit).as_int(); |
| if(!limit) // zero limit = should be 'nothing to copy', for methods zero means 'all' | if(!limit) // zero limit = should be 'nothing to copy', for methods zero means 'all' |
| limit=-1; // thus fixing | limit=-1; // thus fixing |
| } | } |
| Line 96 static void get_copy_options(Request& r, | Line 96 static void get_copy_options(Request& r, |
| static void _create(Request& r, MethodParams* params) { | static void _create(Request& r, MethodParams* params) { |
| // clone/copy part? | // clone/copy part? |
| if(const Table *source=(*params)[0]->get_table()) { | if(Table *source=(*params)[0]->get_table()) { |
| int offset, limit; | int offset, limit; |
| get_copy_options(r, method_name, params, 1, *source, | get_copy_options(r, params, 1, *source, |
| offset, limit); | offset, limit); |
| GET_SELF(r, VTable).set_table(Table*(new Table(*source, offset, limit))); | GET_SELF(r, VTable).set_table(*new Table(*source, offset, limit)); |
| return; | return; |
| } | } |
| Line 109 static void _create(Request& r, MethodPa | Line 109 static void _create(Request& r, MethodPa |
| 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")); |
| size_t pos_after=0; | |
| // 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 |
| Line 118 static void _create(Request& r, MethodPa | Line 118 static void _create(Request& r, MethodPa |
| columns=Table::columns_type(new ArrayString); | columns=Table::columns_type(new ArrayString); |
| ArrayString head; | ArrayString head; |
| data->split(head, &pos_after, "\n", 1, String::L_AS_IS, 1); | data.split(head, raw_pos_after, "\n", String::L_AS_IS, 1); |
| if(head.count()) | if(head.count()) { |
| head[0]->split(*columns, 0, "\t", 1, String::L_AS_IS); | size_t col_pos_after=0; |
| head[0]->split(*columns, col_pos_after, "\t", String::L_AS_IS); | |
| } | |
| } | } |
| Table* table(new Table(method_name, columns)); | Table& table=*new Table(columns); |
| // parse cells | // parse cells |
| ArrayString rows; | ArrayString rows; |
| data->split(rows, &pos_after, "\n", 1, String::L_AS_IS); | data.split(rows, raw_pos_after, "\n", String::L_AS_IS); |
| Array_iterator<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); |
| const String& string=i.next(); | const String& string=*i.next(); |
| // remove comment lines | // remove comment lines |
| if(!string->length()) | if(!string.length()) |
| continue; | continue; |
| string->split(*row, 0, "\t", 1, String::L_AS_IS); | size_t col_pos_after=0; |
| *table+=row; | string.split(*row, col_pos_after, "\t", String::L_AS_IS); |
| table+=row; | |
| } | } |
| // replace any previous table value | // replace any previous table value |
| Line 147 static void _create(Request& r, MethodPa | Line 150 static void _create(Request& r, MethodPa |
| 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; | int options_param_index=filename_param_index+1; |
| Line 156 static void _load(Request& r, MethodPara | Line 159 static void _load(Request& r, MethodPara |
| char *data=file_read_text(r.charsets.source(), | char *data=file_read_text(r.charsets.source(), |
| 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 |
| Table::columns_type columns; | Table::columns_type columns; |
| #ifndef NO_STRING_ORIGIN | |
| const String_fragment::Origin& origin=method_name->origin(); | |
| const char* file=origin.file; | |
| uint line=origin.line; | |
| #endif | |
| if(nameless) { | if(nameless) { |
| columns=Table::columns_type(0); // nameless | columns=Table::columns_type(0); // nameless |
| } else { | } else { |
| Line 176 static void _load(Request& r, MethodPara | Line 174 static void _load(Request& r, MethodPara |
| if(!*row_chars || *row_chars == '#') | if(!*row_chars || *row_chars == '#') |
| continue; | continue; |
| do { | do { |
| *columns+=new String(lsplit(&row_chars, '\t'), 0, String::L_TAINTED); | *columns+=new String(lsplit(&row_chars, '\t'), 0, true); |
| } while(row_chars); | } while(row_chars); |
| break; | break; |
| Line 194 static void _load(Request& r, MethodPara | Line 192 static void _load(Request& r, MethodPara |
| 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); |
| } | } |
| #ifndef NO_STRING_ORIGIN | |
| line++; | |
| #endif | |
| table+=row; | table+=row; |
| }; | }; |
| Line 215 static void _save(Request& r, MethodPara | Line 210 static void _save(Request& r, MethodPara |
| 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<String*> i(*table.columns()); | Array_iterator<const String*> i(*table.columns()); |
| while(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_CONST("\t"); | sdata.append("\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(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_CONST("empty nameless table"); | sdata.append("empty nameless table", 0, String::L_CLEAN); |
| } | } |
| sdata.APPEND_CONST("\n"); | sdata.append("\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") |
| /*ok, already skipped names output*/; | /*ok, already skipped names output*/; |
| else | else |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| mode, | &mode, |
| "unknown mode, must be 'append'"); | "unknown mode, must be 'append'"); |
| } | } |
| // data lines | // data lines |
| Array_iterator<ArrayString*> i(table); | Array_iterator<ArrayString*> i(table); |
| while(i.has_next()) { | while(i.has_next()) { |
| Array_iterator<String*> c(*i.next()); | Array_iterator<const String*> c(*i.next()); |
| while(c.has_next()) { | while(c.has_next()) { |
| if(const String* s=c.next()) | if(const String* s=c.next()) |
| sdata.append(*s, String::L_TABLE); | sdata.append(*s, String::L_TABLE); |
| Line 265 static void _save(Request& r, MethodPara | Line 260 static void _save(Request& r, MethodPara |
| 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(Value*(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(Value*(new VInt(result))); | r.write_no_lang(*new VInt(result)); |
| } | } |
| static void _offset(Request& r, MethodParams* params) { | static void _offset(Request& r, MethodParams* params) { |
| Line 279 static void _offset(Request& r, MethodPa | Line 274 static void _offset(Request& r, MethodPa |
| 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") |
| absolute=true; | absolute=true; |
| else | else |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| whence, | &whence, |
| "is invalid whence, valid are 'cur' or 'set'"); | "is invalid whence, valid are 'cur' or 'set'"); |
| } | } |
| Line 296 static void _offset(Request& r, MethodPa | Line 291 static void _offset(Request& r, MethodPa |
| } | } |
| 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?(*params)[1]:0; |
| Line 308 static void _menu(Request& r, MethodPara | Line 303 static void _menu(Request& r, MethodPara |
| table.set_current(row); | table.set_current(row); |
| StringOrValue sv_processed=r.process(body_code); | StringOrValue sv_processed=r.process(body_code); |
| const String& s_processed=sv_processed.get_string(); | const String* s_processed=sv_processed.get_string(); |
| if(delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body | if(delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body |
| if(need_delim) // need delim & iteration produced string? | if(need_delim) // need delim & iteration produced string? |
| r.write_pass_lang(r.process(delim_maybe_code)); | r.write_pass_lang(r.process(*delim_maybe_code)); |
| need_delim=true; | need_delim=true; |
| } | } |
| r.write_pass_lang(sv_processed); | r.write_pass_lang(sv_processed); |
| Line 332 struct Row_info { | Line 327 struct Row_info { |
| }; | }; |
| #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) { |
| const String& key; | const String* key; |
| if(info->key_code) { | if(info->key_code) { |
| info->table->set_current(info->row++); // change context row | info->table->set_current(info->row++); // change context row |
| StringOrValue sv_processed=info->r->process(info->key_code); | StringOrValue sv_processed=info->r->process(*info->key_code); |
| key=sv_processed.as_string(); | key=&sv_processed.as_string(); |
| } else | } else |
| key=info->key_field<row->count()?row->get(info->key_field):0; | key=info->key_field<row->count()?row->get(info->key_field):0; |
| Line 349 static void table_row_to_hash(Table::ele | Line 344 static void table_row_to_hash(Table::ele |
| int value_field=info->value_fields->get(i); | int value_field=info->value_fields->get(i); |
| if(value_field<row->count()) | if(value_field<row->count()) |
| hash.put( | hash.put( |
| info->table->columns()->get(value_field), | *info->table->columns()->get(value_field), |
| Value*(new VString(row->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) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| &key, | key, |
| "duplicate key"); | "duplicate key"); |
| } | } |
| 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; | bool distinct=false; |
| 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=options->get(sql_distinct_name)) { |
| valid_options++; | valid_options++; |
| distinct=r.process_to_value(vdistinct)->as_bool(); | distinct=r.process_to_value(*vdistinct).as_bool(); |
| } | } |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| method_name, | 0, |
| "called with invalid option"); | "called with invalid option"); |
| } | } |
| } | } |
| if(param_index==2) // bad options param type | if(param_index==2) // bad options param type |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| method_name, | 0, |
| "options must be hash"); | "options must be hash"); |
| 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"); | 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->as_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(int i=0; i<value_fields_table->count(); i++) { |
| const String& value_field_name= | const String& value_field_name= |
| value_fields_table->get(i)->get(0); | *value_fields_table->get(i)->get(0); |
| value_fields+=self_table.column_name2index(value_field_name, true); | value_fields+=self_table.column_name2index(value_field_name, true); |
| } | } |
| } else | } else |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| method_name, | 0, |
| "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 |
| Line 418 static void _hash(Request& r, MethodPara | Line 413 static void _hash(Request& r, MethodPara |
| 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); |
| info.value_fields=&value_fields; | info.value_fields=&value_fields; |
| info.hash=&result->hash(); | info.hash=&result.hash(); |
| info.distinct=distinct; | info.distinct=distinct; |
| info.row=0; | info.row=0; |
| Line 434 static void _hash(Request& r, MethodPara | Line 429 static void _hash(Request& r, MethodPara |
| struct Table_seq_item { | struct Table_seq_item { |
| ArrayString* row; | ArrayString* row; |
| union { | union { |
| char *c_str; | const char *c_str; |
| double d; | double d; |
| } value; | } value; |
| }; | }; |
| Line 459 static void _sort(Request& r, MethodPara | Line 454 static void _sort(Request& r, MethodPara |
| 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(method_name, old_table.columns())); | Table& new_table=*new Table(old_table.columns()); |
| smart_ptr<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 |
| Line 474 static void _sort(Request& r, MethodPara | Line 469 static void _sort(Request& r, MethodPara |
| 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(local_pool); | 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(); |
| } | } |
| Line 489 static void _sort(Request& r, MethodPara | Line 484 static void _sort(Request& r, MethodPara |
| // 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_table.count(); i++) |
| *new_table+=Table::element_type(seq[reverse?old_table.count()-1-i:i].row); | new_table+=Table::element_type(seq[reverse?old_table.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); |
| Line 498 static void _sort(Request& r, MethodPara | Line 495 static void _sort(Request& r, MethodPara |
| static bool _locate_expression(Request& r, MethodParams* params) { | static bool _locate_expression(Request& r, MethodParams* params) { |
| if(params->count()>1) | if(params->count()>1) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| method_name, | 0, |
| "locate by expression has only one parameter - expression"); | "locate by expression has only one parameter - expression"); |
| Value* expression_code=params->as_junction(0, "must be expression"); | Value& expression_code=params->as_junction(0, "must be expression"); |
| Table& table=GET_SELF(r, VTable).table(); | Table& table=GET_SELF(r, VTable).table(); |
| int saved_current=table.current(); | int saved_current=table.current(); |
| Line 509 static bool _locate_expression(Request& | Line 506 static bool _locate_expression(Request& |
| for(int row=0; row<size; row++) { | for(int row=0; row<size; row++) { |
| table.set_current(row); | table.set_current(row); |
| if(r.process_to_value(expression_code)->as_bool()) | if(r.process_to_value(expression_code).as_bool()) |
| return true; | return true; |
| } | } |
| table.set_current(saved_current); | table.set_current(saved_current); |
| Line 518 static bool _locate_expression(Request& | Line 515 static bool _locate_expression(Request& |
| static bool _locate_name_value(Request& r, MethodParams* params) { | static bool _locate_name_value(Request& r, MethodParams* params) { |
| if(params->count()>2) | if(params->count()>2) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| method_name, | 0, |
| "locate by name and value has only two parameters - name and value"); | "locate by name and value has only two parameters - name and value"); |
| Table& table=GET_SELF(r, VTable).table(); | Table& table=GET_SELF(r, VTable).table(); |
| Line 529 static bool _locate_name_value(Request& | Line 526 static bool _locate_name_value(Request& |
| } | } |
| static void _locate(Request& r, MethodParams* params) { | static void _locate(Request& r, MethodParams* params) { |
| bool result=(*params)[0]->get_junction()? | bool result=(*params)[0]->get_junction()? |
| _locate_expression(r, method_name, params) : | _locate_expression(r, params) : |
| _locate_name_value(r, method_name, params); | _locate_name_value(r, params); |
| r.write_no_lang(Value*(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(method_name, old_table.columns())); | Table& new_table=*new Table(old_table.columns()); |
| if(old_table.count()) | if(old_table.count()) |
| if(int old_cols=old_table[0]->count()) | if(int old_cols=old_table[0]->count()) |
| for(int column=0; column<old_cols; column++) { | for(int column=0; column<old_cols; column++) { |
| Table::element_type new_row(new ArrayString(old_table.count())); | Table::element_type new_row(new ArrayString(old_table.count())); |
| for(int i=0; i<old_table.count(); i++) { | for(int i=0; i<old_table.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):String* (new String); | *new_row+=column<old_row->count()?old_row->get(column):new String; |
| } | } |
| *new_table+=new_row; | new_table+=new_row; |
| } | } |
| r.write_no_lang(Value*(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) { |
| Line 557 static void _append(Request& r, MethodPa | Line 554 static void _append(Request& r, MethodPa |
| 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 |
| ArrayString& row(new ArrayString); | Table::element_type row=new ArrayString; |
| string->split(*row, 0, "\t", 1, String::L_AS_IS); | size_t pos_after=0; |
| string.split(*row, pos_after, "\t", String::L_AS_IS); | |
| GET_SELF(r, VTable).table()+=row; | GET_SELF(r, VTable).table()+=row; |
| } | } |
| static void _join(Request& r, MethodParams* params) { | static void _join(Request& r, MethodParams* params) { |
| 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, | 0, |
| "source is not a table"); | "source is not a table"); |
| Table& src=*maybe_src; | Table& src=*maybe_src; |
| 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", |
| method_name, | 0, |
| "source and destination are same table"); | "source and destination are same table"); |
| int offset, limit; | int offset, limit; |
| get_copy_options(r, method_name, params, 1, src, | get_copy_options(r, params, 1, src, |
| offset, limit); | offset, limit); |
| if(Table::columns_type dest_columns=dest.columns()) { // dest is named | if(Table::columns_type dest_columns=dest.columns()) { // dest is named |
| Line 592 static void _join(Request& r, MethodPara | Line 589 static void _join(Request& r, MethodPara |
| src.set_current(src_row); | src.set_current(src_row); |
| Table::element_type dest_row(new ArrayString(src.count())); | Table::element_type dest_row(new ArrayString(src.count())); |
| for(int dest_column=0; dest_column<dest_columns->count(); dest_column++) { | for(int dest_column=0; dest_column<dest_columns->count(); dest_column++) { |
| const String& src_item=src.item(dest_columns->get(dest_column)); | const String* src_item=src.item(*dest_columns->get(dest_column)); |
| *dest_row+=src_item?src_item:String* (new String); | *dest_row+=src_item?src_item:new String; |
| } | } |
| dest+=dest_row; | dest+=dest_row; |
| } | } |
| Line 606 static void _join(Request& r, MethodPara | Line 603 static void _join(Request& r, MethodPara |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| class Table_sql_event_handlers: public SQL_Driver_query_event_handlers { | class Table_sql_event_handlers: public SQL_Driver_query_event_handlers { |
| const String& method_name; | |
| const String& statement_string; const char* statement_cstr; | |
| ArrayString& columns; | ArrayString& columns; |
| ArrayString* row; | ArrayString* row; |
| public: | public: |
| Table* table; | Table* table; |
| public: | public: |
| Table_sql_event_handlers(const String& amethod_name, | Table_sql_event_handlers() : |
| const String& astatement_string, const char* astatement_cstr) : | columns(*new ArrayString), row(0), table(0) { |
| method_name(amethod_name), | |
| statement_string(astatement_string), statement_cstr(astatement_cstr), | |
| columns(new ArrayString), row(0) { | |
| } | } |
| bool add_column(SQL_Error& error, void *ptr, size_t size) { | bool add_column(SQL_Error& error, void *ptr, size_t size) { |
| try { | try { |
| const String& column(new String); | columns+=new String((const char* )ptr, size, true); |
| column->APPEND_TAINTED( | |
| (const char* )ptr, size, | |
| statement_cstr, 0); | |
| *columns+=column; | |
| 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 635 public: | Line 623 public: |
| } | } |
| bool before_rows(SQL_Error& error) { | bool before_rows(SQL_Error& error) { |
| try { | try { |
| table=Table*(new Table( method_name, columns)); | table=new Table(&columns); |
| return false; | return false; |
| } catch(...) { | } catch(...) { |
| error=SQL_Error("exception occured in Table_sql_event_handlers::before_rows"); | error=SQL_Error("exception occured in Table_sql_event_handlers::before_rows"); |
| Line 644 public: | Line 632 public: |
| } | } |
| bool add_row(SQL_Error& error) { | bool add_row(SQL_Error& error) { |
| try { | try { |
| *table+=(ArrayString* (row=new ArrayString)); | *table+=row=new ArrayString; |
| return false; | return false; |
| } catch(...) { | } catch(...) { |
| error=SQL_Error("exception occured in Table_sql_event_handlers::add_row"); | error=SQL_Error("exception occured in Table_sql_event_handlers::add_row"); |
| Line 653 public: | Line 641 public: |
| } | } |
| bool add_row_cell(SQL_Error& error, void *ptr, size_t size) { | bool add_row_cell(SQL_Error& error, void *ptr, size_t size) { |
| try { | try { |
| const String& cell(new String); | String& cell=*new String; |
| if(size) | if(size) |
| cell->APPEND_TAINTED( | cell.append((const char* )ptr, size, String::L_TAINTED); |
| (const char* )ptr, size, | *row+=&cell; |
| statement_cstr, table->count()-1); | |
| *row+=cell; | |
| return false; | return false; |
| } catch(...) { | } catch(...) { |
| error=SQL_Error("exception occured in Table_sql_event_handlers::add_row_cell"); | error=SQL_Error("exception occured in Table_sql_event_handlers::add_row_cell"); |
| Line 668 public: | Line 654 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; |
| if(Value* vlimit=options->get(sql_limit_name)) { | if(Value* vlimit=options->get(sql_limit_name)) { |
| valid_options++; | valid_options++; |
| limit=(ulong)r.process_to_value(vlimit)->as_double(); | limit=(ulong)r.process_to_value(*vlimit).as_double(); |
| } | } |
| if(Value* voffset=options->get(sql_offset_name)) { | if(Value* voffset=options->get(sql_offset_name)) { |
| valid_options++; | valid_options++; |
| offset=(ulong)r.process_to_value(voffset)->as_double(); | offset=(ulong)r.process_to_value(*voffset).as_double(); |
| } | } |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| method_name, | 0, |
| "called with invalid option"); | "called with invalid option"); |
| } else | } else |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| method_name, | 0, |
| "options must be hash"); | "options must be hash"); |
| } | } |
| Temp_lang temp_lang(r, String::L_SQL); | Temp_lang temp_lang(r, String::L_SQL); |
| const String& statement_string=r.process_to_string(statement); | const String& statement_string=r.process_to_string(statement); |
| const char* statement_cstr= | const char* statement_cstr= |
| statement_string->cstr(String::L_UNSPECIFIED, r.connection()); | statement_string.cstr(String::L_UNSPECIFIED, r.connection()); |
| Table_sql_event_handlers handlers(method_name, | Table_sql_event_handlers handlers; |
| statement_string, statement_cstr); | |
| #ifdef RESOURCES_DEBUG | #ifdef RESOURCES_DEBUG |
| struct timeval mt[2]; | struct timeval mt[2]; |
| //measure:before | //measure:before |
| Line 723 static void _sql(Request& r, MethodParam | Line 707 static void _sql(Request& r, MethodParam |
| r.sql_request_time+=t[1]-t[0]; | r.sql_request_time+=t[1]-t[0]; |
| #endif | #endif |
| Table* result= | Table& result= |
| handlers.table?handlers.table: // query resulted in table? return it | handlers.table?*handlers.table: // query resulted in table? return it |
| Table*(new Table(method_name, Table::columns_type(0))); // query returned no table, fake it | *new Table(Table::columns_type(0)); // query returned no table, fake it |
| // replace any previous table value | // replace any previous table value |
| GET_SELF(r, VTable).set_table(result); | GET_SELF(r, VTable).set_table(result); |
| Line 734 static void _sql(Request& r, MethodParam | Line 718 static void _sql(Request& r, MethodParam |
| 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+=String* (new String("column")); | *result_columns+=new String("column"); |
| Table* result_table(new Table(method_name, result_columns)); | Table& result_table=*new Table(result_columns); |
| 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<String*> i(*source_columns); | Array_iterator<const String*> i(*source_columns); |
| while(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; |
| } | } |
| } | } |
| r.write_no_lang(Value*(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( | Table& result_table=*new Table(source_table.columns()); |
| source_table.origin_string(), | |
| source_table.columns() | |
| )); | |
| int saved_current=source_table.current(); | int saved_current=source_table.current(); |
| int size=source_table.count(); | int size=source_table.count(); |
| Line 767 static void _select(Request& r, MethodPa | Line 747 static void _select(Request& r, MethodPa |
| bool condition=r.process_to_value(vcondition, | bool condition=r.process_to_value(vcondition, |
| /*0/*no name* /,*/ | /*0/*no name* /,*/ |
| false/*don't intercept string*/)->as_bool(); | false/*don't intercept string*/).as_bool(); |
| if(condition) // ...condition is true= | if(condition) // ...condition is true= |
| *result_table+=source_table[row]; // =green light to go to result | result_table+=source_table[row]; // =green light to go to result |
| } | } |
| source_table.set_current(saved_current); | source_table.set_current(saved_current); |
| r.write_no_lang(Value*(new VTable(result_table))); | r.write_no_lang(*new VTable(&result_table)); |
| } | } |
| // constructor | // constructor |