--- parser3/src/classes/table.C 2002/08/01 11:26:44 1.154 +++ parser3/src/classes/table.C 2002/08/07 10:25:56 1.158 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_TABLE_C="$Id: table.C,v 1.154 2002/08/01 11:26:44 paf Exp $"; +static const char* IDENT_TABLE_C="$Date: 2002/08/07 10:25:56 $"; #include "classes.h" #include "pa_common.h" @@ -30,14 +30,50 @@ public: // Methoded // methods +static void get_copy_options(Request& r, const String& method_name, MethodParams *params, int param_index, + const Table& source, + int& offset, + int& limit) { + offset=0; + limit=0; + if(params->size()<=param_index) + return; + + Value& voptions=params->as_no_junction(param_index, "options must be hash, not code"); + if(!voptions.is_string()) { + if(Hash *options=voptions.get_hash(&method_name)) { + if(Value *voffset=(Value *)options->get(*sql_offset_name)) + if(voffset->is_string()) { + const String& soffset=*voffset->get_string(); + if(soffset == "cur") + offset=source.current(); + else + throw Exception("parser.runtime", + &soffset, + "must be 'cur' string or expression"); + } else + offset=r.process_to_value(*voffset).as_int(); + if(Value *vlimit=(Value *)options->get(*sql_limit_name)) + limit=r.process_to_value(*vlimit).as_int(); + } else + throw Exception("parser.runtime", + &method_name, + "options must be hash"); + } + if(!limit) // zero limit = sould be 'nothing to copy', for methods zero means 'all' + limit=-1; // thus fixing +} + static void _create(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - // clone? - if(params->size()==1) - if(const Table *source=params->get(0).get_table()) { - static_cast(r.self)->set_table(*new(pool) Table(*source)); - return; - } + // clone/copy part? + if(const Table *source=params->get(0).get_table()) { + int offset, limit; + get_copy_options(r, method_name, params, 1, *source, + offset, limit); + static_cast(r.self)->set_table(*new(pool) Table(pool, *source, offset, limit)); + return; + } // data is last parameter Temp_lang temp_lang(r, String::UL_PASS_APPENDED); @@ -225,7 +261,7 @@ static void _offset(Request& r, const St throw Exception("parser.runtime", &whence, "is invalid whence, valid are 'cur' or 'set'"); - } + } Value& offset_expr=params->as_junction(params->size()-1, "offset must be expression"); table.offset(absolute, r.process_to_value(offset_expr).as_int()); @@ -480,9 +516,17 @@ static void _join(Request& r, const Stri &method_name, "source and destination are same table"); + int offset, limit; + get_copy_options(r, method_name, params, 1, src, + offset, limit); + if(const Array *dest_columns=dest.columns()) { // dest is named int saved_src_current=src.current(); - for(int src_row=0; src_rowm) + limit=m; + int end=offset+limit; + for(int src_row=offset; src_rowsize(); dest_column++) { @@ -554,7 +598,7 @@ static void _sql(Request& r, const Strin ulong offset=0; if(params->size()>1) { Value& voptions=params->as_no_junction(1, "options must be hash, not code"); - if(voptions.is_defined()) + if(!voptions.is_string()) if(Hash *options=voptions.get_hash(&method_name)) { if(Value *vlimit=(Value *)options->get(*sql_limit_name)) limit=(ulong)r.process_to_value(*vlimit).as_double(); @@ -705,8 +749,8 @@ MTable::MTable(Pool& apool) : Methoded(a // ^table.append{r{tab}e{tab}c{tab}o{tab}r{tab}d} add_native_method("append", Method::CT_DYNAMIC, _append, 1, 1); - // ^table.join[table] - add_native_method("join", Method::CT_DYNAMIC, _join, 1, 1); + // ^table.join[table][$.limit(10) $.offset(1) $.offset[cur] ] + add_native_method("join", Method::CT_DYNAMIC, _join, 1, 2); // ^table:sql[query]