|
|
| version 1.64, 2003/11/20 17:09:35 | version 1.70, 2004/06/18 15:55:47 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b hash parser class. | Parser: @b hash parser class. |
| Copyright (c) 2001-2003 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: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 44 class Hash_sql_event_handlers: public SQ | Line 44 class Hash_sql_event_handlers: public SQ |
| HashStringValue* row_hash; | HashStringValue* row_hash; |
| int column_index; | int column_index; |
| ArrayString columns; | ArrayString columns; |
| bool only_one_column; | |
| static VBool only_one_column_value; | |
| public: | public: |
| Hash_sql_event_handlers( | Hash_sql_event_handlers( |
| const String& astatement_string, const char* astatement_cstr, | const String& astatement_string, const char* astatement_cstr, |
| Line 53 public: | Line 55 public: |
| distinct(adistinct), | distinct(adistinct), |
| rows_hash(arows_hash), | rows_hash(arows_hash), |
| row_hash(0), | row_hash(0), |
| column_index(0) { | column_index(0), |
| only_one_column(false) { | |
| } | } |
| bool add_column(SQL_Error& error, const char* str, size_t length) { | bool add_column(SQL_Error& error, const char* str, size_t length) { |
| try { | try { |
| Line 65 public: | Line 68 public: |
| } | } |
| } | } |
| bool before_rows(SQL_Error& error) { | bool before_rows(SQL_Error& error) { |
| if(columns.count()<=1) { | if(columns.count()<1) { |
| error=SQL_Error("parser.runtime", | error=SQL_Error("parser.runtime", "no columns"); |
| /*method_name,*/ | |
| "column count must be more than 1 to create a hash"); | |
| return true; | return true; |
| } | } |
| only_one_column=columns.count()==1; | |
| return false; | return false; |
| } | } |
| Line 83 public: | Line 85 public: |
| String& cell=*new String; | String& cell=*new String; |
| if(length) | if(length) |
| cell.append_know_length(ptr, length, String::L_TAINTED); | cell.append_know_length(ptr, length, String::L_TAINTED); |
| if(column_index==0) { | 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; | 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? | duplicate=rows_hash.put_dont_replace(cell, row_vhash); // put. existed? |
| if(!distinct) { | |
| error=SQL_Error("parser.runtime", | |
| /*cell,*/ | |
| "duplicate key"); | |
| return true; | |
| } | |
| } else | } else |
| row_hash->put(*columns[column_index], new VString(cell)); | row_hash->put(*columns[column_index], new VString(cell)); |
| column_index++; | |
| if(duplicate & !distinct) { | |
| error=SQL_Error("parser.runtime", "duplicate key"); | |
| return true; | |
| } | |
| column_index++; | |
| return false; | return false; |
| } catch(...) { | } catch(...) { |
| error=SQL_Error("exception occured in Hash_sql_event_handlers::add_row_cell"); | error=SQL_Error("exception occured in Hash_sql_event_handlers::add_row_cell"); |
| Line 105 public: | Line 109 public: |
| } | } |
| }; | }; |
| VBool Hash_sql_event_handlers::only_one_column_value(true); | |
| #endif | #endif |
| static void copy_all_overwrite_to( | static void copy_all_overwrite_to( |
| Line 115 static void copy_all_overwrite_to( | Line 121 static void copy_all_overwrite_to( |
| } | } |
| static void _create_or_add(Request& r, 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& vsrc=params.as_no_junction(0, "param must be hash"); |
| if(HashStringValue* b=vb.get_hash()) | if(HashStringValue* src=vsrc.get_hash()) { |
| b->for_each(copy_all_overwrite_to, &(GET_SELF(r, VHash).hash())); | HashStringValue* self=&(GET_SELF(r, VHash).hash()); |
| if(src==self) // same: doing nothing | |
| return; | |
| src->for_each(copy_all_overwrite_to, self); | |
| } | |
| } | } |
| } | } |
| Line 128 static void remove_key_from( | Line 138 static void remove_key_from( |
| dest->remove(key); | dest->remove(key); |
| } | } |
| static void _sub(Request& r, MethodParams& params) { | static void _sub(Request& r, MethodParams& params) { |
| Value& vb=params.as_no_junction(0, "param must be hash"); | Value& vsrc=params.as_no_junction(0, "param must be hash"); |
| if(HashStringValue* b=vb.get_hash()) | if(HashStringValue* src=vsrc.get_hash()) { |
| b->for_each(remove_key_from, &GET_SELF(r, VHash).hash()); | HashStringValue* self=&(GET_SELF(r, VHash).hash()); |
| if(src==self) { // same: clearing | |
| self->clear(); | |
| return; | |
| } | |
| src->for_each(remove_key_from, self); | |
| } | |
| } | } |
| static void copy_all_dontoverwrite_to( | static void copy_all_dontoverwrite_to( |
| Line 143 static void _union(Request& r, MethodPar | Line 159 static void _union(Request& r, MethodPar |
| // 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& vsrc=params.as_no_junction(0, "param must be hash"); |
| if(HashStringValue* b=vb.get_hash()) | if(HashStringValue* src=vsrc.get_hash()) |
| b->for_each(copy_all_dontoverwrite_to, result.get_hash()); | src->for_each(copy_all_dontoverwrite_to, result.get_hash()); |
| // return result | // return result |
| r.write_no_lang(result); | r.write_no_lang(result); |
| Line 244 static void _sql(Request& r, MethodParam | Line 260 static void _sql(Request& r, MethodParam |
| distinct, | distinct, |
| hash); | hash); |
| r.connection()->query( | r.connection()->query( |
| statement_cstr, offset, limit, | statement_cstr, |
| 0, 0, | |
| offset, limit, | |
| handlers, | handlers, |
| statement_string); | statement_string); |
| } | } |
| Line 257 static void keys_collector( | Line 275 static void keys_collector( |
| *row+=new String(key, String::L_TAINTED); | *row+=new String(key, String::L_TAINTED); |
| *table+=row; | *table+=row; |
| } | } |
| static void _keys(Request& r, MethodParams&) { | 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); | Table::columns_type columns(new ArrayString); |
| *columns+=new String("key"); | *columns+=keys_column_name; |
| Table* table=new Table(columns); | Table* table=new Table(columns); |
| GET_SELF(r, VHash).hash().for_each(keys_collector, table); | GET_SELF(r, VHash).hash().for_each(keys_collector, table); |
| Line 346 MHash::MHash(): Methoded("hash") | Line 370 MHash::MHash(): Methoded("hash") |
| // ^hash:sql[query][$.limit(1) $.offset(2)] | // ^hash:sql[query][$.limit(1) $.offset(2)] |
| add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2); | add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2); |
| // ^hash._keys[] | // ^hash._keys[[column name]] |
| add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 0); | add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 1); |
| // ^hash._count[] | // ^hash._count[] |
| add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0); | add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0); |