6 #define ATLASSERT assert
21 typedef std::basic_string
<TCHAR
> Text
;
24 typedef sqlite3_stmt
* statement
;
25 typedef sqlite3
* connection
;
28 static void get_column_Int(statement stmt
, int index
, T
& val
)
30 val
= sqlite3_column_int(stmt
, index
);
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
);
39 val
= (const TCHAR
*)sqlite3_column_text(stmt
, index
);
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
);
54 int nResult
= sqlite3_bind_text(stmt
, index
+ 1, val
.c_str(), -1, SQLITE_TRANSIENT
);
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
)
76 #if defined(_UNICODE) || defined(UNICODE)
77 nResult
= sqlite3_prepare16(db
, sql
, -1, &stmt
, (void const**)&pszTail
);
79 nResult
= sqlite3_prepare(db
, sql
, -1, &stmt
, &pszTail
);
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);
89 params
.set_params(stmt
);
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());
100 nResult
= sqlite3_finalize(stmt
);
101 if (SQLITE_OK
!= nResult
)
111 void set_params(statement
) {}
114 template<class Params
>
115 static int do_execute(connection db
, const char* sql
, Params params
)
120 #if defined(_UNICODE) || defined(UNICODE)
121 nResult
= sqlite3_prepare16(db
, sql
, -1, &stmt
, (void const**)&pszTail
);
123 nResult
= sqlite3_prepare(db
, sql
, -1, &stmt
, &pszTail
);
125 //ATLASSERT(SQLITE_OK == nResult);
126 if (SQLITE_OK
!= 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
);