/** @file
Parser: @b hash parser class.
Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
$Id: hash.C,v 1.23 2001/10/19 12:43:29 parser Exp $
*/
#include "classes.h"
#include "pa_request.h"
#include "pa_vhash.h"
#include "pa_vvoid.h"
#include "pa_sql_connection.h"
#include "pa_vtable.h"
#include "pa_vbool.h"
// defines
#define HASH_CLASS_NAME "hash"
// class
class MHash : public Methoded {
public: // VStateless_class
Value *create_new_value(Pool& pool) { return new(pool) VHash(pool); }
public:
MHash(Pool& pool);
public: // Methoded
bool used_directly() { return true; }
};
// methods
#ifndef DOXYGEN
class Hash_sql_event_handlers : public SQL_Driver_query_event_handlers {
public:
Hash_sql_event_handlers(Pool& apool, const String& amethod_name,
const String& astatement_string, const char *astatement_cstr,
Hash& arows_hash) :
pool(apool),
method_name(amethod_name),
statement_string(astatement_string),
statement_cstr(astatement_cstr),
rows_hash(arows_hash),
columns(pool),
row_index(0) {
}
void add_column(void *ptr, size_t size) {
String *column=new(pool) String(pool);
column->APPEND_TAINTED(
(const char *)ptr, size,
statement_cstr, 0);
columns+=column;
}
void before_rows() {
if(columns.size()<=1)
throw Exception(0, 0,
&method_name,
"column count must be more than 1 to create a hash");
}
void add_row() {
column_index=0;
}
void add_row_cell(void *ptr, size_t size) {
String *cell=new(pool) String(pool);
if(size)
cell->APPEND_TAINTED(
(const char *)ptr, size,
statement_cstr, row_index++);
if(column_index==0) {
VHash *row_vhash=new(pool) VHash(pool);
row_hash=row_vhash->get_hash();
rows_hash.put(*cell, row_vhash);
} else
row_hash->put(*columns.get_string(column_index), new(pool) VString(*cell));
column_index++;
}
private:
Pool& pool;
const String& method_name;
const String& statement_string; const char *statement_cstr;
Hash& rows_hash;
Hash *row_hash;
int column_index;
Array columns;
int row_index;
};
#endif
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();
if(params->size()) {
Value& vb=params->as_no_junction(0, "param must be hash");
if(Hash *b=vb.get_hash())
b->for_each(copy_all_overwrite_to, &static_cast<VHash *>(r.self)->hash());
}
}
static void remove_key_from(const Hash::Key& key, Hash::Val *value, void *info) {
Hash& dest=*static_cast<Hash *>(info);
dest.put(key, 0);
}
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())
b->for_each(remove_key_from, &static_cast<VHash *>(r.self)->hash());
}
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();
// dest = copy of self
Hash& dest=*new(pool) Hash(static_cast<VHash *>(r.self)->hash());
// dest += b
Value& vb=params->as_no_junction(0, "param must be hash");
if(Hash *b=vb.get_hash())
b->for_each(copy_all_dontoverwrite_to, &dest);
// return result
Value& result=*new(pool) VHash(pool, dest);
result.set_name(method_name);
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()) {
Copy_intersection_to_info info={
b,
&dest
};
static_cast<VHash *>(r.self)->hash().for_each(copy_intersection_to, &info);
}
// return result
Value& result=*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) {
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())
yes=static_cast<VHash *>(r.self)->hash().first_that(intersects, b)!=0;
// return result
Value& result=*new(pool) VBool(pool, yes);
result.set_name(method_name);
r.write_no_lang(result);
}
static void _sql(Request& r, const String& method_name, MethodParams *params) {
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");
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;
if(params->size()>2) {
Value& offset_code=params->as_junction(2, "offset must be expression");
offset=(ulong)r.process(offset_code).as_double();
}
Temp_lang temp_lang(r, String::UL_SQL);
const String& statement_string=r.process(statement).as_string();
const char *statement_cstr=
statement_string.cstr(String::UL_UNSPECIFIED, r.connection);
Hash& hash=static_cast<VHash *>(r.self)->hash();
hash.clear();
Hash_sql_event_handlers handlers(pool, method_name,
statement_string, statement_cstr, hash);
r.connection->query(
statement_cstr, offset, limit,
handlers);
}
static void keys_collector(const Hash::Key& key, Hash::Val *value, void *info) {
Table& table=*static_cast<Table *>(info);
Pool& pool=table.pool();
Array& row=*new(pool) Array(pool);
row+=&key;
table+=&row;
}
static void _keys(Request& r, const String& method_name, MethodParams *) {
Pool& pool=r.pool();
Array& columns=*new(pool) Array(pool);
columns+=new(pool) String(pool, "key");
Table& table=*new(pool) Table(pool, &method_name, &columns);
static_cast<VHash *>(r.self)->hash().for_each(keys_collector, &table);
VTable& result=*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();
Value& result=*new(pool) VInt(pool, static_cast<VHash *>(r.self)->hash().size());
result.set_name(method_name);
r.write_no_lang(result);
}
// constructor
MHash::MHash(Pool& apool) : Methoded(apool) {
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:sql[query][(count[;offset])]
add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3);
// ^hash._keys[]
add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 0);
// ^hash._count[]
add_native_method("_count", Method::CT_DYNAMIC, _count, 0, 0);
}
// global variable
Methoded *hash_base_class;
// creator
Methoded *MHash_create(Pool& pool) {
return hash_base_class=new(pool) MHash(pool);
}
E-mail: