Traits::statement and connection
[sqlgg.git] / sqlite3_helper.hpp
blobd90651833674df58af79c97c978f4cd576a943c9
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 typedef sqlite3_stmt* statement;
25 typedef sqlite3* connection;
27 template<class T>
28 static void get_column_Int(statement stmt, int index, T& val)
30 val = sqlite3_column_int(stmt, index);
33 template<class T>
34 static void get_column_Text(statement stmt, int index, T& val)
36 #if defined(_UNICODE) || defined(UNICODE)
37 val = (const TCHAR*)sqlite3_column_text16(stmt, index);
38 #else
39 val = (const TCHAR*)sqlite3_column_text(stmt, index);
40 #endif
43 static void set_param_null(statement stmt, int index)
45 int nResult = sqlite3_bind_null(stmt, index + 1);
46 ATLASSERT(SQLITE_OK == nResult);
49 static void set_param_Text(statement stmt, const Text& val, int index)
51 #if defined(_UNICODE) || defined(UNICODE)
52 int nResult = sqlite3_bind_text16(stmt, index + 1, val.c_str(), -1, SQLITE_TRANSIENT);
53 #else
54 int nResult = sqlite3_bind_text(stmt, index + 1, val.c_str(), -1, SQLITE_TRANSIENT);
55 #endif
56 ATLASSERT(SQLITE_OK == nResult);
59 static void set_param_Any(statement stmt, const Any& val, int index)
61 set_param_Text(stmt,val,index);
64 static void set_param_Int(statement stmt, const Int& val, int index)
66 int nResult = sqlite3_bind_int(stmt, index + 1, val);
67 ATLASSERT(SQLITE_OK == nResult);
70 template<class Container, class Binder, class Params>
71 static bool do_select(connection db, Container& result, const TCHAR* sql, Binder binder, Params params)
73 sqlite3_stmt *stmt;
74 const TCHAR *pszTail;
75 int nResult;
76 #if defined(_UNICODE) || defined(UNICODE)
77 nResult = sqlite3_prepare16(db, sql, -1, &stmt, (void const**)&pszTail);
78 #else
79 nResult = sqlite3_prepare(db, sql, -1, &stmt, &pszTail);
80 #endif
81 ATLASSERT(SQLITE_OK == nResult);
82 //printf("%u\n%S\n%s\n",nResult,sql,sqlite3_errmsg(db));
83 if (SQLITE_OK != nResult)
85 //log_sqlite(sql,instDB);
86 return false;
89 params.set_params(stmt);
91 result.clear();
93 while(SQLITE_ROW == sqlite3_step(stmt)) //Iterate all objects
95 result.push_back(typename Container::value_type());
96 binder.of_stmt(stmt,result.back());
99 //Destroy the command
100 nResult = sqlite3_finalize(stmt);
101 if (SQLITE_OK != nResult)
103 return false;
106 return true;
109 struct no_params
111 void set_params(statement) {}
114 template<class Params>
115 static int do_execute(connection db, const char* sql, Params params)
117 sqlite3_stmt *stmt;
118 int nResult;
119 const char *pszTail;
120 #if defined(_UNICODE) || defined(UNICODE)
121 nResult = sqlite3_prepare16(db, sql, -1, &stmt, (void const**)&pszTail);
122 #else
123 nResult = sqlite3_prepare(db, sql, -1, &stmt, &pszTail);
124 #endif
125 //ATLASSERT(SQLITE_OK == nResult);
126 if (SQLITE_OK != nResult)
128 return nResult;
131 params.set_params(stmt);
133 //Execute the command
134 nResult = sqlite3_step(stmt);
135 ATLASSERT(SQLITE_DONE == nResult);
137 //Destroy the command
138 nResult = sqlite3_finalize(stmt);
140 return nResult;
143 }; // sqlite3_traits