|
|
| version 1.3, 2001/05/21 16:38:46 | version 1.15, 2001/09/06 08:25:08 |
|---|---|
| Line 7 | Line 7 |
| $Id$ | $Id$ |
| */ | */ |
| static const char *RCSId="$Id$"; | |
| #include "classes.h" | #include "classes.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_vhash.h" | #include "pa_vhash.h" |
| #include "pa_vunknown.h" | #include "pa_vvoid.h" |
| #include "pa_sql_connection.h" | #include "pa_sql_connection.h" |
| #include "pa_vtable.h" | |
| // defines | // defines |
| Line 32 public: // Methoded | Line 34 public: // Methoded |
| // methods | // methods |
| static void _default(Request& r, const String&, MethodParams *params) { | #ifndef DOXYGEN |
| Pool& pool=r.pool(); | class Hash_sql_event_handlers : public SQL_Driver_query_event_handlers { |
| public: | |
| VHash& vhash=*static_cast<VHash *>(r.self); | Hash_sql_event_handlers(Pool& apool, const String& amethod_name, |
| if(params->size()) | const String& astatement_string, const char *astatement_cstr, |
| vhash.set_default(params->get(0)); // info: may be code.. | Hash& arows_hash) : |
| else { | pool(apool), |
| Value *default_value=vhash.get_default(); | method_name(amethod_name), |
| r.write_assign_lang(default_value?*default_value:*new(pool) VUnknown(pool)); | 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) { | static void _sql(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| Line 52 static void _sql(Request& r, const Strin | Line 99 static void _sql(Request& r, const Strin |
| &method_name, | &method_name, |
| "without connect"); | "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; | ulong limit=0; |
| if(params->size()>1) { | 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(); | limit=(uint)r.process(limit_code).as_double(); |
| } | } |
| ulong offset=0; | ulong offset=0; |
| if(params->size()>2) { | 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(); | offset=(ulong)r.process(offset_code).as_double(); |
| } | } |
| Line 70 static void _sql(Request& r, const Strin | Line 117 static void _sql(Request& r, const Strin |
| 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); |
| unsigned int sql_column_count; SQL_Driver::Cell *sql_columns; | Hash& hash=static_cast<VHash *>(r.self)->hash(); |
| unsigned long sql_row_count; SQL_Driver::Cell **sql_rows; | hash.clear(); |
| Hash_sql_event_handlers handlers(pool, method_name, | |
| statement_string, statement_cstr, hash); | |
| bool need_rethrow=false; Exception rethrow_me; | bool need_rethrow=false; Exception rethrow_me; |
| PTRY { | PTRY { |
| r.connection->query( | r.connection->query( |
| statement_cstr, offset, limit, | statement_cstr, offset, limit, |
| &sql_column_count, &sql_columns, | handlers); |
| &sql_row_count, &sql_rows); | |
| } | } |
| PCATCH(e) { // query problem | PCATCH(e) { // query problem |
| rethrow_me=e; need_rethrow=true; | rethrow_me=e; need_rethrow=true; |
| Line 87 static void _sql(Request& r, const Strin | Line 135 static void _sql(Request& r, const Strin |
| PTHROW(rethrow_me.type(), rethrow_me.code(), | PTHROW(rethrow_me.type(), rethrow_me.code(), |
| &statement_string, // setting more specific source [were url] | &statement_string, // setting more specific source [were url] |
| rethrow_me.comment()); | rethrow_me.comment()); |
| } | |
| Hash& rows_hash=static_cast<VHash *>(r.self)->hash(); | static void keys_collector(const Hash::Key& key, Hash::Val *value, void *info) { |
| rows_hash.clear(); | Table& table=*static_cast<Table *>(info); |
| Pool& pool=table.pool(); | |
| if(sql_column_count<=1) | |
| return; | 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); | Array& columns=*new(pool) Array(pool); |
| for(unsigned int i=0+1; i<sql_column_count; i++) { | columns+=new(pool) String(pool, "key"); |
| String& column=*new(pool) String(pool); | Table& table=*new(pool) Table(pool, &method_name, &columns); |
| column.APPEND_TAINTED( | |
| (const char *)sql_columns[i].ptr, sql_columns[i].size, | static_cast<VHash *>(r.self)->hash().for_each(keys_collector, &table); |
| statement_cstr, 0); | |
| columns+=&column; | VTable& result=*new(pool) VTable(pool, &table); |
| } | result.set_name(method_name); |
| r.write_no_lang(result); | |
| for(unsigned long row=0; row<sql_row_count; row++) { | |
| SQL_Driver::Cell *sql_cells=sql_rows[row]; | |
| VHash& row_vhash=*new(pool) VHash(pool); | |
| Hash& row_hash=*row_vhash.get_hash(); | |
| String *key=0; // calm, compiler | |
| String *cell; | |
| for(unsigned int i=0; i<sql_column_count; i++) { | |
| cell=new(pool) String(pool); | |
| cell->APPEND_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); | |
| } | |
| } | } |
| // constructor | // constructor |
| Line 129 static void _sql(Request& r, const Strin | Line 164 static void _sql(Request& r, const Strin |
| MHash::MHash(Pool& apool) : Methoded(apool) { | MHash::MHash(Pool& apool) : Methoded(apool) { |
| set_name(*NEW String(pool(), HASH_CLASS_NAME)); | set_name(*NEW String(pool(), HASH_CLASS_NAME)); |
| // ^hash.default[] | |
| // ^hash.default[hash] | |
| add_native_method("default", Method::CT_DYNAMIC, _default, 0, 1); | |
| // ^hash:sql[query][(count[;offset])] | // ^hash:sql[query][(count[;offset])] |
| add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3); | add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3); |
| // ^hash._keys[] | |
| add_native_method("_keys", Method::CT_DYNAMIC, _keys, 0, 0); | |
| } | } |
| // global variable | // global variable |