2003-05-26 13:45:00 +00:00
|
|
|
#ifndef __DB_H
|
|
|
|
#define __DB_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
2003-07-31 16:05:35 +00:00
|
|
|
#include <map>
|
2003-05-26 13:45:00 +00:00
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
#include <db_cxx.h>
|
|
|
|
|
2003-07-07 09:25:26 +00:00
|
|
|
#include "util.hh"
|
|
|
|
|
2003-05-26 13:45:00 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
class Database;
|
|
|
|
|
|
|
|
|
|
|
|
class Transaction
|
|
|
|
{
|
|
|
|
friend class Database;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DbTxn * txn;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Transaction();
|
|
|
|
Transaction(Database & _db);
|
|
|
|
~Transaction();
|
|
|
|
|
2005-02-09 14:37:24 +00:00
|
|
|
void begin(Database & db);
|
2003-07-31 16:05:35 +00:00
|
|
|
void abort();
|
2003-07-31 13:47:13 +00:00
|
|
|
void commit();
|
2003-10-15 12:42:39 +00:00
|
|
|
|
|
|
|
void moveTo(Transaction & t);
|
2003-07-31 13:47:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define noTxn Transaction()
|
|
|
|
|
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
typedef unsigned int TableId; /* table handles */
|
|
|
|
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
class Database
|
|
|
|
{
|
|
|
|
friend class Transaction;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DbEnv * env;
|
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
TableId nextId;
|
|
|
|
map<TableId, Db *> tables;
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
void requireEnv();
|
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
Db * getDb(TableId table);
|
2003-07-31 13:47:13 +00:00
|
|
|
|
2005-05-09 15:25:47 +00:00
|
|
|
void open2(const string & path, bool removeOldEnv);
|
|
|
|
|
2003-07-31 13:47:13 +00:00
|
|
|
public:
|
|
|
|
Database();
|
|
|
|
~Database();
|
|
|
|
|
|
|
|
void open(const string & path);
|
2003-10-14 15:33:00 +00:00
|
|
|
void close();
|
2003-07-31 13:47:13 +00:00
|
|
|
|
2005-12-12 18:24:42 +00:00
|
|
|
TableId openTable(const string & table, bool sorted = false);
|
2005-12-12 19:14:38 +00:00
|
|
|
void closeTable(TableId table);
|
|
|
|
void deleteTable(const string & table);
|
2003-07-31 13:47:13 +00:00
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
bool queryString(const Transaction & txn, TableId table,
|
2003-07-31 13:47:13 +00:00
|
|
|
const string & key, string & data);
|
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
bool queryStrings(const Transaction & txn, TableId table,
|
2003-07-31 13:47:13 +00:00
|
|
|
const string & key, Strings & data);
|
2003-05-26 13:45:00 +00:00
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
void setString(const Transaction & txn, TableId table,
|
2003-07-31 13:47:13 +00:00
|
|
|
const string & key, const string & data);
|
2003-07-07 09:25:26 +00:00
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
void setStrings(const Transaction & txn, TableId table,
|
2004-06-28 10:42:57 +00:00
|
|
|
const string & key, const Strings & data,
|
|
|
|
bool deleteEmpty = true);
|
2003-05-26 13:45:00 +00:00
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
void delPair(const Transaction & txn, TableId table,
|
2003-07-31 13:47:13 +00:00
|
|
|
const string & key);
|
2003-07-07 09:25:26 +00:00
|
|
|
|
2003-07-31 16:05:35 +00:00
|
|
|
void enumTable(const Transaction & txn, TableId table,
|
2005-12-12 18:24:42 +00:00
|
|
|
Strings & keys, const string & keyPrefix = "");
|
2003-07-31 13:47:13 +00:00
|
|
|
};
|
2003-05-26 13:45:00 +00:00
|
|
|
|
|
|
|
|
2004-10-25 14:38:23 +00:00
|
|
|
class DbNoPermission : public Error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DbNoPermission(const format & f) : Error(f) { };
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-05-26 13:45:00 +00:00
|
|
|
#endif /* !__DB_H */
|