|
|
| version 1.172.2.14, 2003/03/08 12:22:06 | version 1.172.2.14.2.12, 2003/03/24 11:05:27 |
|---|---|
| Line 21 static const char* IDENT_TABLE_C="$Date$ | Line 21 static const char* IDENT_TABLE_C="$Date$ |
| class MTable: public Methoded { | class MTable: public Methoded { |
| public: // VStateless_class | public: // VStateless_class |
| ValuePtr create_new_value() { return ValuePtr(new VTable()); } | Value* create_new_value() { return new VTable(); } |
| public: | public: |
| MTable(); | MTable(); |
| Line 43 DECLARE_CLASS_VAR(table, new MTable, 0); | Line 43 DECLARE_CLASS_VAR(table, new MTable, 0); |
| // globals | // globals |
| StringPtr sql_limit_name(new String(SQL_LIMIT_NAME)); | const String sql_limit_name(SQL_LIMIT_NAME); |
| StringPtr sql_offset_name(new String(SQL_OFFSET_NAME)); | const String sql_offset_name(SQL_OFFSET_NAME); |
| StringPtr sql_default_name(new String(SQL_DEFAULT_NAME)); | const String sql_default_name(SQL_DEFAULT_NAME); |
| StringPtr sql_distinct_name(new String(SQL_DISTINCT_NAME)); | const String sql_distinct_name(SQL_DISTINCT_NAME); |
| // methods | // methods |
| static void get_copy_options(Request& r, StringPtr method_name, MethodParams* params, int param_index, | static void get_copy_options(Request& r, MethodParams* params, int param_index, |
| const Table& source, | const Table& source, |
| int& offset, | int& offset, |
| int& limit) { | int& limit) { |
| Pool& pool=r.pool(); | |
| offset=0; | offset=0; |
| limit=0; | limit=0; |
| if(params->count()<=param_index) | if(params->count()<=param_index) |
| return; | return; |
| ValuePtr 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(method_name)) { | if(HashStringValue* options=voptions->get_hash()) { |
| int valid_options=0; | int valid_options=0; |
| if(ValuePtr 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()) { |
| StringPtr soffset=voffset->get_string(&pool); | 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", |
| soffset, | &soffset, |
| "must be 'cur' string or expression"); | "must be 'cur' string or expression"); |
| } else | } else |
| offset=r.process_to_value(voffset)->as_int(); | offset=r.process_to_value(*voffset).as_int(); |
| } | } |
| if(ValuePtr 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 |
| } | } |
| 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"); |
| } | } |
| } | } |
| static void _create(Request& r, StringPtr method_name, MethodParams* params) { | static void _create(Request& r, MethodParams* params) { |
| Pool& pool=r.pool(); | |
| // clone/copy part? | // clone/copy part? |
| if(const Table *source=(*params)[0]->get_table()) { | if(const 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, method_name, params, 1, *source, |
| offset, limit); | offset, limit); |
| GET_SELF(r, VTable).set_table(TablePtr(new Table(*source, offset, limit))); | GET_SELF(r, VTable).set_table(Table*(new Table(*source, offset, limit))); |
| return; | return; |
| } | } |
| // data is last parameter | // data is last parameter |
| Temp_lang temp_lang(r, String::UL_PASS_APPENDED); | Temp_lang temp_lang(r, String::L_PASS_APPENDED); |
| StringPtr 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; | size_t pos_after=0; |
| Line 120 static void _create(Request& r, StringPt | Line 118 static void _create(Request& r, StringPt |
| columns=Table::columns_type(new ArrayString); | columns=Table::columns_type(new ArrayString); |
| ArrayString head; | ArrayString head; |
| data->split(head, &pos_after, "\n", 1, String::UL_AS_IS, 1); | data->split(head, &pos_after, "\n", 1, String::L_AS_IS, 1); |
| if(head.count()) | if(head.count()) |
| head[0]->split(*columns, 0, "\t", 1, String::UL_AS_IS); | head[0]->split(*columns, 0, "\t", 1, String::L_AS_IS); |
| } | } |
| TablePtr table(new Table(method_name, columns)); | Table* table(new Table(method_name, columns)); |
| // parse cells | // parse cells |
| ArrayString rows; | ArrayString rows; |
| data->split(rows, &pos_after, "\n", 1, String::UL_AS_IS); | data->split(rows, &pos_after, "\n", 1, String::L_AS_IS); |
| Array_iterator<StringPtr> i(rows); | Array_iterator<String*> i(rows); |
| while(i.has_next()) { | while(i.has_next()) { |
| Table::element_type row(new ArrayString); | Table::element_type row(new ArrayString); |
| StringPtr string=i.next(); | const String& string=i.next(); |
| // remove comment lines | // remove comment lines |
| if(!string->size()) | if(!string->length()) |
| continue; | continue; |
| string->split(*row, 0, "\t", 1, String::UL_AS_IS); | string->split(*row, 0, "\t", 1, String::L_AS_IS); |
| *table+=row; | *table+=row; |
| } | } |
| Line 146 static void _create(Request& r, StringPt | Line 144 static void _create(Request& r, StringPt |
| GET_SELF(r, VTable).set_table(table); | GET_SELF(r, VTable).set_table(table); |
| } | } |
| static void _load(Request& r, StringPtr method_name, MethodParams* params) { | static void _load(Request& r, MethodParams* params) { |
| Pool& pool=r.pool(); | const String& first_param=params->as_string(0, "file name must be string"); |
| StringPtr 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) |
| Line 156 static void _load(Request& r, StringPtr | Line 153 static void _load(Request& r, StringPtr |
| int options_param_index=filename_param_index+1; | int options_param_index=filename_param_index+1; |
| // loading text | // loading text |
| char *data=file_read_text(pool, 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(method_name):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 179 static void _load(Request& r, StringPtr | Line 176 static void _load(Request& r, StringPtr |
| if(!*row_chars || *row_chars == '#') | if(!*row_chars || *row_chars == '#') |
| continue; | continue; |
| do { | do { |
| StringPtr name(new String); | *columns+=new String(lsplit(&row_chars, '\t'), 0, String::L_TAINTED); |
| name->APPEND_TAINTED(lsplit(&row_chars, '\t'), 0, file, line++); | |
| *columns+=name; | |
| } while(row_chars); | } while(row_chars); |
| break; | break; |
| Line 189 static void _load(Request& r, StringPtr | Line 184 static void _load(Request& r, StringPtr |
| } | } |
| // parse cells | // parse cells |
| TablePtr table(new Table(method_name, columns)); | Table& table=*new Table(columns); |
| char *row_chars; | char *row_chars; |
| while(row_chars=getrow(&data)) { | while(row_chars=getrow(&data)) { |
| // remove empty&comment lines | // remove empty&comment lines |
| Line 197 static void _load(Request& r, StringPtr | Line 192 static void _load(Request& r, StringPtr |
| continue; | continue; |
| 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')) { |
| StringPtr cell(new String); | *row+=new String(cell_chars, 0, true); |
| cell->APPEND_TAINTED(cell_chars, 0, file, line); | |
| *row+=cell; | |
| } | } |
| #ifndef NO_STRING_ORIGIN | #ifndef NO_STRING_ORIGIN |
| line++; | line++; |
| #endif | #endif |
| *table+=row; | table+=row; |
| }; | }; |
| // replace any previous table value | // replace any previous table value |
| Line 212 static void _load(Request& r, StringPtr | Line 205 static void _load(Request& r, StringPtr |
| } | } |
| /// @todo "x\nx" "xxx""xx" | /// @todo "x\nx" "xxx""xx" |
| static void _save(Request& r, StringPtr method_name, MethodParams* params) { | static void _save(Request& r, MethodParams* params) { |
| Pool& pool=r.pool(); | Value& vfile_name=params->as_no_junction(params->count()-1, "file name must not be code"); |
| ValuePtr vfile_name=params->as_no_junction(params->count()-1, | |
| "file name must not be code"); | |
| Table& table=GET_SELF(r, VTable).table(method_name); | 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<StringPtr> i(*table.columns()); | Array_iterator<String*> i(*table.columns()); |
| while(i.has_next()) { | while(i.has_next()) { |
| sdata.append(*i.next(), String::UL_TABLE); | sdata.append(*i.next(), String::L_TABLE); |
| if(i.has_next()) | if(i.has_next()) |
| sdata.APPEND_CONST("\t"); | sdata.APPEND_CONST("\t"); |
| } | } |
| } 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(pool) char[MAX_NUMBER]; | char *cindex_tab=new char[MAX_NUMBER]; |
| snprintf(cindex_tab, MAX_NUMBER, "%d\t", column); | sdata.append(cindex_tab, |
| sdata.APPEND_CONST(cindex_tab); | snprintf(cindex_tab, MAX_NUMBER, "%d\t", column), |
| String::L_CLEAN); | |
| } | } |
| else | else |
| sdata.APPEND_CONST("empty nameless table"); | sdata.APPEND_CONST("empty nameless table"); |
| } | } |
| sdata.APPEND_CONST("\n"); | sdata.APPEND_CONST("\n"); |
| } else { // mode specified | } else { // mode specified |
| StringPtr 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 254 static void _save(Request& r, StringPtr | Line 246 static void _save(Request& r, StringPtr |
| } | } |
| // data lines | // data lines |
| Array_iterator<ArrayStringPtr> i(table); | Array_iterator<ArrayString*> i(table); |
| while(i.has_next()) { | while(i.has_next()) { |
| Array_iterator<StringPtr> c(*i.next()); | Array_iterator<String*> c(*i.next()); |
| while(c.has_next()) { | while(c.has_next()) { |
| if(StringPtr s=c.next()) | if(const String* s=c.next()) |
| sdata.append(*s, String::UL_TABLE); | sdata.append(*s, String::L_TABLE); |
| if(c.has_next()) | if(c.has_next()) |
| sdata.APPEND_CONST("\t"); | sdata.append("\t", 1, String::L_CLEAN); |
| } | } |
| sdata.APPEND_CONST("\n"); | sdata.append("\n", 1, String::L_CLEAN); |
| } | } |
| // write | // write |
| file_write(r.absolute(vfile_name->as_string(&pool)), | file_write(r.absolute(vfile_name.as_string()), |
| sdata.cstr(), sdata.size(), true, do_append); | sdata.cstr(), sdata.length(), true, do_append); |
| } | } |
| static void _count(Request& r, StringPtr method_name, MethodParams* ) { | static void _count(Request& r, MethodParams* ) { |
| int result=GET_SELF(r, VTable).table(method_name).count(); | int result=GET_SELF(r, VTable).table().count(); |
| r.write_no_lang(ValuePtr(new VInt(result))); | r.write_no_lang(Value*(new VInt(result))); |
| } | } |
| static void _line(Request& r, StringPtr method_name, MethodParams* ) { | static void _line(Request& r, MethodParams* ) { |
| int result=1+GET_SELF(r, VTable).table(method_name).current(); | int result=1+GET_SELF(r, VTable).table().current(); |
| r.write_no_lang(ValuePtr(new VInt(result))); | r.write_no_lang(Value*(new VInt(result))); |
| } | } |
| static void _offset(Request& r, StringPtr method_name, MethodParams* params) { | static void _offset(Request& r, MethodParams* params) { |
| Pool& pool=r.pool(); | Table& table=GET_SELF(r, VTable).table(); |
| Table& table=GET_SELF(r, VTable).table(method_name); | |
| if(params->count()) { | if(params->count()) { |
| bool absolute=false; | bool absolute=false; |
| if(params->count()>1) { | if(params->count()>1) { |
| StringPtr 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 298 static void _offset(Request& r, StringPt | Line 289 static void _offset(Request& r, StringPt |
| "is invalid whence, valid are 'cur' or 'set'"); | "is invalid whence, valid are 'cur' or 'set'"); |
| } | } |
| ValuePtr 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(ValuePtr(new VInt(table.current()))); | r.write_no_lang(*new VInt(table.current())); |
| } | } |
| static void _menu(Request& r, StringPtr method_name, MethodParams* params) { | static void _menu(Request& r, MethodParams* params) { |
| ValuePtr body_code=params->as_junction(0, "body must be code"); | Value* body_code=params->as_junction(0, "body must be code"); |
| ValuePtr delim_maybe_code=params->count()>1?(*params)[1]:ValuePtr(0); | Value* delim_maybe_code=params->count()>1?(*params)[1]:0; |
| Table& table=GET_SELF(r, VTable).table(method_name); | Table& table=GET_SELF(r, VTable).table(); |
| bool need_delim=false; | bool need_delim=false; |
| int saved_current=table.current(); | int saved_current=table.current(); |
| int size=table.count(); | int size=table.count(); |
| Line 317 static void _menu(Request& r, StringPtr | Line 308 static void _menu(Request& r, StringPtr |
| table.set_current(row); | table.set_current(row); |
| StringOrValue sv_processed=r.process(body_code); | StringOrValue sv_processed=r.process(body_code); |
| StringPtr s_processed=sv_processed.get_string(); | const String& s_processed=sv_processed.get_string(); |
| if(delim_maybe_code && s_processed && s_processed->size()) { // 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; |
| Line 332 static void _menu(Request& r, StringPtr | Line 323 static void _menu(Request& r, StringPtr |
| struct Row_info { | struct Row_info { |
| Request *r; | Request *r; |
| Table *table; | Table *table; |
| ValuePtr key_code; | Value* key_code; |
| int key_field; | int key_field; |
| Array<int>* value_fields; | Array<int>* value_fields; |
| HashStringValue* hash; | HashStringValue* hash; |
| Line 341 struct Row_info { | Line 332 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) { |
| Pool& pool=info->r->pool(); | const String& key; |
| StringPtr 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(&pool); | key=sv_processed.as_string(); |
| } else | } else |
| key=info->key_field<row->count()?row->get(info->key_field):StringPtr(0); | key=info->key_field<row->count()?row->get(info->key_field):0; |
| 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] |
| VHashPtr vhash(new VHash); | VHash* vhash=new VHash; |
| HashStringValue& hash=vhash->hash(); | HashStringValue& hash=vhash->hash(); |
| for(int i=0; i<info->value_fields->count(); i++) { | for(int i=0; i<info->value_fields->count(); i++) { |
| 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), |
| ValuePtr(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, StringPtr method_name, MethodParams* params) { | static void _hash(Request& r, MethodParams* params) { |
| Pool& pool=r.pool(); | Table& self_table=GET_SELF(r, VTable).table(); |
| Table& self_table=GET_SELF(r, VTable).table(method_name); | VHash* result(new VHash); |
| VHashPtr 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(method_name)) { | 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(ValuePtr 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(); |
| } | } |
| Line 400 static void _hash(Request& r, StringPtr | Line 388 static void _hash(Request& r, StringPtr |
| Array<int> value_fields; | Array<int> value_fields; |
| if(param_index>0) { | if(param_index>0) { |
| ValuePtr 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(&pool), true); | value_fields_param->as_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++) { |
| StringPtr 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); |
| } | } |
| Line 425 static void _hash(Request& r, StringPtr | Line 413 static void _hash(Request& r, StringPtr |
| Row_info info; | Row_info info; |
| info.r=&r; | info.r=&r; |
| info.table=&self_table; | info.table=&self_table; |
| ValuePtr key_param=(*params)[0]; | Value* key_param=(*params)[0]; |
| info.key_code=key_param->get_junction()?key_param:ValuePtr(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(&pool), 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 467 static int sort_cmp_double(const void *a | Line 455 static int sort_cmp_double(const void *a |
| else | else |
| return 0; | return 0; |
| } | } |
| static void _sort(Request& r, StringPtr method_name, MethodParams* params) { | static void _sort(Request& r, MethodParams* params) { |
| Pool local_pool; | Value& key_maker=params->as_junction(0, "key-maker must be code"); |
| ValuePtr 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(&local_pool)=="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(method_name); | Table& old_table=GET_SELF(r, VTable).table(); |
| TablePtr new_table(new Table(method_name, old_table.columns())); | Table* new_table(new Table(method_name, old_table.columns())); |
| smart_ptr<Table_seq_item> seq(new Table_seq_item[old_table.count()]); | smart_ptr<Table_seq_item> seq(new Table_seq_item[old_table.count()]); |
| int i; | int i; |
| Line 486 static void _sort(Request& r, StringPtr | Line 473 static void _sort(Request& r, StringPtr |
| for(i=0; i<old_table.count(); i++) { | for(i=0; i<old_table.count(); i++) { |
| old_table.set_current(i); | old_table.set_current(i); |
| // calculate key value | // calculate key value |
| seq[i].row=old_table[i].get(); | seq[i].row=old_table[i]; |
| ValuePtr 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(&local_pool)->cstr(local_pool); | seq[i].value.c_str=value->as_string()->cstr(local_pool); |
| else | else |
| seq[i].value.d=value->as_double(); | seq[i].value.d=value->as_double(); |
| } | } |
| Line 508 static void _sort(Request& r, StringPtr | Line 495 static void _sort(Request& r, StringPtr |
| GET_SELF(r, VTable).set_table(new_table); | GET_SELF(r, VTable).set_table(new_table); |
| } | } |
| static bool _locate_expression(Request& r, StringPtr method_name, 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, | method_name, |
| "locate by expression has only one parameter - expression"); | "locate by expression has only one parameter - expression"); |
| ValuePtr 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(method_name); | Table& table=GET_SELF(r, VTable).table(); |
| int saved_current=table.current(); | int saved_current=table.current(); |
| int size=table.count(); | int size=table.count(); |
| for(int row=0; row<size; row++) { | for(int row=0; row<size; row++) { |
| Line 528 static bool _locate_expression(Request& | Line 515 static bool _locate_expression(Request& |
| table.set_current(saved_current); | table.set_current(saved_current); |
| return false; | return false; |
| } | } |
| static bool _locate_name_value(Request& r, StringPtr method_name, 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, | method_name, |
| "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(method_name); | Table& table=GET_SELF(r, VTable).table(); |
| return table.locate( | return table.locate( |
| params->as_string(0, "column name must be string"), | params->as_string(0, "column name must be string"), |
| params->as_string(1, "value must be string") | params->as_string(1, "value must be string") |
| ); | ); |
| } | } |
| static void _locate(Request& r, StringPtr method_name, MethodParams* params) { | static void _locate(Request& r, MethodParams* params) { |
| Pool& pool=r.pool(); | |
| bool result=(*params)[0]->get_junction()? | bool result=(*params)[0]->get_junction()? |
| _locate_expression(r, method_name, params) : | _locate_expression(r, method_name, params) : |
| _locate_name_value(r, method_name, params); | _locate_name_value(r, method_name, params); |
| r.write_no_lang(ValuePtr(new VBool(result))); | r.write_no_lang(Value*(new VBool(result))); |
| } | } |
| static void _flip(Request& r, StringPtr method_name, MethodParams* params) { | static void _flip(Request& r, MethodParams* params) { |
| Pool& pool=r.pool(); | Table& old_table=GET_SELF(r, VTable).table(); |
| Table& old_table=GET_SELF(r, VTable).table(method_name); | Table* new_table(new Table(method_name, old_table.columns())); |
| TablePtr new_table(new Table(method_name, 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):StringPtr(new String); | *new_row+=column<old_row->count()?old_row->get(column):String* (new String); |
| } | } |
| *new_table+=new_row; | *new_table+=new_row; |
| } | } |
| r.write_no_lang(ValuePtr(new VTable(new_table))); | r.write_no_lang(Value*(new VTable(new_table))); |
| } | } |
| static void _append(Request& r, StringPtr method_name, MethodParams* params) { | static void _append(Request& r, MethodParams* params) { |
| Pool& pool=r.pool(); | |
| // data | // data |
| Temp_lang temp_lang(r, String::UL_PASS_APPENDED); | Temp_lang temp_lang(r, String::L_PASS_APPENDED); |
| StringPtr 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 |
| ArrayStringPtr row(new ArrayString); | ArrayString& row(new ArrayString); |
| string->split(*row, 0, "\t", 1, String::UL_AS_IS); | string->split(*row, 0, "\t", 1, String::L_AS_IS); |
| GET_SELF(r, VTable).table(method_name)+=row; | GET_SELF(r, VTable).table()+=row; |
| } | } |
| static void _join(Request& r, StringPtr method_name, MethodParams* params) { | static void _join(Request& r, 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) | if(!maybe_src) |
| Line 589 static void _join(Request& r, StringPtr | Line 572 static void _join(Request& r, StringPtr |
| "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(method_name); | Table& dest=GET_SELF(r, VTable).table(); |
| if(&src == &dest) | if(&src == &dest) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| method_name, | method_name, |
| Line 609 static void _join(Request& r, StringPtr | Line 592 static void _join(Request& r, StringPtr |
| 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++) { |
| StringPtr 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:StringPtr(new String); | *dest_row+=src_item?src_item:String* (new String); |
| } | } |
| dest+=dest_row; | dest+=dest_row; |
| } | } |
| Line 623 static void _join(Request& r, StringPtr | Line 606 static void _join(Request& r, StringPtr |
| #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 { |
| StringPtr method_name; | const String& method_name; |
| StringPtr statement_string; const char* statement_cstr; | const String& statement_string; const char* statement_cstr; |
| ArrayStringPtr columns; | ArrayString& columns; |
| ArrayString* row; | ArrayString* row; |
| public: | public: |
| TablePtr table; | Table* table; |
| public: | public: |
| Table_sql_event_handlers(StringPtr amethod_name, | Table_sql_event_handlers(const String& amethod_name, |
| StringPtr astatement_string, const char* astatement_cstr) : | const String& astatement_string, const char* astatement_cstr) : |
| method_name(amethod_name), | method_name(amethod_name), |
| statement_string(astatement_string), statement_cstr(astatement_cstr), | statement_string(astatement_string), statement_cstr(astatement_cstr), |
| columns(new ArrayString), row(0) { | columns(new ArrayString), row(0) { |
| Line 639 public: | Line 622 public: |
| bool add_column(SQL_Error& error, void *ptr, size_t size) { | bool add_column(SQL_Error& error, void *ptr, size_t size) { |
| try { | try { |
| StringPtr column(new String); | const String& column(new String); |
| column->APPEND_TAINTED( | column->APPEND_TAINTED( |
| (const char* )ptr, size, | (const char* )ptr, size, |
| statement_cstr, 0); | statement_cstr, 0); |
| Line 652 public: | Line 635 public: |
| } | } |
| bool before_rows(SQL_Error& error) { | bool before_rows(SQL_Error& error) { |
| try { | try { |
| table=TablePtr(new Table( method_name, columns)); | table=Table*(new Table( method_name, 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 661 public: | Line 644 public: |
| } | } |
| bool add_row(SQL_Error& error) { | bool add_row(SQL_Error& error) { |
| try { | try { |
| *table+=(ArrayStringPtr(row=new ArrayString)); | *table+=(ArrayString* (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 670 public: | Line 653 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 { |
| StringPtr cell(new String); | const String& cell(new String); |
| if(size) | if(size) |
| cell->APPEND_TAINTED( | cell->APPEND_TAINTED( |
| (const char* )ptr, size, | (const char* )ptr, size, |
| Line 684 public: | Line 667 public: |
| } | } |
| }; | }; |
| #endif | #endif |
| static void _sql(Request& r, StringPtr method_name, MethodParams* params) { | static void _sql(Request& r, MethodParams* params) { |
| Pool& pool=r.pool(); | |
| ValuePtr 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) { |
| ValuePtr 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(method_name)) { | if(HashStringValue* options=voptions->get_hash()) { |
| int valid_options=0; | int valid_options=0; |
| if(ValuePtr 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(ValuePtr 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(); |
| } | } |
| Line 714 static void _sql(Request& r, StringPtr m | Line 696 static void _sql(Request& r, StringPtr m |
| "options must be hash"); | "options must be hash"); |
| } | } |
| Temp_lang temp_lang(r, String::UL_SQL); | Temp_lang temp_lang(r, String::L_SQL); |
| StringPtr 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(pool, String::UL_UNSPECIFIED, r.connection(method_name)); | statement_string->cstr(String::L_UNSPECIFIED, r.connection()); |
| Table_sql_event_handlers handlers(method_name, | Table_sql_event_handlers handlers(method_name, |
| statement_string, statement_cstr); | statement_string, statement_cstr); |
| #ifdef RESOURCES_DEBUG | #ifdef RESOURCES_DEBUG |
| Line 725 static void _sql(Request& r, StringPtr m | Line 707 static void _sql(Request& r, StringPtr m |
| //measure:before | //measure:before |
| gettimeofday(&mt[0],NULL); | gettimeofday(&mt[0],NULL); |
| #endif | #endif |
| r.connection(method_name)->query( | r.connection()->query( |
| statement_cstr, offset, limit, | statement_cstr, offset, limit, |
| handlers, | handlers, |
| statement_string); | statement_string); |
| Line 741 static void _sql(Request& r, StringPtr m | Line 723 static void _sql(Request& r, StringPtr m |
| r.sql_request_time+=t[1]-t[0]; | r.sql_request_time+=t[1]-t[0]; |
| #endif | #endif |
| TablePtr result= | Table* result= |
| handlers.table?handlers.table: // query resulted in table? return it | handlers.table?handlers.table: // query resulted in table? return it |
| TablePtr(new Table(method_name, Table::columns_type(0))); // query returned no table, fake it | Table*(new Table(method_name, 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); |
| } | } |
| static void _columns(Request& r, StringPtr method_name, MethodParams* ) { | static void _columns(Request& r, MethodParams* ) { |
| Pool& pool=r.pool(); | |
| Table::columns_type result_columns(new ArrayString); | Table::columns_type result_columns(new ArrayString); |
| *result_columns+=StringPtr(new String("column")); | *result_columns+=String* (new String("column")); |
| TablePtr result_table(new Table(method_name, result_columns)); | Table* result_table(new Table(method_name, result_columns)); |
| Table& source_table=GET_SELF(r, VTable).table(method_name); | 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<StringPtr> i(*source_columns); | Array_iterator<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(); |
| Line 766 static void _columns(Request& r, StringP | Line 747 static void _columns(Request& r, StringP |
| } | } |
| } | } |
| r.write_no_lang(ValuePtr(new VTable(result_table))); | r.write_no_lang(Value*(new VTable(result_table))); |
| } | } |
| static void _select(Request& r, StringPtr method_name, MethodParams* params) { | static void _select(Request& r, MethodParams* params) { |
| Pool& pool=r.pool(); | |
| ValuePtr 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(method_name); | Table& source_table=GET_SELF(r, VTable).table(); |
| TablePtr result_table(new Table( | Table* result_table(new Table( |
| source_table.origin_string(), | source_table.origin_string(), |
| source_table.columns() | source_table.columns() |
| )); | )); |
| Line 794 static void _select(Request& r, StringPt | Line 774 static void _select(Request& r, StringPt |
| } | } |
| source_table.set_current(saved_current); | source_table.set_current(saved_current); |
| r.write_no_lang(ValuePtr(new VTable(result_table))); | r.write_no_lang(Value*(new VTable(result_table))); |
| } | } |
| // constructor | // constructor |