--- parser3/src/classes/hash.C 2001/11/05 11:46:20 1.30 +++ parser3/src/classes/hash.C 2002/04/18 15:33:32 1.40 @@ -1,10 +1,10 @@ /** @file Parser: @b hash parser class. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://paf.design.ru) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) - $Id: hash.C,v 1.30 2001/11/05 11:46:20 paf Exp $ + $Id: hash.C,v 1.40 2002/04/18 15:33:32 paf Exp $ */ #include "classes.h" @@ -16,10 +16,6 @@ #include "pa_vbool.h" #include "pa_vmethod_frame.h" -// defines - -#define HASH_CLASS_NAME "hash" - // class class MHash : public Methoded { @@ -57,7 +53,7 @@ public: } void before_rows() { if(columns.size()<=1) - throw Exception(0, 0, + throw Exception("parser.runtime", &method_name, "column count must be more than 1 to create a hash"); } @@ -95,7 +91,6 @@ static void copy_all_overwrite_to(const Hash& dest=*static_cast(info); dest.put(key, value); } - static void _create_or_add(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); @@ -110,7 +105,6 @@ static void remove_key_from(const Hash:: Hash& dest=*static_cast(info); dest.remove(key); } - static void _sub(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); @@ -123,7 +117,6 @@ static void copy_all_dontoverwrite_to(co Hash& dest=*static_cast(info); dest.put_dont_replace(key, value); } - static void _union(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); @@ -136,7 +129,6 @@ static void _union(Request& r, const Str // return result Value& result=*new(pool) VHash(pool, dest); - result.set_name(method_name); r.write_no_lang(result); } @@ -153,7 +145,6 @@ static void copy_intersection_to(const H if(i.b->get(key)) i.dest->put_dont_replace(key, value); } - static void _intersection(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); @@ -170,9 +161,7 @@ static void _intersection(Request& r, co } // return result - Value& result=*new(pool) VHash(pool, dest); - result.set_name(method_name); - r.write_no_lang(result); + r.write_no_lang(*new(pool) VHash(pool, dest)); } static void *intersects(const Hash::Key& key, Hash::Val *value, void *info) { @@ -192,44 +181,41 @@ static void _intersects(Request& r, cons yes=static_cast(r.self)->hash(&method_name).first_that(intersects, b)!=0; // return result - Value& result=*new(pool) VBool(pool, yes); - result.set_name(method_name); - r.write_no_lang(result); + r.write_no_lang(*new(pool) VBool(pool, yes)); } static void _sql(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - if(!r.connection) - throw Exception(0, 0, - &method_name, - "without connect"); - Value& statement=params->as_junction(0, "statement must be code"); ulong limit=0; - if(params->size()>1) { - Value& limit_code=params->as_junction(1, "limit must be expression"); - limit=(uint)r.process(limit_code).as_double(); - } - ulong offset=0; - if(params->size()>2) { - Value& offset_code=params->as_junction(2, "offset must be expression"); - offset=(ulong)r.process(offset_code).as_double(); + if(params->size()>1) { + Value& voptions=params->as_no_junction(1, "options must be hash, not code"); + if(voptions.is_defined()) + 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(); + if(Value *voffset=(Value *)options->get(*sql_offset_name)) + offset=(ulong)r.process_to_value(*voffset).as_double(); + } else + throw Exception("parser.runtime", + &method_name, + "options must be hash"); } Temp_lang temp_lang(r, String::UL_SQL); - const String& statement_string=r.process(statement).as_string(); + const String& statement_string=r.process_to_string(statement); const char *statement_cstr= - statement_string.cstr(String::UL_UNSPECIFIED, r.connection); + statement_string.cstr(String::UL_UNSPECIFIED, r.connection(&method_name)); Hash& hash=static_cast(r.self)->hash(&method_name); hash.clear(); Hash_sql_event_handlers handlers(pool, method_name, statement_string, statement_cstr, hash); - r.connection->query( + r.connection(&method_name)->query( statement_cstr, offset, limit, handlers); } @@ -251,17 +237,14 @@ static void _keys(Request& r, const Stri static_cast(r.self)->hash(&method_name).for_each(keys_collector, &table); - VTable& result=*new(pool) VTable(pool, &table); - result.set_name(method_name); - r.write_no_lang(result); + r.write_no_lang(*new(pool) VTable(pool, &table)); } static void _count(Request& r, const String& method_name, MethodParams *) { Pool& pool=r.pool(); - Value& result=*new(pool) VInt(pool, static_cast(r.self)->hash(&method_name).size()); - result.set_name(method_name); - r.write_no_lang(result); + r.write_no_lang( + *new(pool) VInt(pool, static_cast(r.self)->hash(&method_name).size())); } static void _delete(Request& r, const String& method_name, MethodParams *params) { @@ -291,7 +274,7 @@ static void one_foreach_cycle(const Hash i.r->root->put_element(*i.key_var_name, i.vkey); i.r->root->put_element(*i.value_var_name, static_cast(avalue)); - Value& processed_body=i.r->process(*i.body_code); + StringOrValue processed_body=i.r->process(*i.body_code); if(i.delim_maybe_code) { // delimiter set? const String *string=processed_body.get_string(); if(i.need_delim && string && string->size()) // need delim & iteration produced string? @@ -324,9 +307,8 @@ static void _foreach(Request& r, const S // constructor -MHash::MHash(Pool& apool) : Methoded(apool) { - set_name(*NEW String(pool(), HASH_CLASS_NAME)); - +MHash::MHash(Pool& apool) : Methoded(apool, "hash") +{ // ^hash::create[[copy_from]] add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1); // ^hash.add[add_from] @@ -343,8 +325,8 @@ MHash::MHash(Pool& apool) : Methoded(apo // ^a.delete[key] add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1); - // ^hash:sql[query][(count[;offset])] - add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3); + // ^hash:sql[query][$.limit(1) $.offset(2)] + add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2); // ^hash._keys[] add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 0); @@ -358,10 +340,10 @@ MHash::MHash(Pool& apool) : Methoded(apo // global variable -Methoded *hash_base_class; +Methoded *hash_class; // creator Methoded *MHash_create(Pool& pool) { - return hash_base_class=new(pool) MHash(pool); + return hash_class=new(pool) MHash(pool); }