--- parser3/src/classes/table.C 2010/11/06 00:05:46 1.277 +++ parser3/src/classes/table.C 2011/11/22 22:23:19 1.283 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_TABLE_C="$Date: 2010/11/06 00:05:46 $"; +static const char * const IDENT_TABLE_C="$Date: 2011/11/22 22:23:19 $"; #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_indexcount() : 0; + else if(param == "cells") + result = table.count() ? table[table.current()]->count() : 0; + else if(param == "rows") // synonim for ^table.count[] + result = table.count(); + else + throw Exception(PARSER_RUNTIME, ¶m, "parameter must be 'columns', 'cells' and 'rows' only"); + } else + result = table.count(); + r.write_no_lang(*new VInt(result)); } @@ -1064,7 +1074,7 @@ public: columns(*new ArrayString), row(0), table(0) { } - bool add_column(SQL_Error& error, const char *str, size_t length) { + bool add_column(SQL_Error& error, const char *str, size_t ) { try { columns+=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */); return false; @@ -1092,7 +1102,7 @@ public: return true; } } - bool add_row_cell(SQL_Error& error, const char* str, size_t length) { + bool add_row_cell(SQL_Error& error, const char* str, size_t ) { try { *row+=new String(str, String::L_TAINTED /* no length as 0x00 can be inside */); return false; @@ -1147,28 +1157,24 @@ static void _sql(Request& r, MethodParam HashStringValue* bind=0; ulong limit=SQL_NO_LIMIT; ulong offset=0; - 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* 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; @@ -1241,62 +1247,63 @@ 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; - 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 + int offset=0; + bool reverse=false; + + 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"); - } + "called with invalid option"); + } - int saved_current=source_table.current(); - int size=source_table.count(); - int appended = 0; + Table& result_table=*new Table(source_table.columns()); - if(reverse){ - for(int row=size-1; row >=0 && result_table.count() < limit; row--) { - source_table.set_current(row); + size_t size=source_table.count(); + if(offset<0) + offset+=size; + if(size && limit>0 && offset>=0 && offset 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(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]; + } } + source_table.set_current(saved_current); } - source_table.set_current(saved_current); r.write_no_lang(*new VTable(&result_table)); } @@ -1322,7 +1329,10 @@ MTable::MTable(): Methoded("table") { // add_native_method("save_old", Method::CT_DYNAMIC, _save_old, 1, 3); // ^table.count[] - add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0); + // ^table.count[rows] + // ^table.count[columns] + // ^table.count[cells] + add_native_method("count", Method::CT_DYNAMIC, _count, 0, 1); // ^table.line[] add_native_method("line", Method::CT_DYNAMIC, _line, 0, 0);