Annotation of parser3/src/classes/hashfile.C, revision 1.3
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.3 ! parser 7: $Id: hashfile.C,v 1.2 2001/10/22 16:44:42 parser Exp $
1.1 parser 8: */
9:
10: #include "pa_config_includes.h"
11: #ifdef HAVE_LIBDB
12:
13: #include "classes.h"
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:
! 74: static void _delete(Request& r, const String& method_name, MethodParams *params) {
! 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:
! 84: connection._delete(key);
! 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:
96: // constructor
97:
98: MHashfile::MHashfile(Pool& apool) : Methoded(apool) {
99: set_name(*NEW String(pool(), HASH_CLASS_NAME));
100:
1.3 ! parser 101: // ^hashfile::open[filename]
! 102: add_native_method("open", Method::CT_DYNAMIC, _open, 1, 1);
! 103: // ^transaction{code}
! 104: add_native_method("transaction", Method::CT_DYNAMIC, _transaction, 1, 1);
1.1 parser 105: // ^hashfile.delete[key]
106: add_native_method("delete", Method::CT_DYNAMIC, _delete, 1, 1);
1.3 ! parser 107: // ^hashfile:clear[filename]
! 108: add_native_method("clear", Method::CT_STATIC, _clear, 1, 1);
! 109: /*
1.1 parser 110: // ^cache[key](seconds){code}
111: add_native_method("cache", Method::CT_DYNAMIC, _cache, 3, 3);
112: // ^cancel[]
113: add_native_method("cancel", Method::CT_DYNAMIC, _cancel, 0, 0);
114: // ^pack[]
115: add_native_method("pack", Method::CT_DYNAMIC, _pack, 0, 0);
1.3 ! parser 116: // ^hash[]
! 117: add_native_method("hash", Method::CT_DYNAMIC, _hash, 0, 0);
1.1 parser 118: */
119: }
120:
121: // global variable
122:
123: Methoded *hashfile_base_class;
124:
125: // creator
126:
127: Methoded *MHashfile_create(Pool& pool) {
128: return hashfile_base_class=new(pool) MHashfile(pool);
129: }
130:
131: #endif
E-mail: