prepare release 0.4.2
[sqlgg.git] / impl / sqlite3_traits.hpp
blobfecf1f6ffbaf84fabb832d0d53b7fb78ad64d240
1 /*
2 Sqlite3 C++ traits for sqlgg
3 by ygrek
4 2014-06-08
6 This is free and unencumbered software released into the public domain.
8 Anyone is free to copy, modify, publish, use, compile, sell, or
9 distribute this software, either in source code form or as a compiled
10 binary, for any purpose, commercial or non-commercial, and by any
11 means.
13 For more information, please refer to <http://unlicense.org/>
16 #include <sqlite3.h>
17 #if defined(_UNICODE)
18 #define TCHAR wchar_t
19 #define SQLGG_STR(x) L##x
20 #else
21 #define TCHAR char
22 #define SQLGG_STR(x) x
23 #endif
24 #include <assert.h>
25 #include <stdio.h>
26 #include <string>
28 struct sqlite3_traits
30 typedef int Int;
31 typedef std::basic_string<TCHAR> Text;
32 typedef Text Any;
34 typedef sqlite3_stmt* row;
35 typedef sqlite3* connection;
37 static void get_column_Int(row r, int index, Int& val)
39 val = sqlite3_column_int(r, index);
42 static void get_column_Text(row r, int index, Text& val)
44 #if defined(_UNICODE) || defined(UNICODE)
45 val = (const TCHAR*)sqlite3_column_text16(r, index);
46 #else
47 val = (const TCHAR*)sqlite3_column_text(r, index);
48 #endif
51 static void set_param(row r, const Text& val, int index)
53 #if defined(_UNICODE) || defined(UNICODE)
54 int nResult = sqlite3_bind_text16(r, index + 1, val.c_str(), -1, SQLITE_TRANSIENT);
55 #else
56 int nResult = sqlite3_bind_text(r, index + 1, val.c_str(), -1, SQLITE_TRANSIENT);
57 #endif
58 assert(SQLITE_OK == nResult);
61 static void set_param(row r, const Int& val, int index)
63 int nResult = sqlite3_bind_int(r, index + 1, val);
64 assert(SQLITE_OK == nResult);
67 class statement
69 private:
70 row stmt;
71 connection db;
72 TCHAR const* sql;
74 public:
75 statement(connection db, TCHAR const* sql) : db(db), sql(sql)
77 stmt = NULL;
80 void finish()
82 if (NULL != stmt)
84 sqlite3_finalize(stmt);
85 stmt = NULL;
89 virtual ~statement()
91 finish();
94 bool prepare()
96 if (NULL != stmt)
98 return true; // already prepared
101 int nResult;
102 TCHAR const* pszTail;
103 #if defined(_UNICODE) || defined(UNICODE)
104 nResult = sqlite3_prepare16(db, sql, -1, &stmt, (void const**)&pszTail);
105 #else
106 nResult = sqlite3_prepare(db, sql, -1, &stmt, &pszTail);
107 #endif
109 if (SQLITE_OK != nResult)
111 #if defined(SQLGG_DEBUG)
112 printf("sqlite3_prepare error (%u):%s\n%s\n",nResult,sqlite3_errmsg(db),sql);
113 #endif
114 assert(false);
115 return false;
118 return true;
121 template<class T, class Binder, class Params>
122 bool select(T result, Binder binder, Params params)
124 prepare();
126 if (NULL == stmt)
128 assert(false);
129 return false;
132 sqlite3_reset(stmt);
133 sqlite3_clear_bindings(stmt);
135 params.set_params(stmt);
137 while (SQLITE_ROW == sqlite3_step(stmt)) // iterate all objects
139 binder.get(stmt,result);
142 return true;
145 template<class Params>
146 int execute(Params params)
148 int nResult;
150 prepare();
152 if (NULL == stmt)
154 assert(false);
155 return false;
158 sqlite3_reset(stmt);
159 sqlite3_clear_bindings(stmt);
161 params.set_params(stmt);
163 //Execute the command
164 nResult = sqlite3_step(stmt);
165 assert(SQLITE_DONE == nResult);
167 return nResult;
170 }; // statement
172 struct no_params
174 void set_params(row) {}
177 }; // sqlite3_traits