--- parser3/src/classes/hash.C 2001/10/11 11:52:55 1.20 +++ parser3/src/classes/hash.C 2001/10/23 14:43:44 1.24 @@ -4,7 +4,7 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: hash.C,v 1.20 2001/10/11 11:52:55 parser Exp $ + $Id: hash.C,v 1.24 2001/10/23 14:43:44 parser Exp $ */ #include "classes.h" @@ -13,6 +13,7 @@ #include "pa_vvoid.h" #include "pa_sql_connection.h" #include "pa_vtable.h" +#include "pa_vbool.h" // defines @@ -55,7 +56,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"); } @@ -70,7 +71,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)); @@ -89,30 +90,118 @@ private: }; #endif -static void copy_pair_to(const Hash::Key& key, Hash::Val *value, void *info) { +static void copy_all_overwrite_to(const Hash::Key& key, Hash::Val *value, void *info) { Hash& dest=*static_cast(info); dest.put(key, value); } -static void _create(Request& r, const String& method_name, MethodParams *params) { +static void _create_or_add(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); if(params->size()) { - Value& vsrc=params->as_no_junction(0, "copy_from must be hash"); - if(Hash *src=vsrc.get_hash()) - src->for_each(copy_pair_to, &static_cast(r.self)->hash()); - else - PTHROW(0, 0, - &method_name, - "copy_from must be hash"); + Value& vb=params->as_no_junction(0, "param must be hash"); + if(Hash *b=vb.get_hash(&method_name)) + b->for_each(copy_all_overwrite_to, &static_cast(r.self)->hash()); } } +static void remove_key_from(const Hash::Key& key, Hash::Val *value, void *info) { + Hash& dest=*static_cast(info); + dest.put(key, 0); +} + +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(&method_name)) + b->for_each(remove_key_from, &static_cast(r.self)->hash()); +} + +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()); + // dest += b + Value& vb=params->as_no_junction(0, "param must be hash"); + if(Hash *b=vb.get_hash(&method_name)) + b->for_each(copy_all_dontoverwrite_to, &dest); + + // return result + Value& result=*new(pool) VHash(pool, dest); + result.set_name(method_name); + r.write_no_lang(result); +} + +#ifndef DOXYGEN +struct Copy_intersection_to_info { + Hash *b; + Hash *dest; +}; +#endif + +static void copy_intersection_to(const Hash::Key& key, Hash::Val *value, void *info) { + Copy_intersection_to_info& i=*static_cast(info); + + 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(); + + // dest = copy of self + 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(&method_name)) { + Copy_intersection_to_info info={ + b, + &dest + }; + static_cast(r.self)->hash().for_each(copy_intersection_to, &info); + } + + // return result + Value& result=*new(pool) VHash(pool, dest); + result.set_name(method_name); + r.write_no_lang(result); +} + +static void *intersects(const Hash::Key& key, Hash::Val *value, void *info) { + return static_cast(info)->get(key); +} + +static void _intersects(Request& r, const String& method_name, MethodParams *params) { + Pool& pool=r.pool(); + + bool yes=false; + + // dest = copy of self + 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(&method_name)) + yes=static_cast(r.self)->hash().first_that(intersects, b)!=0; + + // return result + Value& result=*new(pool) VBool(pool, yes); + result.set_name(method_name); + r.write_no_lang(result); +} + + static void _sql(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); if(!r.connection) - PTHROW(0, 0, + throw Exception(0, 0, &method_name, "without connect"); @@ -138,20 +227,10 @@ static void _sql(Request& r, const Strin 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->query( + statement_cstr, offset, limit, + handlers); } static void keys_collector(const Hash::Key& key, Hash::Val *value, void *info) { @@ -179,9 +258,9 @@ static void _keys(Request& r, const Stri static void _count(Request& r, const String& method_name, MethodParams *) { Pool& pool=r.pool(); - Value& value=*new(pool) VInt(pool, static_cast(r.self)->hash().size()); - value.set_name(method_name); - r.write_no_lang(value); + Value& result=*new(pool) VInt(pool, static_cast(r.self)->hash().size()); + result.set_name(method_name); + r.write_no_lang(result); } // constructor @@ -189,8 +268,18 @@ static void _count(Request& r, const Str MHash::MHash(Pool& apool) : Methoded(apool) { set_name(*NEW String(pool(), HASH_CLASS_NAME)); - // ^hash::create[[default value]] - add_native_method("create", Method::CT_DYNAMIC, _create, 0, 1); + // ^hash::create[[copy_from]] + add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1); + // ^hash.add[add_from] + add_native_method("add", Method::CT_DYNAMIC, _create_or_add, 1, 1); + // ^hash.sub[sub_from] + add_native_method("sub", Method::CT_DYNAMIC, _sub, 1, 1); + // ^a.union[b] = hash + add_native_method("union", Method::CT_DYNAMIC, _union, 1, 1); + // ^a.intersection[b] = hash + add_native_method("intersection", Method::CT_DYNAMIC, _intersection, 1, 1); + // ^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);