--- parser3/src/classes/table.C 2001/11/23 12:56:37 1.134 +++ parser3/src/classes/table.C 2002/03/15 10:08:13 1.145 @@ -1,14 +1,13 @@ /** @file Parser: @b table parser class. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://paf.design.ru) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) - $Id: table.C,v 1.134 2001/11/23 12:56:37 paf Exp $ + $Id: table.C,v 1.145 2002/03/15 10:08:13 paf Exp $ */ #include "classes.h" -#include "pa_config_includes.h" #include "pa_common.h" #include "pa_request.h" #include "pa_vtable.h" @@ -35,8 +34,15 @@ public: // Methoded // methods -static void _set(Request& r, const String& method_name, MethodParams *params) { +static void _create(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); + // clone? + if(params->size()==1) + if(const Table *source=params->get(0).get_table()) { + static_cast(r.self)->set_table(*new(pool) Table(*source)); + return; + } + // data is last parameter Temp_lang temp_lang(r, String::UL_PASS_APPENDED); const String& data= @@ -51,15 +57,15 @@ static void _set(Request& r, const Strin columns=new(pool) Array(pool); Array head(pool); - data.split(head, &pos_after, "\n", 1, String::UL_CLEAN, 1); + data.split(head, &pos_after, "\n", 1, String::UL_AS_IS, 1); if(head.size()) - head.get_string(0)->split(*columns, 0, "\t", 1, String::UL_CLEAN); + head.get_string(0)->split(*columns, 0, "\t", 1, String::UL_AS_IS); } Table& table=*new(pool) Table(pool, &method_name, columns); // parse cells Array rows(pool); - data.split(rows, &pos_after, "\n", 1, String::UL_CLEAN); + data.split(rows, &pos_after, "\n", 1, String::UL_AS_IS); Array_iter i(rows); while(i.has_next()) { Array& row=*new(pool) Array(pool); @@ -68,7 +74,7 @@ static void _set(Request& r, const Strin if(!string.size()) continue; - string.split(row, 0, "\t", 1, String::UL_CLEAN); + string.split(row, 0, "\t", 1, String::UL_AS_IS); table+=&row; } @@ -93,16 +99,22 @@ static void _load(Request& r, const Stri uint line=origin.line; #endif if(params->size()==2) { - columns=0; + columns=0; // nameless } else { columns=new(pool) Array(pool); - if(char *row_chars=getrow(&data)) + while(char *row_chars=getrow(&data)) { + // remove empty&comment lines + if(!*row_chars || *row_chars == '#') + continue; do { String *name=new(pool) String(pool); name->APPEND_TAINTED(lsplit(&row_chars, '\t'), 0, file, line++); *columns+=name; } while(row_chars); + + break; + } } // parse cells @@ -185,7 +197,7 @@ static void _save(Request& r, const Stri } // write - file_write(pool, r.absolute(vfile_name.as_string()), + file_write(r.absolute(vfile_name.as_string()), sdata.cstr(), sdata.size(), true, do_append); } @@ -390,15 +402,46 @@ static void _sort(Request& r, const Stri static_cast(r.self)->set_table(new_table); } -static void _locate(Request& r, const String& method_name, MethodParams *params) { - Pool& pool=r.pool(); +static bool _locate_expression(Request& r, const String& method_name, MethodParams *params) { + if(params->size()>1) + throw Exception(0, 0, + &method_name, + "locate by expression has only one parameter - expression"); + + Value& expression_code=params->as_junction(0, "must be expression"); VTable& vtable=*static_cast(r.self); Table& table=vtable.table(); - Value& result=*new(pool) VBool(pool, table.locate( + int saved_current=table.current(); + int size=table.size(); + for(int row=0; rowsize()>2) + throw Exception(0, 0, + &method_name, + "locate by name and value has only two parameters - name and value"); + + VTable& vtable=*static_cast(r.self); + Table& table=vtable.table(); + return table.locate( params->as_string(0, "column name must be string"), params->as_string(1, "value must be string") - )); + ); +} +static void _locate(Request& r, const String& method_name, MethodParams *params) { + Pool& pool=r.pool(); + Value& result=*new(pool) VBool(pool, + params->get(0).get_junction()? + _locate_expression(r, method_name, params) : + _locate_name_value(r, method_name, params)); result.set_name(method_name); r.write_no_lang(result); } @@ -432,7 +475,7 @@ static void _append(Request& r, const St // parse cells Array& row=*new(pool) Array(pool); - string.split(row, 0, "\t", 1, String::UL_CLEAN); + string.split(row, 0, "\t", 1, String::UL_AS_IS); VTable& vtable=*static_cast(r.self); vtable.table()+=&row; @@ -459,8 +502,10 @@ static void _join(Request& r, const Stri for(int src_row=0; src_rowsize(); dest_column++) - dest_row+=src.item(*dest_columns->get_string(dest_column)); + for(int dest_column=0; dest_columnsize(); dest_column++) { + const String *src_item=src.item(*dest_columns->get_string(dest_column)); + dest_row+=src_item?src_item:new(pool) String(pool); + } dest+=&dest_row; } src.set_current(saved_src_current); @@ -520,11 +565,6 @@ public: static void _sql(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - if(!r.connection) - throw Exception(0, 0, - &method_name, - "without connect"); - Value& statement=params->as_junction(0, "statement must be code"); ulong limit=0; @@ -546,13 +586,29 @@ static void _sql(Request& r, const Strin Temp_lang temp_lang(r, String::UL_SQL); const String& statement_string=r.process(statement).as_string(); const char *statement_cstr= - statement_string.cstr(String::UL_UNSPECIFIED, r.connection); + statement_string.cstr(String::UL_UNSPECIFIED, r.connection(&method_name)); Table_sql_event_handlers handlers(pool, method_name, statement_string, statement_cstr); try { - r.connection->query( +#ifdef RESOURCES_DEBUG + struct timeval mt[2]; + //measure:before + gettimeofday(&mt[0],NULL); +#endif + r.connection(&method_name)->query( statement_cstr, offset, limit, handlers); + +#ifdef RESOURCES_DEBUG + //measure:after connect + gettimeofday(&mt[1],NULL); + + double t[2]; + for(int i=0;i<2;i++) + t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0; + + r.sql_request_time+=t[1]-t[0]; +#endif } catch(const Exception& e) { // query problem // more specific source [were url] throw Exception(e.type(), e.code(), @@ -595,12 +651,15 @@ static void _columns(Request& r, const S MTable::MTable(Pool& apool) : Methoded(apool) { set_name(*NEW String(pool(), TABLE_CLASS_NAME)); - // ^table:set{data} - // ^table:set[nameless]{data} - add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2); + // ^table::create{data} + // ^table::create[nameless]{data} + // ^table::create[table] + add_native_method("create", Method::CT_DYNAMIC, _create, 1, 2); + // old name for compatibility with <= v 1.141 2002/01/25 11:33:45 paf + add_native_method("set", Method::CT_DYNAMIC, _create, 1, 2); - // ^table:load[file] - // ^table:load[nameless;file] + // ^table::load[file] + // ^table::load[nameless;file] add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2); // ^table.save[file] @@ -631,7 +690,7 @@ MTable::MTable(Pool& apool) : Methoded(a add_native_method("sort", Method::CT_DYNAMIC, _sort, 1, 2); // ^table.locate[field;value] - add_native_method("locate", Method::CT_DYNAMIC, _locate, 2, 2); + add_native_method("locate", Method::CT_DYNAMIC, _locate, 1, 2); // ^table.flip[] add_native_method("flip", Method::CT_DYNAMIC, _flip, 0, 0);