00001
00002
00003
00004
00005
00006
00007 #ifndef SQLDBM_H
00008 #define SQLDBM_H
00009
00010 #ifdef USE_SQLITE_DBM
00011
00012 #include <sys/types.h>
00013 #include <memory>
00014
00015 #define SQLITE_INT64_TYPE int
00016 #define SQLITE_UINT64_TYPE unsigned int
00017
00018 #include <sqlite3.h>
00019 #include <string>
00020
00021
00022
00023
00024 class sql_datum
00025 {
00026 public:
00027 sql_datum();
00028 sql_datum(const std::string &s);
00029 sql_datum(const sql_datum &other);
00030 virtual ~sql_datum();
00031
00032 sql_datum &operator = (const sql_datum &other);
00033
00034 std::string to_str() const;
00035
00036 public:
00037 char *dptr;
00038 size_t dsize;
00039
00040 private:
00041 bool need_free;
00042
00043 void reset();
00044 void init_from(const sql_datum &other);
00045 };
00046
00047 #define DBM_REPLACE 1
00048
00049 class SQL_DBM
00050 {
00051 public:
00052 SQL_DBM(const std::string &db = "", bool readonly = true,
00053 bool open = false);
00054 ~SQL_DBM();
00055
00056 bool is_open() const;
00057
00058 int open(const std::string &db = "");
00059 void close();
00060
00061 std::auto_ptr<std::string> firstkey();
00062 std::auto_ptr<std::string> nextkey();
00063
00064 std::string query(const std::string &key);
00065 int insert(const std::string &key, const std::string &value);
00066 int remove(const std::string &key);
00067
00068 public:
00069 std::string error;
00070 int errc;
00071
00072 private:
00073 int finalise_query(sqlite3_stmt **query);
00074 int prepare_query(sqlite3_stmt **query, const char *sql);
00075 int init_query();
00076 int init_iterator();
00077 int init_insert();
00078 int init_remove();
00079 int init_schema();
00080 int ec(int err);
00081
00082 int try_insert(const std::string &key, const std::string &value);
00083 int do_insert(const std::string &key, const std::string &value);
00084 int do_query(const std::string &key, std::string *result);
00085
00086 private:
00087 sqlite3 *db;
00088 sqlite3_stmt *s_insert;
00089 sqlite3_stmt *s_remove;
00090 sqlite3_stmt *s_query;
00091 sqlite3_stmt *s_iterator;
00092 std::string dbfile;
00093 bool readonly;
00094 };
00095
00096 SQL_DBM *dbm_open(const char *filename, int open_mode, int permissions);
00097 int dbm_close(SQL_DBM *db);
00098
00099 sql_datum dbm_fetch(SQL_DBM *db, const sql_datum &key);
00100 sql_datum dbm_firstkey(SQL_DBM *db);
00101 sql_datum dbm_nextkey(SQL_DBM *db);
00102 int dbm_store(SQL_DBM *db, const sql_datum &key,
00103 const sql_datum &value, int overwrite);
00104
00105 typedef sql_datum datum;
00106 typedef SQL_DBM DBM;
00107
00108 #endif
00109
00110 #endif