Diff for /parser3/src/classes/hash.C between versions 1.14 and 1.48

version 1.14, 2001/08/09 14:28:36 version 1.48, 2002/09/17 09:30:06
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: 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* IDENT_HASH_C="$Date$";
   
 #include "classes.h"  #include "classes.h"
 #include "pa_request.h"  #include "pa_request.h"
Line 15  static const char *RCSId="$Id$"; Line 13  static const char *RCSId="$Id$";
 #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
   
Line 34  public: // Methoded Line 30  public: // Methoded
   
 // methods  // methods
   
 static void _default(Request& r, const String&, MethodParams *params) {   
         Pool& pool=r.pool();  
   
         VHash& vhash=*static_cast<VHash *>(r.self);  
         if(params->size())  
                 vhash.set_default(params->get(0)); // info: may be code..  
         else {  
                 Value *default_value=vhash.get_default();  
                 r.write_assign_lang(default_value?*default_value:*new(pool) VVoid(pool));  
         }  
 }  
   
 #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 {
 public:  public:
Line 69  public: Line 53  public:
         }          }
         void before_rows() {           void before_rows() { 
                 if(columns.size()<=1)                  if(columns.size()<=1)
                         PTHROW(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 84  public: Line 68  public:
                                 statement_cstr, row_index++);                                  statement_cstr, row_index++);
                 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();                          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 103  private: Line 90  private:
 };  };
 #endif  #endif
   
 static void _sql(Request& r, const String& method_name, MethodParams *params) {  static void copy_all_overwrite_to(const Hash::Key& key, Hash::Val *value, void *info) {
           Hash& dest=*static_cast<Hash *>(info);
           dest.put(key, value);
   }
   static void _create_or_add(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
           
           if(params->size()) {
                   Value& vb=params->as_no_junction(0, "param must be hash");
                   if(Hash *b=vb.get_hash(&method_name))
                           b->for_each(copy_all_overwrite_to, &static_cast<VHash *>(r.self)->hash(&method_name));
           }
   }
   
         if(!r.connection)  static void remove_key_from(const Hash::Key& key, Hash::Val *value, void *info) {
                 PTHROW(0, 0,          Hash& dest=*static_cast<Hash *>(info);
                         &method_name,          dest.remove(key);
                         "without connect");  }
   static void _sub(Request& r, const String& method_name, MethodParams *params) {
           Pool& pool=r.pool();
           
           Value& vb=params->as_no_junction(0, "param must be hash");
           if(Hash *b=vb.get_hash(&method_name))
                   b->for_each(remove_key_from, &static_cast<VHash *>(r.self)->hash(&method_name));
   }
   
         Value& statement=params->as_junction(0, "statement must be code");  static void copy_all_dontoverwrite_to(const Hash::Key& key, Hash::Val *value, void *info) {
           Hash& dest=*static_cast<Hash *>(info);
           dest.put_dont_replace(key, value);
   }
   static void _union(Request& r, const String& method_name, MethodParams *params) {
           Pool& pool=r.pool();
   
         ulong limit=0;          // dest = copy of self
         if(params->size()>1) {          Hash& dest=*new(pool) Hash(static_cast<VHash *>(r.self)->hash(&method_name));
                 Value& limit_code=params->as_junction(1, "limit must be expression");          // dest += b
                 limit=(uint)r.process(limit_code).as_double();          Value& vb=params->as_no_junction(0, "param must be hash");
           if(Hash *b=vb.get_hash(&method_name))
                   b->for_each(copy_all_dontoverwrite_to, &dest);
   
           // return result
           Value& result=*new(pool) VHash(pool, dest);
           r.write_no_lang(result);
   }
   
   #ifndef DOXYGEN
   struct Copy_intersection_to_info {
           Hash *b;
           Hash *dest;
   };
   #endif
   
   static void copy_intersection_to(const Hash::Key& key, Hash::Val *value, void *info) {
           Copy_intersection_to_info& i=*static_cast<Copy_intersection_to_info *>(info);
   
           if(i.b->get(key))
                   i.dest->put_dont_replace(key, value);
   }
   static void _intersection(Request& r, const String& method_name, MethodParams *params) {
           Pool& pool=r.pool();
   
           // dest = copy of self
           Hash& dest=*new(pool) Hash(pool);
           // dest += b
           Value& vb=params->as_no_junction(0, "param must be hash");
           if(Hash *b=vb.get_hash(&method_name)) {
                   Copy_intersection_to_info info={
                           b,
                           &dest
                   };
                   static_cast<VHash *>(r.self)->hash(&method_name).for_each(copy_intersection_to, &info);
         }          }
   
           // return result
           r.write_no_lang(*new(pool) VHash(pool, dest));
   }
   
   static void *intersects(const Hash::Key& key, Hash::Val *value, void *info) {
           return static_cast<Hash *>(info)->get(key);
   }
   
   static void _intersects(Request& r, const String& method_name, MethodParams *params) {
           Pool& pool=r.pool();
   
           bool yes=false;
   
           // dest = copy of self
           Hash& dest=*new(pool) Hash(pool);
           // dest += b
           Value& vb=params->as_no_junction(0, "param must be hash");
           if(Hash *b=vb.get_hash(&method_name))
                   yes=static_cast<VHash *>(r.self)->hash(&method_name).first_that(intersects, b)!=0;
   
           // return result
           r.write_no_lang(*new(pool) VBool(pool, yes));
   }
   
   
   static void _sql(Request& r, const String& method_name, MethodParams *params) {
           Pool& pool=r.pool();
   
           Value& statement=params->as_junction(0, "statement must be code");
   
           ulong limit=0;
         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_string())
                           if(Hash *options=voptions.get_hash(&method_name)) {
                                   if(Value *vlimit=(Value *)options->get(*sql_limit_name))
                                           limit=(ulong)r.process_to_value(*vlimit).as_double();
                                   if(Value *voffset=(Value *)options->get(*sql_offset_name))
                                           offset=(ulong)r.process_to_value(*voffset).as_double();
                           } else
                                   throw Exception("parser.runtime",
                                           &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_to_string(statement);
         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);
         bool need_rethrow=false; Exception rethrow_me;  
         PTRY {          r.connection(&method_name)->query(
                 r.connection->query(                  statement_cstr, offset, limit,
                         statement_cstr, offset, limit,                  handlers,
                         handlers);                  statement_string);
         }  
         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());  
 }  }
   
 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 164  static void _keys(Request& r, const Stri Line 239  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);          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 *) {
           Pool& pool=r.pool();
   
           r.write_no_lang(
                   *new(pool) VInt(pool, static_cast<VHash *>(r.self)->hash(&method_name).size()));
   }
   
   static void _delete(Request& r, const String& method_name, MethodParams *params) {
           Pool& pool=r.pool();
   
           static_cast<VHash *>(r.self)->hash(&method_name).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(const Hash::Key& akey, Hash::Val *avalue, 
                                                                                     void *info) {
           Foreach_info& i=*static_cast<Foreach_info *>(info);
   
           i.vkey->set_string(akey);
           i.r->method_frame->put_element(*i.key_var_name, i.vkey, false);
           i.r->method_frame->put_element(*i.value_var_name, static_cast<Value *>(avalue), false);
   
           StringOrValue sv_processed=i.r->process(*i.body_code);
           const String *s_processed=sv_processed.get_string();
           if(i.delim_maybe_code && s_processed && s_processed->size()) { // delimiter set and we have body
                   if(i.need_delim) // need delim & iteration produced string?
                           i.r->write_pass_lang(i.r->process(*i.delim_maybe_code));
                   i.need_delim=true;
           }
           i.r->write_pass_lang(sv_processed);
   }
   static void _foreach(Request& r, const String& method_name, MethodParams *params) {
           Pool& pool=r.pool();
           const String& key_var_name=params->as_string(0, "key-var name must be string");
           const String& value_var_name=params->as_string(1, "value-var name must be string");
           Value& body_code=params->as_junction(2, "body must be code");
           Value *delim_maybe_code=params->size()>3?&params->get(3):0;
   
           Foreach_info info={
                   &r,
                   &key_var_name, &value_var_name,
                   &body_code,
                   delim_maybe_code,
   
                   new(pool) VString(pool),
                   false
           };
           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
   
 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]]
           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);
   
         // ^hash._default[]          // ^a.delete[key]
         // ^hash._default[hash]          add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
         add_native_method("_default", Method::CT_DYNAMIC, _default, 0, 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);    
   
           // ^hash._count[]
           add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);  
   
           // ^hash.foreach[key;value]{code}[delim]
           add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
 }  }
   
 // 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);
 }  }

Removed from v.1.14  
changed lines
  Added in v.1.48


E-mail: