2 Sqlite3 C++ traits for sqlgg
6 This is free and unencumbered software released into the public domain.
8 Anyone is free to copy, modify, publish, use, compile, sell, or
9 distribute this software, either in source code form or as a compiled
10 binary, for any purpose, commercial or non-commercial, and by any
13 For more information, please refer to <http://unlicense.org/>
19 #define SQLGG_STR(x) L##x
22 #define SQLGG_STR(x) x
31 typedef std::basic_string
<TCHAR
> Text
;
34 typedef sqlite3_stmt
* row
;
35 typedef sqlite3
* connection
;
37 static void get_column_Int(row r
, int index
, Int
& val
)
39 val
= sqlite3_column_int(r
, index
);
42 static void get_column_Text(row r
, int index
, Text
& val
)
44 #if defined(_UNICODE) || defined(UNICODE)
45 val
= (const TCHAR
*)sqlite3_column_text16(r
, index
);
47 val
= (const TCHAR
*)sqlite3_column_text(r
, index
);
51 static void set_param(row r
, const Text
& val
, int index
)
53 #if defined(_UNICODE) || defined(UNICODE)
54 int nResult
= sqlite3_bind_text16(r
, index
+ 1, val
.c_str(), -1, SQLITE_TRANSIENT
);
56 int nResult
= sqlite3_bind_text(r
, index
+ 1, val
.c_str(), -1, SQLITE_TRANSIENT
);
58 assert(SQLITE_OK
== nResult
);
61 static void set_param(row r
, const Int
& val
, int index
)
63 int nResult
= sqlite3_bind_int(r
, index
+ 1, val
);
64 assert(SQLITE_OK
== nResult
);
75 statement(connection db
, TCHAR
const* sql
) : db(db
), sql(sql
)
84 sqlite3_finalize(stmt
);
98 return true; // already prepared
102 TCHAR
const* pszTail
;
103 #if defined(_UNICODE) || defined(UNICODE)
104 nResult
= sqlite3_prepare16(db
, sql
, -1, &stmt
, (void const**)&pszTail
);
106 nResult
= sqlite3_prepare(db
, sql
, -1, &stmt
, &pszTail
);
109 if (SQLITE_OK
!= nResult
)
111 #if defined(SQLGG_DEBUG)
112 printf("sqlite3_prepare error (%u):%s\n%s\n",nResult
,sqlite3_errmsg(db
),sql
);
121 template<class T
, class Binder
, class Params
>
122 bool select(T result
, Binder binder
, Params params
)
133 sqlite3_clear_bindings(stmt
);
135 params
.set_params(stmt
);
137 while (SQLITE_ROW
== sqlite3_step(stmt
)) // iterate all objects
139 binder
.get(stmt
,result
);
145 template<class Params
>
146 int execute(Params params
)
159 sqlite3_clear_bindings(stmt
);
161 params
.set_params(stmt
);
163 //Execute the command
164 nResult
= sqlite3_step(stmt
);
165 assert(SQLITE_DONE
== nResult
);
174 void set_params(row
) {}