--- parser3/src/classes/hash.C 2002/02/08 08:30:10 1.35 +++ parser3/src/classes/hash.C 2002/12/09 12:19:16 1.53 @@ -3,10 +3,10 @@ Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - - $Id: hash.C,v 1.35 2002/02/08 08:30:10 paf Exp $ */ +static const char* IDENT_HASH_C="$Date: 2002/12/09 12:19:16 $"; + #include "classes.h" #include "pa_request.h" #include "pa_vhash.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 { @@ -35,54 +31,82 @@ public: // Methoded // methods #ifndef DOXYGEN -class Hash_sql_event_handlers : public SQL_Driver_query_event_handlers { +class Hash_sql_event_handlers: public SQL_Driver_query_event_handlers { public: Hash_sql_event_handlers(Pool& apool, const String& amethod_name, const String& astatement_string, const char *astatement_cstr, - Hash& arows_hash) : + bool adistinct, + Hash& arows_hash): pool(apool), method_name(amethod_name), statement_string(astatement_string), statement_cstr(astatement_cstr), + distinct(adistinct), rows_hash(arows_hash), columns(pool), row_index(0) { } - void add_column(void *ptr, size_t size) { - String *column=new(pool) String(pool); - column->APPEND_TAINTED( - (const char *)ptr, size, - statement_cstr, 0); - columns+=column; + bool add_column(SQL_Error& error, void *ptr, size_t size) { + try { + String *column=new(pool) String(pool); + column->APPEND_TAINTED( + (const char *)ptr, size, + statement_cstr, 0); + columns+=column; + + return false; + } catch(...) { + error=SQL_Error("exception occured in Hash_sql_event_handlers::add_column"); + return true; + } } - void before_rows() { - if(columns.size()<=1) - throw Exception(0, 0, + bool before_rows(SQL_Error& error) { + if(columns.size()<=1) { + error=SQL_Error("parser.runtime", &method_name, "column count must be more than 1 to create a hash"); + return true; + } + + return false; } - void add_row() { + bool add_row(SQL_Error& /*error*/) { column_index=0; + return false; } - void add_row_cell(void *ptr, size_t size) { - String *cell=new(pool) String(pool); - if(size) - cell->APPEND_TAINTED( - (const char *)ptr, size, - statement_cstr, row_index++); - if(column_index==0) { - VHash *row_vhash=new(pool) VHash(pool); - 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)); - column_index++; + bool add_row_cell(SQL_Error& error, void *ptr, size_t size) { + try { + String *cell=new(pool) String(pool); + if(size) + cell->APPEND_TAINTED( + (const char *)ptr, size, + statement_cstr, row_index++); + if(column_index==0) { + VHash *row_vhash=new(pool) VHash(pool); + row_hash=row_vhash->get_hash(0); + if(rows_hash.put_dont_replace(*cell, row_vhash)) // put. existed? + if(!distinct) { + error=SQL_Error("parser.runtime", + cell, + "duplicate key"); + return true; + } + } else + row_hash->put(*columns.get_string(column_index), new(pool) VString(*cell)); + column_index++; + + return false; + } catch(...) { + error=SQL_Error("exception occured in Hash_sql_event_handlers::add_row_cell"); + return true; + } } private: Pool& pool; const String& method_name; const String& statement_string; const char *statement_cstr; + bool distinct; Hash& rows_hash; Hash *row_hash; int column_index; @@ -101,7 +125,7 @@ static void _create_or_add(Request& r, c if(params->size()) { 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(&method_name)); + b->for_each(copy_all_overwrite_to, &static_cast(r.get_self())->hash(&method_name)); } } @@ -114,7 +138,7 @@ static void _sub(Request& r, const Strin 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(&method_name)); + b->for_each(remove_key_from, &static_cast(r.get_self())->hash(&method_name)); } static void copy_all_dontoverwrite_to(const Hash::Key& key, Hash::Val *value, void *info) { @@ -125,7 +149,7 @@ static void _union(Request& r, const Str Pool& pool=r.pool(); // dest = copy of self - Hash& dest=*new(pool) Hash(static_cast(r.self)->hash(&method_name)); + Hash& dest=*new(pool) Hash(static_cast(r.get_self())->hash(&method_name)); // dest += b Value& vb=params->as_no_junction(0, "param must be hash"); if(Hash *b=vb.get_hash(&method_name)) @@ -133,7 +157,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); } @@ -162,13 +185,11 @@ static void _intersection(Request& r, co b, &dest }; - static_cast(r.self)->hash(&method_name).for_each(copy_intersection_to, &info); + static_cast(r.get_self())->hash(&method_name).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); + r.write_no_lang(*new(pool) VHash(pool, dest)); } static void *intersects(const Hash::Key& key, Hash::Val *value, void *info) { @@ -185,12 +206,10 @@ static void _intersects(Request& r, cons // 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(&method_name).first_that(intersects, b)!=0; + yes=static_cast(r.get_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)); } @@ -201,32 +220,49 @@ static void _sql(Request& r, const Strin ulong limit=0; ulong offset=0; + bool distinct=false; 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(*vlimit).as_double(); - if(Value *voffset=(Value *)options->get(*sql_offset_name)) - offset=(ulong)r.process(*voffset).as_double(); + int valid_options=0; + if(Value *vlimit=(Value *)options->get(*sql_limit_name)) { + valid_options++; + limit=(ulong)r.process_to_value(*vlimit).as_double(); + } + if(Value *voffset=(Value *)options->get(*sql_offset_name)) { + valid_options++; + offset=(ulong)r.process_to_value(*voffset).as_double(); + } + if(Value *vdistinct=(Value *)options->get(*sql_distinct_name)) { + valid_options++; + distinct=r.process_to_value(*vdistinct).as_bool(); + } + if(valid_options!=options->size()) + throw Exception("parser.runtime", + &method_name, + "called with invalid option"); } else - throw Exception(0, 0, + 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(&method_name)); - Hash& hash=static_cast(r.self)->hash(&method_name); + Hash& hash=static_cast(r.get_self())->hash(&method_name); hash.clear(); Hash_sql_event_handlers handlers(pool, method_name, - statement_string, statement_cstr, hash); + statement_string, statement_cstr, + distinct, + hash); r.connection(&method_name)->query( statement_cstr, offset, limit, - handlers); + handlers, + statement_string); } static void keys_collector(const Hash::Key& key, Hash::Val *value, void *info) { @@ -244,25 +280,22 @@ 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(&method_name).for_each(keys_collector, &table); + static_cast(r.get_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.get_self())->hash(&method_name).size())); } 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")); + static_cast(r.get_self())->hash(&method_name).remove(params->as_string(0, "key must be string")); } #ifndef DOXYGEN @@ -283,17 +316,18 @@ static void one_foreach_cycle(const Hash 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? + Value& ncontext=*i.r->get_method_frame()->caller(); + ncontext.put_element(*i.key_var_name, i.vkey, false); + ncontext.put_element(*i.value_var_name, static_cast(avalue), false); + + StringOrValue sv_processed=i.r->process(*i.body_code); + const String *s_processed=sv_processed.get_string(); + if(i.delim_maybe_code && s_processed && s_processed->size()) { // delimiter set and we have body + if(i.need_delim) // 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); + i.r->write_pass_lang(sv_processed); } static void _foreach(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); @@ -311,7 +345,7 @@ static void _foreach(Request& r, const S new(pool) VString(pool), false }; - VHash& self=*static_cast(r.self); + VHash& self=*static_cast(r.get_self()); Hash& hash=self.hash(&method_name); VHash_lock lock(self); hash.for_each(one_foreach_cycle, &info); @@ -319,9 +353,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] @@ -353,10 +386,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); }