Annotation of parser3/src/main/pa_db_connection.C, revision 1.2

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: 
1.2     ! parser      7:        $Id: pa_db_connection.C,v 1.1 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 "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)", 
1.2     ! parser     39:                        operation, strerror(error), error);
1.1       parser     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),
1.2     ! parser     46:        fservices_pool(0), db(0), ftid(0), needs_recovery(false), 
1.1       parser     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, 
1.2     ! parser     57:                DB_CREATE /* used in single thread, no need for |DB_THREAD*/,
1.1       parser     58:                0666, 
                     59:                &fdbenv, &dbinfo, &db));
                     60: }
1.2     ! parser     61: /// @todo this one of reasons of not having ^try for now
        !            62: void DB_Connection::disconnect() { 
        !            63:        check("close", &ffile_spec, db->close(db, 0/*flags*/));  db=0; 
        !            64: }
1.1       parser     65: 
                     66: ///    @test string pieces [get/put preserve lang]
                     67: void DB_Connection::put(const String& key, const String& data) {
                     68:        // key
                     69:        const char *cstr_key=key.cstr(String::UL_AS_IS);
                     70:        DBT dbt_key={
                     71:                (void *)cstr_key,
                     72:                key.size()
                     73:        };
                     74: 
                     75:        // data
                     76:        const char *cstr_data=data.cstr(String::UL_AS_IS);
                     77:        DBT dbt_data={
                     78:                (void *)cstr_data,
                     79:                data.size()
                     80:        };
1.2     ! parser     81:        check("put", &key, db->put(db, ftid, &dbt_key, &dbt_data, 0/*flags*/));
1.1       parser     82: }
                     83: 
                     84: String *DB_Connection::get(const String& key) {
                     85:        // key
                     86:        const char *cstr_key=key.cstr(String::UL_AS_IS);
                     87:        DBT dbt_key={
                     88:                (void *)cstr_key,
                     89:                key.size()
                     90:        };
                     91: 
                     92:        // data
                     93:        DBT dbt_data={0}; // must be zeroed
1.2     ! parser     94:        int error=db->get(db, ftid, &dbt_key, &dbt_data, 0/*flags*/);
        !            95:        if(error==DB_NOTFOUND)
        !            96:                return 0;
        !            97:        else {
        !            98:                check("get", &key, error);
        !            99: 
        !           100:                if(dbt_data.size) {
        !           101:                        char *request_data=(char *)malloc(dbt_data.size);
        !           102:                        memcpy(request_data, dbt_data.data, dbt_data.size);
        !           103:                        return NEW String(pool(), request_data, dbt_data.size, true/*tainted*/);
        !           104:                } else
        !           105:                        return NEW String(pool());
        !           106:        }               
        !           107: }
        !           108: 
        !           109: void DB_Connection::_delete(const String& key) {
        !           110:        // key
        !           111:        const char *cstr_key=key.cstr(String::UL_AS_IS);
        !           112:        DBT dbt_key={
        !           113:                (void *)cstr_key,
        !           114:                key.size()
        !           115:        };
1.1       parser    116: 
1.2     ! parser    117:        int error=db->del(db, ftid, &dbt_key, 0/*flags*/);
        !           118:        if(error!=DB_NOTFOUND)
        !           119:                check("del", &key, error);
1.1       parser    120: }
                    121: 
                    122: #endif

E-mail: