CREATE TABLE AS
[sqlgg.git] / sqlite3_helper.hpp
blobd5fa3d0053c14ebdb385af04ff65f7576b76a4fb
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;
22 typedef Text Any;
24 template<class T>
25 static void get_column_Int(sqlite3_stmt* stmt, int index, T& val)
27 val = sqlite3_column_int(stmt, index);
30 template<class T>
31 static void get_column_Text(sqlite3_stmt* stmt, int index, T& val)
33 #if defined(_UNICODE) || defined(UNICODE)
34 val = (const TCHAR*)sqlite3_column_text16(stmt, index);
35 #else
36 val = (const TCHAR*)sqlite3_column_text(stmt, index);
37 #endif
40 static void set_param_null(sqlite3_stmt* stmt, int index)
42 int nResult = sqlite3_bind_null(stmt, index + 1);
43 ATLASSERT(SQLITE_OK == nResult);
46 static void set_param_Text(sqlite3_stmt* stmt, const Text& val, int index)
48 #if defined(_UNICODE) || defined(UNICODE)
49 int nResult = sqlite3_bind_text16(stmt, index + 1, val.c_str(), -1, SQLITE_TRANSIENT);
50 #else
51 int nResult = sqlite3_bind_text(stmt, index + 1, val.c_str(), -1, SQLITE_TRANSIENT);
52 #endif
53 ATLASSERT(SQLITE_OK == nResult);
56 static void set_param_Any(sqlite3_stmt* stmt, const Any& val, int index)
58 set_param_Text(stmt,val,index);
61 static void set_param_Int(sqlite3_stmt* stmt, const Int& val, int index)
63 int nResult = sqlite3_bind_int(stmt, index + 1, val);
64 ATLASSERT(SQLITE_OK == nResult);
67 template<class Container, class Binder, class Params>
68 static bool do_select(sqlite3* db, Container& result, const TCHAR* sql, Binder binder, Params params)
70 sqlite3_stmt *stmt;
71 const TCHAR *pszTail;
72 int nResult;
73 #if defined(_UNICODE) || defined(UNICODE)
74 nResult = sqlite3_prepare16(db, sql, -1, &stmt, (void const**)&pszTail);
75 #else
76 nResult = sqlite3_prepare(db, sql, -1, &stmt, &pszTail);
77 #endif
78 ATLASSERT(SQLITE_OK == nResult);
79 //printf("%u\n%S\n%s\n",nResult,sql,sqlite3_errmsg(db));
80 if (SQLITE_OK != nResult)
82 //log_sqlite(sql,instDB);
83 return false;
86 params.set_params(stmt);
88 result.clear();
90 while(SQLITE_ROW == sqlite3_step(stmt)) //Iterate all objects
92 result.push_back(typename Container::value_type());
93 binder.of_stmt(stmt,result.back());
96 //Destroy the command
97 nResult = sqlite3_finalize(stmt);
98 if (SQLITE_OK != nResult)
100 return false;
103 return true;
106 struct no_params
108 void set_params(sqlite3_stmt*) {}
111 template<class Params>
112 static int do_execute(sqlite3* db, const char* sql, Params params)
114 sqlite3_stmt *stmt;
115 int nResult;
116 const char *pszTail;
117 #if defined(_UNICODE) || defined(UNICODE)
118 nResult = sqlite3_prepare16(db, sql, -1, &stmt, (void const**)&pszTail);
119 #else
120 nResult = sqlite3_prepare(db, sql, -1, &stmt, &pszTail);
121 #endif
122 //ATLASSERT(SQLITE_OK == nResult);
123 if (SQLITE_OK != nResult)
125 return nResult;
128 params.set_params(stmt);
130 //Execute the command
131 nResult = sqlite3_step(stmt);
132 ATLASSERT(SQLITE_DONE == nResult);
134 //Destroy the command
135 nResult = sqlite3_finalize(stmt);
137 return nResult;
140 }; // sqlite3_traits