Diff for /parser3/src/classes/table.C between versions 1.280 and 1.288

version 1.280, 2011/05/15 13:20:07 version 1.288, 2012/06/08 11:44:02
Line 1 Line 1
 /** @file  /** @file
         Parser: @b table parser class.          Parser: @b table parser class.
   
         Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001-2012 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)
 */  */
   
 static const char * const IDENT_TABLE_C="$Date$";  
   
 #if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4))  #if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4))
 #include <sstream>  #include <sstream>
 #endif  #endif
Line 23  static const char * const IDENT_TABLE_C= Line 21  static const char * const IDENT_TABLE_C=
 #include "pa_vbool.h"  #include "pa_vbool.h"
 #include "pa_array.h"  #include "pa_array.h"
   
   volatile const char * IDENT_TABLE_C="$Id$";
   
 // class  // class
   
 class MTable: public Methoded {  class MTable: public Methoded {
Line 56  static Table::Action_options get_action_ Line 56  static Table::Action_options get_action_
         if(params.count() <= options_index)          if(params.count() <= options_index)
                 return result;                  return result;
   
         Value& maybe_options=params[options_index];          HashStringValue* options=params.as_hash(options_index);
 /* can not do it:   
         want to enable ^table::create[$source;  
 #               $.option[]  
         ]  
         but there is ^table.locate[name;value]  
   
         ...if(voptions.is_defined() && !voptions.is_string()))  
         if(maybe_options.is_string()) { // allow empty options  
                 result.defined=true;  
                 return result;  
         }  
 */  
         HashStringValue* options=maybe_options.get_hash();  
         if(!options)          if(!options)
                 return result;                  return result;
   
Line 179  static void _create(Request& r, MethodPa Line 166  static void _create(Request& r, MethodPa
         size_t options_param_index=data_param_index+1;          size_t options_param_index=data_param_index+1;
         if(          if(
                 options_param_index<params.count()                  options_param_index<params.count()
                 && (options=params.as_no_junction(options_param_index, "additional params must be hash").get_hash())                  && (options=params.as_hash(options_param_index))
         ) {          ) {
                 // cloning, so that we could change                  // cloning, so that we could change
                 options=new HashStringValue(*options);                  options=new HashStringValue(*options);
Line 209  static void _create(Request& r, MethodPa Line 196  static void _create(Request& r, MethodPa
                 data.split(head, raw_pos_after, "\n", String::L_AS_IS, 1);                  data.split(head, raw_pos_after, "\n", String::L_AS_IS, 1);
                 if(head.count()) {                  if(head.count()) {
                         size_t col_pos_after=0;                          size_t col_pos_after=0;
                         head[0]->split(*columns, col_pos_after, *separators.scolumn, String::L_AS_IS);                          if(head[0]->is_empty())
                                   *columns += new String();
                           else
                                   head[0]->split(*columns, col_pos_after, *separators.scolumn, String::L_AS_IS);
                 }                  }
         }          }
   
Line 344  static void _load(Request& r, MethodPara Line 334  static void _load(Request& r, MethodPara
         HashStringValue *options=0;          HashStringValue *options=0;
         TableSeparators separators;          TableSeparators separators;
         if(options_param_index<params.count()          if(options_param_index<params.count()
                 && (options=params.as_no_junction(options_param_index, "additional params must be hash").get_hash())) {                  && (options=params.as_hash(options_param_index))) {
                 // cloning, so that we could change [to simplify checks on params inside file_read_text                  // cloning, so that we could change [to simplify checks on params inside file_read_text
                 options=new HashStringValue(*options);                  options=new HashStringValue(*options);
                 separators.load(*options);                  separators.load(*options);
Line 471  static void _save(Request& r, MethodPara Line 461  static void _save(Request& r, MethodPara
                 output_column_names=false;                  output_column_names=false;
   
         TableSeparators separators;          TableSeparators separators;
         if(param_index<params.count()) {          if(param_index<params.count())
                 Value& voptions=params.as_no_junction(param_index++, "additional params must be hash");                  if(HashStringValue* options=params.as_hash(param_index++)) {
                 if( voptions.is_defined() && !voptions.is_string() ) {                          int valid_options=separators.load(*options);
                         if(HashStringValue* options=voptions.get_hash()) {                          if(valid_options!=options->count())
                                 int valid_options=separators.load(*options);                                  throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                                 if(valid_options!=options->count())  
                                         throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);  
                         } else {  
                                 throw Exception(PARSER_RUNTIME,  
                                         0,  
                                         "additional params must be hash (did you spell mode parameter correctly?)");  
                         }  
                 }                  }
         }  
         if(param_index<params.count())          if(param_index<params.count())
                 throw Exception(PARSER_RUNTIME,                  throw Exception(PARSER_RUNTIME,
                         0,                          0,
Line 783  static void _hash(Request& r, MethodPara Line 766  static void _hash(Request& r, MethodPara
                         Table2hash_value_type value_type=C_HASH;                          Table2hash_value_type value_type=C_HASH;
                         int param_index=params.count()-1;                          int param_index=params.count()-1;
                         if(param_index>0) {                          if(param_index>0) {
                                 if(HashStringValue* options=params.as_no_junction(param_index, PARAM_MUST_NOT_BE_CODE).get_hash()){ // options where specified                                  if(HashStringValue* options=params.as_hash(param_index)){ // options where specified
                                         --param_index;                                          --param_index;
                                         int valid_options=0;                                          int valid_options=0;
                                         if(Value* vdistinct_code=options->get(sql_distinct_name)) { // $.distinct ?                                          if(Value* vdistinct_code=options->get(sql_distinct_name)) { // $.distinct ?
Line 818  static void _hash(Request& r, MethodPara Line 801  static void _hash(Request& r, MethodPara
                                 }                                  }
                         }                          }
   
                         if(param_index==2) // options was specified but not as hash  
                                 throw Exception(PARSER_RUNTIME, 0, OPTIONS_MUST_BE_HASH);  
   
                         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]
Line 928  static void _sort(Request& r, MethodPara Line 908  static void _sort(Request& r, MethodPara
                 old_table.set_current(i);                  old_table.set_current(i);
                 // calculate key value                  // calculate key value
                 seq[i].row=old_table[i];                  seq[i].row=old_table[i];
                 Value& value=r.process_to_value(key_maker).as_expr_result(true/*return string as-is*/);                  Value& value=r.process_to_value(key_maker);
                 if(i==0) // determining key values type by first one                  if(i==0) // determining key values type by first one
                         key_values_are_strings=value.is_string();                          key_values_are_strings=value.is_string();
   
                 if(key_values_are_strings)                  if(key_values_are_strings)
                         seq[i].value.c_str=value.as_string().cstr();                          seq[i].value.c_str=value.as_string().cstr();
                 else                  else
                         seq[i].value.d=value.as_double();                          seq[i].value.d=value.as_expr_result().as_double();
         }          }
   
         // @todo: handle this elsewhere          // @todo: handle this elsewhere
Line 1043  static void join_nameless_row(Table& src Line 1023  static void join_nameless_row(Table& src
         *dest+=src[src.current()];          *dest+=src[src.current()];
 }  }
 static void _join(Request& r, MethodParams& params) {  static void _join(Request& r, MethodParams& params) {
         Table* maybe_src=params.as_no_junction(0, "table ref must not be code").get_table();          Table& src=*params.as_table(0, "source");
         if(!maybe_src)  
                 throw Exception(PARSER_RUNTIME,   
                         0,   
                         "source is not a table");  
         Table& src=*maybe_src;  
   
         Table::Action_options o=get_action_options(r, params, 1, src);          Table::Action_options o=get_action_options(r, params, 1, src);
         check_option_param(o.defined, params, 1,          check_option_param(o.defined, params, 1, "invalid extra parameter");
                 "invalid extra parameter");  
   
         Table& dest=GET_SELF(r, VTable).table();          Table& dest=GET_SELF(r, VTable).table();
         if(&src == &dest)          if(&src == &dest)
Line 1161  static void _sql(Request& r, MethodParam Line 1135  static void _sql(Request& r, MethodParam
         HashStringValue* bind=0;          HashStringValue* bind=0;
         ulong limit=SQL_NO_LIMIT;          ulong limit=SQL_NO_LIMIT;
         ulong offset=0;          ulong offset=0;
         if(params.count()>1) {          if(params.count()>1)
                 Value& voptions=params.as_no_junction(1, "options must be hash, not code");                  if(HashStringValue* options=params.as_hash(1, "sql options")) {
                 if(voptions.is_defined() && !voptions.is_string())                          int valid_options=0;
                         if(HashStringValue* options=voptions.get_hash()) {                          if(Value* vbind=options->get(sql_bind_name)) {
                                 int valid_options=0;                                  valid_options++;
                                 if(Value* vbind=options->get(sql_bind_name)) {                                  bind=vbind->get_hash();
                                         valid_options++;                          }
                                         bind=vbind->get_hash();                          if(Value* vlimit=options->get(sql_limit_name)) {
                                 }                                  valid_options++;
                                 if(Value* vlimit=options->get(sql_limit_name)) {                                  limit=(ulong)r.process_to_value(*vlimit).as_double();
                                         valid_options++;                          }
                                         limit=(ulong)r.process_to_value(*vlimit).as_double();                          if(Value* voffset=options->get(sql_offset_name)) {
                                 }                                  valid_options++;
                                 if(Value* voffset=options->get(sql_offset_name)) {                                  offset=(ulong)r.process_to_value(*voffset).as_double();
                                         valid_options++;                          }
                                         offset=(ulong)r.process_to_value(*voffset).as_double();                          if(valid_options!=options->count())
                                 }                                  throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                                 if(valid_options!=options->count())                  }
                                         throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);  
                         } else  
                                 throw Exception(PARSER_RUNTIME, 0, OPTIONS_MUST_BE_HASH);  
         }  
   
         SQL_Driver::Placeholder* placeholders=0;          SQL_Driver::Placeholder* placeholders=0;
         uint placeholders_count=0;          uint placeholders_count=0;
Line 1255  static void _select(Request& r, MethodPa Line 1225  static void _select(Request& r, MethodPa
         Value& vcondition=params.as_expression(0, "condition must be number, bool or expression");          Value& vcondition=params.as_expression(0, "condition must be number, bool or expression");
   
         Table& source_table=GET_SELF(r, VTable).table();          Table& source_table=GET_SELF(r, VTable).table();
         Table& result_table=*new Table(source_table.columns());  
   
         int limit=source_table.count();          int limit=source_table.count();
         int offset = 0;          int offset=0;
         bool reverse = false;          bool reverse=false;
                   
         if(params.count()>1) {          if(params.count()>1)
                 Value& voptions=params.as_no_junction(1, "options must be hash, not code");                  if(HashStringValue* options=params.as_hash(1)) {
                 if(voptions.is_defined() && !voptions.is_string())                          int valid_options=0;
                         if(HashStringValue* options=voptions.get_hash()) {                          if(Value* vlimit=options->get(sql_limit_name)) {
                                 int valid_options=0;                                  valid_options++;
                                 if(Value* vlimit=options->get(sql_limit_name)) {                                  limit=r.process_to_value(*vlimit).as_int();
                                         valid_options++;                          }
                                         limit=r.process_to_value(*vlimit).as_int();                          if(Value* voffset=options->get(sql_offset_name)) {
                                 }                                  valid_options++;
                                 if(Value* voffset=options->get(sql_offset_name)) {                                  offset=r.process_to_value(*voffset).as_int();
                                         valid_options++;                          }
                                         offset=r.process_to_value(*voffset).as_int();                          if(Value* vreverse=options->get(table_reverse_name)) {
                                 }                                  valid_options++;
                                 if(Value* vreverse=options->get(table_reverse_name)) {                                  reverse=r.process_to_value(*vreverse).as_bool();
                                         valid_options++;                          }
                                         reverse=r.process_to_value(*vreverse).as_bool();                          if(valid_options!=options->count())
                                 }                                  throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                                 if(valid_options!=options->count())                  }
                                         throw Exception(PARSER_RUNTIME,  
                                                 0,  
                                                 "called with invalid option");  
                         } else  
                                 throw Exception(PARSER_RUNTIME,  
                                         0,  
                                         "options must be hash");  
         }  
   
         int saved_current=source_table.current();  
         int size=source_table.count();  
         int appended = 0;  
   
         if(reverse){  
                 for(int row=size-1; row >=0 && result_table.count() < limit; row--) {  
                         source_table.set_current(row);  
   
                         bool condition=r.process_to_value(vcondition, false/*don't intercept string*/).as_bool();          Table& result_table=*new Table(source_table.columns());
   
                         if(condition && ++appended > offset) // ...condition is true, adding to the result          size_t size=source_table.count();
                                 result_table+=source_table[row];          if(offset<0)
                 }                  offset+=size;
         } else {          if(size && limit>0 && offset>=0 && (size_t)offset<size){
                 for(int row=0; row < size && result_table.count() < limit; row++) {                  size_t saved_current=source_table.current();
                         source_table.set_current(row);                  size_t appended=0;
   
                   if(reverse){
                           for(size_t row=size-1; result_table.count() < (size_t)limit; row--) {
                                   source_table.set_current(row);
   
                                   bool condition=r.process_to_value(vcondition, false/*don't intercept string*/).as_bool();
   
                                   if(condition && ++appended > (size_t)offset) // ...condition is true, adding to the result
                                           result_table+=source_table[row];
                                   if(row==0) break;
                           }
                   } else {
                           for(size_t row=0; row < size && result_table.count() < (size_t)limit; row++) {
                                   source_table.set_current(row);
   
                         bool condition=r.process_to_value(vcondition, false/*don't intercept string*/).as_bool();                                  bool condition=r.process_to_value(vcondition, false/*don't intercept string*/).as_bool();
   
                         if(condition && ++appended > offset) // ...condition is true, adding to the result                                  if(condition && ++appended > (size_t)offset) // ...condition is true, adding to the result
                                 result_table+=source_table[row];                                          result_table+=source_table[row];
                           }
                 }                  }
                   source_table.set_current(saved_current);
         }          }
         source_table.set_current(saved_current);  
   
         r.write_no_lang(*new VTable(&result_table));          r.write_no_lang(*new VTable(&result_table));
 }  }

Removed from v.1.280  
changed lines
  Added in v.1.288


E-mail: