--- parser3/src/classes/table.C 2006/04/09 13:38:47 1.221 +++ parser3/src/classes/table.C 2007/04/23 10:30:10 1.226 @@ -5,7 +5,10 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_TABLE_C="$Date: 2006/04/09 13:38:47 $"; +static const char * const IDENT_TABLE_C="$Date: 2007/04/23 10:30:10 $"; + +#include +using namespace std; #include "classes.h" #include "pa_vmethod_frame.h" @@ -91,7 +94,7 @@ static Table::Action_options get_action_ if(soffset == "cur") result.offset=source.current(); else - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, &soffset, "must be 'cur' string or expression"); } else @@ -109,7 +112,7 @@ static Table::Action_options get_action_ } if(valid_options!=options->count()) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "called with invalid option"); @@ -119,7 +122,7 @@ static void check_option_param(bool opti MethodParams& params, size_t next_param_index, const char *msg) { if(next_param_index+(options_defined?1:0) != params.count()) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "%s", msg); } @@ -145,7 +148,7 @@ static void _create(Request& r, MethodPa if(params.count()==2) { const String& snameless=params.as_string(0, "called with two params, first param may only be string 'nameless'"); if(snameless!="nameless") - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, &snameless, "table::create called with two params, first param may only be 'nameless'"); columns=Table::columns_type(0); // nameless @@ -277,7 +280,7 @@ struct TableSeparators { if(Value* vseparator=options.get(PA_COLUMN_SEPARATOR_NAME)) { scolumn=&vseparator->as_string(); if(scolumn->length()!=1) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, scolumn, "separator must be one character long"); column=scolumn->first_char(); @@ -286,7 +289,7 @@ struct TableSeparators { if(Value* vencloser=options.get(PA_COLUMN_ENCLOSER_NAME)) { sencloser=&vencloser->as_string(); if(sencloser->length()!=1) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, sencloser, "encloser must be one character long"); encloser=sencloser->first_char(); @@ -359,7 +362,7 @@ static void _load(Request& r, MethodPara GET_SELF(r, VTable).set_table(table); } -static void maybe_enclose( String& to, const String& from, char encloser, const String* sencloser ) { +void maybe_enclose( String& to, const String& from, char encloser, const String* sencloser ) { if(encloser) { to<<*sencloser; // while we have 'encloser'... @@ -379,8 +382,28 @@ static void maybe_enclose( String& to, c to<count()) - throw Exception("parser.runtime", + Value& voptions=params.as_no_junction(param_index++, "additional params must be hash"); + if( voptions.is_defined() && !voptions.is_string() ) { + if(HashStringValue* options=voptions.get_hash()) { + int valid_options=separators.load(*options); + if(valid_options!=options->count()) + throw Exception(PARSER_RUNTIME, + 0, + "invalid option passed"); + } else { + throw Exception(PARSER_RUNTIME, 0, - "invalid option passed"); - } else - throw Exception("parser.runtime", - 0, - "additional params must be hash (did you spell mode parameter correctly?)"); - + "additional params must be hash (did you spell mode parameter correctly?)"); + } + } } if(param_indexcount()) + throw Exception(PARSER_RUNTIME, + 0, + "invalid option passed"); + } else { + throw Exception(PARSER_RUNTIME, + 0, + "additional params must be hash (did you spell mode parameter correctly?)"); + } + } + } + if(param_index i(*table.columns()); i.has_next(); ) { + maybe_enclose( ost, *i.next(), separators.encloser ); + if(i.has_next()){ + ost<count():0) + for(int column=0; column i(table); + while(i.has_next()) { + for(Array_iterator c(*i.next()); c.has_next(); ) { + maybe_enclose( ost, *c.next(), separators.encloser ); + if(c.has_next()) + ost<count()) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "called with invalid option"); } } if(param_index==2) // bad options param type - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "options must be hash"); @@ -647,10 +764,10 @@ static void _hash(Request& r, MethodPara +=self_table.column_name2index(value_field_name, true); } } else - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, - "value field(s) must be string or table" - ); + "value field(s) must be string or table"); + } else { // by all columns, including key if(!(distinct!=D_ILLEGAL && distinct!=D_FIRST)) for(size_t i=0; icount(); i++) @@ -835,7 +952,7 @@ static void join_nameless_row(Table& src static void _join(Request& r, MethodParams& params) { Table* maybe_src=params.as_no_junction(0, "table ref must not be code").get_table(); if(!maybe_src) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "source is not a table"); Table& src=*maybe_src; @@ -846,7 +963,7 @@ static void _join(Request& r, MethodPara Table& dest=GET_SELF(r, VTable).table(); if(&src == &dest) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "source and destination are same table"); @@ -975,11 +1092,11 @@ static void _sql(Request& r, MethodParam offset=(ulong)r.process_to_value(*voffset).as_double(); } if(valid_options!=options->count()) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "called with invalid option"); } else - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "options must be hash"); } @@ -1086,6 +1203,8 @@ MTable::MTable(): Methoded("table") { // ^table.save[nameless;file] add_native_method("save", Method::CT_DYNAMIC, _save, 1, 3); + // add_native_method("save_old", Method::CT_DYNAMIC, _save_old, 1, 3); + // ^table.count[] add_native_method("count", Method::CT_DYNAMIC, _count, 0, 0);