|
|
| version 1.313, 2016/03/31 21:46:20 | version 1.317, 2016/07/29 22:40:58 |
|---|---|
| Line 705 static void _menu(Request& r, MethodPara | Line 705 static void _menu(Request& r, MethodPara |
| Table& table=GET_SELF(r, VTable).table(); | Table& table=GET_SELF(r, VTable).table(); |
| size_t saved_current=table.current(); | size_t saved_current=table.current(); |
| size_t size=table.count(); | |
| if(delim_maybe_code) { // delimiter set | if(delim_maybe_code) { // delimiter set |
| bool need_delim=false; | bool need_delim=false; |
| for(size_t row=0; row<size; row++) { | for(size_t row=0; row<table.count(); row++) { |
| table.set_current(row); | table.set_current(row); |
| StringOrValue sv_processed=r.process(body_code); | StringOrValue sv_processed=r.process(body_code); |
| Line 729 static void _menu(Request& r, MethodPara | Line 728 static void _menu(Request& r, MethodPara |
| break; | break; |
| } | } |
| } else { | } else { |
| for(size_t row=0; row<size; row++) { | for(size_t row=0; row<table.count(); row++) { |
| table.set_current(row); | table.set_current(row); |
| r.process_write(body_code); | r.process_write(body_code); |
| Line 749 struct Row_info { | Line 748 struct Row_info { |
| Value* key_code; | Value* key_code; |
| size_t key_field; | size_t key_field; |
| Array<int>* value_fields; | Array<int>* value_fields; |
| Value* value_code; | |
| HashStringValue* hash; | HashStringValue* hash; |
| Table2hash_distint distinct; | Table2hash_distint distinct; |
| size_t row; | size_t row; |
| Line 771 static void table_row_to_hash(Table::ele | Line 771 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: { |
| size_t index=info->value_fields->get(0); | if(info->value_fields->count()){ |
| exist=info->hash->put_dont_replace(*key, (index < row->count()) ? new VString(*row->get(index)) : new VString()); | size_t index=info->value_fields->get(0); |
| exist=info->hash->put_dont_replace(*key, (index < row->count()) ? new VString(*row->get(index)) : VString::empty()); | |
| } else { | |
| exist=info->hash->put_dont_replace(*key, VString::empty()); | |
| } | |
| break; | break; |
| } | } |
| case C_HASH: { | case C_HASH: { |
| Line 781 static void table_row_to_hash(Table::ele | Line 785 static void table_row_to_hash(Table::ele |
| for(Array_iterator<int> i(*info->value_fields); i.has_next(); ) { | for(Array_iterator<int> i(*info->value_fields); i.has_next(); ) { |
| size_t value_field=i.next(); | size_t value_field=i.next(); |
| if(value_field<row->count()) | if(value_field<row->count()) |
| hash.put( | hash.put(*info->table->columns()->get(value_field), new VString(*row->get(value_field))); |
| *info->table->columns()->get(value_field), | |
| new VString(*row->get(value_field))); | |
| } | } |
| exist=info->hash->put_dont_replace(*key, vhash); | exist=info->hash->put_dont_replace(*key, vhash); |
| Line 809 static void table_row_to_hash(Table::ele | Line 811 static void table_row_to_hash(Table::ele |
| *table+=row_copy; | *table+=row_copy; |
| break; | break; |
| } | } |
| case C_CODE: { | |
| if(!info->key_code) | |
| info->table->set_current(info->row++); // change context row | |
| exist=info->hash->put_dont_replace(*key, &info->r->process(*info->value_code).as_value()); | |
| break; | |
| } | |
| } | } |
| if(exist && info->distinct==D_ILLEGAL) | if(exist && info->distinct==D_ILLEGAL) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, key, "duplicate key"); |
| key, | |
| "duplicate key"); | |
| } | } |
| Table2hash_value_type get_value_type(Value& vvalue_type){ | Table2hash_value_type get_value_type(Value& vvalue_type){ |
| Line 826 Table2hash_value_type get_value_type(Val | Line 832 Table2hash_value_type get_value_type(Val |
| } else if (svalue_type == "hash") { | } else if (svalue_type == "hash") { |
| return C_HASH; | return C_HASH; |
| } else { | } else { |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, &svalue_type, "must be 'hash', 'table' or 'string'"); |
| &svalue_type, | |
| "must be 'hash', 'table' or 'string'"); | |
| } | } |
| } else { | } else { |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, 0, "'type' must be string"); |
| 0, | } |
| "'type' must be hash"); | } |
| static Table2hash_distint get_distinct(Value& vdistinct, Table2hash_value_type& value_type){ | |
| if(vdistinct.is_string()) { | |
| const String& sdistinct=*vdistinct.get_string(); | |
| if(sdistinct!="tables") | |
| throw Exception(PARSER_RUNTIME, &sdistinct, "must be 'tables' or true/false"); | |
| value_type=C_TABLE; | |
| return D_FIRST; | |
| } | } |
| return vdistinct.as_bool() ? D_FIRST : D_ILLEGAL; | |
| } | } |
| static void _hash(Request& r, MethodParams& params) { | static void _hash(Request& r, MethodParams& params) { |
| Line 846 static void _hash(Request& r, MethodPara | Line 859 static void _hash(Request& r, MethodPara |
| Table2hash_value_type value_type=C_HASH; | Table2hash_value_type value_type=C_HASH; |
| int param_index=params.count()-1; | int param_index=params.count()-1; |
| if(param_index>0) { | if(param_index>0) { |
| if(HashStringValue* options=params.as_no_junction(param_index, PARAM_MUST_NOT_BE_CODE).get_hash()){ // can't use .as_has because the 2nd param could be table so .as_hash throws an error | |
| if(params[1].get_junction()) | |
| value_type=C_CODE; | |
| if(HashStringValue* options=params[param_index].get_hash()){ // can't use .as_hash because the 2nd param could be table so .as_hash throws an error | |
| --param_index; | --param_index; |
| int valid_options=0; | int valid_options=0; |
| if(Value* vdistinct_code=options->get(sql_distinct_name)) { // $.distinct ? | if(Value* vdistinct_code=options->get(sql_distinct_name)) { // $.distinct ? |
| valid_options++; | valid_options++; |
| Value& vdistinct_value=r.process_to_value(*vdistinct_code); | distinct=get_distinct(r.process_to_value(*vdistinct_code), value_type); |
| if(vdistinct_value.is_string()) { | |
| const String& sdistinct=*vdistinct_value.get_string(); | |
| if(sdistinct=="tables") { | |
| value_type=C_TABLE; | |
| distinct=D_FIRST; | |
| } else { | |
| throw Exception(PARSER_RUNTIME, | |
| &sdistinct, | |
| "must be 'tables' or true/false"); | |
| } | |
| } else { | |
| distinct=vdistinct_value.as_bool()?D_FIRST:D_ILLEGAL; | |
| } | |
| } | } |
| if(Value* vvalue_type_code=options->get(sql_value_type_name)) { // $.type ? | if(Value* vvalue_type_code=options->get(sql_value_type_name)) { // $.type ? |
| if(value_type==C_TABLE) // $.distinct[tables] already was specified | if(value_type==C_TABLE) // $.distinct[tables] already was specified |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, 0, "you can't specify $.distinct[tables] and $.type[] together"); |
| 0, | if(value_type==C_CODE) |
| "you can't specify $.distinct[tables] and $.type[] together"); | throw Exception(PARSER_RUNTIME, 0, "you can't specify $.type[] if value is code"); |
| valid_options++; | valid_options++; |
| value_type=get_value_type(r.process_to_value(*vvalue_type_code)); | value_type=get_value_type(r.process_to_value(*vvalue_type_code)); |
| } | } |
| Line 881 static void _hash(Request& r, MethodPara | Line 884 static void _hash(Request& r, MethodPara |
| } | } |
| } | } |
| if(param_index==2) // options was specified but not as hash | if(param_index==2) // options were specified but not as hash |
| throw Exception(PARSER_RUNTIME, 0, "options must be hash"); | throw Exception(PARSER_RUNTIME, 0, "options must be hash"); |
| Array<int> value_fields; | Array<int> value_fields; |
| Value* value_code=0; | |
| if(param_index==0){ // list of columns wasn't specified | if(param_index==0){ // list of columns wasn't specified |
| if(value_type==C_STRING) // $.type[string] | if(value_type==C_STRING) // $.type[string] |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, 0, "you must specify one value field with option $.type[string]"); |
| 0, | |
| "you must specify one value field with option $.type[string]"); | |
| for(size_t i=0; i<columns->count(); i++) // by all columns, including key | for(size_t i=0; i<columns->count(); i++) // by all columns, including key |
| value_fields+=i; | value_fields+=i; |
| } else { // list of columns was specified | } else { // list of columns or code was specified |
| if(value_type==C_TABLE) | if(value_type==C_TABLE) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, 0, "you can't specify value field(s) with option $.distinct[tables] or $.type[tables]"); |
| 0, | |
| "you can't specify value field(s) with option $.distinct[tables] or $.type[tables]"); | Value& value_fields_param=params[1]; |
| if(value_fields_param.get_junction()){ // code specified | |
| Value& value_fields_param=params.as_no_junction(param_index, "value field(s) must not be code"); | value_code=&value_fields_param; |
| if(value_fields_param.is_string()) { // one column as string was specified | } else if(value_fields_param.is_string()) { // one column as string was specified |
| value_fields+=self_table.column_name2index(*value_fields_param.get_string(), true); | const String &field_name=*value_fields_param.get_string(); |
| if(!field_name.is_empty()) | |
| value_fields+=self_table.column_name2index(field_name, true); | |
| } else if(Table* value_fields_table=value_fields_param.get_table()) { // list of columns were specified in table | } else if(Table* value_fields_table=value_fields_param.get_table()) { // list of columns were specified in table |
| for(Array_iterator<Table::element_type> i(*value_fields_table); i.has_next(); ) { | for(Array_iterator<Table::element_type> i(*value_fields_table); i.has_next(); ) { |
| const String& value_field_name =*i.next()->get(0); | const String& value_field_name =*i.next()->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, 0, "value field(s) must be string or table or code"); |
| 0, | |
| "value field(s) must be string or table"); | |
| } | } |
| if(value_type==C_STRING && value_fields.count()!=1) | if(value_type==C_STRING && value_fields.count()!=1) |
| throw Exception(PARSER_RUNTIME, | throw Exception(PARSER_RUNTIME, 0, "you can specify only one value field with option $.type[string]"); |
| 0, | |
| "you can specify only one value field with option $.type[string]"); | |
| { | { |
| Value* key_param=¶ms[0]; | Value* key_param=¶ms[0]; |
| Row_info info={ | Row_info info={ |
| &r, | &r, |
| &self_table, | &self_table, |
| /*key_code=*/key_param->get_junction()?key_param:0, | /*key_code=*/key_param->get_junction() ? key_param : 0, |
| /*key_field=*/0/*filled below*/, | /*key_field=*/0/*filled below*/, |
| &value_fields, | &value_fields, |
| value_code, | |
| &result.hash(), | &result.hash(), |
| distinct, | distinct, |
| /*row=*/0, | /*row=*/0, |
| value_type | value_type |
| }; | }; |
| 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)); | |
| int saved_current=self_table.current(); | int saved_current=self_table.current(); |
| self_table.for_each(table_row_to_hash, &info); | self_table.for_each(table_row_to_hash, &info); |
| Line 1087 static void _foreach(Request& r, MethodP | Line 1088 static void _foreach(Request& r, MethodP |
| Table& table=GET_SELF(r, VTable).table(); | Table& table=GET_SELF(r, VTable).table(); |
| size_t saved_current=table.current(); | size_t saved_current=table.current(); |
| size_t size=table.count(); | |
| const String* rownum_var_name=rownum_name.is_empty()? 0 : &rownum_name; | const String* rownum_var_name=rownum_name.is_empty()? 0 : &rownum_name; |
| const String* value_var_name=value_name.is_empty()? 0 : &value_name; | const String* value_var_name=value_name.is_empty()? 0 : &value_name; |
| Line 1096 static void _foreach(Request& r, MethodP | Line 1096 static void _foreach(Request& r, MethodP |
| if(delim_maybe_code) { // delimiter set | if(delim_maybe_code) { // delimiter set |
| bool need_delim=false; | bool need_delim=false; |
| for(size_t row=0; row<size; row++) { | for(size_t row=0; row<table.count(); row++) { |
| table.set_current(row); | table.set_current(row); |
| if(rownum_var_name) | if(rownum_var_name) |
| Line 1121 static void _foreach(Request& r, MethodP | Line 1121 static void _foreach(Request& r, MethodP |
| break; | break; |
| } | } |
| } else { | } else { |
| for(size_t row=0; row<size; row++) { | for(size_t row=0; row<table.count(); row++) { |
| table.set_current(row); | table.set_current(row); |
| if(rownum_var_name) | if(rownum_var_name) |