Diff for /parser3/src/classes/table.C between versions 1.307 and 1.314

version 1.307, 2015/07/28 21:23:38 version 1.314, 2016/07/28 21:48:57
Line 1 Line 1
 /** @file  /** @file
         Parser: @b table parser class.          Parser: @b table parser class.
   
         Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)          Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
         Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)          Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
 */  */
   
Line 35  public: Line 35  public:
   
 // global variable  // global variable
   
 DECLARE_CLASS_VAR(table, new MTable, 0);  DECLARE_CLASS_VAR(table, new MTable);
   
 #define TABLE_REVERSE_NAME "reverse"  #define TABLE_REVERSE_NAME "reverse"
   
Line 656  static void _count(Request& r, MethodPar Line 656  static void _count(Request& r, MethodPar
         if(params.count()) {          if(params.count()) {
                 const String& param=params.as_string(0, PARAMETER_MUST_BE_STRING);                  const String& param=params.as_string(0, PARAMETER_MUST_BE_STRING);
                 if(param == "columns")                  if(param == "columns")
                         result = table.columns() ? table.columns()->count() : 0;                          result = table.columns() ? table.columns()->count() : table.max_cells();
                 else if(param == "cells")                  else if(param == "cells")
                         result = table.count() ? table[table.current()]->count() : 0;                          result = table.count() ? table[table.current()]->count() : 0;
                 else if(param == "rows") // synonim for ^table.count[]                  else if(param == "rows") // synonim for ^table.count[]
Line 781  static void table_row_to_hash(Table::ele Line 781  static void table_row_to_hash(Table::ele
                         for(Array_iterator<int> i(*info->value_fields); i.has_next(); ) {                          for(Array_iterator<int> i(*info->value_fields); i.has_next(); ) {
                                 size_t value_field=i.next();                                  size_t value_field=i.next();
                                 if(value_field<row->count())                                  if(value_field<row->count())
                                         hash.put(                                          hash.put(*info->table->columns()->get(value_field), new VString(*row->get(value_field)));
                                                 *info->table->columns()->get(value_field),   
                                                 new VString(*row->get(value_field)));  
                         }                          }
   
                         exist=info->hash->put_dont_replace(*key, vhash);                          exist=info->hash->put_dont_replace(*key, vhash);
Line 804  static void table_row_to_hash(Table::ele Line 802  static void table_row_to_hash(Table::ele
                                 table=new Table(*info->table, table_options/*no rows, just structure*/);                                  table=new Table(*info->table, table_options/*no rows, just structure*/);
                                 info->hash->put(*key, new VTable(table));                                  info->hash->put(*key, new VTable(table));
                         }                          }
                         *table+=row;                          Table::element_type row_copy(new ArrayString(row->count()));
                           row_copy->append(*row);
                           *table+=row_copy;
                         break;                          break;
                 }                  }
         }          }
         if(exist && info->distinct==D_ILLEGAL)          if(exist && info->distinct==D_ILLEGAL)
                 throw Exception(PARSER_RUNTIME,                  throw Exception(PARSER_RUNTIME, key, "duplicate key");
                         key,  
                         "duplicate key");  
 }  }
   
 Table2hash_value_type get_value_type(Value& vvalue_type){  Table2hash_value_type get_value_type(Value& vvalue_type){
Line 824  Table2hash_value_type get_value_type(Val Line 822  Table2hash_value_type get_value_type(Val
                 } else if (svalue_type == "hash") {                  } else if (svalue_type == "hash") {
                         return C_HASH;                          return C_HASH;
                 } else {                  } else {
                         throw Exception(PARSER_RUNTIME,                          throw Exception(PARSER_RUNTIME, &svalue_type, "must be 'hash', 'table' or 'string'");
                                 &svalue_type,  
                                 "must be 'hash', 'table' or 'string'");  
                 }                  }
         } else {          } else {
                 throw Exception(PARSER_RUNTIME,                  throw Exception(PARSER_RUNTIME, 0, "'type' must be string");
                         0,  
                         "'type' must be hash");  
         }          }
 }  }
   
Line 856  static void _hash(Request& r, MethodPara Line 850  static void _hash(Request& r, MethodPara
                                                                 value_type=C_TABLE;                                                                  value_type=C_TABLE;
                                                                 distinct=D_FIRST;                                                                  distinct=D_FIRST;
                                                         } else {                                                          } else {
                                                                 throw Exception(PARSER_RUNTIME,                                                                  throw Exception(PARSER_RUNTIME, &sdistinct, "must be 'tables' or true/false");
                                                                         &sdistinct,  
                                                                         "must be 'tables' or true/false");  
                                                         }                                                          }
                                                 } else {                                                  } else {
                                                         distinct=vdistinct_value.as_bool()?D_FIRST:D_ILLEGAL;                                                          distinct=vdistinct_value.as_bool()?D_FIRST:D_ILLEGAL;
Line 866  static void _hash(Request& r, MethodPara Line 858  static void _hash(Request& r, MethodPara
                                         }                                          }
                                         if(Value* vvalue_type_code=options->get(sql_value_type_name)) { // $.type ?                                          if(Value* vvalue_type_code=options->get(sql_value_type_name)) { // $.type ?
                                                 if(value_type==C_TABLE) // $.distinct[tables] already was specified                                                  if(value_type==C_TABLE) // $.distinct[tables] already was specified
                                                         throw Exception(PARSER_RUNTIME,                                                          throw Exception(PARSER_RUNTIME, 0, "you can't specify $.distinct[tables] and $.type[] together");
                                                                 0,  
                                                                 "you can't specify $.distinct[tables] and $.type[] together");  
   
                                                 valid_options++;                                                  valid_options++;
                                                 value_type=get_value_type(r.process_to_value(*vvalue_type_code));                                                  value_type=get_value_type(r.process_to_value(*vvalue_type_code));
                                         }                                          }
Line 885  static void _hash(Request& r, MethodPara Line 874  static void _hash(Request& r, MethodPara
                         Array<int> value_fields;                          Array<int> value_fields;
                         if(param_index==0){ // list of columns wasn't specified                          if(param_index==0){ // list of columns wasn't specified
                                 if(value_type==C_STRING) // $.type[string]                                  if(value_type==C_STRING) // $.type[string]
                                         throw Exception(PARSER_RUNTIME,                                          throw Exception(PARSER_RUNTIME, 0, "you must specify one value field with option $.type[string]");
                                                 0,  
                                                 "you must specify one value field with option $.type[string]");  
                                                                   
                                 for(size_t i=0; i<columns->count(); i++) // by all columns, including key                                  for(size_t i=0; i<columns->count(); i++) // by all columns, including key
                                         value_fields+=i;                                          value_fields+=i;
   
                         } else { // list of columns was specified                          } else { // list of columns was specified
                                 if(value_type==C_TABLE)                                  if(value_type==C_TABLE)
                                         throw Exception(PARSER_RUNTIME,                                          throw Exception(PARSER_RUNTIME, 0, "you can't specify value field(s) with option $.distinct[tables] or $.type[tables]");
                                                 0,  
                                                 "you can't specify value field(s) with option $.distinct[tables] or $.type[tables]");  
   
                                 Value& value_fields_param=params.as_no_junction(param_index, "value field(s) must not be code");                                  Value& value_fields_param=params.as_no_junction(param_index, "value field(s) must not be code");
                                 if(value_fields_param.is_string()) { // one column as string was specified                                  if(value_fields_param.is_string()) { // one column as string was specified
Line 907  static void _hash(Request& r, MethodPara Line 892  static void _hash(Request& r, MethodPara
                                                 value_fields +=self_table.column_name2index(value_field_name, true);                                                  value_fields +=self_table.column_name2index(value_field_name, true);
                                         }                                          }
                                 } else                                  } else
                                         throw Exception(PARSER_RUNTIME,                                          throw Exception(PARSER_RUNTIME, 0, "value field(s) must be string or table");
                                                 0,  
                                                 "value field(s) must be string or table");  
                         }                          }
   
                         if(value_type==C_STRING && value_fields.count()!=1)                          if(value_type==C_STRING && value_fields.count()!=1)
                                 throw Exception(PARSER_RUNTIME,                                  throw Exception(PARSER_RUNTIME, 0, "you can specify only one value field with option $.type[string]");
                                         0,  
                                         "you can specify only one value field with option $.type[string]");  
   
                         {                          {
                                 Value* key_param=&params[0];                                  Value* key_param=&params[0];
Line 930  static void _hash(Request& r, MethodPara Line 911  static void _hash(Request& r, MethodPara
                                         /*row=*/0,                                          /*row=*/0,
                                         value_type                                          value_type
                                 };                                  };
                                 info.key_field=(info.key_code?-1                                  info.key_field=(info.key_code ? -1 : self_table.column_name2index(key_param->as_string(), true));
                                                 :self_table.column_name2index(key_param->as_string(), true));  
   
                                 int saved_current=self_table.current();                                  int saved_current=self_table.current();
                                 self_table.for_each(table_row_to_hash, &info);                                  self_table.for_each(table_row_to_hash, &info);
Line 1060  static void _flip(Request& r, MethodPara Line 1040  static void _flip(Request& r, MethodPara
         Table& old_table=GET_SELF(r, VTable).table();          Table& old_table=GET_SELF(r, VTable).table();
         Table& new_table=*new Table(0);          Table& new_table=*new Table(0);
         if(size_t old_count=old_table.count())          if(size_t old_count=old_table.count())
                 if(size_t old_cols=old_table.columns()?old_table.columns()->count():old_table[0]->count())                   if(size_t old_cols=old_table.columns()?old_table.columns()->count():old_table.max_cells())
                         for(size_t column=0; column<old_cols; column++) {                          for(size_t column=0; column<old_cols; column++) {
                                 Table::element_type new_row(new ArrayString(old_count));                                  Table::element_type new_row(new ArrayString(old_count));
                                 for(size_t i=0; i<old_count; i++) {                                  for(size_t i=0; i<old_count; i++) {
Line 1137  static void _foreach(Request& r, MethodP Line 1117  static void _foreach(Request& r, MethodP
         table.set_current(saved_current);          table.set_current(saved_current);
 }  }
   
 static void _append(Request& r, MethodParams& params) {  static void update_cell(HashStringValue::key_type aname, HashStringValue::value_type avalue, VTable *dest) {
           dest->put_element(String(aname, String::L_CLEAN), avalue); // new not required
   }
   
   inline Table::element_type row_from_string(Request& r, Value &param){
           if(!param.is_string() && !param.get_junction())
                   throw Exception(PARSER_RUNTIME, 0, "row must be string, code or hash");
   
         Temp_lang temp_lang(r, String::L_PASS_APPENDED);          Temp_lang temp_lang(r, String::L_PASS_APPENDED);
         const String& string=r.process_to_string(params[0]);          const String& string=r.process_to_string(param);
   
         // parse cells          // parse cells
         Table::element_type row=new ArrayString;          Table::element_type row=new ArrayString;
         size_t pos_after=0;          size_t pos_after=0;
         string.split(*row, pos_after, "\t", String::L_AS_IS);          string.split(*row, pos_after, "\t", String::L_AS_IS);
   
         GET_SELF(r, VTable).table()+=row;          return row;
 }  }
   
 static void _insert(Request& r, MethodParams& params) {  static void _append(Request& r, MethodParams& params) {
         Temp_lang temp_lang(r, String::L_PASS_APPENDED);          VTable vtable=GET_SELF(r, VTable);
         const String& string=r.process_to_string(params[0]);          Table& table=vtable.table();
   
         // parse cells          HashStringValue* hash=params[0].get_hash();
         Table::element_type row=new ArrayString;          if(hash){
         size_t pos_after=0;                  table+=new ArrayString();
         string.split(*row, pos_after, "\t", String::L_AS_IS);                  size_t saved_current=table.current();
                   table.set_current(table.count()-1);
                   hash->for_each<VTable*>(update_cell, &vtable);
                   table.set_current(saved_current);
           } else {
                   table+=row_from_string(r, params[0]);
           }
   }
   
         Table& table=GET_SELF(r, VTable).table();  static void _insert(Request& r, MethodParams& params) {
         table.insert(table.current(), row);          VTable vtable=GET_SELF(r, VTable);
           Table& table=vtable.table();
           HashStringValue* hash=params[0].get_hash();
           if(hash){
                   table.insert(table.current(), new ArrayString());
                   hash->for_each<VTable*>(update_cell, &vtable);
           } else {
                   table.insert(table.current(), row_from_string(r, params[0]));
           }
 }  }
   
 static void _delete(Request& r, MethodParams& params) {  static void _delete(Request& r, MethodParams&) {
         Table& table=GET_SELF(r, VTable).table();          Table& table=GET_SELF(r, VTable).table();
         table.remove_current();          table.remove_current();
 }  }

Removed from v.1.307  
changed lines
  Added in v.1.314


E-mail: