|
|
| version 1.54.2.13.2.7, 2003/03/21 16:16:42 | version 1.60, 2003/11/07 13:59:21 |
|---|---|
| Line 22 static const char* IDENT_HASH_C="$Date$" | Line 22 static const char* IDENT_HASH_C="$Date$" |
| class MHash: public Methoded { | class MHash: public Methoded { |
| public: // VStateless_class | public: // VStateless_class |
| Value* create_new_value() { return Value*(new VHash()); } | Value* create_new_value(Pool& apool) { return new VHash(); } |
| public: | public: |
| MHash(); | MHash(); |
| Line 38 DECLARE_CLASS_VAR(hash, new MHash, 0); | Line 38 DECLARE_CLASS_VAR(hash, new MHash, 0); |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| class Hash_sql_event_handlers: public SQL_Driver_query_event_handlers { | class Hash_sql_event_handlers: public SQL_Driver_query_event_handlers { |
| const String& method_name; | |
| const String& statement_string; const char* statement_cstr; | const String& statement_string; const char* statement_cstr; |
| bool distinct; | bool distinct; |
| HashStringValue& rows_hash; | HashStringValue& rows_hash; |
| HashStringValue* row_hash; | HashStringValue* row_hash; |
| int column_index; | int column_index; |
| ArrayString columns; | ArrayString columns; |
| int row_index; | |
| public: | public: |
| Hash_sql_event_handlers(const String& amethod_name, | Hash_sql_event_handlers( |
| const String& astatement_string, const char* astatement_cstr, | const String& astatement_string, const char* astatement_cstr, |
| bool adistinct, | bool adistinct, |
| HashStringValue& arows_hash): | HashStringValue& arows_hash): |
| method_name(amethod_name), | |
| statement_string(astatement_string), statement_cstr(astatement_cstr), | statement_string(astatement_string), statement_cstr(astatement_cstr), |
| distinct(adistinct), | distinct(adistinct), |
| rows_hash(arows_hash), | rows_hash(arows_hash), |
| row_hash(), | row_hash(0), |
| column_index(0), | column_index(0) { |
| row_index(0){ | |
| } | } |
| bool add_column(SQL_Error& error, void *ptr, size_t size) { | bool add_column(SQL_Error& error, const char* str, size_t length) { |
| try { | try { |
| const String& column(new String); | columns+=new String(str, length, true); |
| column->APPEND_TAINTED( | |
| (const char* )ptr, size, | |
| statement_cstr, 0); | |
| columns+=column; | |
| return false; | return false; |
| } catch(...) { | } catch(...) { |
| error=SQL_Error("exception occured in Hash_sql_event_handlers::add_column"); | error=SQL_Error("exception occured in Hash_sql_event_handlers::add_column"); |
| Line 87 public: | Line 78 public: |
| column_index=0; | column_index=0; |
| return false; | return false; |
| } | } |
| bool add_row_cell(SQL_Error& error, void *ptr, size_t size) { | bool add_row_cell(SQL_Error& error, const char *ptr, size_t length) { |
| try { | try { |
| const String& cell(new String); | String& cell=*new String; |
| if(size) | if(length) |
| cell->APPEND_TAINTED( | cell.append_know_length(ptr, length, String::L_TAINTED); |
| (const char* )ptr, size, | |
| statement_cstr, row_index++); | |
| if(column_index==0) { | if(column_index==0) { |
| VHash* row_vhash(new VHash); | VHash* row_vhash=new VHash; |
| row_hash=&row_vhash->hash(); | row_hash=&row_vhash->hash(); |
| if(rows_hash.put_dont_replace(cell, row_vhash)) // put. existed? | if(rows_hash.put_dont_replace(cell, row_vhash)) // put. existed? |
| if(!distinct) { | if(!distinct) { |
| Line 105 public: | Line 94 public: |
| return true; | return true; |
| } | } |
| } else | } else |
| row_hash->put(columns[column_index], Value*(new VString(cell))); | row_hash->put(*columns[column_index], new VString(cell)); |
| column_index++; | column_index++; |
| return false; | return false; |
| Line 124 static void copy_all_overwrite_to( | Line 113 static void copy_all_overwrite_to( |
| HashStringValue* dest) { | HashStringValue* dest) { |
| dest->put(key, value); | dest->put(key, value); |
| } | } |
| static void _create_or_add(Request& r, const String& method_name, MethodParams* params) { | static void _create_or_add(Request& r, MethodParams& params) { |
| if(params->count()) { | if(params.count()) { |
| Value* vb=params->as_no_junction(0, "param must be hash"); | Value& vb=params.as_no_junction(0, "param must be hash"); |
| if(HashStringValue* b=vb->get_hash(method_name)) | if(HashStringValue* b=vb.get_hash()) |
| b->for_each(copy_all_overwrite_to, &(GET_SELF(r, VHash).hash())); | b->for_each(copy_all_overwrite_to, &(GET_SELF(r, VHash).hash())); |
| } | } |
| } | } |
| Line 138 static void remove_key_from( | Line 127 static void remove_key_from( |
| HashStringValue* dest) { | HashStringValue* dest) { |
| dest->remove(key); | dest->remove(key); |
| } | } |
| static void _sub(Request& r, const String& method_name, MethodParams* params) { | static void _sub(Request& r, MethodParams& params) { |
| Value* vb=params->as_no_junction(0, "param must be hash"); | Value& vb=params.as_no_junction(0, "param must be hash"); |
| if(HashStringValue* b=vb->get_hash(method_name)) | if(HashStringValue* b=vb.get_hash()) |
| b->for_each(remove_key_from, &GET_SELF(r, VHash).hash()); | b->for_each(remove_key_from, &GET_SELF(r, VHash).hash()); |
| } | } |
| Line 150 static void copy_all_dontoverwrite_to( | Line 139 static void copy_all_dontoverwrite_to( |
| HashStringValue* dest) { | HashStringValue* dest) { |
| dest->put_dont_replace(key, value); | dest->put_dont_replace(key, value); |
| } | } |
| static void _union(Request& r, const String& method_name, MethodParams* params) { | static void _union(Request& r, MethodParams& params) { |
| // dest = copy of self | // dest = copy of self |
| Value* result(new VHash(GET_SELF(r, VHash).hash())); | Value& result=*new VHash(GET_SELF(r, VHash).hash()); |
| // dest += b | // dest += b |
| Value* vb=params->as_no_junction(0, "param must be hash"); | Value& vb=params.as_no_junction(0, "param must be hash"); |
| if(HashStringValue* b=vb->get_hash(method_name)) | if(HashStringValue* b=vb.get_hash()) |
| b->for_each(copy_all_dontoverwrite_to, result->get_hash()); | b->for_each(copy_all_dontoverwrite_to, result.get_hash()); |
| // return result | // return result |
| r.write_no_lang(result); | r.write_no_lang(result); |
| Line 175 static void copy_intersection_to( | Line 164 static void copy_intersection_to( |
| if(info->b->get(key)) | if(info->b->get(key)) |
| info->dest->put_dont_replace(key, value); | info->dest->put_dont_replace(key, value); |
| } | } |
| static void _intersection(Request& r, const String& method_name, MethodParams* params) { | static void _intersection(Request& r, MethodParams& params) { |
| Value* result(new VHash); | Value& result=*new VHash; |
| // dest += b | // dest += b |
| Value* vb=params->as_no_junction(0, "param must be hash"); | Value& vb=params.as_no_junction(0, "param must be hash"); |
| if(HashStringValue* b=vb->get_hash(method_name)) { | if(HashStringValue* b=vb.get_hash()) { |
| Copy_intersection_to_info info={b, result->get_hash()}; | Copy_intersection_to_info info={b, result.get_hash()}; |
| GET_SELF(r, VHash).hash().for_each(copy_intersection_to, &info); | GET_SELF(r, VHash).hash().for_each(copy_intersection_to, &info); |
| } | } |
| Line 192 static bool intersects( | Line 181 static bool intersects( |
| HashStringValue::key_type key, | HashStringValue::key_type key, |
| HashStringValue::value_type /*value*/, | HashStringValue::value_type /*value*/, |
| HashStringValue* b) { | HashStringValue* b) { |
| return b->get(key); | return b->get(key)!=0; |
| } | } |
| static void _intersects(Request& r, const String& method_name, MethodParams* params) { | static void _intersects(Request& r, MethodParams& params) { |
| bool result=false; | bool result=false; |
| Value* vb=params->as_no_junction(0, "param must be hash"); | Value& vb=params.as_no_junction(0, "param must be hash"); |
| if(HashStringValue* b=vb->get_hash(method_name)) | if(HashStringValue* b=vb.get_hash()) |
| result=GET_SELF(r, VHash).hash().first_that(intersects, b); | result=GET_SELF(r, VHash).hash().first_that(intersects, b)!=0; |
| // return result | // return result |
| r.write_no_lang(Value*(new VBool(result))); | r.write_no_lang(*new VBool(result)); |
| } | } |
| extern const String& sql_limit_name; | extern String sql_limit_name; |
| extern const String& sql_offset_name; | extern String sql_offset_name; |
| extern const String& sql_default_name; | extern String sql_default_name; |
| extern const String& sql_distinct_name; | extern String sql_distinct_name; |
| static void _sql(Request& r, const String& method_name, MethodParams* params) { | static void _sql(Request& r, MethodParams& params) { |
| Value* statement=params->as_junction(0, "statement must be code"); | Value& statement=params.as_junction(0, "statement must be code"); |
| ulong limit=0; | ulong limit=0; |
| ulong offset=0; | ulong offset=0; |
| bool distinct=false; | bool distinct=false; |
| if(params->count()>1) { | if(params.count()>1) { |
| Value* voptions=params->as_no_junction(1, "options must be hash, not code"); | Value& voptions=params.as_no_junction(1, "options must be hash, not code"); |
| if(!voptions->is_string()) | if(!voptions.is_string()) |
| if(HashStringValue* options=voptions->get_hash(method_name)) { | if(HashStringValue* options=voptions.get_hash()) { |
| int valid_options=0; | int valid_options=0; |
| if(Value* vlimit=options->get(sql_limit_name)) { | if(Value* vlimit=options->get(sql_limit_name)) { |
| valid_options++; | valid_options++; |
| limit=(ulong)r.process_to_value(vlimit)->as_double(); | limit=(ulong)r.process_to_value(*vlimit).as_double(); |
| } | } |
| if(Value* voffset=options->get(sql_offset_name)) { | if(Value* voffset=options->get(sql_offset_name)) { |
| valid_options++; | valid_options++; |
| offset=(ulong)r.process_to_value(voffset)->as_double(); | offset=(ulong)r.process_to_value(*voffset).as_double(); |
| } | } |
| if(Value* vdistinct=options->get(sql_distinct_name)) { | if(Value* vdistinct=options->get(sql_distinct_name)) { |
| valid_options++; | valid_options++; |
| distinct=r.process_to_value(vdistinct)->as_bool(); | distinct=r.process_to_value(*vdistinct).as_bool(); |
| } | } |
| if(valid_options!=options->count()) | if(valid_options!=options->count()) |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| method_name, | 0, |
| "called with invalid option"); | "called with invalid option"); |
| } else | } else |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| method_name, | 0, |
| "options must be hash"); | "options must be hash"); |
| } | } |
| Temp_lang temp_lang(r, String::L_SQL); | Temp_lang temp_lang(r, String::L_SQL); |
| const String& statement_string=r.process_to_string(statement); | const String& statement_string=r.process_to_string(statement); |
| const char* statement_cstr= | const char* statement_cstr= |
| statement_string->cstr(r.String::L_UNSPECIFIED, r.connection(method_name)); | statement_string.cstr(String::L_UNSPECIFIED, r.connection()); |
| HashStringValue& hash=GET_SELF(r, VHash).hash(method_name); | HashStringValue& hash=GET_SELF(r, VHash).hash(); |
| hash.clear(); | hash.clear(); |
| Hash_sql_event_handlers handlers(method_name, | Hash_sql_event_handlers handlers( |
| statement_string, statement_cstr, | statement_string, statement_cstr, |
| distinct, | distinct, |
| hash); | hash); |
| r.connection(method_name)->query( | r.connection()->query( |
| statement_cstr, offset, limit, | statement_cstr, offset, limit, |
| handlers, | handlers, |
| statement_string); | statement_string); |
| } | } |
| static void keys_collector( | static void keys_collector( |
| HashStringValue::key_type key, | HashStringValue::key_type key, |
| HashStringValue::value_type value, | HashStringValue::value_type value, |
| Table *table) { | Table *table) { |
| Table::element_type row(new ArrayString); | Table::element_type row(new ArrayString); |
| *row+=key; | *row+=new String(key, String::L_TAINTED); |
| *table+=row; | *table+=row; |
| } | } |
| static void _keys(Request& r, const String& method_name, MethodParams* ) { | static void _keys(Request& r, MethodParams&) { |
| Table::columns_type columns(new ArrayString); | Table::columns_type columns(new ArrayString); |
| *columns+=String* (new String("key")); | *columns+=new String("key"); |
| Table* table(new Table(method_name, columns)); | Table* table=new Table(columns); |
| GET_SELF(r, VHash).hash(method_name).for_each(keys_collector, table.get()); | GET_SELF(r, VHash).hash().for_each(keys_collector, table); |
| r.write_no_lang(Value*(new VTable(table))); | r.write_no_lang(*new VTable(table)); |
| } | } |
| static void _count(Request& r, const String& method_name, MethodParams* ) { | static void _count(Request& r, MethodParams&) { |
| r.write_no_lang(Value*(new VInt(GET_SELF(r, VHash).hash(method_name).count()))); | r.write_no_lang(*new VInt(GET_SELF(r, VHash).hash().count())); |
| } | } |
| static void _delete(Request& r, const String& method_name, MethodParams* params) { | static void _delete(Request& r, MethodParams& params) { |
| GET_SELF(r, VHash).hash(method_name).remove(params->as_string(0, "key must be string")); | GET_SELF(r, VHash).hash().remove(params.as_string(0, "key must be string")); |
| } | } |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| struct Foreach_info { | struct Foreach_info { |
| Request *r; | Request *r; |
| const String& key_var_name; | const String* key_var_name; |
| const String& value_var_name; | const String* value_var_name; |
| Value* body_code; | Value* body_code; |
| Value* delim_maybe_code; | Value* delim_maybe_code; |
| const VString& vkey; | VString* vkey; |
| bool need_delim; | bool need_delim; |
| }; | }; |
| #endif | #endif |
| static void one_foreach_cycle( | static void one_foreach_cycle( |
| HashStringValue::key_type akey, | HashStringValue::key_type akey, |
| HashStringValue::value_type avalue, | HashStringValue::value_type avalue, |
| Foreach_info *info) { | Foreach_info *info) { |
| info->vkey->set_string(akey); | info->vkey->set_string(*new String(akey, String::L_TAINTED)); |
| Value& ncontext=*info->r->get_method_frame()->caller(); | Value& ncontext=*info->r->get_method_frame()->caller(); |
| ncontext.put_element(info->key_var_name, info->vkey, false); | ncontext.put_element(*info->key_var_name, info->vkey, false); |
| ncontext.put_element(info->value_var_name, avalue, false); | ncontext.put_element(*info->value_var_name, avalue, false); |
| StringOrValue sv_processed=info->r->process(info->body_code); | StringOrValue sv_processed=info->r->process(*info->body_code); |
| const String& s_processed=sv_processed.get_string(); | 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->delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body |
| if(info->need_delim) // need delim & iteration produced string? | if(info->need_delim) // need delim & iteration produced string? |
| info->r->write_pass_lang(info->r->process(info->delim_maybe_code)); | info->r->write_pass_lang(info->r->process(*info->delim_maybe_code)); |
| info->need_delim=true; | info->need_delim=true; |
| } | } |
| info->r->write_pass_lang(sv_processed); | info->r->write_pass_lang(sv_processed); |
| } | } |
| static void _foreach(Request& r, const String& method_name, MethodParams* params) { | static void _foreach(Request& r, MethodParams& params) { |
| Foreach_info info; | Foreach_info info={0}; |
| info.r=&r; | info.r=&r; |
| info.key_var_name=params->as_string(0, "key-var name must be string"); | info.key_var_name=¶ms.as_string(0, "key-var name must be string"); |
| info.value_var_name=params->as_string(1, "value-var name must be string"); | info.value_var_name=¶ms.as_string(1, "value-var name must be string"); |
| info.body_code=params->as_junction(2, "body must be code"); | info.body_code=¶ms.as_junction(2, "body must be code"); |
| info.delim_maybe_code=params->count()>3?params->get(3):0; | info.delim_maybe_code=params.count()>3?params.get(3):0; |
| info.vkey=VString* (new VString); | info.vkey=new VString; |
| VHash& self=GET_SELF(r, VHash); | VHash& self=GET_SELF(r, VHash); |
| HashStringValue& hash=self.hash(method_name); | HashStringValue& hash=self.hash(); |
| VHash_lock lock(self); | VHash_lock lock(self); |
| hash.for_each(one_foreach_cycle, &info); | hash.for_each(one_foreach_cycle, &info); |
| } | } |