--- parser3/src/classes/table.C 2001/10/09 13:17:45 1.124 +++ parser3/src/classes/table.C 2002/01/25 11:33:45 1.141 @@ -2,13 +2,12 @@ Parser: @b table parser class. Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Author: Alexander Petrosyan (http://paf.design.ru) - $Id: table.C,v 1.124 2001/10/09 13:17:45 parser Exp $ + $Id: table.C,v 1.141 2002/01/25 11:33:45 paf Exp $ */ #include "classes.h" -#include "pa_config_includes.h" #include "pa_common.h" #include "pa_request.h" #include "pa_vtable.h" @@ -38,10 +37,9 @@ public: // Methoded static void _set(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); // data is last parameter - Value& vdata=params->as_junction(params->size()-1, "body must be code"); - Temp_lang temp_lang(r, String::UL_PASS_APPENDED); - const String& data=r.process(vdata).as_string(); + const String& data= + r.process(params->as_junction(params->size()-1, "body must be code")).as_string(); size_t pos_after=0; // parse columns @@ -52,15 +50,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); @@ -69,7 +67,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; } @@ -80,11 +78,11 @@ static void _set(Request& r, const Strin static void _load(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); // filename is last parameter - Value& vfilename=params->as_no_junction(params->size()-1, + Value& vfile_name=params->as_no_junction(params->size()-1, "file name must not be code"); // loading text - char *data=file_read_text(pool, r.absolute(vfilename.as_string())); + char *data=file_read_text(pool, r.absolute(vfile_name.as_string())); // parse columns Array *columns; @@ -94,16 +92,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 @@ -129,15 +133,17 @@ static void _load(Request& r, const Stri static_cast(r.self)->set_table(table); } +/// @todo "z\nz" "zzz""zz" static void _save(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - Value& vtable_name=params->as_no_junction(params->size()-1, + Value& vfile_name=params->as_no_junction(params->size()-1, "file name must not be code"); Table& table=static_cast(r.self)->table(); + bool do_append=false; String sdata(pool); - if(params->size()==1) { // not nameless=named output + if(params->size()==1) { // named output // write out names line if(table.columns()) { // named table Array_iter i(*table.columns()); @@ -158,6 +164,17 @@ static void _save(Request& r, const Stri sdata.APPEND_CONST("empty nameless table"); } sdata.APPEND_CONST("\n"); + } else { // mode specified + const String& mode=params->as_string(0, "mode must be string"); + if(mode=="append") + do_append=true; + else if(mode=="nameless") + /*ok, already skipped names output*/; + else + throw Exception(0, 0, + &mode, + "unknown mode, must be 'append'"); + } // data lines Array_iter i(table); @@ -173,8 +190,8 @@ static void _save(Request& r, const Stri } // write - file_write(pool, r.absolute(vtable_name.as_string()), - sdata.cstr(), sdata.size(), true); + file_write(r.absolute(vfile_name.as_string()), + sdata.cstr(), sdata.size(), true, do_append); } static void _count(Request& r, const String& method_name, MethodParams *) { @@ -195,8 +212,21 @@ static void _offset(Request& r, const St Pool& pool=r.pool(); Table& table=static_cast(r.self)->table(); if(params->size()) { - Value& offset_expr=params->as_junction(0, "offset must be expression"); - table.shift(r.process(offset_expr).as_int()); + bool absolute=false; + if(params->size()>1) { + const String& whence=params->as_string(0, "whence must be string"); + if(whence=="cur") + absolute=false; + else if(whence=="set") + absolute=true; + else + throw Exception(0, 0, + &whence, + "is invalid whence, valid are 'cur' or 'set'"); + } + + Value& offset_expr=params->as_junction(params->size()-1, "offset must be expression"); + table.offset(absolute, r.process(offset_expr).as_int()); } else { Value& value=*new(pool) VInt(pool, table.current()); value.set_name(method_name); @@ -244,7 +274,7 @@ static void table_row_to_hash(Array::Ite if(ri.key_fieldsize(); i++) { int value_field=ri.value_fields->get_int(i); if(value_fieldas_junction(0, "body must be code")).as_string(); // 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; @@ -417,14 +448,14 @@ static void _join(Request& r, const Stri Table *maybe_src=params->as_no_junction(0, "table ref must not be code").get_table(); if(!maybe_src) - PTHROW(0, 0, + throw Exception(0, 0, &method_name, "source is not a table"); Table& src=*maybe_src; Table& dest=static_cast(r.self)->table(); if(&src == &dest) - PTHROW(0, 0, + throw Exception(0, 0, &method_name, "source and destination are same table"); @@ -433,8 +464,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); @@ -454,7 +487,7 @@ public: statement_string(astatement_string), statement_cstr(astatement_cstr), columns(*new(pool) Array(pool)), - row(0), row_index(0), + row(0), table(0) { } @@ -477,7 +510,7 @@ public: if(size) cell->APPEND_TAINTED( (const char *)ptr, size, - statement_cstr, row_index++); + statement_cstr, table->size()-1); (*row)+=cell; } @@ -487,7 +520,6 @@ private: const String& statement_string; const char *statement_cstr; Array& columns; Array *row; - uint row_index; public: Table *table; }; @@ -495,11 +527,6 @@ public: static void _sql(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - if(!r.connection) - PTHROW(0, 0, - &method_name, - "without connect"); - Value& statement=params->as_junction(0, "statement must be code"); ulong limit=0; @@ -507,13 +534,13 @@ static void _sql(Request& r, const Strin if(params->size()>1) { Value& voptions=params->as_no_junction(1, "options must be hash, not code"); if(voptions.is_defined()) - if(Hash *options=voptions.get_hash()) { + if(Hash *options=voptions.get_hash(&method_name)) { if(Value *vlimit=(Value *)options->get(*sql_limit_name)) limit=(ulong)r.process(*vlimit).as_double(); if(Value *voffset=(Value *)options->get(*sql_offset_name)) offset=(ulong)r.process(*voffset).as_double(); } else - PTHROW(0, 0, + throw Exception(0, 0, &method_name, "options must be hash"); } @@ -521,23 +548,35 @@ 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); - bool need_rethrow=false; Exception rethrow_me; - PTRY { - r.connection->query( + try { +#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(), + &statement_string, + "%s", e.comment()); } - PCATCH(e) { // query problem - rethrow_me=e; need_rethrow=true; - } - PEND_CATCH - if(need_rethrow) - PTHROW(rethrow_me.type(), rethrow_me.code(), - &statement_string, // setting more specific source [were url] - rethrow_me.comment()); Table *result= handlers.table?handlers.table: // query resulted in table? return it @@ -593,8 +632,9 @@ MTable::MTable(Pool& apool) : Methoded(a add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0); // ^table.offset[] - // ^table.offset[offset] - add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1); + // ^table.offset(offset) + // ^table.offset[cur|set](offset) + add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 2); // ^table.menu{code} // ^table.menu{code}[delim]