--- parser3/src/classes/hash.C 2001/10/11 12:43:28 1.22 +++ parser3/src/classes/hash.C 2002/02/08 08:30:10 1.35 @@ -1,10 +1,10 @@ /** @file Parser: @b hash parser class. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) - $Id: hash.C,v 1.22 2001/10/11 12:43:28 parser Exp $ + $Id: hash.C,v 1.35 2002/02/08 08:30:10 paf Exp $ */ #include "classes.h" @@ -14,6 +14,7 @@ #include "pa_sql_connection.h" #include "pa_vtable.h" #include "pa_vbool.h" +#include "pa_vmethod_frame.h" // defines @@ -56,7 +57,7 @@ public: } void before_rows() { if(columns.size()<=1) - PTHROW(0, 0, + throw Exception(0, 0, &method_name, "column count must be more than 1 to create a hash"); } @@ -71,7 +72,7 @@ public: statement_cstr, row_index++); if(column_index==0) { VHash *row_vhash=new(pool) VHash(pool); - row_hash=row_vhash->get_hash(); + row_hash=row_vhash->get_hash(0); rows_hash.put(*cell, row_vhash); } else row_hash->put(*columns.get_string(column_index), new(pool) VString(*cell)); @@ -94,43 +95,40 @@ 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(); if(params->size()) { Value& vb=params->as_no_junction(0, "param must be hash"); - if(Hash *b=vb.get_hash()) - b->for_each(copy_all_overwrite_to, &static_cast(r.self)->hash()); + if(Hash *b=vb.get_hash(&method_name)) + b->for_each(copy_all_overwrite_to, &static_cast(r.self)->hash(&method_name)); } } static void remove_key_from(const Hash::Key& key, Hash::Val *value, void *info) { Hash& dest=*static_cast(info); - dest.put(key, 0); + dest.remove(key); } - static void _sub(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); Value& vb=params->as_no_junction(0, "param must be hash"); - if(Hash *b=vb.get_hash()) - b->for_each(remove_key_from, &static_cast(r.self)->hash()); + if(Hash *b=vb.get_hash(&method_name)) + b->for_each(remove_key_from, &static_cast(r.self)->hash(&method_name)); } static void copy_all_dontoverwrite_to(const Hash::Key& key, Hash::Val *value, void *info) { 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(); // dest = copy of self - Hash& dest=*new(pool) Hash(static_cast(r.self)->hash()); + Hash& dest=*new(pool) Hash(static_cast(r.self)->hash(&method_name)); // dest += b Value& vb=params->as_no_junction(0, "param must be hash"); - if(Hash *b=vb.get_hash()) + if(Hash *b=vb.get_hash(&method_name)) b->for_each(copy_all_dontoverwrite_to, &dest); // return result @@ -152,7 +150,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(); @@ -160,12 +157,12 @@ static void _intersection(Request& r, co Hash& dest=*new(pool) Hash(pool); // dest += b Value& vb=params->as_no_junction(0, "param must be hash"); - if(Hash *b=vb.get_hash()) { + if(Hash *b=vb.get_hash(&method_name)) { Copy_intersection_to_info info={ b, &dest }; - static_cast(r.self)->hash().for_each(copy_intersection_to, &info); + static_cast(r.self)->hash(&method_name).for_each(copy_intersection_to, &info); } // return result @@ -187,8 +184,8 @@ static void _intersects(Request& r, cons Hash& dest=*new(pool) Hash(pool); // dest += b Value& vb=params->as_no_junction(0, "param must be hash"); - if(Hash *b=vb.get_hash()) - yes=static_cast(r.self)->hash().first_that(intersects, b)!=0; + if(Hash *b=vb.get_hash(&method_name)) + yes=static_cast(r.self)->hash(&method_name).first_that(intersects, b)!=0; // return result Value& result=*new(pool) VBool(pool, yes); @@ -200,47 +197,36 @@ static void _intersects(Request& r, cons static void _sql(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - if(!r.connection) - PTHROW(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(*vlimit).as_double(); + if(Value *voffset=(Value *)options->get(*sql_offset_name)) + offset=(ulong)r.process(*voffset).as_double(); + } else + throw Exception(0, 0, + &method_name, + "options must be hash"); } Temp_lang temp_lang(r, String::UL_SQL); const String& statement_string=r.process(statement).as_string(); const char *statement_cstr= - statement_string.cstr(String::UL_UNSPECIFIED, r.connection); - Hash& hash=static_cast(r.self)->hash(); + 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); - bool need_rethrow=false; Exception rethrow_me; - PTRY { - r.connection->query( - statement_cstr, offset, limit, - handlers); - } - PCATCH(e) { // query problem - rethrow_me=e; need_rethrow=true; - } - PEND_CATCH - if(need_rethrow) - PTHROW(rethrow_me.type(), rethrow_me.code(), - &statement_string, // setting more specific source [were url] - rethrow_me.comment()); + + r.connection(&method_name)->query( + statement_cstr, offset, limit, + handlers); } static void keys_collector(const Hash::Key& key, Hash::Val *value, void *info) { @@ -258,7 +244,7 @@ static void _keys(Request& r, const Stri columns+=new(pool) String(pool, "key"); Table& table=*new(pool) Table(pool, &method_name, &columns); - static_cast(r.self)->hash().for_each(keys_collector, &table); + static_cast(r.self)->hash(&method_name).for_each(keys_collector, &table); VTable& result=*new(pool) VTable(pool, &table); result.set_name(method_name); @@ -268,11 +254,69 @@ static void _keys(Request& r, const Stri 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().size()); + Value& result=*new(pool) VInt(pool, static_cast(r.self)->hash(&method_name).size()); result.set_name(method_name); r.write_no_lang(result); } +static void _delete(Request& r, const String& method_name, MethodParams *params) { + Pool& pool=r.pool(); + + static_cast(r.self)->hash(&method_name).remove(params->as_string(0, "key must be string")); +} + +#ifndef DOXYGEN +struct Foreach_info { + Request *r; + const String* key_var_name; + const String* value_var_name; + Value *body_code; + Value *delim_maybe_code; + + VString *vkey; + bool need_delim; +}; +#endif + +static void one_foreach_cycle(const Hash::Key& akey, Hash::Val *avalue, + void *info) { + Foreach_info& i=*static_cast(info); + + i.vkey->set_string(akey); + 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); + 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? + i.r->write_pass_lang(i.r->process(*i.delim_maybe_code)); + i.need_delim=true; + } + i.r->write_pass_lang(processed_body); +} +static void _foreach(Request& r, const String& method_name, MethodParams *params) { + Pool& pool=r.pool(); + const String& key_var_name=params->as_string(0, "key-var name must be string"); + const String& value_var_name=params->as_string(1, "value-var name must be string"); + Value& body_code=params->as_junction(2, "body must be code"); + Value *delim_maybe_code=params->size()>3?¶ms->get(3):0; + + Foreach_info info={ + &r, + &key_var_name, &value_var_name, + &body_code, + delim_maybe_code, + + new(pool) VString(pool), + false + }; + VHash& self=*static_cast(r.self); + Hash& hash=self.hash(&method_name); + VHash_lock lock(self); + hash.for_each(one_foreach_cycle, &info); +} + // constructor MHash::MHash(Pool& apool) : Methoded(apool) { @@ -291,14 +335,20 @@ MHash::MHash(Pool& apool) : Methoded(apo // ^a.intersects[b] = bool add_native_method("intersects", Method::CT_DYNAMIC, _intersects, 1, 1); - // ^hash:sql[query][(count[;offset])] - add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3); + // ^a.delete[key] + add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1); + + // ^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); // ^hash._count[] add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0); + + // ^hash.foreach[key;value]{code}[delim] + add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1); } // global variable