|
|
| version 1.35, 2002/02/08 08:30:10 | version 1.48, 2002/09/17 09:30:06 |
|---|---|
| Line 3 | Line 3 |
| Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001, 2002 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) |
| $Id$ | |
| */ | */ |
| static const char* IDENT_HASH_C="$Date$"; | |
| #include "classes.h" | #include "classes.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_vhash.h" | #include "pa_vhash.h" |
| Line 16 | Line 16 |
| #include "pa_vbool.h" | #include "pa_vbool.h" |
| #include "pa_vmethod_frame.h" | #include "pa_vmethod_frame.h" |
| // defines | |
| #define HASH_CLASS_NAME "hash" | |
| // class | // class |
| class MHash : public Methoded { | class MHash : public Methoded { |
| Line 57 public: | Line 53 public: |
| } | } |
| void before_rows() { | void before_rows() { |
| if(columns.size()<=1) | if(columns.size()<=1) |
| throw Exception(0, 0, | throw Exception("parser.runtime", |
| &method_name, | &method_name, |
| "column count must be more than 1 to create a hash"); | "column count must be more than 1 to create a hash"); |
| } | } |
| Line 73 public: | Line 69 public: |
| if(column_index==0) { | if(column_index==0) { |
| VHash *row_vhash=new(pool) VHash(pool); | VHash *row_vhash=new(pool) VHash(pool); |
| row_hash=row_vhash->get_hash(0); | row_hash=row_vhash->get_hash(0); |
| rows_hash.put(*cell, row_vhash); | if(rows_hash.put_dont_replace(*cell, row_vhash)) // put. existed? |
| throw Exception("parser.runtime", | |
| cell, | |
| "duplicate key"); | |
| } else | } else |
| row_hash->put(*columns.get_string(column_index), new(pool) VString(*cell)); | row_hash->put(*columns.get_string(column_index), new(pool) VString(*cell)); |
| column_index++; | column_index++; |
| Line 133 static void _union(Request& r, const Str | Line 132 static void _union(Request& r, const Str |
| // return result | // return result |
| Value& result=*new(pool) VHash(pool, dest); | Value& result=*new(pool) VHash(pool, dest); |
| result.set_name(method_name); | |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| Line 166 static void _intersection(Request& r, co | Line 164 static void _intersection(Request& r, co |
| } | } |
| // return result | // return result |
| Value& result=*new(pool) VHash(pool, dest); | r.write_no_lang(*new(pool) VHash(pool, dest)); |
| result.set_name(method_name); | |
| r.write_no_lang(result); | |
| } | } |
| static void *intersects(const Hash::Key& key, Hash::Val *value, void *info) { | static void *intersects(const Hash::Key& key, Hash::Val *value, void *info) { |
| Line 188 static void _intersects(Request& r, cons | Line 184 static void _intersects(Request& r, cons |
| yes=static_cast<VHash *>(r.self)->hash(&method_name).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); | r.write_no_lang(*new(pool) VBool(pool, yes)); |
| result.set_name(method_name); | |
| r.write_no_lang(result); | |
| } | } |
| Line 203 static void _sql(Request& r, const Strin | Line 197 static void _sql(Request& r, const Strin |
| ulong offset=0; | ulong offset=0; |
| if(params->size()>1) { | if(params->size()>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_defined()) | if(!voptions.is_string()) |
| if(Hash *options=voptions.get_hash(&method_name)) { | if(Hash *options=voptions.get_hash(&method_name)) { |
| if(Value *vlimit=(Value *)options->get(*sql_limit_name)) | if(Value *vlimit=(Value *)options->get(*sql_limit_name)) |
| limit=(ulong)r.process(*vlimit).as_double(); | limit=(ulong)r.process_to_value(*vlimit).as_double(); |
| if(Value *voffset=(Value *)options->get(*sql_offset_name)) | if(Value *voffset=(Value *)options->get(*sql_offset_name)) |
| offset=(ulong)r.process(*voffset).as_double(); | offset=(ulong)r.process_to_value(*voffset).as_double(); |
| } else | } else |
| throw Exception(0, 0, | throw Exception("parser.runtime", |
| &method_name, | &method_name, |
| "options must be hash"); | "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_to_string(statement); |
| const char *statement_cstr= | const char *statement_cstr= |
| statement_string.cstr(String::UL_UNSPECIFIED, r.connection(&method_name)); | statement_string.cstr(String::UL_UNSPECIFIED, r.connection(&method_name)); |
| Hash& hash=static_cast<VHash *>(r.self)->hash(&method_name); | Hash& hash=static_cast<VHash *>(r.self)->hash(&method_name); |
| Line 226 static void _sql(Request& r, const Strin | Line 220 static void _sql(Request& r, const Strin |
| r.connection(&method_name)->query( | r.connection(&method_name)->query( |
| statement_cstr, offset, limit, | statement_cstr, offset, limit, |
| handlers); | handlers, |
| statement_string); | |
| } | } |
| static void keys_collector(const Hash::Key& key, Hash::Val *value, void *info) { | static void keys_collector(const Hash::Key& key, Hash::Val *value, void *info) { |
| Line 246 static void _keys(Request& r, const Stri | Line 241 static void _keys(Request& r, const Stri |
| static_cast<VHash *>(r.self)->hash(&method_name).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); | r.write_no_lang(*new(pool) VTable(pool, &table)); |
| result.set_name(method_name); | |
| r.write_no_lang(result); | |
| } | } |
| 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(&method_name).size()); | r.write_no_lang( |
| result.set_name(method_name); | *new(pool) VInt(pool, static_cast<VHash *>(r.self)->hash(&method_name).size())); |
| r.write_no_lang(result); | |
| } | } |
| static void _delete(Request& r, const String& method_name, MethodParams *params) { | static void _delete(Request& r, const String& method_name, MethodParams *params) { |
| Line 283 static void one_foreach_cycle(const Hash | Line 275 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.r->root->put_element(*i.key_var_name, i.vkey); | i.r->method_frame->put_element(*i.key_var_name, i.vkey, false); |
| i.r->root->put_element(*i.value_var_name, static_cast<Value *>(avalue)); | i.r->method_frame->put_element(*i.value_var_name, static_cast<Value *>(avalue), false); |
| Value& processed_body=i.r->process(*i.body_code); | StringOrValue sv_processed=i.r->process(*i.body_code); |
| if(i.delim_maybe_code) { // delimiter set? | const String *s_processed=sv_processed.get_string(); |
| const String *string=processed_body.get_string(); | if(i.delim_maybe_code && s_processed && s_processed->size()) { // delimiter set and we have body |
| if(i.need_delim && string && string->size()) // need delim & iteration produced string? | if(i.need_delim) // need delim & iteration produced string? |
| i.r->write_pass_lang(i.r->process(*i.delim_maybe_code)); | i.r->write_pass_lang(i.r->process(*i.delim_maybe_code)); |
| i.need_delim=true; | 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) { | static void _foreach(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Line 319 static void _foreach(Request& r, const S | Line 311 static void _foreach(Request& r, const S |
| // constructor | // constructor |
| MHash::MHash(Pool& apool) : Methoded(apool) { | MHash::MHash(Pool& apool) : Methoded(apool, "hash") |
| set_name(*NEW String(pool(), HASH_CLASS_NAME)); | { |
| // ^hash::create[[copy_from]] | // ^hash::create[[copy_from]] |
| add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1); | add_native_method("create", Method::CT_DYNAMIC, _create_or_add, 0, 1); |
| // ^hash.add[add_from] | // ^hash.add[add_from] |
| Line 353 MHash::MHash(Pool& apool) : Methoded(apo | Line 344 MHash::MHash(Pool& apool) : Methoded(apo |
| // global variable | // global variable |
| Methoded *hash_base_class; | Methoded *hash_class; |
| // creator | // creator |
| Methoded *MHash_create(Pool& pool) { | Methoded *MHash_create(Pool& pool) { |
| return hash_base_class=new(pool) MHash(pool); | return hash_class=new(pool) MHash(pool); |
| } | } |