--- parser3/src/classes/hash.C 2001/05/21 16:01:10 1.2 +++ parser3/src/classes/hash.C 2001/08/06 09:33:19 1.13 @@ -5,14 +5,16 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: hash.C,v 1.2 2001/05/21 16:01:10 parser Exp $ + $Id: hash.C,v 1.13 2001/08/06 09:33:19 parser Exp $ */ +static const char *RCSId="$Id: hash.C,v 1.13 2001/08/06 09:33:19 parser Exp $"; #include "classes.h" #include "pa_request.h" #include "pa_vhash.h" -#include "pa_vunknown.h" +#include "pa_vvoid.h" #include "pa_sql_connection.h" +#include "pa_vtable.h" // defines @@ -40,10 +42,67 @@ static void _default(Request& r, const S 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) VUnknown(pool)); + r.write_assign_lang(default_value?*default_value:*new(pool) VVoid(pool)); } } +#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) + PTHROW(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 _sql(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); @@ -52,17 +111,17 @@ static void _sql(Request& r, const Strin &method_name, "without connect"); - Value& statement=params->get_junction(0, "statement must be code"); + Value& statement=params->as_junction(0, "statement must be code"); ulong limit=0; if(params->size()>1) { - Value& limit_code=params->get_junction(1, "limit must be expression"); + 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->get_junction(2, "offset must be expression"); + Value& offset_code=params->as_junction(2, "offset must be expression"); offset=(ulong)r.process(offset_code).as_double(); } @@ -70,16 +129,17 @@ static void _sql(Request& r, const Strin const String& statement_string=r.process(statement).as_string(); const char *statement_cstr= statement_string.cstr(String::UL_UNSPECIFIED, r.connection); - unsigned int sql_column_count; SQL_Driver::Cell *sql_columns; - unsigned long sql_row_count; SQL_Driver::Cell **sql_rows; + Hash& hash=static_cast(r.self)->hash(); + hash.clear(); + Hash_sql_event_handlers handlers(pool, method_name, + statement_string, statement_cstr, hash); bool need_rethrow=false; Exception rethrow_me; PTRY { r.connection->query( statement_cstr, offset, limit, - &sql_column_count, &sql_columns, - &sql_row_count, &sql_rows); + handlers); } - PCATCH(e) { // connect/process problem + PCATCH(e) { // query problem rethrow_me=e; need_rethrow=true; } PEND_CATCH @@ -87,41 +147,28 @@ static void _sql(Request& r, const Strin PTHROW(rethrow_me.type(), rethrow_me.code(), &statement_string, // setting more specific source [were url] rethrow_me.comment()); +} - Hash& rows_hash=static_cast(r.self)->hash(); - rows_hash.clear(); - - if(sql_column_count<=1) - return; +static void keys_collector(const Hash::Key& key, Hash::Val *value, void *info) { + Table& table=*static_cast(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); - for(unsigned int i=0+1; iAPPEND_TAINTED( - (const char *)sql_cells[i].ptr, sql_cells[i].size, - statement_cstr, row); - if(i==0) - key=cell; - else - row_hash.put(*columns.get_string(i-1), new(pool) VString(*cell)); - } - rows_hash.put(*key, &row_vhash); - } + columns+=new(pool) String(pool, "key"); + Table& table=*new(pool) Table(pool, &method_name, &columns); + + static_cast(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); } // constructor @@ -136,6 +183,8 @@ MHash::MHash(Pool& apool) : Methoded(apo // ^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); } // global variable