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