|
|
| version 1.26, 2001/11/01 14:59:57 | version 1.35, 2002/02/08 08:30:10 |
|---|---|
| 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, 2002 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| $Id$ | $Id$ |
| */ | */ |
| Line 95 static void copy_all_overwrite_to(const | Line 95 static void copy_all_overwrite_to(const |
| Hash& dest=*static_cast<Hash *>(info); | Hash& dest=*static_cast<Hash *>(info); |
| 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, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| if(params->size()) { | if(params->size()) { |
| Value& vb=params->as_no_junction(0, "param must be hash"); | Value& vb=params->as_no_junction(0, "param must be hash"); |
| if(Hash *b=vb.get_hash(&method_name)) | if(Hash *b=vb.get_hash(&method_name)) |
| b->for_each(copy_all_overwrite_to, &static_cast<VHash *>(r.self)->hash()); | b->for_each(copy_all_overwrite_to, &static_cast<VHash *>(r.self)->hash(&method_name)); |
| } | } |
| } | } |
| Line 110 static void remove_key_from(const Hash:: | Line 109 static void remove_key_from(const Hash:: |
| Hash& dest=*static_cast<Hash *>(info); | Hash& dest=*static_cast<Hash *>(info); |
| dest.remove(key); | dest.remove(key); |
| } | } |
| static void _sub(Request& r, const String& method_name, MethodParams *params) { | static void _sub(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& vb=params->as_no_junction(0, "param must be hash"); | Value& vb=params->as_no_junction(0, "param must be hash"); |
| if(Hash *b=vb.get_hash(&method_name)) | if(Hash *b=vb.get_hash(&method_name)) |
| b->for_each(remove_key_from, &static_cast<VHash *>(r.self)->hash()); | b->for_each(remove_key_from, &static_cast<VHash *>(r.self)->hash(&method_name)); |
| } | } |
| static void copy_all_dontoverwrite_to(const Hash::Key& key, Hash::Val *value, void *info) { | static void copy_all_dontoverwrite_to(const Hash::Key& key, Hash::Val *value, void *info) { |
| Hash& dest=*static_cast<Hash *>(info); | Hash& dest=*static_cast<Hash *>(info); |
| 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, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| // dest = copy of self | // dest = copy of self |
| Hash& dest=*new(pool) Hash(static_cast<VHash *>(r.self)->hash()); | Hash& dest=*new(pool) Hash(static_cast<VHash *>(r.self)->hash(&method_name)); |
| // 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(Hash *b=vb.get_hash(&method_name)) | if(Hash *b=vb.get_hash(&method_name)) |
| Line 153 static void copy_intersection_to(const H | Line 150 static void copy_intersection_to(const H |
| if(i.b->get(key)) | if(i.b->get(key)) |
| i.dest->put_dont_replace(key, value); | i.dest->put_dont_replace(key, value); |
| } | } |
| static void _intersection(Request& r, const String& method_name, MethodParams *params) { | static void _intersection(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Line 166 static void _intersection(Request& r, co | Line 162 static void _intersection(Request& r, co |
| b, | b, |
| &dest | &dest |
| }; | }; |
| static_cast<VHash *>(r.self)->hash().for_each(copy_intersection_to, &info); | static_cast<VHash *>(r.self)->hash(&method_name).for_each(copy_intersection_to, &info); |
| } | } |
| // return result | // return result |
| Line 189 static void _intersects(Request& r, cons | Line 185 static void _intersects(Request& r, cons |
| // 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(Hash *b=vb.get_hash(&method_name)) | if(Hash *b=vb.get_hash(&method_name)) |
| yes=static_cast<VHash *>(r.self)->hash().first_that(intersects, b)!=0; | yes=static_cast<VHash *>(r.self)->hash(&method_name).first_that(intersects, b)!=0; |
| // return result | // return result |
| Value& result=*new(pool) VBool(pool, yes); | Value& result=*new(pool) VBool(pool, yes); |
| Line 201 static void _intersects(Request& r, cons | Line 197 static void _intersects(Request& r, cons |
| static void _sql(Request& r, const String& method_name, MethodParams *params) { | static void _sql(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| if(!r.connection) | |
| throw Exception(0, 0, | |
| &method_name, | |
| "without connect"); | |
| 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; |
| if(params->size()>1) { | |
| Value& limit_code=params->as_junction(1, "limit must be expression"); | |
| limit=(uint)r.process(limit_code).as_double(); | |
| } | |
| ulong offset=0; | ulong offset=0; |
| if(params->size()>2) { | if(params->size()>1) { |
| Value& offset_code=params->as_junction(2, "offset must be expression"); | Value& voptions=params->as_no_junction(1, "options must be hash, not code"); |
| offset=(ulong)r.process(offset_code).as_double(); | if(voptions.is_defined()) |
| 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(); | |
| } else | |
| throw Exception(0, 0, | |
| &method_name, | |
| "options must be hash"); | |
| } | } |
| Temp_lang temp_lang(r, String::UL_SQL); | Temp_lang temp_lang(r, String::UL_SQL); |
| const String& statement_string=r.process(statement).as_string(); | const String& statement_string=r.process(statement).as_string(); |
| const char *statement_cstr= | const char *statement_cstr= |
| statement_string.cstr(String::UL_UNSPECIFIED, r.connection); | statement_string.cstr(String::UL_UNSPECIFIED, r.connection(&method_name)); |
| Hash& hash=static_cast<VHash *>(r.self)->hash(); | Hash& hash=static_cast<VHash *>(r.self)->hash(&method_name); |
| hash.clear(); | hash.clear(); |
| Hash_sql_event_handlers handlers(pool, method_name, | Hash_sql_event_handlers handlers(pool, method_name, |
| statement_string, statement_cstr, hash); | statement_string, statement_cstr, hash); |
| r.connection->query( | r.connection(&method_name)->query( |
| statement_cstr, offset, limit, | statement_cstr, offset, limit, |
| handlers); | handlers); |
| } | } |
| Line 249 static void _keys(Request& r, const Stri | Line 244 static void _keys(Request& r, const Stri |
| columns+=new(pool) String(pool, "key"); | columns+=new(pool) String(pool, "key"); |
| Table& table=*new(pool) Table(pool, &method_name, &columns); | Table& table=*new(pool) Table(pool, &method_name, &columns); |
| static_cast<VHash *>(r.self)->hash().for_each(keys_collector, &table); | static_cast<VHash *>(r.self)->hash(&method_name).for_each(keys_collector, &table); |
| VTable& result=*new(pool) VTable(pool, &table); | VTable& result=*new(pool) VTable(pool, &table); |
| result.set_name(method_name); | result.set_name(method_name); |
| Line 259 static void _keys(Request& r, const Stri | Line 254 static void _keys(Request& r, const Stri |
| static void _count(Request& r, const String& method_name, MethodParams *) { | static void _count(Request& r, const String& method_name, MethodParams *) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Value& result=*new(pool) VInt(pool, static_cast<VHash *>(r.self)->hash().size()); | Value& result=*new(pool) VInt(pool, static_cast<VHash *>(r.self)->hash(&method_name).size()); |
| result.set_name(method_name); | result.set_name(method_name); |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| Line 267 static void _count(Request& r, const Str | Line 262 static void _count(Request& r, const Str |
| static void _delete(Request& r, const String& method_name, MethodParams *params) { | static void _delete(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| static_cast<VHash *>(r.self)->hash().remove(params->as_string(0, "key must be string")); | static_cast<VHash *>(r.self)->hash(&method_name).remove(params->as_string(0, "key must be string")); |
| } | } |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| Line 278 struct Foreach_info { | Line 273 struct Foreach_info { |
| Value *body_code; | Value *body_code; |
| Value *delim_maybe_code; | Value *delim_maybe_code; |
| Value *var_context; | |
| VString *vkey; | VString *vkey; |
| bool need_delim; | bool need_delim; |
| }; | }; |
| Line 289 static void one_foreach_cycle(const Hash | Line 283 static void one_foreach_cycle(const Hash |
| Foreach_info& i=*static_cast<Foreach_info *>(info); | Foreach_info& i=*static_cast<Foreach_info *>(info); |
| i.vkey->set_string(akey); | i.vkey->set_string(akey); |
| i.var_context->put_element(*i.key_var_name, i.vkey); | i.r->root->put_element(*i.key_var_name, i.vkey); |
| i.var_context->put_element(*i.value_var_name, static_cast<Value *>(avalue)); | i.r->root->put_element(*i.value_var_name, static_cast<Value *>(avalue)); |
| Value& processed_body=i.r->process(*i.body_code); | Value& processed_body=i.r->process(*i.body_code); |
| if(i.delim_maybe_code) { // delimiter set? | if(i.delim_maybe_code) { // delimiter set? |
| Line 314 static void _foreach(Request& r, const S | Line 308 static void _foreach(Request& r, const S |
| &body_code, | &body_code, |
| delim_maybe_code, | delim_maybe_code, |
| body_code.get_junction()->wcontext, | |
| new(pool) VString(pool), | new(pool) VString(pool), |
| false | false |
| }; | }; |
| static_cast<VHash *>(r.self)->hash().for_each(one_foreach_cycle, &info); | VHash& self=*static_cast<VHash *>(r.self); |
| Hash& hash=self.hash(&method_name); | |
| VHash_lock lock(self); | |
| hash.for_each(one_foreach_cycle, &info); | |
| } | } |
| // constructor | // constructor |
| Line 342 MHash::MHash(Pool& apool) : Methoded(apo | Line 338 MHash::MHash(Pool& apool) : Methoded(apo |
| // ^a.delete[key] | // ^a.delete[key] |
| add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1); | add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1); |
| // ^hash:sql[query][(count[;offset])] | // ^hash:sql[query][$.limit(1) $.offset(2)] |
| add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3); | add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 2); |
| // ^hash._keys[] | // ^hash._keys[] |
| add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 0); | add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 0); |
| Line 352 MHash::MHash(Pool& apool) : Methoded(apo | Line 348 MHash::MHash(Pool& apool) : Methoded(apo |
| add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0); | add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0); |
| // ^hash.foreach[key;value]{code}[delim] | // ^hash.foreach[key;value]{code}[delim] |
| Array *for_each_locals=NEW Array(pool(), 2); | |
| *for_each_locals += NEW String(pool(), "key"); | |
| add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1); | add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1); |
| } | } |