--- parser3/src/classes/table.C 2016/03/31 21:46:20 1.313 +++ parser3/src/classes/table.C 2016/09/06 22:19:47 1.319 @@ -22,7 +22,7 @@ #define USE_STRINGSTREAM #endif -volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.313 2016/03/31 21:46:20 moko Exp $"; +volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.319 2016/09/06 22:19:47 moko Exp $"; // class @@ -51,8 +51,7 @@ String table_reverse_name(TABLE_REVERSE_ // methods -static Table::Action_options get_action_options(Request& r, MethodParams& params, - size_t options_index, const Table& source) { +static Table::Action_options get_action_options(Request& r, MethodParams& params, size_t options_index, const Table& source) { Table::Action_options result; if(params.count() <= options_index) return result; @@ -73,9 +72,7 @@ static Table::Action_options get_action_ if(soffset == "cur") result.offset=source.current(); else - throw Exception(PARSER_RUNTIME, - &soffset, - "must be 'cur' string or expression"); + throw Exception(PARSER_RUNTIME, &soffset, "must be 'cur' string or expression"); } else result.offset=r.process_to_value(*voffset).as_int(); } @@ -96,23 +93,21 @@ static Table::Action_options get_action_ return result; } -struct TableSeparators { - char column; const String* scolumn; +struct TableControlChars { + char separator; const String* sseparator; char encloser; const String* sencloser; - TableSeparators(): - column('\t'), scolumn(new String("\t")), + TableControlChars(): + separator('\t'), sseparator(new String("\t")), encloser(0), sencloser(0) {} int load( HashStringValue& options ) { int result=0; if(Value* vseparator=options.get(PA_COLUMN_SEPARATOR_NAME)) { - scolumn=&vseparator->as_string(); - if(scolumn->length()!=1) - throw Exception(PARSER_RUNTIME, - scolumn, - "separator must be one character long"); - column=scolumn->first_char(); + sseparator=&vseparator->as_string(); + if(sseparator->length()!=1) + throw Exception(PARSER_RUNTIME, sseparator, "separator must be one character long"); + separator=sseparator->first_char(); result++; } if(Value* vencloser=options.get(PA_COLUMN_ENCLOSER_NAME)) { @@ -121,9 +116,7 @@ struct TableSeparators { encloser=0; } else { if(sencloser->length()!=1) - throw Exception(PARSER_RUNTIME, - sencloser, - "encloser must be one character long"); + throw Exception(PARSER_RUNTIME, sencloser, "encloser must be one character long"); encloser=sencloser->first_char(); } result++; @@ -132,6 +125,7 @@ struct TableSeparators { } }; + static void _create(Request& r, MethodParams& params) { // clone/copy part? if(Table *source=params[0].get_table()) { @@ -149,16 +143,14 @@ static void _create(Request& r, MethodPa if(params[0].is_string()){ // can be nameless only const String& snameless=params.as_string(0, "called with more then 1 param, first param may be only string 'nameless' or junction"); if(snameless!="nameless") - throw Exception(PARSER_RUNTIME, - &snameless, - "table::create called with more then 1 param, first param may be only 'nameless'"); + throw Exception(PARSER_RUNTIME, &snameless, "table::create called with more then 1 param, first param may be only 'nameless'"); nameless=true; data_param_index++; } } HashStringValue *options=0; - TableSeparators separators; + TableControlChars control_chars; size_t options_param_index=data_param_index+1; if( @@ -167,11 +159,9 @@ static void _create(Request& r, MethodPa ) { // cloning, so that we could change options=new HashStringValue(*options); - separators.load(*options); - if(separators.encloser){ - throw Exception(PARSER_RUNTIME, - 0, - "encloser not supported for table::create yet"); + control_chars.load(*options); + if(control_chars.encloser){ + throw Exception(PARSER_RUNTIME, 0, "encloser not supported for table::create yet"); } } @@ -196,7 +186,7 @@ static void _create(Request& r, MethodPa if(head[0]->is_empty()) *columns += new String(); else - head[0]->split(*columns, col_pos_after, *separators.scolumn, String::L_AS_IS); + head[0]->split(*columns, col_pos_after, *control_chars.sseparator, String::L_AS_IS); } } @@ -214,7 +204,7 @@ static void _create(Request& r, MethodPa continue; size_t col_pos_after=0; - string.split(*row, col_pos_after, *separators.scolumn, String::L_AS_IS); + string.split(*row, col_pos_after, *control_chars.sseparator, String::L_AS_IS); table+=row; } @@ -333,12 +323,12 @@ static void _load(Request& r, MethodPara size_t options_param_index=filename_param_index+1; HashStringValue *options=0; - TableSeparators separators; + TableControlChars control_chars; if(options_param_indexcount()) // append last empty column [if without \n] break; *row+=new String(sr.piece, String::L_TAINTED); @@ -415,32 +405,32 @@ static void enclose( pa_stringstream& to } } -static void table_to_csv(pa_stringstream& result, Table& table, TableSeparators& separators, bool output_column_names) { +static void table_to_csv(pa_stringstream& result, Table& table, TableControlChars& control_chars, bool output_column_names) { if(output_column_names) { if(table.columns()) { // named table - if(separators.encloser){ + if(control_chars.encloser){ for(Array_iterator i(*table.columns()); i.has_next(); ) { - enclose( result, i.next(), separators.encloser ); + enclose( result, i.next(), control_chars.encloser ); if(i.has_next()) - result< i(*table.columns()); i.has_next(); ) { result<cstr(); if(i.has_next()) - result<count():0) for(int column=0; column i(table); - if(separators.encloser){ + if(control_chars.encloser){ while(i.has_next()) { for(Array_iterator c(*i.next()); c.has_next(); ) { - enclose( result, c.next(), separators.encloser ); + enclose( result, c.next(), control_chars.encloser ); if(c.has_next()) - result< c(*i.next()); c.has_next(); ) { result<cstr(); if(c.has_next()) - result< i(*table.columns()); i.has_next(); ) { - enclose( result, i.next(), separators.encloser, separators.sencloser ); + enclose( result, i.next(), control_chars.encloser, control_chars.sencloser ); if(i.has_next()) - result<<*separators.scolumn; + result<<*control_chars.sseparator; } } else { for(Array_iterator i(*table.columns()); i.has_next(); ) { result<<*i.next(); if(i.has_next()) - result<<*separators.scolumn; + result<<*control_chars.sseparator; } } } else { // nameless table [we were asked to output column names] if(int lsize=table.count()?table[0]->count():0) for(int column=0; column i(table); - if(separators.encloser){ + if(control_chars.encloser){ while(i.has_next()) { for(Array_iterator c(*i.next()); c.has_next(); ) { - enclose( result, c.next(), separators.encloser, separators.sencloser ); + enclose( result, c.next(), control_chars.encloser, control_chars.sencloser ); if(c.has_next()) - result<<*separators.scolumn; + result<<*control_chars.sseparator; } result.append_know_length("\n", 1, String::L_CLEAN); } @@ -544,7 +534,7 @@ static void table_to_csv(String& result, for(Array_iterator c(*i.next()); c.has_next(); ) { result<<*c.next(); if(c.has_next()) - result<<*separators.scolumn; + result<<*control_chars.sseparator; } result.append_know_length("\n", 1, String::L_CLEAN); } @@ -574,25 +564,23 @@ static void _save(Request& r, MethodPara if(do_append && file_exist(file_spec)) output_column_names=false; - TableSeparators separators; + TableControlChars control_chars; if(param_indexcount()) throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } if(param_indexcount()) throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } @@ -638,13 +624,13 @@ static void _csv_string(Request& r, Meth #ifdef USE_STRINGSTREAM pa_stringstream ost(std::stringstream::out); - table_to_csv(ost, table, separators, output_column_names); + table_to_csv(ost, table, control_chars, output_column_names); r.write_no_lang(*new VString(*new String(pa_strdup(ost.str().c_str()), String::L_CLEAN))); #else String sdata; - table_to_csv(sdata, table, separators, output_column_names); + table_to_csv(sdata, table, control_chars, output_column_names); r.write_no_lang(*new VString(*new String(sdata.cstr(), String::L_CLEAN))); #endif @@ -685,9 +671,7 @@ static void _offset(Request& r, MethodPa else if(whence=="set") absolute=true; else - throw Exception(PARSER_RUNTIME, - &whence, - "is invalid whence, valid are 'cur' or 'set'"); + throw Exception(PARSER_RUNTIME, &whence, "is invalid whence, valid are 'cur' or 'set'"); } int offset=params.as_int(params.count()-1, "offset must be expression", r); @@ -705,11 +689,10 @@ static void _menu(Request& r, MethodPara Table& table=GET_SELF(r, VTable).table(); size_t saved_current=table.current(); - size_t size=table.count(); if(delim_maybe_code) { // delimiter set bool need_delim=false; - for(size_t row=0; row* value_fields; + Value* value_code; HashStringValue* hash; Table2hash_distint distinct; size_t row; @@ -771,8 +755,12 @@ static void table_row_to_hash(Table::ele bool exist=false; switch(info->value_type) { case C_STRING: { - 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()); + if(info->value_fields->count()){ + 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; } case C_HASH: { @@ -781,9 +769,7 @@ static void table_row_to_hash(Table::ele for(Array_iterator i(*info->value_fields); i.has_next(); ) { size_t value_field=i.next(); if(value_fieldcount()) - hash.put( - *info->table->columns()->get(value_field), - new VString(*row->get(value_field))); + hash.put(*info->table->columns()->get(value_field), new VString(*row->get(value_field))); } exist=info->hash->put_dont_replace(*key, vhash); @@ -809,11 +795,15 @@ static void table_row_to_hash(Table::ele *table+=row_copy; 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) - throw Exception(PARSER_RUNTIME, - key, - "duplicate key"); + throw Exception(PARSER_RUNTIME, key, "duplicate key"); } Table2hash_value_type get_value_type(Value& vvalue_type){ @@ -826,17 +816,24 @@ Table2hash_value_type get_value_type(Val } else if (svalue_type == "hash") { return C_HASH; } else { - throw Exception(PARSER_RUNTIME, - &svalue_type, - "must be 'hash', 'table' or 'string'"); + throw Exception(PARSER_RUNTIME, &svalue_type, "must be 'hash', 'table' or 'string'"); } } else { - throw Exception(PARSER_RUNTIME, - 0, - "'type' must be hash"); + throw Exception(PARSER_RUNTIME, 0, "'type' must be string"); } } +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) { Table& self_table=GET_SELF(r, VTable).table(); VHash& result=*new VHash; @@ -846,32 +843,22 @@ static void _hash(Request& r, MethodPara Table2hash_value_type value_type=C_HASH; int param_index=params.count()-1; 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; int valid_options=0; if(Value* vdistinct_code=options->get(sql_distinct_name)) { // $.distinct ? valid_options++; - Value& vdistinct_value=r.process_to_value(*vdistinct_code); - 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; - } + distinct=get_distinct(r.process_to_value(*vdistinct_code), value_type); } if(Value* vvalue_type_code=options->get(sql_value_type_name)) { // $.type ? if(value_type==C_TABLE) // $.distinct[tables] already was specified - throw Exception(PARSER_RUNTIME, - 0, - "you can't specify $.distinct[tables] and $.type[] together"); - + throw Exception(PARSER_RUNTIME, 0, "you can't specify $.distinct[tables] and $.type[] together"); + if(value_type==C_CODE) + throw Exception(PARSER_RUNTIME, 0, "you can't specify $.type[] if value is code"); valid_options++; value_type=get_value_type(r.process_to_value(*vvalue_type_code)); } @@ -881,59 +868,57 @@ 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"); Array value_fields; + Value* value_code=0; + if(param_index==0){ // list of columns wasn't specified if(value_type==C_STRING) // $.type[string] - throw Exception(PARSER_RUNTIME, - 0, - "you must specify one value field with option $.type[string]"); + throw Exception(PARSER_RUNTIME, 0, "you must specify one value field with option $.type[string]"); for(size_t i=0; icount(); i++) // by all columns, including key value_fields+=i; - } else { // list of columns was specified + } else { // list of columns or code was specified if(value_type==C_TABLE) - throw Exception(PARSER_RUNTIME, - 0, - "you can't specify value field(s) with option $.distinct[tables] or $.type[tables]"); - - Value& value_fields_param=params.as_no_junction(param_index, "value field(s) must not be code"); - if(value_fields_param.is_string()) { // one column as string was specified - value_fields+=self_table.column_name2index(*value_fields_param.get_string(), true); + throw Exception(PARSER_RUNTIME, 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_code=&value_fields_param; + } else if(value_fields_param.is_string()) { // one column as string was specified + 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 for(Array_iterator i(*value_fields_table); i.has_next(); ) { const String& value_field_name =*i.next()->get(0); value_fields +=self_table.column_name2index(value_field_name, true); } } else - throw Exception(PARSER_RUNTIME, - 0, - "value field(s) must be string or table"); + throw Exception(PARSER_RUNTIME, 0, "value field(s) must be string or table or code"); } if(value_type==C_STRING && value_fields.count()!=1) - throw Exception(PARSER_RUNTIME, - 0, - "you can specify only one value field with option $.type[string]"); + throw Exception(PARSER_RUNTIME, 0, "you can specify only one value field with option $.type[string]"); { Value* key_param=¶ms[0]; Row_info info={ &r, &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*/, &value_fields, + value_code, &result.hash(), distinct, /*row=*/0, value_type }; - info.key_field=(info.key_code?-1 - :self_table.column_name2index(key_param->as_string(), true)); + info.key_field=(info.key_code ? -1 : self_table.column_name2index(key_param->as_string(), true)); int saved_current=self_table.current(); self_table.for_each(table_row_to_hash, &info); @@ -1087,7 +1072,6 @@ static void _foreach(Request& r, MethodP Table& table=GET_SELF(r, VTable).table(); 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* value_var_name=value_name.is_empty()? 0 : &value_name; @@ -1096,7 +1080,7 @@ static void _foreach(Request& r, MethodP if(delim_maybe_code) { // delimiter set bool need_delim=false; - for(size_t row=0; row