summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/sqldbm.h
diff options
context:
space:
mode:
authordshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573>2007-04-22 20:31:54 +0000
committerdshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573>2007-04-22 20:31:54 +0000
commit8548e1273bf068feeeed283e9ae90ca826fd0efa (patch)
tree1ca4b3c73d85ae973e70b9a47567483c57963c25 /crawl-ref/source/sqldbm.h
parentfac344b62ebbf48e7b1d2a7b5f0bcdd3608bd4a7 (diff)
downloadcrawl-ref-8548e1273bf068feeeed283e9ae90ca826fd0efa.tar.gz
crawl-ref-8548e1273bf068feeeed283e9ae90ca826fd0efa.zip
Fixed trunk build for DOS and Windows. DOS and Windows builds use a SQLite db,
with a dbm-like wrapper so database.cc builds unchanged. Added SQLite to the source tree. Only DOS and Windows builds use it at the moment, but it can be added to Unix builds easily (and will be added automatically if a suitable db.h or ndbm.h is not found). git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@1342 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref/source/sqldbm.h')
-rw-r--r--crawl-ref/source/sqldbm.h92
1 files changed, 92 insertions, 0 deletions
diff --git a/crawl-ref/source/sqldbm.h b/crawl-ref/source/sqldbm.h
new file mode 100644
index 0000000000..f9ba9ca284
--- /dev/null
+++ b/crawl-ref/source/sqldbm.h
@@ -0,0 +1,92 @@
+#ifndef SQLDBM_H
+#define SQLDBM_H
+
+#include "AppHdr.h"
+
+#ifdef USE_SQLITE_DBM
+
+#include <sys/types.h>
+
+#ifdef DOS
+#define SQLITE_INT64_TYPE int
+#define SQLITE_UINT64_TYPE unsigned int
+#else
+#define SQLITE_INT64_TYPE int64_t
+#define SQLITE_UINT64_TYPE uint64_t
+#endif
+#include <sqlite3.h>
+#include <string>
+
+// A string dbm interface for SQLite. Makes no attempt to store arbitrary
+// data, only valid C strings.
+
+class sql_datum
+{
+public:
+ sql_datum();
+ sql_datum(const std::string &s);
+ sql_datum(const sql_datum &other);
+ virtual ~sql_datum();
+
+ sql_datum &operator = (const sql_datum &other);
+
+ std::string to_str() const;
+
+public:
+ char *dptr; // Canonically void*, but we're not a real Berkeley DB.
+ size_t dsize;
+
+private:
+ bool need_free;
+
+ void reset();
+ void init_from(const sql_datum &other);
+};
+
+#define DBM_REPLACE 1
+
+class SQL_DBM
+{
+public:
+ SQL_DBM(const std::string &db = "", bool open = false);
+ ~SQL_DBM();
+
+ bool is_open() const;
+
+ int open(const std::string &db = "");
+ void close();
+ std::string query(const std::string &key);
+ int insert(const std::string &key, const std::string &value);
+
+public:
+ std::string error;
+ int errc;
+
+private:
+ int finalise_query(sqlite3_stmt **query);
+ int prepare_query(sqlite3_stmt **query, const char *sql);
+ int init_query();
+ int init_insert();
+ int init_schema();
+ int ec(int err);
+
+private:
+ sqlite3 *db;
+ sqlite3_stmt *s_insert;
+ sqlite3_stmt *s_query;
+ std::string dbfile;
+};
+
+SQL_DBM *dbm_open(const char *filename, int open_mode, int permissions);
+int dbm_close(SQL_DBM *db);
+
+sql_datum dbm_fetch(SQL_DBM *db, const sql_datum &key);
+int dbm_store(SQL_DBM *db, const sql_datum &key,
+ const sql_datum &value, int overwrite);
+
+typedef sql_datum datum;
+typedef SQL_DBM DBM;
+
+#endif
+
+#endif