From 8548e1273bf068feeeed283e9ae90ca826fd0efa Mon Sep 17 00:00:00 2001 From: dshaligram Date: Sun, 22 Apr 2007 20:31:54 +0000 Subject: 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 --- crawl-ref/source/sqldbm.h | 92 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 crawl-ref/source/sqldbm.h (limited to 'crawl-ref/source/sqldbm.h') 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 + +#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 +#include + +// 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 -- cgit v1.2.3-54-g00ecf