--- parser3/src/classes/table.C 2013/10/17 21:52:32 1.299 +++ parser3/src/classes/table.C 2016/09/21 15:14:39 1.331 @@ -1,14 +1,10 @@ /** @file Parser: @b table parser class. - Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -#if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4)) -#include -#endif - #include "classes.h" #include "pa_vmethod_frame.h" @@ -21,7 +17,12 @@ #include "pa_vbool.h" #include "pa_array.h" -volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.299 2013/10/17 21:52:32 moko Exp $"; +#if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4)) +#include +#define USE_STRINGSTREAM +#endif + +volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.331 2016/09/21 15:14:39 moko Exp $"; // class @@ -34,7 +35,7 @@ public: // global variable -DECLARE_CLASS_VAR(table, new MTable, 0); +DECLARE_CLASS_VAR(table, new MTable); #define TABLE_REVERSE_NAME "reverse" @@ -50,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; @@ -72,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(); } @@ -94,32 +92,28 @@ static Table::Action_options get_action_ return result; } -static void check_option_param(bool options_defined, - MethodParams& params, size_t next_param_index, - const char *msg) { - if(next_param_index+(options_defined?1:0) != params.count()) - throw Exception(PARSER_RUNTIME, - 0, - "%s", msg); -} -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")), + char separators[3]; + + TableControlChars(): + separator('\t'), sseparator(new String("\t")), encloser(0), sencloser(0) - {} + { + strcpy(separators,"\t\n"); + } + 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 byte character"); + separator=sseparator->first_char(); + separators[0]=separator; result++; } if(Value* vencloser=options.get(PA_COLUMN_ENCLOSER_NAME)) { @@ -127,11 +121,9 @@ struct TableSeparators { if(sencloser->is_empty()){ encloser=0; } else { - if(sencloser->length()!=1) - throw Exception(PARSER_RUNTIME, - sencloser, - "encloser must be one character long"); - encloser=sencloser->first_char(); + if(sencloser->length()!=1) + throw Exception(PARSER_RUNTIME, sencloser, "encloser must be empty or one byte character"); + encloser=sencloser->first_char(); } result++; } @@ -139,12 +131,125 @@ struct TableSeparators { } }; + +struct lsplit_sresult { + String* piece; + char delim; + + lsplit_sresult() : piece(0), delim(0){} + + operator bool() { return piece!=0; } + + void append(String *str){ + if(piece) + *piece << *str; + else + piece = str; + } +}; + +class StringSplitHelper : public String { +public: + char* base; + + StringSplitHelper(String astring) : String(astring), base(cstrm()) {} + + bool check_lang(const char *pos){ + return langs.check_lang(L_AS_IS, pos-base, 1); + } + + String *extract(char *pos){ + String *result=new String; + if(size_t len=strlen(pos)){ + // first: their langs + result->langs.append(result->body, langs, pos-base, len); + // next: letters themselves + result->body=Body(pos); + } + return result; + } +}; + +inline lsplit_sresult lsplit(char* *string_ref, const char* delims, StringSplitHelper& helper) { + lsplit_sresult result; + if(char *pos=*string_ref) { + while(pos=strpbrk(pos, delims)) { + if(helper.check_lang(pos)){ + result.delim=*pos; + *pos=0; + result.piece=helper.extract(*string_ref); + *string_ref=pos+1; + return result; + } + pos++; + } + result.piece=helper.extract(*string_ref); + *string_ref=0; + } + return result; +} + +static lsplit_sresult lsplit(char** string_ref, const char* delims, char encloser, StringSplitHelper& helper) { + lsplit_sresult result; + + if(char *pos=*string_ref) { + if(encloser && *pos==encloser && helper.check_lang(pos)) { + *string_ref=++pos; + + // we are enclosed, searching for second encloser + while(1) { + if(pos=strchr(pos, encloser)){ + if(helper.check_lang(pos)){ + *(pos++)=0; + result.append(helper.extract(*string_ref)); + if(*pos==encloser && helper.check_lang(pos)){ // double-encloser stands for encloser + *string_ref=pos++; + } else { + *string_ref=pos; + break; + } + } + } else { + result.append(helper.extract(*string_ref)); + *string_ref=0; + return result; + } + } + + // we are no longer enclosed, searching for delimiter + while(pos=strpbrk(pos, delims)) { + if(helper.check_lang(pos)){ + result.delim=*pos; + if(pos>*string_ref){ + *pos=0; + result.append(helper.extract(*string_ref)); + } + *string_ref=pos+1; + return result; + } + pos++; + } + result.append(helper.extract(*string_ref)); + *string_ref=0; + } else + return lsplit(string_ref, delims, helper); + } + return result; +} + +static void skip_clean_empty_lines(char** data_ref, StringSplitHelper& helper) { + if(*data_ref) { + while(**data_ref == '\n' && helper.check_lang(*data_ref)) + (*data_ref)++; + } +} + static void _create(Request& r, MethodParams& params) { // clone/copy part? if(Table *source=params[0].get_table()) { Table::Action_options o=get_action_options(r, params, 1, *source); - check_option_param(o.defined, params, 1, - "too many parameters"); + if(params.count()>2) + throw Exception(PARSER_RUNTIME, 0, "too many parameters"); GET_SELF(r, VTable).set_table(*new Table(*source, o)); return; } @@ -156,75 +261,59 @@ 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( - options_param_indexcount()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } // data Temp_lang temp_lang(r, String::L_PASS_APPENDED); - const String& data= - r.process_to_string(params.as_junction(data_param_index, "body must be code")); + StringSplitHelper sdata(r.process_to_string(params.as_junction(data_param_index, "body must be table or code"))); + char *data=sdata.base; // parse columns - size_t raw_pos_after=0; Table::columns_type columns; - - if(nameless){ - columns=Table::columns_type(0); // nameless + if(nameless) { + columns=0; // nameless } else { - columns=Table::columns_type(new ArrayString); - - ArrayString head; - data.split(head, raw_pos_after, "\n", String::L_AS_IS, 1); - if(head.count()) { - size_t col_pos_after=0; - if(head[0]->is_empty()) - *columns += new String(); - else - head[0]->split(*columns, col_pos_after, *separators.scolumn, String::L_AS_IS); + columns=new ArrayString; + while( lsplit_sresult sr=lsplit(&data, control_chars.separators, control_chars.encloser, sdata) ) { + *columns+=sr.piece; + if(sr.delim=='\n') + break; } } - + Table& table=*new Table(columns); - // parse cells + int columns_count=columns ? columns->count(): 0; - ArrayString rows; - data.split(rows, raw_pos_after, "\n", String::L_AS_IS); - Array_iterator i(rows); - while(i.has_next()) { - Table::element_type row(new ArrayString); - const String& string=*i.next(); - // remove comment lines - if(string.is_empty()) - continue; - - size_t col_pos_after=0; - string.split(*row, col_pos_after, *separators.scolumn, String::L_AS_IS); - table+=row; + // parse cells + Table::element_type row(new ArrayString(columns_count)); + skip_clean_empty_lines(&data, sdata); + while( lsplit_sresult sr=lsplit(&data, control_chars.separators, control_chars.encloser, sdata) ) { + if(sr.piece->is_empty() && !sr.delim && !row->count()) // append last empty column [if without \n] + break; + *row+=sr.piece; + if(sr.delim=='\n') { + table+=row; + row=new ArrayString(columns_count); + skip_clean_empty_lines(&data, sdata); + } } - + // last line [if without \n] + if(row->count()) + table+=row; + // replace any previous table value GET_SELF(r, VTable).set_table(table); } @@ -233,45 +322,36 @@ struct lsplit_result { char* piece; char delim; + lsplit_result(char *apiece=0) : piece(apiece), delim(0){} operator bool() { return piece!=0; } }; -inline lsplit_result lsplit(char* string, char delim1, char delim2) { - lsplit_result result; - if(string) { - char delims[]={delim1, delim2, 0}; - if(char* v=strpbrk(string, delims)) { +inline lsplit_result lsplit(char* *string_ref, const char* delims) { + lsplit_result result(*string_ref); + if(result.piece) { + if(char* v=strpbrk(result.piece, delims)) { result.delim=*v; *v=0; - result.piece=v+1; + *string_ref=v+1; return result; } + *string_ref=0; } - result.piece=0; - result.delim=0; return result; } -inline lsplit_result lsplit(char* *string_ref, char delim1, char delim2) { - lsplit_result result; - result.piece=*string_ref; - lsplit_result next=lsplit(*string_ref, delim1, delim2); - result.delim=next.delim; - *string_ref=next.piece; - return result; -} +static lsplit_result lsplit(char** string_ref, const char* delims, char encloser) { + lsplit_result result(*string_ref); -static lsplit_result lsplit(char** string_ref, char delim1, char delim2, char encloser) { - lsplit_result result; - - if(char* string=*string_ref) { - if(encloser && *string==encloser) { - string++; + if(result.piece) { + if(encloser && *result.piece==encloser) { + result.piece++; + char c; char *read; char *write; - write=read=string; - char c; + write=read=result.piece; + // we are enclosed, searching for second encloser while(c=*read++) { if(c==encloser) { @@ -282,50 +362,42 @@ static lsplit_result lsplit(char** strin } *write++=c; } - // we are no longer enclosed, searching for delimiter, skipping extra enclosers + + // we are no longer enclosed, searching for delimiter while(c=*read++) { - if(c==delim1 || c==delim2) { + if(c==delims[0] || c==delims[1]) { result.delim=c; break; - } else if(c!=encloser) + } else *write++=c; } + *write=0; // terminate - *string_ref=c? read: 0; - result.piece=string; + *string_ref=c ? read : 0; return result; } else - return lsplit(string_ref, delim1, delim2); + return lsplit(string_ref, delims); } - result.piece=0; return result; } static void skip_empty_and_comment_lines( char** data_ref ) { - if(char *data=*data_ref) { - while( char c=*data ) { - if( c== '\n' || c == '#' ) { - /*nowhere=*/getrow(&data); // remove empty&comment lines - if(!(*data_ref=data)) - break; - continue; - } - break; + while(*data_ref) { + if(**data_ref == '\n'){ + (*data_ref)++; + } else { + if(**data_ref == '#' ) + /*nowhere=*/getrow(data_ref); + else + break; } } } 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; - } + if(*data_ref) { + while(**data_ref == '\n') + (*data_ref)++; } } @@ -340,32 +412,28 @@ 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(): 0; + int columns_count=columns ? columns->count(): 0; // parse cells Table::element_type row(new ArrayString(columns_count)); skip_lines_action(&data); - while( lsplit_result sr=lsplit(&data, separators.column, '\n', separators.encloser) ) { + while( lsplit_result sr=lsplit(&data, control_chars.separators, control_chars.encloser) ) { if(!*sr.piece && !sr.delim && !row->count()) // append last empty column [if without \n] break; *row+=new String(sr.piece, String::L_TAINTED); @@ -396,10 +464,6 @@ static void _load(Request& r, MethodPara GET_SELF(r, VTable).set_table(table); } -#if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4)) - #define USE_STRINGSTREAM -#endif - #ifdef USE_STRINGSTREAM #include "gc_allocator.h" @@ -426,32 +490,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); } @@ -555,7 +619,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); } @@ -585,25 +649,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); } @@ -649,13 +709,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 @@ -667,7 +727,7 @@ static void _count(Request& r, MethodPar if(params.count()) { const String& param=params.as_string(0, PARAMETER_MUST_BE_STRING); if(param == "columns") - result = table.columns() ? table.columns()->count() : 0; + result = table.columns() ? table.columns()->count() : table.max_cells(); else if(param == "cells") result = table.count() ? table[table.current()]->count() : 0; else if(param == "rows") // synonim for ^table.count[] @@ -696,9 +756,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); @@ -716,11 +774,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; @@ -782,8 +840,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: { @@ -792,9 +854,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); @@ -815,14 +875,20 @@ static void table_row_to_hash(Table::ele table=new Table(*info->table, table_options/*no rows, just structure*/); info->hash->put(*key, new VTable(table)); } - *table+=row; + Table::element_type row_copy(new ArrayString(row->count())); + row_copy->append(*row); + *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){ @@ -835,15 +901,22 @@ 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) { @@ -855,32 +928,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)); } @@ -890,59 +953,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); @@ -1043,10 +1104,9 @@ static bool expression_is_true(Table&, E static bool _locate_expression(Table& table, Request& r, MethodParams& params) { Value& expression_code=params.as_junction(0, "must be expression"); - const size_t options_index=1; - Table::Action_options o=get_action_options(r, params, options_index, table); - check_option_param(o.defined, params, options_index, "locate by expression only has parameters: expression and, maybe, options"); - + Table::Action_options o=get_action_options(r, params, 1, table); + if(params.count()>2) + throw Exception(PARSER_RUNTIME, 0, "locate by expression only has parameters: expression and, maybe, options"); Expression_is_true_info info={&r, &expression_code}; return table.table_first_that(expression_is_true, &info, o); } @@ -1054,10 +1114,7 @@ static bool _locate_expression(Table& ta static bool _locate_name_value(Table& table, Request& r, MethodParams& params) { const String& name=params.as_string(0, "column name must be string"); const String& value=params.as_string(1, VALUE_MUST_BE_STRING); - const size_t options_index=2; - Table::Action_options o=get_action_options(r, params, options_index, table); - check_option_param(o.defined, params, options_index, "locate by name has parameters: name, value and, maybe, options"); - + Table::Action_options o=get_action_options(r, params, 2, table); return table.locate(name, value, o); } @@ -1075,7 +1132,7 @@ static void _flip(Request& r, MethodPara Table& old_table=GET_SELF(r, VTable).table(); Table& new_table=*new Table(0); if(size_t old_count=old_table.count()) - if(size_t old_cols=old_table.columns()?old_table.columns()->count():old_table[0]->count()) + if(size_t old_cols=old_table.columns()?old_table.columns()->count():old_table.max_cells()) for(size_t column=0; columnis_empty()? 0 : rownum_var_name; + value_var_name=value_var_name->is_empty()? 0 : value_var_name; Value* var_context=r.get_method_frame()->caller(); if(delim_maybe_code) { // delimiter set bool need_delim=false; - for(size_t row=0; rowput_element(*rownum_var_name, new VString(*new String(String::Body::Format(row), String::L_CLEAN))); + r.put_element(*var_context, *rownum_var_name, new VString(*new String(String::Body::Format(row), String::L_CLEAN))); if(value_var_name) - var_context->put_element(*value_var_name, new VTable(&table)); + r.put_element(*var_context, *value_var_name, new VTable(&table)); r.process_write(body_code); Request::Skip lskip=r.get_skip(); r.set_skip(Request::SKIP_NOTHING); @@ -1152,16 +1208,55 @@ static void _foreach(Request& r, MethodP table.set_current(saved_current); } -static void _append(Request& r, MethodParams& params) { +static void update_cell(HashStringValue::key_type aname, HashStringValue::value_type avalue, VTable *dest) { + dest->put_element(String(aname, String::L_CLEAN), avalue); // new not required +} + +inline Table::element_type row_from_string(Request& r, Value ¶m){ + if(!param.is_string() && !param.get_junction()) + throw Exception(PARSER_RUNTIME, 0, "row must be string, code or hash"); + Temp_lang temp_lang(r, String::L_PASS_APPENDED); - const String& string=r.process_to_string(params[0]); + const String& string=r.process_to_string(param); // parse cells Table::element_type row=new ArrayString; - size_t pos_after=0; - string.split(*row, pos_after, "\t", String::L_AS_IS); + string.split(*row, 0, "\t", String::L_AS_IS); - GET_SELF(r, VTable).table()+=row; + return row; +} + +static void _append(Request& r, MethodParams& params) { + VTable vtable=GET_SELF(r, VTable); + Table& table=vtable.table(); + + HashStringValue* hash=params[0].get_hash(); + if(hash){ + table+=new ArrayString(); + size_t saved_current=table.current(); + table.set_current(table.count()-1); + hash->for_each(update_cell, &vtable); + table.set_current(saved_current); + } else { + table+=row_from_string(r, params[0]); + } +} + +static void _insert(Request& r, MethodParams& params) { + VTable vtable=GET_SELF(r, VTable); + Table& table=vtable.table(); + HashStringValue* hash=params[0].get_hash(); + if(hash){ + table.insert(table.current(), new ArrayString()); + hash->for_each(update_cell, &vtable); + } else { + table.insert(table.current(), row_from_string(r, params[0])); + } +} + +static void _delete(Request& r, MethodParams&) { + Table& table=GET_SELF(r, VTable).table(); + table.remove_current(); } static void join_named_row(Table& src, Table* dest) { @@ -1181,13 +1276,10 @@ static void _join(Request& r, MethodPara Table& src=*params.as_table(0, "source"); Table::Action_options o=get_action_options(r, params, 1, src); - check_option_param(o.defined, params, 1, "invalid extra parameter"); Table& dest=GET_SELF(r, VTable).table(); if(&src == &dest) - throw Exception(PARSER_RUNTIME, - 0, - "source and destination are same table"); + throw Exception(PARSER_RUNTIME, 0, "source and destination are same table"); if(dest.columns()) // dest is named src.table_for_each(join_named_row, &dest, o); @@ -1264,7 +1356,7 @@ static void marshal_bind( // not static, used elsewhere int marshal_binds(HashStringValue& hash, SQL_Driver::Placeholder*& placeholders) { int hash_count=hash.count(); - placeholders=new(UseGC) SQL_Driver::Placeholder[hash_count]; + placeholders=new SQL_Driver::Placeholder[hash_count]; SQL_Driver::Placeholder* ptr=placeholders; hash.for_each(marshal_bind, &ptr); return hash_count; @@ -1485,9 +1577,15 @@ MTable::MTable(): Methoded("table") { // ^table.foreach[row-num;value]{code}[delim] add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 3, 4); - // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d} + // ^table.append{row{tab}data} add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1); + // ^table.insert{row{tab}data} before current row + add_native_method("insert", Method::CT_DYNAMIC, _insert, 1, 1); + + // ^table.delete[] current row + add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 0); + // ^table.join[table][$.limit(10) $.offset(1) $.offset[cur] ] add_native_method("join", Method::CT_DYNAMIC, _join, 1, 2);