|
|
| version 1.9, 2001/07/02 13:59:59 | version 1.69, 2004/03/25 11:49:25 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b hash parser class. | Parser: @b hash parser class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | |
| $Id$ | |
| */ | */ |
| static const char *RCSId="$Id$"; | |
| static const char * const IDENT_HASH_C="$Date$"; | |
| #include "classes.h" | #include "classes.h" |
| #include "pa_vmethod_frame.h" | |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_vhash.h" | #include "pa_vhash.h" |
| #include "pa_vvoid.h" | #include "pa_vvoid.h" |
| #include "pa_sql_connection.h" | #include "pa_sql_connection.h" |
| #include "pa_vtable.h" | #include "pa_vtable.h" |
| #include "pa_vbool.h" | |
| // defines | #include "pa_vmethod_frame.h" |
| #define HASH_CLASS_NAME "hash" | |
| // class | // class |
| class MHash : public Methoded { | class MHash: public Methoded { |
| public: // VStateless_class | public: // VStateless_class |
| Value *create_new_value(Pool& pool) { return new(pool) VHash(pool); } | Value* create_new_value(Pool&) { return new VHash(); } |
| public: | public: |
| MHash(Pool& pool); | MHash(); |
| public: // Methoded | public: // Methoded |
| bool used_directly() { return true; } | bool used_directly() { return true; } |
| }; | }; |
| // methods | // global variable |
| static void _default(Request& r, const String&, MethodParams *params) { | DECLARE_CLASS_VAR(hash, new MHash, 0); |
| Pool& pool=r.pool(); | |
| VHash& vhash=*static_cast<VHash *>(r.self); | // methods |
| if(params->size()) | |
| vhash.set_default(params->get(0)); // info: may be code.. | #ifndef DOXYGEN |
| else { | class Hash_sql_event_handlers: public SQL_Driver_query_event_handlers { |
| Value *default_value=vhash.get_default(); | const String& statement_string; const char* statement_cstr; |
| r.write_assign_lang(default_value?*default_value:*new(pool) VVoid(pool)); | bool distinct; |
| HashStringValue& rows_hash; | |
| HashStringValue* row_hash; | |
| int column_index; | |
| ArrayString columns; | |
| bool only_one_column; | |
| static VBool only_one_column_value; | |
| public: | |
| Hash_sql_event_handlers( | |
| const String& astatement_string, const char* astatement_cstr, | |
| bool adistinct, | |
| HashStringValue& arows_hash): | |
| statement_string(astatement_string), statement_cstr(astatement_cstr), | |
| distinct(adistinct), | |
| rows_hash(arows_hash), | |
| row_hash(0), | |
| column_index(0), | |
| only_one_column(false) { | |
| } | } |
| } | bool add_column(SQL_Error& error, const char* str, size_t length) { |
| try { | |
| columns+=new String(str, length, true); | |
| return false; | |
| } catch(...) { | |
| error=SQL_Error("exception occured in Hash_sql_event_handlers::add_column"); | |
| return true; | |
| } | |
| } | |
| bool before_rows(SQL_Error& error) { | |
| if(columns.count()<1) { | |
| error=SQL_Error("parser.runtime", "no columns"); | |
| return true; | |
| } | |
| only_one_column=columns.count()==1; | |
| static void _sql(Request& r, const String& method_name, MethodParams *params) { | return false; |
| Pool& pool=r.pool(); | } |
| bool add_row(SQL_Error& /*error*/) { | |
| column_index=0; | |
| return false; | |
| } | |
| bool add_row_cell(SQL_Error& error, const char *ptr, size_t length) { | |
| try { | |
| String& cell=*new String; | |
| if(length) | |
| cell.append_know_length(ptr, length, String::L_TAINTED); | |
| bool duplicate=false; | |
| if(only_one_column) { | |
| duplicate=rows_hash.put_dont_replace(cell, &only_one_column_value); // put. existed? | |
| } else if(column_index==0) { | |
| VHash* row_vhash=new VHash; | |
| row_hash=&row_vhash->hash(); | |
| duplicate=rows_hash.put_dont_replace(cell, row_vhash); // put. existed? | |
| } else | |
| row_hash->put(*columns[column_index], new VString(cell)); | |
| if(duplicate & !distinct) { | |
| error=SQL_Error("parser.runtime", "duplicate key"); | |
| return true; | |
| } | |
| column_index++; | |
| return false; | |
| } catch(...) { | |
| error=SQL_Error("exception occured in Hash_sql_event_handlers::add_row_cell"); | |
| return true; | |
| } | |
| } | |
| if(!r.connection) | }; |
| PTHROW(0, 0, | VBool Hash_sql_event_handlers::only_one_column_value(true); |
| &method_name, | |
| "without connect"); | |
| Value& statement=params->get_junction(0, "statement must be code"); | #endif |
| ulong limit=0; | static void copy_all_overwrite_to( |
| if(params->size()>1) { | HashStringValue::key_type key, |
| Value& limit_code=params->get_junction(1, "limit must be expression"); | HashStringValue::value_type value, |
| limit=(uint)r.process(limit_code).as_double(); | HashStringValue* dest) { |
| dest->put(key, value); | |
| } | |
| static void _create_or_add(Request& r, MethodParams& params) { | |
| if(params.count()) { | |
| Value& vsrc=params.as_no_junction(0, "param must be hash"); | |
| if(HashStringValue* src=vsrc.get_hash()) { | |
| HashStringValue* self=&(GET_SELF(r, VHash).hash()); | |
| if(src==self) // same: doing nothing | |
| return; | |
| src->for_each(copy_all_overwrite_to, self); | |
| } | |
| } | } |
| } | |
| ulong offset=0; | static void remove_key_from( |
| if(params->size()>2) { | HashStringValue::key_type key, |
| Value& offset_code=params->get_junction(2, "offset must be expression"); | HashStringValue::value_type /*value*/, |
| offset=(ulong)r.process(offset_code).as_double(); | HashStringValue* dest) { |
| } | dest->remove(key); |
| } | |
| Temp_lang temp_lang(r, String::UL_SQL); | static void _sub(Request& r, MethodParams& params) { |
| const String& statement_string=r.process(statement).as_string(); | Value& vsrc=params.as_no_junction(0, "param must be hash"); |
| const char *statement_cstr= | if(HashStringValue* src=vsrc.get_hash()) { |
| statement_string.cstr(String::UL_UNSPECIFIED, r.connection); | HashStringValue* self=&(GET_SELF(r, VHash).hash()); |
| unsigned int sql_column_count; SQL_Driver::Cell *sql_columns; | if(src==self) { // same: clearing |
| unsigned long sql_row_count; SQL_Driver::Cell **sql_rows; | self->clear(); |
| bool need_rethrow=false; Exception rethrow_me; | return; |
| PTRY { | |
| r.connection->query( | |
| statement_cstr, offset, limit, | |
| &sql_column_count, &sql_columns, | |
| &sql_row_count, &sql_rows); | |
| } | |
| 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()); | |
| Hash& rows_hash=static_cast<VHash *>(r.self)->hash(); | |
| rows_hash.clear(); | |
| if(sql_column_count<=1) | |
| return; | |
| Array& columns=*new(pool) Array(pool); | |
| for(unsigned int i=0+1; i<sql_column_count; i++) { | |
| String& column=*new(pool) String(pool); | |
| column.APPEND_TAINTED( | |
| (const char *)sql_columns[i].ptr, sql_columns[i].size, | |
| statement_cstr, 0); | |
| columns+=&column; | |
| } | |
| for(unsigned long row=0; row<sql_row_count; row++) { | |
| SQL_Driver::Cell *sql_cells=sql_rows[row]; | |
| VHash& row_vhash=*new(pool) VHash(pool); | |
| Hash& row_hash=*row_vhash.get_hash(); | |
| String *key=0; // calm, compiler | |
| String *cell; | |
| for(unsigned int i=0; i<sql_column_count; i++) { | |
| cell=new(pool) String(pool); | |
| cell->APPEND_TAINTED( | |
| (const char *)sql_cells[i].ptr, sql_cells[i].size, | |
| statement_cstr, row); | |
| if(i==0) | |
| key=cell; | |
| else | |
| row_hash.put(*columns.get_string(i-1), new(pool) VString(*cell)); | |
| } | } |
| rows_hash.put(*key, &row_vhash); | src->for_each(remove_key_from, self); |
| } | } |
| } | } |
| static void keys_collector(const Hash::Key& key, Hash::Val *value, void *info) { | static void copy_all_dontoverwrite_to( |
| Table& table=*static_cast<Table *>(info); | HashStringValue::key_type key, |
| Pool& pool=table.pool(); | HashStringValue::value_type value, |
| HashStringValue* dest) { | |
| Array& row=*new(pool) Array(pool); | dest->put_dont_replace(key, value); |
| row+=&key; | |
| table+=&row; | |
| } | } |
| static void _keys(Request& r, const String& method_name, MethodParams *) { | static void _union(Request& r, MethodParams& params) { |
| Pool& pool=r.pool(); | // dest = copy of self |
| Value& result=*new VHash(GET_SELF(r, VHash).hash()); | |
| // dest += b | |
| Value& vsrc=params.as_no_junction(0, "param must be hash"); | |
| if(HashStringValue* src=vsrc.get_hash()) | |
| src->for_each(copy_all_dontoverwrite_to, result.get_hash()); | |
| Array& columns=*new(pool) Array(pool); | // return result |
| columns+=new(pool) String(pool, "key"); | r.write_no_lang(result); |
| Table& table=*new(pool) Table(pool, &method_name, &columns); | } |
| static_cast<VHash *>(r.self)->hash().for_each(keys_collector, &table); | #ifndef DOXYGEN |
| struct Copy_intersection_to_info { | |
| HashStringValue* b; | |
| HashStringValue* dest; | |
| }; | |
| #endif | |
| static void copy_intersection_to( | |
| HashStringValue::key_type key, | |
| HashStringValue::value_type value, | |
| Copy_intersection_to_info *info) { | |
| if(info->b->get(key)) | |
| info->dest->put_dont_replace(key, value); | |
| } | |
| static void _intersection(Request& r, MethodParams& params) { | |
| Value& result=*new VHash; | |
| // dest += b | |
| Value& vb=params.as_no_junction(0, "param must be hash"); | |
| if(HashStringValue* b=vb.get_hash()) { | |
| Copy_intersection_to_info info={b, result.get_hash()}; | |
| GET_SELF(r, VHash).hash().for_each(copy_intersection_to, &info); | |
| } | |
| VTable& result=*new(pool) VTable(pool, &table); | // return result |
| result.set_name(method_name); | |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| // constructor | static bool intersects( |
| HashStringValue::key_type key, | |
| HashStringValue::value_type /*value*/, | |
| HashStringValue* b) { | |
| return b->get(key)!=0; | |
| } | |
| MHash::MHash(Pool& apool) : Methoded(apool) { | static void _intersects(Request& r, MethodParams& params) { |
| set_name(*NEW String(pool(), HASH_CLASS_NAME)); | bool result=false; |
| // ^hash.default[] | Value& vb=params.as_no_junction(0, "param must be hash"); |
| // ^hash.default[hash] | if(HashStringValue* b=vb.get_hash()) |
| add_native_method("default", Method::CT_DYNAMIC, _default, 0, 1); | result=GET_SELF(r, VHash).hash().first_that(intersects, b)!=0; |
| // return result | |
| r.write_no_lang(*new VBool(result)); | |
| } | |
| // ^hash:sql[query][(count[;offset])] | |
| add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3); | |
| // ^hash.keys[] | extern String sql_limit_name; |
| add_native_method("keys", Method::CT_DYNAMIC, _keys, 0, 0); | extern String sql_offset_name; |
| extern String sql_default_name; | |
| extern String sql_distinct_name; | |
| static void _sql(Request& r, MethodParams& params) { | |
| Value& statement=params.as_junction(0, "statement must be code"); | |
| ulong limit=0; | |
| ulong offset=0; | |
| bool distinct=false; | |
| if(params.count()>1) { | |
| Value& voptions=params.as_no_junction(1, "options must be hash, not code"); | |
| if(!voptions.is_string()) | |
| if(HashStringValue* options=voptions.get_hash()) { | |
| int valid_options=0; | |
| if(Value* vlimit=options->get(sql_limit_name)) { | |
| valid_options++; | |
| limit=(ulong)r.process_to_value(*vlimit).as_double(); | |
| } | |
| if(Value* voffset=options->get(sql_offset_name)) { | |
| valid_options++; | |
| offset=(ulong)r.process_to_value(*voffset).as_double(); | |
| } | |
| if(Value* vdistinct=options->get(sql_distinct_name)) { | |
| valid_options++; | |
| distinct=r.process_to_value(*vdistinct).as_bool(); | |
| } | |
| if(valid_options!=options->count()) | |
| throw Exception("parser.runtime", | |
| 0, | |
| "called with invalid option"); | |
| } else | |
| throw Exception("parser.runtime", | |
| 0, | |
| "options must be hash"); | |
| } | |
| Temp_lang temp_lang(r, String::L_SQL); | |
| const String& statement_string=r.process_to_string(statement); | |
| const char* statement_cstr= | |
| statement_string.cstr(String::L_UNSPECIFIED, r.connection()); | |
| HashStringValue& hash=GET_SELF(r, VHash).hash(); | |
| hash.clear(); | |
| Hash_sql_event_handlers handlers( | |
| statement_string, statement_cstr, | |
| distinct, | |
| hash); | |
| r.connection()->query( | |
| statement_cstr, offset, limit, | |
| handlers, | |
| statement_string); | |
| } | } |
| // global variable | static void keys_collector( |
| HashStringValue::key_type key, | |
| HashStringValue::value_type, | |
| Table *table) { | |
| Table::element_type row(new ArrayString); | |
| *row+=new String(key, String::L_TAINTED); | |
| *table+=row; | |
| } | |
| static void _keys(Request& r, MethodParams& params) { | |
| const String* keys_column_name; | |
| if(params.count()>0) | |
| keys_column_name=¶ms.as_string(0, "column name must be string"); | |
| else | |
| keys_column_name=new String("key"); | |
| Table::columns_type columns(new ArrayString); | |
| *columns+=keys_column_name; | |
| Table* table=new Table(columns); | |
| GET_SELF(r, VHash).hash().for_each(keys_collector, table); | |
| r.write_no_lang(*new VTable(table)); | |
| } | |
| static void _count(Request& r, MethodParams&) { | |
| r.write_no_lang(*new VInt(GET_SELF(r, VHash).hash().count())); | |
| } | |
| static void _delete(Request& r, MethodParams& params) { | |
| GET_SELF(r, VHash).hash().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( | |
| HashStringValue::key_type akey, | |
| HashStringValue::value_type avalue, | |
| Foreach_info *info) { | |
| info->vkey->set_string(*new String(akey, String::L_TAINTED)); | |
| Value& ncontext=*info->r->get_method_frame()->caller(); | |
| ncontext.put_element(*info->key_var_name, info->vkey, false); | |
| ncontext.put_element(*info->value_var_name, avalue, false); | |
| StringOrValue sv_processed=info->r->process(*info->body_code); | |
| const String* s_processed=sv_processed.get_string(); | |
| if(info->delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body | |
| if(info->need_delim) // need delim & iteration produced string? | |
| info->r->write_pass_lang(info->r->process(*info->delim_maybe_code)); | |
| info->need_delim=true; | |
| } | |
| info->r->write_pass_lang(sv_processed); | |
| } | |
| static void _foreach(Request& r, MethodParams& params) { | |
| Foreach_info info={ | |
| &r, | |
| ¶ms.as_string(0, "key-var name must be string"), | |
| ¶ms.as_string(1, "value-var name must be string"), | |
| ¶ms.as_junction(2, "body must be code"), | |
| params.count()>3?params.get(3):0, | |
| /*vkey=*/new VString, | |
| false | |
| }; | |
| VHash& self=GET_SELF(r, VHash); | |
| HashStringValue& hash=self.hash(); | |
| VHash_lock lock(self); | |
| hash.for_each(one_foreach_cycle, &info); | |
| } | |
| // constructor | |
| MHash::MHash(): Methoded("hash") | |
| { | |
| // ^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); | |
| // ^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); | |
| Methoded *hash_base_class; | // ^hash._keys[[column name]] |
| add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1); | |
| // creator | // ^hash._count[] |
| add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0); | |
| Methoded *MHash_create(Pool& pool) { | // ^hash.foreach[key;value]{code}[delim] |
| return hash_base_class=new(pool) MHash(pool); | add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1); |
| } | } |