--- parser3/src/classes/table.C 2024/10/02 22:54:03 1.366 +++ parser3/src/classes/table.C 2024/11/16 02:57:05 1.372 @@ -1,7 +1,7 @@ /** @file Parser: @b table parser class. - Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) Authors: Konstantin Morshnev , Alexandr Petrosian */ @@ -24,8 +24,9 @@ #include "pa_sql_connection.h" #include "pa_vbool.h" #include "pa_array.h" +#include "pa_varray.h" -volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.366 2024/10/02 22:54:03 moko Exp $"; +volatile const char * IDENT_TABLE_C="$Id: table.C,v 1.372 2024/11/16 02:57:05 moko Exp $"; // class @@ -750,7 +751,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 an invalid whence, valid are 'cur' or 'set'"); } int offset=params.as_int(params.count()-1, "offset must be expression", r); @@ -767,7 +768,7 @@ static void _menu(Request& r, MethodPara Value* delim_maybe_code=params.count()>1?¶ms[1]:0; Table& table=GET_SELF(r, VTable).table(); - size_t saved_current=table.current(); + Temp_current tc(table); if(delim_maybe_code) { // delimiter set bool need_delim=false; @@ -800,7 +801,6 @@ static void _menu(Request& r, MethodPara break; } } - table.set_current(saved_current); } #ifndef DOXYGEN @@ -1001,15 +1001,37 @@ static void _hash(Request& r, MethodPara }; 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); - self_table.set_current(saved_current); result.extract_default(); r.write(result); } +static void _cells(Request& r, MethodParams& params) { + Table& self_table=GET_SELF(r, VTable).table(); + size_t row_size=self_table[self_table.current()]->count(); // number of columns in current row + + if(params.count()){ + int limit=params.as_int(params.count()-1, "offset must be expression", r); + if(limit<0) + limit=0; + if((size_t)limit3?¶ms[3]:0; Table& table=GET_SELF(r, VTable).table(); - size_t saved_current=table.current(); + Temp_current tc(table); rownum_var_name=rownum_var_name->is_empty()? 0 : rownum_var_name; value_var_name=value_var_name->is_empty()? 0 : value_var_name; @@ -1196,7 +1218,6 @@ static void _foreach(Request& r, MethodP break; } } - table.set_current(saved_current); } static void update_cell(HashStringValue::key_type aname, HashStringValue::value_type avalue, VTable *dest) { @@ -1222,11 +1243,10 @@ static void _append(Request& r, MethodPa HashStringValue* hash=params[0].get_hash(); if(hash){ + Temp_current tc(table); 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]); } @@ -1477,7 +1497,7 @@ static void _select(Request& r, MethodPa if(offset<0) offset+=size; if(size && limit>0 && offset>=0 && (size_t)offsetcount(); i++) { + for(size_t i=0; icount(); i++) { const String *column = columns->get(i); if(Value* vto=names->get(*column)){ if(const String *sto=vto->get_string()) @@ -1537,7 +1556,7 @@ static void _rename(Request& r, MethodPa } } } else if(name_from){ - for(int i=0; icount(); i++) { + for(size_t i=0; icount(); i++) { const String *column = columns->get(i); if(*column == *name_from) columns->put(i, name_to); @@ -1595,6 +1614,10 @@ MTable::MTable(): Methoded("table") { // ^table.hash[key field name][value field name(s) string/table] add_native_method("hash", Method::CT_DYNAMIC, _hash, 1, 3); + // ^table.cells[] + // ^table.cells(limit) + add_native_method("cells", Method::CT_DYNAMIC, _cells, 0, 1); + // ^table.sort{string-key-maker} ^table.sort{string-key-maker}[desc|asc] // ^table.sort(numeric-key-maker) ^table.sort(numeric-key-maker)[desc|asc] add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2);