Annotation of parser3/src/classes/hash.C, revision 1.2
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.2 ! parser 8: $Id: hash.C,v 1.1.2.1 2001/05/21 15:44:53 parser 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.2 ! 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.2 ! 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.2 ! 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.2 ! 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: Hash& rows_hash=static_cast<VHash *>(r.self)->hash();
! 92: rows_hash.clear();
! 93:
! 94: if(sql_column_count<=1)
! 95: return;
! 96:
! 97: Array& columns=*new(pool) Array(pool);
! 98: for(unsigned int i=0+1; i<sql_column_count; i++) {
! 99: String& column=*new(pool) String(pool);
! 100: column.APPEND_TAINTED(
! 101: (const char *)sql_columns[i].ptr, sql_columns[i].size,
! 102: statement_cstr, 0);
! 103: columns+=&column;
! 104: }
! 105:
! 106: for(unsigned long row=0; row<sql_row_count; row++) {
! 107: SQL_Driver::Cell *sql_cells=sql_rows[row];
! 108:
! 109: VHash& row_vhash=*new(pool) VHash(pool);
! 110: Hash& row_hash=*row_vhash.get_hash();
! 111: String *key=0; // calm, compiler
! 112: String *cell;
! 113: for(unsigned int i=0; i<sql_column_count; i++) {
! 114: cell=new(pool) String(pool);
! 115: cell->APPEND_TAINTED(
! 116: (const char *)sql_cells[i].ptr, sql_cells[i].size,
! 117: statement_cstr, row);
! 118: if(i==0)
! 119: key=cell;
! 120: else
! 121: row_hash.put(*columns.get_string(i-1), new(pool) VString(*cell));
! 122: }
! 123: rows_hash.put(*key, &row_vhash);
! 124: }
! 125: }
! 126:
1.1 paf 127: // constructor
128:
129: MHash::MHash(Pool& apool) : Methoded(apool) {
1.2 ! parser 130: set_name(*NEW String(pool(), HASH_CLASS_NAME));
! 131:
1.1 paf 132: // ^hash.default[]
133: // ^hash.default[hash]
134: add_native_method("default", Method::CT_DYNAMIC, _default, 0, 1);
1.2 ! parser 135:
! 136: // ^hash:sql[query][(count[;offset])]
! 137: add_native_method("sql", Method::CT_DYNAMIC, _sql, 1, 3);
! 138:
1.1 paf 139: }
140:
141: // global variable
142:
143: Methoded *hash_base_class;
144:
145: // creator
146:
147: Methoded *MHash_create(Pool& pool) {
148: return hash_base_class=new(pool) MHash(pool);
149: }
E-mail: