--- parser3/src/classes/table.C 2011/05/15 13:20:07 1.280 +++ parser3/src/classes/table.C 2011/11/19 04:15:31 1.282 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_TABLE_C="$Date: 2011/05/15 13:20:07 $"; +static const char * const IDENT_TABLE_C="$Date: 2011/11/19 04:15:31 $"; #if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4)) #include @@ -471,20 +471,16 @@ static void _save(Request& r, MethodPara output_column_names=false; TableSeparators separators; - if(param_indexcount()) - 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_indexcount()) + throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } - } + if(param_index1) { - Value& voptions=params.as_no_junction(1, "options must be hash, not code"); - if(voptions.is_defined() && !voptions.is_string()) - if(HashStringValue* options=voptions.get_hash()) { - int valid_options=0; - if(Value* vbind=options->get(sql_bind_name)) { - valid_options++; - bind=vbind->get_hash(); - } - if(Value* vlimit=options->get(sql_limit_name)) { - valid_options++; - limit=(ulong)r.process_to_value(*vlimit).as_double(); - } - if(Value* voffset=options->get(sql_offset_name)) { - 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); - } else - throw Exception(PARSER_RUNTIME, 0, OPTIONS_MUST_BE_HASH); - } + if(params.count()>1) + if(HashStringValue* options=params.as_hash(1)) { + int valid_options=0; + if(Value* vbind=options->get(sql_bind_name)) { + valid_options++; + bind=vbind->get_hash(); + } + if(Value* vlimit=options->get(sql_limit_name)) { + valid_options++; + limit=(ulong)r.process_to_value(*vlimit).as_double(); + } + if(Value* voffset=options->get(sql_offset_name)) { + 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); + } SQL_Driver::Placeholder* placeholders=0; uint placeholders_count=0; @@ -1255,63 +1247,60 @@ static void _select(Request& r, MethodPa Value& vcondition=params.as_expression(0, "condition must be number, bool or expression"); Table& source_table=GET_SELF(r, VTable).table(); - Table& result_table=*new Table(source_table.columns()); int limit=source_table.count(); - int offset = 0; - bool reverse = false; + int offset=0; + bool reverse=false; - if(params.count()>1) { - Value& voptions=params.as_no_junction(1, "options must be hash, not code"); - if(voptions.is_defined() && !voptions.is_string()) - if(HashStringValue* options=voptions.get_hash()) { - int valid_options=0; - if(Value* vlimit=options->get(sql_limit_name)) { - valid_options++; - limit=r.process_to_value(*vlimit).as_int(); - } - if(Value* voffset=options->get(sql_offset_name)) { - valid_options++; - offset=r.process_to_value(*voffset).as_int(); - } - if(Value* vreverse=options->get(table_reverse_name)) { - valid_options++; - reverse=r.process_to_value(*vreverse).as_bool(); - } - if(valid_options!=options->count()) - throw Exception(PARSER_RUNTIME, - 0, - "called with invalid option"); - } else + if(params.count()>1) + if(HashStringValue* options=params.as_hash(1)) { + int valid_options=0; + if(Value* vlimit=options->get(sql_limit_name)) { + valid_options++; + limit=r.process_to_value(*vlimit).as_int(); + } + if(Value* voffset=options->get(sql_offset_name)) { + valid_options++; + offset=r.process_to_value(*voffset).as_int(); + } + if(Value* vreverse=options->get(table_reverse_name)) { + valid_options++; + reverse=r.process_to_value(*vreverse).as_bool(); + } + if(valid_options!=options->count()) 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); + "called with invalid option"); + } - 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 - result_table+=source_table[row]; - } - } else { - for(int row=0; row < size && result_table.count() < limit; row++) { - source_table.set_current(row); + if(size_t size=source_table.count() && limit>0){ + size_t saved_current=source_table.current(); + 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 - result_table+=source_table[row]; + if(condition && ++appended > (size_t)offset) // ...condition is true, adding to the result + 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)); }