--- parser3/src/classes/table.C 2010/11/06 00:05:46 1.277 +++ parser3/src/classes/table.C 2011/05/15 13:20:07 1.280 @@ -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/05/15 13:20:07 $"; #if (!defined(NO_STRINGSTREAM) && !defined(FREEBSD4)) #include @@ -589,8 +589,22 @@ static void _save(Request& r, MethodPara #endif // don't use stringstream } -static void _count(Request& r, MethodParams&) { - int result=GET_SELF(r, VTable).table().count(); +static void _count(Request& r, MethodParams& params) { + Table& table=GET_SELF(r, VTable).table(); + size_t result=0; + if(params.count()) { + const String& param=params.as_string(0, PARAMETER_MUST_BE_STRING); + if(param == "columns") + result = table.columns() ? table.columns()->count() : 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 +1078,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 +1106,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; @@ -1246,6 +1260,7 @@ static void _select(Request& r, MethodPa 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()) @@ -1279,13 +1294,13 @@ static void _select(Request& r, MethodPa if(reverse){ for(int row=size-1; row >=0 && result_table.count() < limit; row--) { - source_table.set_current(row); + source_table.set_current(row); 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]; - } + result_table+=source_table[row]; + } } else { for(int row=0; row < size && result_table.count() < limit; row++) { source_table.set_current(row); @@ -1293,7 +1308,7 @@ static void _select(Request& r, MethodPa 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]; + result_table+=source_table[row]; } } source_table.set_current(saved_current); @@ -1322,7 +1337,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);