Annotation of parser3/src/classes/hashfile.C, revision 1.6
1.1 parser 1: /** @file
2: Parser: @b hashfile parser class.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
6:
1.6 ! parser 7: $Id: hashfile.C,v 1.5 2001/10/23 14:43:44 parser Exp $
1.1 parser 8: */
9:
10: #include "pa_config_includes.h"
1.4 parser 11: #include "classes.h"
1.1 parser 12: #ifdef HAVE_LIBDB
13:
14: #include "pa_request.h"
15: #include "pa_vhashfile.h"
16: #include "pa_vhash.h"
17:
18: // defines
19:
20: #define HASH_CLASS_NAME "hashfile"
21:
22: // class
23:
24: class MHashfile : public Methoded {
25: public: // VStateless_class
26: Value *create_new_value(Pool& pool) { return new(pool) VHashfile(pool); }
27:
28: public:
29: MHashfile(Pool& pool);
30: public: // Methoded
31: bool used_directly() { return true; }
32: };
33:
34: // methods
35:
1.3 parser 36: static void _open(Request& r, const String& method_name, MethodParams *params) {
1.1 parser 37: Pool& pool=r.pool();
38: VHashfile& self=*static_cast<VHashfile *>(r.self);
39:
1.3 parser 40: // file_spec
41: const String &file_spec=params->as_string(0, "filename must be string");
1.1 parser 42:
1.3 parser 43: self.set_connection(
44: DB_manager->get_connection(
45: r.absolute(file_spec),
46: method_name)
47: );
48: }
49:
50:
51: static void _transaction(Request& r, const String& method_name, MethodParams *params) {
52: Pool& pool=r.pool();
53: VHashfile& self=*static_cast<VHashfile *>(r.self);
54:
55: // body code
56: Value& body_code=params->as_junction(0, "body must be code");
57:
58: // connection
59: DB_Connection& connection=self.get_connection(&method_name);
60:
61: // transaction
62: Auto_transaction transaction(connection);
63:
64: // execute body
65: try {
66: r.write_assign_lang(r.process(body_code));
67: } catch(...) { // process/commit problem
68: transaction.mark_to_rollback();
69:
70: /*re*/throw;
71: }
72: }
73:
1.6 ! parser 74: static void remove(Request& r, const String& method_name, MethodParams *params) {
1.3 parser 75: Pool& pool=r.pool();
76: VHashfile& self=*static_cast<VHashfile *>(r.self);
77:
78: // key
79: const String &key=params->as_string(0, "key must be string");
80:
81: // connection
82: DB_Connection& connection=self.get_connection(&method_name);
83:
1.6 ! parser 84: connection.remove(key);
1.3 parser 85: }
86:
87: static void _clear(Request& r, const String& method_name, MethodParams *params) {
88: Pool& pool=r.pool();
89:
90: // file_spec
91: const String &file_spec=params->as_string(0, "filename must be string");
92:
93: DB_manager->clear_dbfile(r.absolute(file_spec));
1.1 parser 94: }
95:
1.5 parser 96: static void _hash(Request& r, const String& method_name, MethodParams *params) {
97: Pool& pool=r.pool();
98: VHashfile& self=*static_cast<VHashfile *>(r.self);
99:
100: // write out result
101: VHash& result=*new(pool) VHash(pool, *self.get_hash(&method_name));
102: result.set_name(method_name);
103: r.write_no_lang(result);
104: }
105:
1.1 parser 106: // constructor
107:
108: MHashfile::MHashfile(Pool& apool) : Methoded(apool) {
109: set_name(*NEW String(pool(), HASH_CLASS_NAME));
110:
1.3 parser 111: // ^hashfile::open[filename]
112: add_native_method("open", Method::CT_DYNAMIC, _open, 1, 1);
113: // ^transaction{code}
114: add_native_method("transaction", Method::CT_DYNAMIC, _transaction, 1, 1);
1.1 parser 115: // ^hashfile.delete[key]
1.6 ! parser 116: add_native_method("delete", Method::CT_DYNAMIC, remove, 1, 1);
1.3 parser 117: // ^hashfile:clear[filename]
118: add_native_method("clear", Method::CT_STATIC, _clear, 1, 1);
1.5 parser 119: // ^hash[]
120: add_native_method("hash", Method::CT_DYNAMIC, _hash, 0, 0);
1.3 parser 121: /*
1.1 parser 122: // ^cache[key](seconds){code}
123: add_native_method("cache", Method::CT_DYNAMIC, _cache, 3, 3);
124: // ^cancel[]
125: add_native_method("cancel", Method::CT_DYNAMIC, _cancel, 0, 0);
126: */
127: }
128:
129: // global variable
130:
131: Methoded *hashfile_base_class;
132:
1.4 parser 133: #endif
134:
1.1 parser 135: // creator
136:
137: Methoded *MHashfile_create(Pool& pool) {
1.4 parser 138: return
139: #ifdef HAVE_LIBDB
140: hashfile_base_class=new(pool) MHashfile(pool)
141: #else
142: 0
143: #endif
144: ;
1.1 parser 145: }
E-mail: