Diff for /parser3/src/classes/table.C between versions 1.129 and 1.142

version 1.129, 2001/10/31 14:08:36 version 1.142, 2002/02/07 11:55:30
Line 2 Line 2
         Parser: @b table parser class.          Parser: @b table parser class.
   
         Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
         Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)          Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
   
         $Id$          $Id$
 */  */
   
 #include "classes.h"  #include "classes.h"
 #include "pa_config_includes.h"  
 #include "pa_common.h"  #include "pa_common.h"
 #include "pa_request.h"  #include "pa_request.h"
 #include "pa_vtable.h"  #include "pa_vtable.h"
Line 35  public: // Methoded Line 34  public: // Methoded
   
 // methods  // 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();          Pool& pool=r.pool();
         // data is last parameter          // clone?
         Value& vdata=params->as_junction(params->size()-1, "body must be code");          if(params->size()==1) 
                   if(const Table *source=params->get(0).get_table()) {
                           static_cast<VTable *>(r.self)->set_table(*new(pool) Table(*source));
                           return;
                   }
   
           // data is last parameter
         Temp_lang temp_lang(r, String::UL_PASS_APPENDED);          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;          size_t pos_after=0;
         // parse columns          // parse columns
Line 52  static void _set(Request& r, const Strin Line 57  static void _set(Request& r, const Strin
                 columns=new(pool) Array(pool);                  columns=new(pool) Array(pool);
   
                 Array head(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())                  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);          Table& table=*new(pool) Table(pool, &method_name, columns);
         // parse cells          // parse cells
         Array rows(pool);          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);          Array_iter i(rows);
         while(i.has_next()) {          while(i.has_next()) {
                 Array& row=*new(pool) Array(pool);                  Array& row=*new(pool) Array(pool);
Line 69  static void _set(Request& r, const Strin Line 74  static void _set(Request& r, const Strin
                 if(!string.size())                  if(!string.size())
                         continue;                          continue;
   
                 string.split(row, 0, "\t", 1, String::UL_CLEAN);                  string.split(row, 0, "\t", 1, String::UL_AS_IS);
                 table+=&row;                  table+=&row;
         }          }
   
Line 94  static void _load(Request& r, const Stri Line 99  static void _load(Request& r, const Stri
         uint line=origin.line;          uint line=origin.line;
 #endif  #endif
         if(params->size()==2) {          if(params->size()==2) {
                 columns=0;                  columns=0; // nameless
         } else {          } else {
                 columns=new(pool) Array(pool);                  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 {                          do {
                                 String *name=new(pool) String(pool);                                  String *name=new(pool) String(pool);
                                 name->APPEND_TAINTED(lsplit(&row_chars, '\t'), 0, file, line++);                                  name->APPEND_TAINTED(lsplit(&row_chars, '\t'), 0, file, line++);
                                 *columns+=name;                                  *columns+=name;
                         } while(row_chars);                          } while(row_chars);
   
                           break;
                   }
         }          }
   
         // parse cells          // parse cells
Line 186  static void _save(Request& r, const Stri Line 197  static void _save(Request& r, const Stri
         }          }
   
         // write          // 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);                  sdata.cstr(), sdata.size(), true, do_append);
 }  }
   
Line 208  static void _offset(Request& r, const St Line 219  static void _offset(Request& r, const St
         Pool& pool=r.pool();          Pool& pool=r.pool();
         Table& table=static_cast<VTable *>(r.self)->table();          Table& table=static_cast<VTable *>(r.self)->table();
         if(params->size()) {          if(params->size()) {
                 Value& offset_expr=params->as_junction(0, "offset must be expression");                  bool absolute=false;
                 table.shift(r.process(offset_expr).as_int());                  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 {          } else {
                 Value& value=*new(pool) VInt(pool, table.current());                  Value& value=*new(pool) VInt(pool, table.current());
                 value.set_name(method_name);                  value.set_name(method_name);
Line 413  static void _flip(Request& r, const Stri Line 437  static void _flip(Request& r, const Stri
   
 static void _append(Request& r, const String& method_name, MethodParams *params) {  static void _append(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         // data is last parameter          // data
           Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
         const String& string=          const String& string=
                 r.process(params->as_junction(0, "body must be code")).as_string();                  r.process(params->as_junction(0, "body must be code")).as_string();
   
         // parse cells          // parse cells
         Array& row=*new(pool) Array(pool);          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<VTable *>(r.self);          VTable& vtable=*static_cast<VTable *>(r.self);
         vtable.table()+=&row;          vtable.table()+=&row;
Line 446  static void _join(Request& r, const Stri Line 471  static void _join(Request& r, const Stri
                 for(int src_row=0; src_row<src.size(); src_row++) {                  for(int src_row=0; src_row<src.size(); src_row++) {
                         src.set_current(src_row);                          src.set_current(src_row);
                         Array& dest_row=*new(pool) Array(pool);                          Array& dest_row=*new(pool) Array(pool);
                         for(int dest_column=0; dest_column<dest_columns->size(); dest_column++)                           for(int dest_column=0; dest_column<dest_columns->size(); dest_column++) {
                                 dest_row+=src.item(*dest_columns->get_string(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;                          dest+=&dest_row;
                 }                  }
                 src.set_current(saved_src_current);                  src.set_current(saved_src_current);
Line 467  public: Line 494  public:
                 statement_string(astatement_string),                  statement_string(astatement_string),
                 statement_cstr(astatement_cstr),                  statement_cstr(astatement_cstr),
                 columns(*new(pool) Array(pool)),                  columns(*new(pool) Array(pool)),
                 row(0), row_index(0),                  row(0), 
                 table(0)                  table(0)
         {          {
         }          }
Line 490  public: Line 517  public:
                 if(size)                  if(size)
                         cell->APPEND_TAINTED(                          cell->APPEND_TAINTED(
                                 (const char *)ptr, size,                                   (const char *)ptr, size, 
                                 statement_cstr, row_index++);                                  statement_cstr, table->size()-1);
                 (*row)+=cell;                  (*row)+=cell;
         }          }
   
Line 500  private: Line 527  private:
         const String& statement_string; const char *statement_cstr;          const String& statement_string; const char *statement_cstr;
         Array& columns;          Array& columns;
         Array *row;          Array *row;
         uint row_index;  
 public:  public:
         Table *table;          Table *table;
 };  };
Line 508  public: Line 534  public:
 static void _sql(Request& r, const String& method_name, MethodParams *params) {  static void _sql(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          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");          Value& statement=params->as_junction(0, "statement must be code");
   
         ulong limit=0;          ulong limit=0;
Line 534  static void _sql(Request& r, const Strin Line 555  static void _sql(Request& r, const Strin
         Temp_lang temp_lang(r, String::UL_SQL);          Temp_lang temp_lang(r, String::UL_SQL);
         const String& statement_string=r.process(statement).as_string();          const String& statement_string=r.process(statement).as_string();
         const char *statement_cstr=          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,          Table_sql_event_handlers handlers(pool, method_name,
                 statement_string, statement_cstr);                  statement_string, statement_cstr);
         try {          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,                           statement_cstr, offset, limit, 
                         handlers);                          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          } catch(const Exception& e) { // query problem
                 // more specific source [were url]                  // more specific source [were url]
                 throw Exception(e.type(), e.code(),                   throw Exception(e.type(), e.code(), 
Line 583  static void _columns(Request& r, const S Line 620  static void _columns(Request& r, const S
 MTable::MTable(Pool& apool) : Methoded(apool) {  MTable::MTable(Pool& apool) : Methoded(apool) {
         set_name(*NEW String(pool(), TABLE_CLASS_NAME));          set_name(*NEW String(pool(), TABLE_CLASS_NAME));
   
         // ^table:set{data}          // ^table::create{data}
         // ^table:set[nameless]{data}          // ^table::create[nameless]{data}
         add_native_method("set", Method::CT_DYNAMIC, _set, 1, 2);          // ^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[file]  
         // ^table:load[nameless;file]          // ^table::load[nameless;file]
         add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);          add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2);
   
         // ^table.save[file]            // ^table.save[file]  
Line 602  MTable::MTable(Pool& apool) : Methoded(a Line 642  MTable::MTable(Pool& apool) : Methoded(a
         add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);          add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);
   
         // ^table.offset[]            // ^table.offset[]  
         // ^table.offset[offset]          // ^table.offset(offset)
         add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 1);          // ^table.offset[cur|set](offset)
           add_native_method("offset", Method::CT_DYNAMIC, _offset, 0, 2);
   
         // ^table.menu{code}            // ^table.menu{code}  
         // ^table.menu{code}[delim]          // ^table.menu{code}[delim]

Removed from v.1.129  
changed lines
  Added in v.1.142


E-mail: