|
|
| version 1.263, 2009/07/29 05:01:33 | version 1.274, 2010/10/05 20:56:12 |
|---|---|
| Line 9 static const char * const IDENT_TABLE_C= | Line 9 static const char * const IDENT_TABLE_C= |
| #if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4)) | #if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4)) |
| #include <sstream> | #include <sstream> |
| using namespace std; | |
| #endif | #endif |
| #include "classes.h" | #include "classes.h" |
| Line 17 using namespace std; | Line 16 using namespace std; |
| #include "pa_common.h" | #include "pa_common.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_charsets.h" | |
| #include "pa_vtable.h" | #include "pa_vtable.h" |
| #include "pa_vint.h" | #include "pa_vint.h" |
| #include "pa_sql_connection.h" | #include "pa_sql_connection.h" |
| Line 26 using namespace std; | Line 26 using namespace std; |
| class MTable: public Methoded { | class MTable: public Methoded { |
| public: // VStateless_class | public: // VStateless_class |
| Value* create_new_value(Pool&, HashStringValue*) { return new VTable(); } | Value* create_new_value(Pool&) { return new VTable(); } |
| public: | public: |
| MTable(); | MTable(); |
| Line 54 String table_reverse_name(TABLE_REVERSE_ | Line 54 String table_reverse_name(TABLE_REVERSE_ |
| // methods | // methods |
| static Table::Action_options get_action_options(Request& r, MethodParams& params, | static Table::Action_options get_action_options(Request& r, MethodParams& params, |
| const Table& source) { | size_t options_index, const Table& source) { |
| Table::Action_options result; | Table::Action_options result; |
| if(!params.count()) | if(params.count() <= options_index) |
| return result; | return result; |
| Value& maybe_options=params.last(); | Value& maybe_options=params[options_index]; |
| /* can not do it: | /* can not do it: |
| want to enable ^table::create[$source; | want to enable ^table::create[$source; |
| # $.option[] | # $.option[] |
| Line 106 static Table::Action_options get_action_ | Line 106 static Table::Action_options get_action_ |
| } | } |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); |
| 0, | |
| "called with invalid option"); | |
| return result; | return result; |
| } | } |
| Line 156 struct TableSeparators { | Line 154 struct TableSeparators { |
| 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()) { |
| Table::Action_options o=get_action_options(r, params, *source); | Table::Action_options o=get_action_options(r, params, 1, *source); |
| check_option_param(o.defined, params, 1, | check_option_param(o.defined, params, 1, |
| "too many parameters"); | "too many parameters"); |
| GET_SELF(r, VTable).set_table(*new Table(*source, o)); | GET_SELF(r, VTable).set_table(*new Table(*source, o)); |
| Line 249 struct lsplit_result { | Line 247 struct lsplit_result { |
| inline lsplit_result lsplit(char* string, char delim1, char delim2) { | inline lsplit_result lsplit(char* string, char delim1, char delim2) { |
| lsplit_result result; | lsplit_result result; |
| if(string) { | if(string) { |
| char delims[]={delim1, delim2, 0}; | char delims[]={delim1, delim2, 0}; |
| if(char* v=strpbrk(string, delims)) { | if(char* v=strpbrk(string, delims)) { |
| result.delim=*v; | result.delim=*v; |
| Line 257 inline lsplit_result lsplit(char* string | Line 255 inline lsplit_result lsplit(char* string |
| result.piece=v+1; | result.piece=v+1; |
| return result; | return result; |
| } | } |
| } | } |
| result.piece=0; | result.piece=0; |
| result.delim=0; | result.delim=0; |
| return result; | return result; |
| } | } |
| inline lsplit_result lsplit(char* *string_ref, char delim1, char delim2) { | inline lsplit_result lsplit(char* *string_ref, char delim1, char delim2) { |
| lsplit_result result; | lsplit_result result; |
| result.piece=*string_ref; | result.piece=*string_ref; |
| lsplit_result next=lsplit(*string_ref, delim1, delim2); | lsplit_result next=lsplit(*string_ref, delim1, delim2); |
| result.delim=next.delim; | result.delim=next.delim; |
| *string_ref=next.piece; | *string_ref=next.piece; |
| return result; | return result; |
| } | } |
| static lsplit_result lsplit(char** string_ref, char delim1, char delim2, char encloser) { | static lsplit_result lsplit(char** string_ref, char delim1, char delim2, char encloser) { |
| lsplit_result result; | lsplit_result result; |
| if(char* string=*string_ref) { | if(char* string=*string_ref) { |
| if(encloser && *string==encloser) { | if(encloser && *string==encloser) { |
| string++; | string++; |
| char *read; | char *read; |
| char *write; | char *write; |
| Line 303 static lsplit_result lsplit(char** strin | Line 301 static lsplit_result lsplit(char** strin |
| return result; | return result; |
| } else | } else |
| return lsplit(string_ref, delim1, delim2); | return lsplit(string_ref, delim1, delim2); |
| } | } |
| result.piece=0; | result.piece=0; |
| return result; | return result; |
| } | } |
| static void skip_empty_and_comment_lines( char** data_ref ) { | static void skip_empty_and_comment_lines( char** data_ref ) { |
| Line 322 static void skip_empty_and_comment_lines | Line 320 static void skip_empty_and_comment_lines |
| } | } |
| } | } |
| static void skip_empty_lines( char** data_ref ) { | |
| if(char *data=*data_ref) { | |
| while( char c=*data ) { | |
| if( c== '\n' ) { | |
| /*nowhere=*/getrow(&data); // remove empty lines | |
| if(!(*data_ref=data)) | |
| break; | |
| continue; | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| typedef void (*Skip_lines_action)(char** data_ref); | |
| 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; |
| Line 346 static void _load(Request& r, MethodPara | Line 360 static void _load(Request& r, MethodPara |
| options | options |
| ); | ); |
| Skip_lines_action skip_lines_action = (separators.column=='#' || separators.encloser=='#') ? skip_empty_lines : skip_empty_and_comment_lines; | |
| // parse columns | // parse columns |
| Table::columns_type columns; | Table::columns_type columns; |
| if(nameless) { | if(nameless) { |
| Line 353 static void _load(Request& r, MethodPara | Line 369 static void _load(Request& r, MethodPara |
| } else { | } else { |
| columns=Table::columns_type(new ArrayString); | columns=Table::columns_type(new ArrayString); |
| skip_empty_and_comment_lines(&data); | skip_lines_action(&data); |
| while( lsplit_result sr=lsplit(&data, separators.column, '\n', separators.encloser) ) { | while( lsplit_result sr=lsplit(&data, separators.column, '\n', separators.encloser) ) { |
| *columns+=new String(sr.piece, String::L_TAINTED); | *columns+=new String(sr.piece, String::L_TAINTED); |
| if(sr.delim=='\n') | if(sr.delim=='\n') |
| Line 366 static void _load(Request& r, MethodPara | Line 382 static void _load(Request& r, MethodPara |
| // parse cells | // parse cells |
| Table::element_type row(new ArrayString(columns_count)); | Table::element_type row(new ArrayString(columns_count)); |
| skip_empty_and_comment_lines(&data); | skip_lines_action(&data); |
| while( lsplit_result sr=lsplit(&data, separators.column, '\n', separators.encloser) ) { | while( lsplit_result sr=lsplit(&data, separators.column, '\n', separators.encloser) ) { |
| if(!*sr.piece && !sr.delim && !row->count()) // append last empty column [if without \n] | if(!*sr.piece && !sr.delim && !row->count()) // append last empty column [if without \n] |
| break; | break; |
| Line 374 static void _load(Request& r, MethodPara | Line 390 static void _load(Request& r, MethodPara |
| if(sr.delim=='\n') { | if(sr.delim=='\n') { |
| table+=row; | table+=row; |
| row=new ArrayString(columns_count); | row=new ArrayString(columns_count); |
| skip_empty_and_comment_lines(&data); | skip_lines_action(&data); |
| } | } |
| } | } |
| // last line [if without \n] | // last line [if without \n] |
| Line 387 static void _load(Request& r, MethodPara | Line 403 static void _load(Request& r, MethodPara |
| #if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4)) | #if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4)) |
| void maybe_enclose( ostringstream& to, const String& from, char encloser ) { | #include "gc_allocator.h" |
| typedef std::basic_stringstream<char, std::char_traits<char>, gc_allocator<char> > pa_stringstream; | |
| typedef std::basic_string<char, std::char_traits<char>, gc_allocator<char> > pa_string; | |
| void maybe_enclose( pa_stringstream& to, const String& from, char encloser ) { | |
| if(encloser) { | if(encloser) { |
| to<<encloser; | to<<encloser; |
| // while we have 'encloser'... | // while we have 'encloser'... |
| Line 459 static void _save(Request& r, MethodPara | Line 480 static void _save(Request& r, MethodPara |
| if(HashStringValue* options=voptions.get_hash()) { | if(HashStringValue* options=voptions.get_hash()) { |
| int valid_options=separators.load(*options); | int valid_options=separators.load(*options); |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); |
| 0, | |
| "invalid option passed"); | |
| } else { | } else { |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, |
| 0, | 0, |
| Line 478 static void _save(Request& r, MethodPara | Line 497 static void _save(Request& r, MethodPara |
| #if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4)) | #if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4)) |
| ostringstream ost(stringstream::out); | pa_stringstream ost(std::stringstream::out); |
| // process header | // process header |
| if(output_column_names) { | if(output_column_names) { |
| Line 520 static void _save(Request& r, MethodPara | Line 539 static void _save(Request& r, MethodPara |
| // write | // write |
| { | { |
| string data=ost.str(); | pa_string data=ost.str(); |
| const char* data_cstr=data.c_str(); | const char* data_cstr=data.c_str(); |
| file_write(file_spec, data_cstr, data.length(), true /* as text */, do_append); | file_write(r.charsets, file_spec, data_cstr, data.length(), true /* as text */, do_append); |
| } | } |
| #else | #else |
| Line 565 static void _save(Request& r, MethodPara | Line 584 static void _save(Request& r, MethodPara |
| // write | // write |
| { | { |
| const char* data_cstr=sdata.cstr(); | const char* data_cstr=sdata.cstr(); |
| file_write(file_spec, | file_write(r.charsets, file_spec, data_cstr, sdata.length(), true, do_append); |
| data_cstr, sdata.length(), true, do_append); | |
| if(*data_cstr) // not empty (when empty it's not heap memory) | if(*data_cstr) // not empty (when empty it's not heap memory) |
| pa_free((void*)data_cstr); // not needed anymore | pa_free((void*)data_cstr); // not needed anymore |
| } | } |
| Line 681 static void table_row_to_hash(Table::ele | Line 699 static void table_row_to_hash(Table::ele |
| bool exist=false; | bool exist=false; |
| switch(info->value_type) { | switch(info->value_type) { |
| case C_STRING: { | case C_STRING: { |
| exist=info->hash->put_dont_replace(*key, new VString(*row->get(info->value_fields->get(0)))); | size_t index=info->value_fields->get(0); |
| exist=info->hash->put_dont_replace(*key, (index < row->count()) ? new VString(*row->get(index)) : new VString()); | |
| break; | break; |
| } | } |
| case C_HASH: { | case C_HASH: { |
| Line 784 static void _hash(Request& r, MethodPara | Line 803 static void _hash(Request& r, MethodPara |
| } | } |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); |
| 0, | |
| "called with invalid option"); | |
| } | } |
| } | } |
| if(param_index==2) // options was specified but not as hash | if(param_index==2) // options was specified but not as hash |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, 0, OPTIONS_MUST_BE_HASH); |
| 0, | |
| "options must be hash"); | |
| Array<int> value_fields; | Array<int> value_fields; |
| if(param_index==0){ // list of columns wasn't specified | if(param_index==0){ // list of columns wasn't specified |
| Line 911 static void _sort(Request& r, MethodPara | Line 926 static void _sort(Request& r, MethodPara |
| else | else |
| seq[i].value.d=value.as_double(); | seq[i].value.d=value.as_double(); |
| } | } |
| // @todo: handle this elsewhere | |
| if(r.charsets.source().NAME()=="KOI8-R" && key_values_are_strings) { | |
| for(i=0; i<old_count; i++) | |
| if(*seq[i].value.c_str) | |
| seq[i].value.c_str=Charset::transcode(seq[i].value.c_str, r.charsets.source(), UTF8_charset).cstr(); | |
| } | |
| // sort keys | // sort keys |
| _qsort(seq, old_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); |
| Line 954 static bool _locate_name_value(Table& ta | Line 977 static bool _locate_name_value(Table& ta |
| static void _locate(Request& r, MethodParams& params) { | static void _locate(Request& r, MethodParams& params) { |
| Table& table=GET_SELF(r, VTable).table(); | Table& table=GET_SELF(r, VTable).table(); |
| Table::Action_options o=get_action_options(r, params, table); | Table::Action_options o=get_action_options(r, params, 1, table); |
| bool result=params[0].get_junction()? | bool result=params[0].get_junction()? |
| _locate_expression(table, o, r, params) : | _locate_expression(table, o, r, params) : |
| Line 967 static void _flip(Request& r, MethodPara | Line 990 static void _flip(Request& r, MethodPara |
| Table& old_table=GET_SELF(r, VTable).table(); | Table& old_table=GET_SELF(r, VTable).table(); |
| Table& new_table=*new Table(0); | Table& new_table=*new Table(0); |
| if(size_t old_count=old_table.count()) | if(size_t old_count=old_table.count()) |
| if(size_t old_cols=old_table[0]->count()) | if(size_t old_cols=old_table.columns()?old_table.columns()->count():old_table[0]->count()) |
| for(size_t column=0; column<old_cols; column++) { | for(size_t column=0; column<old_cols; column++) { |
| Table::element_type new_row(new ArrayString(old_count)); | Table::element_type new_row(new ArrayString(old_count)); |
| for(size_t i=0; i<old_count; i++) { | for(size_t i=0; i<old_count; i++) { |
| Line 981 static void _flip(Request& r, MethodPara | Line 1004 static void _flip(Request& r, MethodPara |
| } | } |
| static void _append(Request& r, MethodParams& params) { | static void _append(Request& r, MethodParams& params) { |
| // 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[0]); |
| // parse cells | // parse cells |
| Table::element_type row=new ArrayString; | Table::element_type row=new ArrayString; |
| Line 1014 static void _join(Request& r, MethodPara | Line 1036 static void _join(Request& r, MethodPara |
| "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); | Table::Action_options o=get_action_options(r, params, 1, src); |
| check_option_param(o.defined, params, 1, | check_option_param(o.defined, params, 1, |
| "invalid extra parameter"); | "invalid extra parameter"); |
| Line 1044 public: | Line 1066 public: |
| bool add_column(SQL_Error& error, const char *str, size_t length) { | bool add_column(SQL_Error& error, const char *str, size_t length) { |
| try { | try { |
| columns+=new String(str, String::L_TAINTED, length); | columns+=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */); |
| 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 1072 public: | Line 1094 public: |
| } | } |
| bool add_row_cell(SQL_Error& error, const char* str, size_t length) { | bool add_row_cell(SQL_Error& error, const char* str, size_t length) { |
| try { | try { |
| *row+=new String(str, String::L_TAINTED, length); | *row+=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */); |
| 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 1143 static void _sql(Request& r, MethodParam | Line 1165 static void _sql(Request& r, MethodParam |
| 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, 0, CALLED_WITH_INVALID_OPTION); |
| 0, | |
| "called with invalid option"); | |
| } else | } else |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, 0, OPTIONS_MUST_BE_HASH); |
| 0, | |
| "options must be hash"); | |
| } | } |
| SQL_Driver::Placeholder* placeholders=0; | SQL_Driver::Placeholder* placeholders=0; |