Apply the new ground_level method.
[crawl.git] / crawl-ref / source / sqldbm.h
blob35cff068d09c871b952fd12c8b8d3bbe172791ff
1 /*
2 * File: sqldbm.cc
3 * Summary: dbm wrapper for SQLite
4 * Written by: Darshan Shaligram
5 */
7 #ifndef SQLDBM_H
8 #define SQLDBM_H
10 #ifdef USE_SQLITE_DBM
12 #include <sys/types.h>
13 #include <memory>
15 #define SQLITE_INT64_TYPE int
16 #define SQLITE_UINT64_TYPE unsigned int
18 #include <sqlite3.h>
19 #include <string>
21 // A string dbm interface for SQLite. Makes no attempt to store arbitrary
22 // data, only valid C strings.
24 class sql_datum
26 public:
27 sql_datum();
28 sql_datum(const std::string &s);
29 sql_datum(const sql_datum &other);
30 virtual ~sql_datum();
32 sql_datum &operator = (const sql_datum &other);
34 std::string to_str() const;
36 public:
37 char *dptr; // Canonically void*, but we're not a real Berkeley DB.
38 size_t dsize;
40 private:
41 bool need_free;
43 void reset();
44 void init_from(const sql_datum &other);
47 #define DBM_REPLACE 1
49 class SQL_DBM
51 public:
52 SQL_DBM(const std::string &db = "", bool readonly = true,
53 bool open = false);
54 ~SQL_DBM();
56 bool is_open() const;
58 int open(const std::string &db = "");
59 void close();
61 std::auto_ptr<std::string> firstkey();
62 std::auto_ptr<std::string> nextkey();
64 std::string query(const std::string &key);
65 int insert(const std::string &key, const std::string &value);
66 int remove(const std::string &key);
68 public:
69 std::string error;
70 int errc;
72 private:
73 int finalise_query(sqlite3_stmt **query);
74 int prepare_query(sqlite3_stmt **query, const char *sql);
75 int init_query();
76 int init_iterator();
77 int init_insert();
78 int init_remove();
79 int init_schema();
80 int ec(int err);
82 int try_insert(const std::string &key, const std::string &value);
83 int do_insert(const std::string &key, const std::string &value);
84 int do_query(const std::string &key, std::string *result);
86 private:
87 sqlite3 *db;
88 sqlite3_stmt *s_insert;
89 sqlite3_stmt *s_remove;
90 sqlite3_stmt *s_query;
91 sqlite3_stmt *s_iterator;
92 std::string dbfile;
93 bool readonly;
96 SQL_DBM *dbm_open(const char *filename, int open_mode, int permissions);
97 int dbm_close(SQL_DBM *db);
99 sql_datum dbm_fetch(SQL_DBM *db, const sql_datum &key);
100 sql_datum dbm_firstkey(SQL_DBM *db);
101 sql_datum dbm_nextkey(SQL_DBM *db);
102 int dbm_store(SQL_DBM *db, const sql_datum &key,
103 const sql_datum &value, int overwrite);
105 typedef sql_datum datum;
106 typedef SQL_DBM DBM;
108 #endif
110 #endif