--- parser3/src/classes/table.C 2019/09/11 15:26:09 1.348 +++ parser3/src/classes/table.C 2020/12/25 22:05:31 1.356 @@ -1,10 +1,18 @@ /** @file Parser: @b table parser class. - Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ +#include "pa_config_includes.h" + +#if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4) && !defined(PA_DEBUG_DISABLE_GC)) +#include +#include "../lib/gc/include/gc_allocator.h" +#define USE_STRINGSTREAM +#endif + #include "classes.h" #include "pa_vmethod_frame.h" @@ -17,12 +25,7 @@ #include "pa_vbool.h" #include "pa_array.h" -#if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4) && !defined(PA_DEBUG_DISABLE_GC)) -#include -#define USE_STRINGSTREAM -#endif - -volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.348 2019/09/11 15:26:09 moko Exp $"; +volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.356 2020/12/25 22:05:31 moko Exp $"; // class @@ -406,15 +409,11 @@ static void _load(Request& r, MethodPara HashStringValue *options=0; TableControlChars control_chars; - if(options_param_index, gc_allocator > pa_stringstream; typedef std::basic_string, gc_allocator > pa_string; @@ -637,7 +635,7 @@ static void _save(Request& r, MethodPara --param_index; const String& file_name=params.as_string(param_index++, FILE_NAME_MUST_NOT_BE_CODE); - String file_spec=r.absolute(file_name); + String file_spec=r.full_disk_path(file_name); if(do_append && file_exist(file_spec)) output_column_names=false; @@ -1261,18 +1259,18 @@ static void join_nameless_row(Table& src *dest+=src[src.current()]; } static void _join(Request& r, MethodParams& params) { - Table& src=*params.as_table(0, "source"); - - Table::Action_options o=get_action_options(r, params, 1, src); + if(Table* src=params.as_table(0, "source")){ + Table::Action_options o=get_action_options(r, params, 1, *src); - Table& dest=GET_SELF(r, VTable).table(); - if(&src == &dest) - 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); - else // dest is nameless - src.table_for_each(join_nameless_row, &dest, o); + Table& dest=GET_SELF(r, VTable).table(); + if(src == &dest) + 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); + else // dest is nameless + src->table_for_each(join_nameless_row, &dest, o); + } } #ifndef DOXYGEN @@ -1503,6 +1501,42 @@ static void _select(Request& r, MethodPa r.write(*new VTable(&result_table)); } + +static void _rename(Request& r, MethodParams& params) { + const String* name_from=NULL; + const String* name_to=NULL; + HashStringValue* names=NULL; + + if(params.count()>1){ + name_from=¶ms.as_string(0, COLUMN_NAME_MUST_BE_STRING); + name_to=¶ms.as_string(1, COLUMN_NAME_MUST_BE_STRING); + } else + if (!(names=params.as_hash(0))) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); + + Table& table=GET_SELF(r, VTable).table(); + if(Table::columns_type columns=table.columns()) { + if(names){ + for(int i=0; icount(); i++) { + const String *column = columns->get(i); + if(Value* vto=names->get(*column)){ + const String *sto=vto->get_string(); + if(!sto) + throw Exception(PARSER_RUNTIME, column, COLUMN_NAME_MUST_BE_STRING); + columns->put(i, sto); + } + } + } else { + for(int i=0; icount(); i++) { + const String *column = columns->get(i); + if(*column == *name_from) + columns->put(i, name_to); + } + } + table.column_names_init(); + } +} + // constructor MTable::MTable(): Methoded("table") { @@ -1586,4 +1620,8 @@ MTable::MTable(): Methoded("table") { // ^table.select(expression) = table add_native_method("select", Method::CT_DYNAMIC, _select, 1, 2); + + // ^table.rename[column name from;column name to] + // ^table.rename[ $.[column name from][column name to] ... ] + add_native_method("rename", Method::CT_DYNAMIC, _rename, 1, 2); }