Annotation of parser3/src/main/pa_db_connection.C, revision 1.1
1.1 ! parser 1: /** @file
! 2: Parser: Charset connection implementation.
! 3:
! 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
! 5: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
! 6:
! 7: $Id: pa_db_connection.C,v 1.10 2001/10/19 14:15:23 parser Exp $
! 8: */
! 9:
! 10: #include "pa_config_includes.h"
! 11: #ifdef HAVE_LIBDB
! 12:
! 13: #include "pa_db_connection.h"
! 14: #include "pa_exception.h"
! 15:
! 16: void DB_Connection::check(const char *operation, const String *source, int error) {
! 17: switch(error) {
! 18: case 0:
! 19: // no error
! 20: break;
! 21:
! 22: case DB_KEYEXIST:
! 23: // DB_KEYEXIST is a "normal" return, so should not be
! 24: // thrown as an error
! 25: break;
! 26:
! 27: case DB_RUNRECOVERY:
! 28: // mark as unsafe, so not to cache it
! 29: needs_recovery=true;
! 30: throw Exception(0, 0,
! 31: &ffile_spec,
! 32: "db %s error, run recovery utility",
! 33: operation);
! 34:
! 35: default:
! 36: throw Exception(0, 0,
! 37: &ffile_spec,
! 38: "db %s error: %s (%d)",
! 39: operation, strerror(errno), errno);
! 40: }
! 41: }
! 42:
! 43: DB_Connection::DB_Connection(Pool& pool, const String& afile_spec, DB_ENV& adbenv) : Pooled(pool),
! 44: fdbenv(adbenv),
! 45: ffile_spec(afile_spec),
! 46: fservices_pool(0), db(0), needs_recovery(false), tid(0),
! 47: time_used(0) {
! 48: }
! 49:
! 50: void DB_Connection::connect() {
! 51: // open
! 52: DB_INFO dbinfo;
! 53: memset(&dbinfo, 0, sizeof(dbinfo));
! 54: check("open/create", &ffile_spec, db_open(
! 55: ffile_spec.cstr(String::UL_FILE_SPEC),
! 56: PA_DB_ACCESS_METHOD,
! 57: DB_CREATE /*| DB_THREAD*/,
! 58: 0666,
! 59: &fdbenv, &dbinfo, &db));
! 60: }
! 61:
! 62: /// @test string pieces [get/put preserve lang]
! 63: void DB_Connection::put(const String& key, const String& data) {
! 64: // key
! 65: const char *cstr_key=key.cstr(String::UL_AS_IS);
! 66: DBT dbt_key={
! 67: (void *)cstr_key,
! 68: key.size()
! 69: };
! 70:
! 71: // data
! 72: const char *cstr_data=data.cstr(String::UL_AS_IS);
! 73: DBT dbt_data={
! 74: (void *)cstr_data,
! 75: data.size()
! 76: };
! 77: check("put", &key, db->put(db, tid, &dbt_key, &dbt_data, 0/*flags*/));
! 78: }
! 79:
! 80: String *DB_Connection::get(const String& key) {
! 81: // key
! 82: const char *cstr_key=key.cstr(String::UL_AS_IS);
! 83: DBT dbt_key={
! 84: (void *)cstr_key,
! 85: key.size()
! 86: };
! 87:
! 88: // data
! 89: DBT dbt_data={0}; // must be zeroed
! 90: check("get", &key, db->get(db, tid, &dbt_key, &dbt_data, 0/*flags*/));
! 91:
! 92: if(dbt_data.size) {
! 93: char *request_data=(char *)malloc(dbt_data.size);
! 94: memcpy(request_data, dbt_data.data, dbt_data.size);
! 95: return NEW String(pool(), request_data, dbt_data.size, true/*tainted*/);
! 96: } else
! 97: return 0;
! 98: }
! 99:
! 100: #endif
E-mail: