--- parser3/src/classes/table.C 2002/04/25 09:39:45 1.152 +++ parser3/src/classes/table.C 2002/08/06 14:23:22 1.157 @@ -3,10 +3,10 @@ Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - - $Id: table.C,v 1.152 2002/04/25 09:39:45 paf Exp $ */ +static const char* IDENT_TABLE_C="$Date: 2002/08/06 14:23:22 $"; + #include "classes.h" #include "pa_common.h" #include "pa_request.h" @@ -30,14 +30,47 @@ 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"); +} + 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); @@ -184,8 +217,9 @@ static void _save(Request& r, const Stri while(i.has_next()) { Array_iter c(*static_cast(i.next())); while(c.has_next()) { - sdata.append(*c.next_string(), //*static_cast(row->quick_get(column)), - String::UL_TABLE); + if(const String *s=c.next_string()) + sdata.append(*s, + String::UL_TABLE); if(c.has_next()) sdata.APPEND_CONST("\t"); } @@ -224,7 +258,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()); @@ -479,9 +513,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++) { @@ -553,7 +595,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(); @@ -704,8 +746,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]