Update ooo320-m1
[ooovba.git] / desktop / source / deployment / misc / db.cxx
blob3b449246c44160f71ba0648efacbef6695523d13
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: db.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
34 #include <db.hxx>
36 #include <rtl/alloc.h>
37 #include <cstring>
38 #include <errno.h>
40 namespace berkeleydbproxy {
42 //----------------------------------------------------------------------------
43 namespace db_internal
45 static void raise_error(int dberr, const char * where);
47 static inline int check_error(int dberr, const char * where)
49 if (dberr) raise_error(dberr,where);
50 return dberr;
54 //----------------------------------------------------------------------------
56 char *DbEnv::strerror(int error) {
57 return (db_strerror(error));
60 //----------------------------------------------------------------------------
62 Db::Db(DbEnv* pDbenv,u_int32_t flags)
63 : m_pDBP(0)
65 db_internal::check_error( db_create(&m_pDBP,pDbenv ? pDbenv->m_pDBENV:0,flags),"Db::Db" );
69 Db::~Db()
71 if (m_pDBP)
73 // should not happen
74 // TODO: add assert
80 int Db::close(u_int32_t flags)
82 int error = m_pDBP->close(m_pDBP,flags);
83 m_pDBP = 0;
84 return db_internal::check_error(error,"Db::close");
87 int Db::open(DB_TXN *txnid,
88 const char *file,
89 const char *database,
90 DBTYPE type,
91 u_int32_t flags,
92 int mode)
94 int err = m_pDBP->open(m_pDBP,txnid,file,database,type,flags,mode);
95 return db_internal::check_error( err,"Db::open" );
99 int Db::get(DB_TXN *txnid, Dbt *key, Dbt *data, u_int32_t flags)
101 int err = m_pDBP->get(m_pDBP,txnid,key,data,flags);
103 // these are non-exceptional outcomes
104 if (err != DB_NOTFOUND && err != DB_KEYEMPTY)
105 db_internal::check_error( err,"Db::get" );
107 return err;
110 int Db::put(DB_TXN* txnid, Dbt *key, Dbt *data, u_int32_t flags)
112 int err = m_pDBP->put(m_pDBP,txnid,key,data,flags);
114 if (err != DB_KEYEXIST) // this is a non-exceptional outcome
115 db_internal::check_error( err,"Db::put" );
116 return err;
119 int Db::cursor(DB_TXN *txnid, Dbc **cursorp, u_int32_t flags)
121 DBC * dbc = 0;
122 int error = m_pDBP->cursor(m_pDBP,txnid,&dbc,flags);
124 if (!db_internal::check_error(error,"Db::cursor"))
125 *cursorp = new Dbc(dbc);
127 return error;
131 #define DB_INCOMPLETE (-30999)/* Sync didn't finish. */
133 int Db::sync(u_int32_t flags)
135 int err;
136 DB *db = m_pDBP;
138 if (!db) {
139 db_internal::check_error(EINVAL,"Db::sync");
140 return (EINVAL);
142 if ((err = db->sync(db, flags)) != 0 && err != DB_INCOMPLETE) {
143 db_internal::check_error(err, "Db::sync");
144 return (err);
146 return (err);
149 int Db::del(Dbt *key, u_int32_t flags)
151 DB *db = m_pDBP;
152 int err;
154 if ((err = db->del(db, 0, key, flags)) != 0) {
155 // DB_NOTFOUND is a "normal" return, so should not be
156 // thrown as an error
158 if (err != DB_NOTFOUND) {
159 db_internal::check_error(err, "Db::del");
160 return (err);
163 return (err);
166 //----------------------------------------------------------------------------
168 Dbc::Dbc(DBC * dbc)
169 : m_pDBC(dbc)
173 Dbc::~Dbc()
177 int Dbc::close()
179 int err = m_pDBC->c_close(m_pDBC);
180 delete this;
181 return db_internal::check_error( err,"Dbcursor::close" );
184 int Dbc::get(Dbt *key, Dbt *data, u_int32_t flags)
186 int err = m_pDBC->c_get(m_pDBC,key,data,flags);
188 // these are non-exceptional outcomes
189 if (err != DB_NOTFOUND && err != DB_KEYEMPTY)
190 db_internal::check_error( err, "Dbcursor::get" );
192 return err;
195 //----------------------------------------------------------------------------
197 Dbt::Dbt()
199 using namespace std;
200 DBT * thispod = this;
201 memset(thispod, 0, sizeof *thispod);
205 Dbt::Dbt(void *data_arg, u_int32_t size_arg)
207 using namespace std;
208 DBT * thispod = this;
209 memset(thispod, 0, sizeof *thispod);
210 this->set_data(data_arg);
211 this->set_size(size_arg);
214 Dbt::Dbt(const Dbt & other)
216 using namespace std;
217 const DBT *otherpod = &other;
218 DBT *thispod = this;
219 memcpy(thispod, otherpod, sizeof *thispod);
222 Dbt& Dbt::operator = (const Dbt & other)
224 if (this != &other)
226 using namespace std;
227 const DBT *otherpod = &other;
228 DBT *thispod = this;
229 memcpy(thispod, otherpod, sizeof *thispod);
231 return *this;
234 Dbt::~Dbt()
238 void * Dbt::get_data() const
240 return this->data;
243 void Dbt::set_data(void *value)
245 this->data = value;
248 u_int32_t Dbt::get_size() const
250 return this->size;
253 void Dbt::set_size(u_int32_t value)
255 this->size = value;
258 //----------------------------------------------------------------------------
259 void db_internal::raise_error(int dberr, const char * where)
261 if (!where) where = "<unknown>";
263 const char * dberrmsg = db_strerror(dberr);
264 if (!dberrmsg || !*dberrmsg) dberrmsg = "<unknown DB error>";
266 rtl::OString msg = where;
267 msg += ": ";
268 msg += dberrmsg;
270 throw DbException(msg);
273 //----------------------------------------------------------------------------
274 } // namespace ecomp