6 #define ATLASSERT assert
21 typedef std::basic_string
<TCHAR
> Text
;
24 static void bind_column_Int(sqlite3_stmt
* stmt
, int index
, T
& val
)
26 val
= sqlite3_column_int(stmt
, index
);
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
);
35 val
= (const TCHAR
*)sqlite3_column_text(stmt
, index
);
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
);
50 int nResult
= sqlite3_bind_text(stmt
, index
, val
.c_str(), -1, SQLITE_TRANSIENT
);
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
)
67 #if defined(_UNICODE) || defined(UNICODE)
68 nResult
= sqlite3_prepare16(db
, sql
, -1, &stmt
, (void const**)&pszTail
);
70 nResult
= sqlite3_prepare(db
, sql
, -1, &stmt
, &pszTail
);
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);
80 params
.set_params(stmt
);
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());
91 nResult
= sqlite3_finalize(stmt
);
92 if (SQLITE_OK
!= nResult
)
100 template<class Binder
>
101 static bool do_insert(sqlite3
* db
, const typename
Binder::value_type
& val
, const TCHAR
* sql
)
105 const TCHAR
*pszTail
;
106 #if defined(_UNICODE) || defined(UNICODE)
107 nResult
= sqlite3_prepare16(db
, sql
, -1, &stmt
, (void const**)&pszTail
);
109 nResult
= sqlite3_prepare(db
, sql
, -1, &stmt
, &pszTail
);
111 ATLASSERT(SQLITE_OK
== nResult
);
112 if (SQLITE_OK
!= nResult
)
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
)
134 void set_params(sqlite3_stmt
*) {}
137 template<class Params
>
138 static int do_execute(sqlite3
* db
, const char* sql
, Params params
)
143 nResult
= sqlite3_prepare(db
, sql
, -1, &stmt
, &pszTail
);
144 //ATLASSERT(SQLITE_OK == nResult);
145 if (SQLITE_OK
!= 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
);