escape comments
[sqlgg.git] / sqlite3_helper.hpp
blob5020208b01deaecb2ae02e91753f4e682900b175
1 //
3 #include "sqlite3.h"
4 //#include <tchar.h>
5 //#include <atlbase.h>
6 #define ATLASSERT assert
7 #if defined(_UNICODE)
8 #define TCHAR wchar_t
9 #define _T(x) L##x
10 #else
11 #define TCHAR char
12 #define _T(x) x
13 #endif
14 #include <assert.h>
15 #include <string>
17 struct sqlite3_traits
20 typedef int Int;
21 typedef std::basic_string<TCHAR> Text;
23 template<class T>
24 static void bind_column_Int(sqlite3_stmt* stmt, int index, T& val)
26 val = sqlite3_column_int(stmt, index);
29 template<class T>
30 static void bind_column_Text(sqlite3_stmt* stmt, int index, T& val)
32 #if defined(_UNICODE) || defined(UNICODE)
33 val = (const TCHAR*)sqlite3_column_text16(stmt, index);
34 #else
35 val = (const TCHAR*)sqlite3_column_text(stmt, index);
36 #endif
39 static void bind_param_null(sqlite3_stmt* stmt, int index)
41 int nResult = sqlite3_bind_null(stmt, index);
42 ATLASSERT(SQLITE_OK == nResult);
45 static void bind_param_Text(sqlite3_stmt* stmt, const Text& val, int index)
47 #if defined(_UNICODE) || defined(UNICODE)
48 int nResult = sqlite3_bind_text16(stmt, index, val.c_str(), -1, SQLITE_TRANSIENT);
49 #else
50 int nResult = sqlite3_bind_text(stmt, index, val.c_str(), -1, SQLITE_TRANSIENT);
51 #endif
52 ATLASSERT(SQLITE_OK == nResult);
55 static void bind_param_Int(sqlite3_stmt* stmt, const Int& val, int index)
57 int nResult = sqlite3_bind_int(stmt, index, val);
58 ATLASSERT(SQLITE_OK == nResult);
61 template<class Container, class Binder, class Params>
62 static bool do_select(sqlite3* db, Container& result, const TCHAR* sql, Binder binder, Params params)
64 sqlite3_stmt *stmt;
65 const TCHAR *pszTail;
66 int nResult;
67 #if defined(_UNICODE) || defined(UNICODE)
68 nResult = sqlite3_prepare16(db, sql, -1, &stmt, (void const**)&pszTail);
69 #else
70 nResult = sqlite3_prepare(db, sql, -1, &stmt, &pszTail);
71 #endif
72 ATLASSERT(SQLITE_OK == nResult);
73 //printf("%u\n%S\n%s\n",nResult,sql,sqlite3_errmsg(db));
74 if (SQLITE_OK != nResult)
76 //log_sqlite(sql,instDB);
77 return false;
80 params.set_params(stmt);
82 result.clear();
84 while(SQLITE_ROW == sqlite3_step(stmt)) //Iterate all objects
86 result.push_back(typename Binder::value_type());
87 binder.of_stmt(stmt,result.back());
90 //Destroy the command
91 nResult = sqlite3_finalize(stmt);
92 if (SQLITE_OK != nResult)
94 return false;
97 return true;
100 template<class Binder>
101 static bool do_insert(sqlite3* db, const typename Binder::value_type& val, const TCHAR* sql)
103 sqlite3_stmt *stmt;
104 int nResult;
105 const TCHAR *pszTail;
106 #if defined(_UNICODE) || defined(UNICODE)
107 nResult = sqlite3_prepare16(db, sql, -1, &stmt, (void const**)&pszTail);
108 #else
109 nResult = sqlite3_prepare(db, sql, -1, &stmt, &pszTail);
110 #endif
111 ATLASSERT(SQLITE_OK == nResult);
112 if (SQLITE_OK != nResult)
114 return false;
117 Binder::to_stmt(stmt,val);
118 //Execute the command
119 nResult = sqlite3_step(stmt);
120 ATLASSERT(SQLITE_DONE == nResult);
122 //Destroy the command
123 nResult = sqlite3_finalize(stmt);
124 if (SQLITE_OK != nResult)
126 return false;
129 return true;
132 struct no_params
134 void set_params(sqlite3_stmt*) {}
137 template<class Params>
138 static int do_execute(sqlite3* db, const char* sql, Params params)
140 sqlite3_stmt *stmt;
141 int nResult;
142 const char *pszTail;
143 nResult = sqlite3_prepare(db, sql, -1, &stmt, &pszTail);
144 //ATLASSERT(SQLITE_OK == nResult);
145 if (SQLITE_OK != nResult)
147 return nResult;
150 params.set_params(stmt);
152 //Execute the command
153 nResult = sqlite3_step(stmt);
154 ATLASSERT(SQLITE_DONE == nResult);
156 //Destroy the command
157 nResult = sqlite3_finalize(stmt);
159 return nResult;
162 }; // sqlite3_traits