|
|
| version 1.124, 2001/10/09 13:17:45 | version 1.129, 2001/10/31 14:08:36 |
|---|---|
| Line 80 static void _set(Request& r, const Strin | Line 80 static void _set(Request& r, const Strin |
| static void _load(Request& r, const String& method_name, MethodParams *params) { | static void _load(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| // filename is last parameter | // filename is last parameter |
| Value& vfilename=params->as_no_junction(params->size()-1, | Value& vfile_name=params->as_no_junction(params->size()-1, |
| "file name must not be code"); | "file name must not be code"); |
| // loading text | // loading text |
| char *data=file_read_text(pool, r.absolute(vfilename.as_string())); | char *data=file_read_text(pool, r.absolute(vfile_name.as_string())); |
| // parse columns | // parse columns |
| Array *columns; | Array *columns; |
| Line 129 static void _load(Request& r, const Stri | Line 129 static void _load(Request& r, const Stri |
| static_cast<VTable *>(r.self)->set_table(table); | static_cast<VTable *>(r.self)->set_table(table); |
| } | } |
| /// @todo "z\nz" "zzz""zz" | |
| static void _save(Request& r, const String& method_name, MethodParams *params) { | static void _save(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& vtable_name=params->as_no_junction(params->size()-1, | Value& vfile_name=params->as_no_junction(params->size()-1, |
| "file name must not be code"); | "file name must not be code"); |
| Table& table=static_cast<VTable *>(r.self)->table(); | Table& table=static_cast<VTable *>(r.self)->table(); |
| bool do_append=false; | |
| String sdata(pool); | String sdata(pool); |
| if(params->size()==1) { // not nameless=named output | if(params->size()==1) { // named output |
| // write out names line | // write out names line |
| if(table.columns()) { // named table | if(table.columns()) { // named table |
| Array_iter i(*table.columns()); | Array_iter i(*table.columns()); |
| Line 158 static void _save(Request& r, const Stri | Line 160 static void _save(Request& r, const Stri |
| sdata.APPEND_CONST("empty nameless table"); | sdata.APPEND_CONST("empty nameless table"); |
| } | } |
| sdata.APPEND_CONST("\n"); | sdata.APPEND_CONST("\n"); |
| } else { // mode specified | |
| const String& mode=params->as_string(0, "mode must be string"); | |
| if(mode=="append") | |
| do_append=true; | |
| else if(mode=="nameless") | |
| /*ok, already skipped names output*/; | |
| else | |
| throw Exception(0, 0, | |
| &mode, | |
| "unknown mode, must be 'append'"); | |
| } | } |
| // data lines | // data lines |
| Array_iter i(table); | Array_iter i(table); |
| Line 173 static void _save(Request& r, const Stri | Line 186 static void _save(Request& r, const Stri |
| } | } |
| // write | // write |
| file_write(pool, r.absolute(vtable_name.as_string()), | file_write(pool, r.absolute(vfile_name.as_string()), |
| sdata.cstr(), sdata.size(), true); | sdata.cstr(), sdata.size(), true, do_append); |
| } | } |
| static void _count(Request& r, const String& method_name, MethodParams *) { | static void _count(Request& r, const String& method_name, MethodParams *) { |
| Line 244 static void table_row_to_hash(Array::Ite | Line 257 static void table_row_to_hash(Array::Ite |
| if(ri.key_field<row.size()) { | if(ri.key_field<row.size()) { |
| VHash& result=*new(pool) VHash(pool); | VHash& result=*new(pool) VHash(pool); |
| Hash& hash=*result.get_hash(); | Hash& hash=*result.get_hash(0); |
| for(int i=0; i<ri.value_fields->size(); i++) { | for(int i=0; i<ri.value_fields->size(); i++) { |
| int value_field=ri.value_fields->get_int(i); | int value_field=ri.value_fields->get_int(i); |
| if(value_field<row.size()) | if(value_field<row.size()) |
| Line 278 static void _hash(Request& r, const Stri | Line 291 static void _hash(Request& r, const Stri |
| value_fields+=self_table.column_name2index(value_field_name, true); | value_fields+=self_table.column_name2index(value_field_name, true); |
| } | } |
| } else | } else |
| PTHROW(0, 0, | throw Exception(0, 0, |
| &method_name, | &method_name, |
| "value field(s) must be string or self_table" | "value field(s) must be string or self_table" |
| ); | ); |
| Line 288 static void _hash(Request& r, const Stri | Line 301 static void _hash(Request& r, const Stri |
| } | } |
| // integers: key_field & value_fields | // integers: key_field & value_fields |
| Row_info row_info={&self_table, key_field, &value_fields, result.get_hash()}; | Row_info row_info={&self_table, key_field, &value_fields, result.get_hash(0)}; |
| self_table.for_each(table_row_to_hash, &row_info); | self_table.for_each(table_row_to_hash, &row_info); |
| } | } |
| result.set_name(method_name); | result.set_name(method_name); |
| Line 390 static void _flip(Request& r, const Stri | Line 403 static void _flip(Request& r, const Stri |
| Array& new_row=*new(pool) Array(pool, old_table.size()); | Array& new_row=*new(pool) Array(pool, old_table.size()); |
| for(int i=0; i<old_table.size(); i++) { | for(int i=0; i<old_table.size(); i++) { |
| const Array& old_row=old_table.at(i); | const Array& old_row=old_table.at(i); |
| new_row+=column<old_row.size()?old_row.get(column):empty_string; | new_row+=column<old_row.size()?old_row.get(column):new(pool) String(pool); |
| } | } |
| new_table+=&new_row; | new_table+=&new_row; |
| } | } |
| Line 417 static void _join(Request& r, const Stri | Line 430 static void _join(Request& r, const Stri |
| 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) |
| PTHROW(0, 0, | throw Exception(0, 0, |
| &method_name, | &method_name, |
| "source is not a table"); | "source is not a table"); |
| Table& src=*maybe_src; | Table& src=*maybe_src; |
| Table& dest=static_cast<VTable *>(r.self)->table(); | Table& dest=static_cast<VTable *>(r.self)->table(); |
| if(&src == &dest) | if(&src == &dest) |
| PTHROW(0, 0, | throw Exception(0, 0, |
| &method_name, | &method_name, |
| "source and destination are same table"); | "source and destination are same table"); |
| Line 496 static void _sql(Request& r, const Strin | Line 509 static void _sql(Request& r, const Strin |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| if(!r.connection) | if(!r.connection) |
| PTHROW(0, 0, | throw Exception(0, 0, |
| &method_name, | &method_name, |
| "without connect"); | "without connect"); |
| Line 507 static void _sql(Request& r, const Strin | Line 520 static void _sql(Request& r, const Strin |
| if(params->size()>1) { | if(params->size()>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_defined()) | if(voptions.is_defined()) |
| if(Hash *options=voptions.get_hash()) { | if(Hash *options=voptions.get_hash(&method_name)) { |
| if(Value *vlimit=(Value *)options->get(*sql_limit_name)) | if(Value *vlimit=(Value *)options->get(*sql_limit_name)) |
| limit=(ulong)r.process(*vlimit).as_double(); | limit=(ulong)r.process(*vlimit).as_double(); |
| if(Value *voffset=(Value *)options->get(*sql_offset_name)) | if(Value *voffset=(Value *)options->get(*sql_offset_name)) |
| offset=(ulong)r.process(*voffset).as_double(); | offset=(ulong)r.process(*voffset).as_double(); |
| } else | } else |
| PTHROW(0, 0, | throw Exception(0, 0, |
| &method_name, | &method_name, |
| "options must be hash"); | "options must be hash"); |
| } | } |
| Line 524 static void _sql(Request& r, const Strin | Line 537 static void _sql(Request& r, const Strin |
| statement_string.cstr(String::UL_UNSPECIFIED, r.connection); | statement_string.cstr(String::UL_UNSPECIFIED, r.connection); |
| Table_sql_event_handlers handlers(pool, method_name, | Table_sql_event_handlers handlers(pool, method_name, |
| statement_string, statement_cstr); | statement_string, statement_cstr); |
| bool need_rethrow=false; Exception rethrow_me; | try { |
| PTRY { | |
| r.connection->query( | r.connection->query( |
| statement_cstr, offset, limit, | statement_cstr, offset, limit, |
| handlers); | handlers); |
| } catch(const Exception& e) { // query problem | |
| // more specific source [were url] | |
| throw Exception(e.type(), e.code(), | |
| &statement_string, | |
| "%s", e.comment()); | |
| } | } |
| PCATCH(e) { // query problem | |
| rethrow_me=e; need_rethrow=true; | |
| } | |
| PEND_CATCH | |
| if(need_rethrow) | |
| PTHROW(rethrow_me.type(), rethrow_me.code(), | |
| &statement_string, // setting more specific source [were url] | |
| rethrow_me.comment()); | |
| Table *result= | Table *result= |
| handlers.table?handlers.table: // query resulted in table? return it | handlers.table?handlers.table: // query resulted in table? return it |