Annotation of parser3/src/classes/hash.C, revision 1.1.2.1
1.1 paf 1: /** @file
2: Parser: @b hash parser class.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5:
6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.1.2.1 ! parser 8: $Id: hash.C,v 1.1 2001/05/10 14:00:18 paf Exp $
1.1 paf 9: */
10:
11: #include "classes.h"
12: #include "pa_request.h"
13: #include "pa_vhash.h"
14: #include "pa_vunknown.h"
1.1.2.1 ! parser 15: #include "pa_sql_connection.h"
! 16:
! 17: // defines
! 18:
! 19: #define HASH_CLASS_NAME "hash"
1.1 paf 20:
21: // class
22:
23: class MHash : public Methoded {
1.1.2.1 ! parser 24: public: // VStateless_class
! 25: Value *create_new_value(Pool& pool) { return new(pool) VHash(pool); }
! 26:
1.1 paf 27: public:
28: MHash(Pool& pool);
29: public: // Methoded
1.1.2.1 ! parser 30: bool used_directly() { return true; }
1.1 paf 31: };
32:
33: // methods
34:
35: static void _default(Request& r, const String&, MethodParams *params) {
36: Pool& pool=r.pool();
37:
38: VHash& vhash=*static_cast<VHash *>(r.self);
39: if(params->size())
40: vhash.set_default(params->get(0)); // info: may be code..
41: else {
42: Value *default_value=vhash.get_default();
43: r.write_assign_lang(default_value?*default_value:*new(pool) VUnknown(pool));
44: }
45: }
46:
1.1.2.1 ! parser 47: static void _sql(Request& r, const String& method_name, MethodParams *params) {
! 48: Pool& pool=r.pool();
! 49:
! 50: if(!r.connection)
! 51: PTHROW(0, 0,
! 52: &method_name,
! 53: "without connect");
! 54:
! 55: Value& statement=params->get_junction(0, "statement must be code");
! 56:
! 57: ulong limit=0;
! 58: if(params->size()>1) {
! 59: Value& limit_code=params->get_junction(1, "limit must be expression");
! 60: limit=(uint)r.process(limit_code).as_double();
! 61: }
! 62:
! 63: ulong offset=0;
! 64: if(params->size()>2) {
! 65: Value& offset_code=params->get_junction(2, "offset must be expression");
! 66: offset=(ulong)r.process(offset_code).as_double();
! 67: }
! 68:
! 69: Temp_lang temp_lang(r, String::UL_SQL);
! 70: const String& statement_string=r.process(statement).as_string();
! 71: const char *statement_cstr=
! 72: statement_string.cstr(String::UL_UNSPECIFIED, r.connection);
! 73: unsigned int sql_column_count; SQL_Driver::Cell *sql_columns;
! 74: unsigned long sql_row_count; SQL_Driver::Cell **sql_rows;
! 75: bool need_rethrow=false; Exception rethrow_me;
! 76: PTRY {
! 77: r.connection->query(
! 78: statement_cstr, offset, limit,
! 79: &sql_column_count, &sql_columns,
! 80: &sql_row_count, &sql_rows);
! 81: }
! 82: PCATCH(e) { // connect/process problem
! 83: rethrow_me=e; need_rethrow=true;
! 84: }
! 85: PEND_CATCH
! 86: if(need_rethrow)
! 87: PTHROW(rethrow_me.type(), rethrow_me.code(),
! 88: &statement_string, // setting more specific source [were url]
! 89: rethrow_me.comment());
! 90:
! 91: // r.self->set_name(method_name);
! 92: Hash& rows_hash=static_cast<VHash *>(r.self)->hash();
! 93: rows_hash.clear();
! 94:
! 95: if(sql_column_count<=1)
! 96: return;
! 97:
! 98: Array& columns=*new(pool) Array(pool);
! 99: for(unsigned int i=0+1; i<sql_column_count; i++) {
! 100: String& column=*new(pool) String(pool);
! 101: column.APPEND_TAINTED(
! 102: (const char *)sql_columns[i].ptr, sql_columns[i].size,
! 103: statement_cstr, 0);
! 104: columns+=&column;
! 105: }
! 106:
! 107: for(unsigned long row=0; row<sql_row_count; row++) {
! 108: SQL_Driver::Cell *sql_cells=sql_rows[row];
! 109:
! 110: VHash& row_vhash=*new(pool) VHash(pool);
! 111: Hash& row_hash=*row_vhash.get_hash();
! 112: String *key=0; // calm, compiler
! 113: String *cell;
! 114: for(unsigned int i=0; i<sql_column_count; i++) {
! 115: cell=new(pool) String(pool);
! 116: cell->APPEND_TAINTED(
! 117: (const char *)sql_cells[i].ptr, sql_cells[i].size,
! 118: statement_cstr, row);
! 119: if(i==0)
! 120: key=cell;
! 121: else
! 122: row_hash.put(*columns.get_string(i-1), new(pool) VString(*cell));
! 123: }
! 124: rows_hash.put(*key, &row_vhash);
! 125: }
! 126: }
! 127:
1.1 paf 128: // constructor
129:
130: MHash::MHash(Pool& apool) : Methoded(apool) {
1.1.2.1 ! parser 131: set_name(*NEW String(pool(), HASH_CLASS_NAME));
! 132:
1.1 paf 133: // ^hash.default[]
134: // ^hash.default[hash]
135: add_native_method("default", Method::CT_DYNAMIC, _default, 0, 1);
1.1.2.1 ! parser 136:
! 137: // ^hash:sql[query][(count[;offset])]
! 138: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3);
! 139:
1.1 paf 140: }
141:
142: // global variable
143:
144: Methoded *hash_base_class;
145:
146: // creator
147:
148: Methoded *MHash_create(Pool& pool) {
149: return hash_base_class=new(pool) MHash(pool);
150: }
E-mail: