Diff for /parser3/src/classes/table.C between versions 1.124 and 1.136

version 1.124, 2001/10/09 13:17:45 version 1.136, 2001/12/15 21:28:18
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 38  public: // Methoded Line 37  public: // Methoded
 static void _set(Request& r, const String& method_name, MethodParams *params) {  static void _set(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         // data is last parameter          // 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);          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 80  static void _set(Request& r, const Strin Line 78  static void _set(Request& r, const Strin
 static void _load(Request& r, const String& method_name, MethodParams *params) {  static void _load(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         // filename is last parameter          // 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");                  "file name must not be code");
   
         // loading text          // 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          // parse columns
         Array *columns;          Array *columns;
Line 129  static void _load(Request& r, const Stri Line 127  static void _load(Request& r, const Stri
         static_cast<VTable *>(r.self)->set_table(table);          static_cast<VTable *>(r.self)->set_table(table);
 }  }
   
   /// @todo "z\nz" "zzz""zz"
 static void _save(Request& r, const String& method_name, MethodParams *params) {  static void _save(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          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");                  "file name must not be code");
   
         Table& table=static_cast<VTable *>(r.self)->table();          Table& table=static_cast<VTable *>(r.self)->table();
   
           bool do_append=false;
         String sdata(pool);          String sdata(pool);
         if(params->size()==1) { // not nameless=named output          if(params->size()==1) { // named output
                 // write out names line                  // write out names line
                 if(table.columns()) { // named table                  if(table.columns()) { // named table
                         Array_iter i(*table.columns());                          Array_iter i(*table.columns());
Line 158  static void _save(Request& r, const Stri Line 158  static void _save(Request& r, const Stri
                                 sdata.APPEND_CONST("empty nameless table");                                  sdata.APPEND_CONST("empty nameless table");
                 }                  }
                 sdata.APPEND_CONST("\n");                  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          // data lines
         Array_iter i(table);          Array_iter i(table);
Line 173  static void _save(Request& r, const Stri Line 184  static void _save(Request& r, const Stri
         }          }
   
         // write          // write
         file_write(pool, r.absolute(vtable_name.as_string()),           file_write(pool, r.absolute(vfile_name.as_string()), 
                 sdata.cstr(), sdata.size(), true);                  sdata.cstr(), sdata.size(), true, do_append);
 }  }
   
 static void _count(Request& r, const String& method_name, MethodParams *) {  static void _count(Request& r, const String& method_name, MethodParams *) {
Line 195  static void _offset(Request& r, const St Line 206  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 244  static void table_row_to_hash(Array::Ite Line 268  static void table_row_to_hash(Array::Ite
   
         if(ri.key_field<row.size()) {          if(ri.key_field<row.size()) {
                 VHash& result=*new(pool) VHash(pool);                  VHash& result=*new(pool) VHash(pool);
                 Hash& hash=*result.get_hash();                  Hash& hash=*result.get_hash(0);
                 for(int i=0; i<ri.value_fields->size(); i++) {                  for(int i=0; i<ri.value_fields->size(); i++) {
                         int value_field=ri.value_fields->get_int(i);                          int value_field=ri.value_fields->get_int(i);
                         if(value_field<row.size())                          if(value_field<row.size())
Line 278  static void _hash(Request& r, const Stri Line 302  static void _hash(Request& r, const Stri
                                                 value_fields+=self_table.column_name2index(value_field_name, true);                                                  value_fields+=self_table.column_name2index(value_field_name, true);
                                         }                                          }
                                 } else                                  } else
                                         PTHROW(0, 0,                                          throw Exception(0, 0,
                                                 &method_name,                                                  &method_name,
                                                 "value field(s) must be string or self_table"                                                  "value field(s) must be string or self_table"
                                         );                                          );
Line 288  static void _hash(Request& r, const Stri Line 312  static void _hash(Request& r, const Stri
                         }                          }
   
                         // integers: key_field & value_fields                          // integers: key_field & value_fields
                         Row_info row_info={&self_table, key_field, &value_fields, result.get_hash()};                          Row_info row_info={&self_table, key_field, &value_fields, result.get_hash(0)};
                         self_table.for_each(table_row_to_hash, &row_info);                          self_table.for_each(table_row_to_hash, &row_info);
                 }                  }
         result.set_name(method_name);          result.set_name(method_name);
Line 390  static void _flip(Request& r, const Stri Line 414  static void _flip(Request& r, const Stri
                                 Array& new_row=*new(pool) Array(pool, old_table.size());                                  Array& new_row=*new(pool) Array(pool, old_table.size());
                                 for(int i=0; i<old_table.size(); i++) {                                  for(int i=0; i<old_table.size(); i++) {
                                         const Array& old_row=old_table.at(i);                                          const Array& old_row=old_table.at(i);
                                         new_row+=column<old_row.size()?old_row.get(column):empty_string;                                          new_row+=column<old_row.size()?old_row.get(column):new(pool) String(pool);
                                 }                                  }
                                 new_table+=&new_row;                                  new_table+=&new_row;
                         }                          }
Line 400  static void _flip(Request& r, const Stri Line 424  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();
   
Line 417  static void _join(Request& r, const Stri Line 442  static void _join(Request& r, const Stri
   
         Table *maybe_src=params->as_no_junction(0, "table ref must not be code").get_table();          Table *maybe_src=params->as_no_junction(0, "table ref must not be code").get_table();
         if(!maybe_src)          if(!maybe_src)
                 PTHROW(0, 0,                   throw Exception(0, 0, 
                         &method_name,                           &method_name, 
                         "source is not a table");                          "source is not a table");
   
         Table& src=*maybe_src;          Table& src=*maybe_src;
         Table& dest=static_cast<VTable *>(r.self)->table();          Table& dest=static_cast<VTable *>(r.self)->table();
         if(&src == &dest)          if(&src == &dest)
                 PTHROW(0, 0,                   throw Exception(0, 0, 
                         &method_name,                           &method_name, 
                         "source and destination are same table");                          "source and destination are same table");
   
Line 454  public: Line 479  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 477  public: Line 502  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 487  private: Line 512  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 496  static void _sql(Request& r, const Strin Line 520  static void _sql(Request& r, const Strin
         Pool& pool=r.pool();          Pool& pool=r.pool();
   
         if(!r.connection)          if(!r.connection)
                 PTHROW(0, 0,                   throw Exception(0, 0, 
                         &method_name,                           &method_name, 
                         "without connect");                          "without connect");
   
Line 507  static void _sql(Request& r, const Strin Line 531  static void _sql(Request& r, const Strin
         if(params->size()>1) {          if(params->size()>1) {
                 Value& voptions=params->as_no_junction(1, "options must be hash, not code");                  Value& voptions=params->as_no_junction(1, "options must be hash, not code");
                 if(voptions.is_defined())                  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))                                  if(Value *vlimit=(Value *)options->get(*sql_limit_name))
                                         limit=(ulong)r.process(*vlimit).as_double();                                          limit=(ulong)r.process(*vlimit).as_double();
                                 if(Value *voffset=(Value *)options->get(*sql_offset_name))                                  if(Value *voffset=(Value *)options->get(*sql_offset_name))
                                         offset=(ulong)r.process(*voffset).as_double();                                          offset=(ulong)r.process(*voffset).as_double();
                         } else                          } else
                                 PTHROW(0, 0,                                  throw Exception(0, 0,
                                         &method_name,                                          &method_name,
                                         "options must be hash");                                          "options must be hash");
         }          }
Line 524  static void _sql(Request& r, const Strin Line 548  static void _sql(Request& r, const Strin
                 statement_string.cstr(String::UL_UNSPECIFIED, r.connection);                  statement_string.cstr(String::UL_UNSPECIFIED, r.connection);
         Table_sql_event_handlers handlers(pool, method_name,          Table_sql_event_handlers handlers(pool, method_name,
                 statement_string, statement_cstr);                  statement_string, statement_cstr);
         bool need_rethrow=false; Exception rethrow_me;          try {
                 PTRY {  #ifdef RESOURCES_DEBUG
           struct timeval mt[2];
           //measure:before
           gettimeofday(&mt[0],NULL);
   #endif  
                 r.connection->query(                  r.connection->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
                   // 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=          Table *result=
                 handlers.table?handlers.table: // query resulted in table? return it                  handlers.table?handlers.table: // query resulted in table? return it
Line 593  MTable::MTable(Pool& apool) : Methoded(a Line 629  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.124  
changed lines
  Added in v.1.136


E-mail: