4
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
4
* Copyright (C) 2009 Tim Marston <tim@ed.am>
6
6
* This file is part of sqlite3cc (hereafter referred to as "this program").
7
* See http://www.waxworlds.org/edam/software/sqlite3cc for more information.
9
* This program is free software: you can redistribute it and/or modify
10
* it under the terms of the GNU Lesser General Public License as published
11
* by the Free Software Foundation, either version 3 of the License, or
12
* (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU Lesser General Public License for more details.
7
* See http://ed.am/dev/sqlite3cc for more information.
9
* This program is free software: you can redistribute it and/or modify it under
10
* the terms of the GNU Lesser General Public License as published by the Free
11
* Software Foundation, either version 3 of the License, or (at your option) any
14
* This program is distributed in the hope that it will be useful, but WITHOUT
15
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19
19
* You should have received a copy of the GNU Lesser General Public License
20
20
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28
28
#include <boost/utility.hpp>
29
29
#include <boost/lexical_cast.hpp>
30
30
#include <sqlite3cc/exception.h>
42
43
struct set_index_t;
47
* The statement class represents an SQL statement. It is the base class for
53
* The statement class represents an SQL statement. It is the base class for
48
54
* both the command and the query classes, which should be used for those
49
* purposes. The basic_statement class its self has protected instantiation.
55
* purposes. The basic_statement class its self has protected instantiation.
51
57
class basic_statement
53
private boost::noncopyable
55
59
//______________________________________________________________________________
60
* Constructor that provides a database upon which to act and the SQL
64
* Constructor that provides a connection upon which to act and the SQL
62
* @param database a reference to a database
67
* @param connection a reference to a connection
63
68
* @param sql an SQL statement in UTF-8
65
70
explicit basic_statement(
71
connection &connection,
67
72
const std::string &sql );
70
* Constructor that provides a database upon which to act.
71
* @param database a reference to a database
72
* @param sql an SQL statement in UTF-8
75
* Constructor that provides a connection upon which to act.
77
* @param connection a reference to a connection
74
79
explicit basic_statement(
80
connection &connection );
77
82
virtual ~basic_statement();
90
96
const std::string &sql );
93
* Reset the statement, ready to re-execute it. This does not clear any of
99
* Reset the statement, ready to re-execute it. This does not clear any of
94
100
* the values bound to the statement.
95
102
* @returns an sqlite error code
96
103
* @see sqlite3_reset()
101
108
* Clears the values bound to a statement to NULL.
102
110
* @returns an sqlite error code
103
111
* @see sqlite3_clear_bindings()
105
113
int clear_bindings();
108
* Bind a value to the SQL statement via it's index. This template will take
109
* a variety of data types and bind them as text. This is how sqlite
110
* internally stores the data anyway, so always binding as text just means
111
* we do the conversion instead of sqlite and is no less efficient.
116
* Bind a value to the SQL statement via it's index.
112
118
* @param index the index of the parameter to bind to
113
119
* @param value the value to bind
114
120
* @returns an sqlite error code
117
123
template< class T >
119
125
unsigned int index,
128
// bind as text (applying the type affinity of the underlying column)
122
129
std::string string_value = boost::lexical_cast< std::string >( value );
123
130
return sqlite3_bind_text( _handle, index, string_value.c_str(),
124
131
string_value.length(), SQLITE_TRANSIENT );
128
* Bind a string value to the SQL statement via it's index where the value
129
* of that string will not change for the duration of the statement. This is
130
* more optimal because sqlite will not have to make it's own copy of the
132
* @param index the index of the parameter to bind to
133
* @param value the invariant string value
134
* @returns an sqlite error code
135
* @see sqlite3_bind_text()
140
unsigned int value_length );
143
* Bind a string value to the SQL statement via it's index where the value
144
* of that string will not change for the duration of the statement. This is
145
* more optimal because sqlite will not have to make it's own copy of the
147
* @param index the index of the parameter to bind to
148
* @param value the invariant string value
149
* @returns an sqlite error code
150
* @see sqlite3_bind_text()
157
* Bind a string value to the SQL statement via it's index where the value
158
* of that string will not change for the duration of the statement. This is
159
* more optimal because sqlite will not have to make it's own copy of the
161
* @param index the index of the parameter to bind to
162
* @param value the invariant string value
163
* @returns an sqlite error code
164
* @see sqlite3_bind_text()
135
* Bind a value to the SQL statement via a named parameter.
137
* @param name the named parameter to bind to
138
* @param value the value to bind
139
* @returns an sqlite error code
140
* @see sqlite3_bind_text()
144
const std::string &name,
147
return bind( bind_parameter_index( name ), value );
151
* Stream operator is used to bind values to parameters automatically, in
152
* ascending order. In addition, the null and set_index() auto-binding
153
* manipulators can be used.
155
* @param value a value to bind
158
basic_statement &operator <<(
161
int code = bind( _bind_index, value );
162
if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
168
* Bind a string value to the SQL statement via it's index where the value
169
* of that string will not change for the duration of the statement. This
170
* is more optimal because sqlite will not have to take it's own copy of the
173
* @param index the index of the parameter to bind to
174
* @param value the invariant string value
175
* @param value_length the length of the string including zero-terminator
176
* @returns an sqlite error code
177
* @see sqlite3_bind_text()
182
unsigned int value_length );
185
* Bind a string value to the SQL statement via it's index where the value
186
* of that string will not change for the duration of the statement. This
187
* is more optimal because sqlite will not have to take it's own copy of the
190
* @param index the index of the parameter to bind to
191
* @param value the invariant string value
192
* @returns an sqlite error code
193
* @see sqlite3_bind_text()
200
* Bind a string value to the SQL statement via it's index where the value
201
* of that string will not change for the duration of the statement. This
202
* is more optimal because sqlite will not have to take it's own copy of the
205
* @param index the index of the parameter to bind to
206
* @param value the invariant string value
207
* @returns an sqlite error code
208
* @see sqlite3_bind_text()
212
const std::string &value );
215
* Bind a string value to the SQL statement via a named parameter where the
216
* string value will not change for the duration of the statement. This
217
* prevents sqlite from taking its own copy of the string.
219
* @param name the named parameter to bind to
220
* @param value the invariant string value
221
* @param value_length the length of the string including zero-terminator
222
* @returns an sqlite error code
223
* @see sqlite3_bind_text()
226
const std::string &name,
228
unsigned int value_length );
231
* Bind a string value to the SQL statement via a named parameter where the
232
* string value will not change for the duration of the statement. This
233
* prevents a copy of the string being taken.
235
* @param name the named parameter to bind to
236
* @param value the invariant string value
237
* @returns an sqlite error code
238
* @see sqlite3_bind_text()
241
const std::string &name,
245
* Bind a string value to the SQL statement via a named parameter where the
246
* string value will not change for the duration of the statement. This
247
* prevents a copy of the string being taken.
249
* @param name the named parameter to bind to
250
* @param value the invariant string value
251
* @returns an sqlite error code
252
* @see sqlite3_bind_text()
255
const std::string &name,
168
256
const std::string &value );
171
259
* Bind a NULL value to the SQL statement via it's index.
172
261
* @param index the index of the parameter to bind to
173
262
* @returns an sqlite error code
174
263
* @see sqlite3_bind_null()
177
266
unsigned int index );
180
* Bind a value to the SQL statement via a named parameter. This template
181
* will take a variety of data types and bind them as text. This is how
182
* sqlite internally stores the data anyway, so always binding as text just
183
* means we do the conversion instead of sqlite and is no less efficient.
184
* @param name the named parameter to bind to
185
* @param value the value to bind
186
* @returns an sqlite error code
187
* @see sqlite3_bind_text()
191
const std::string &name,
194
return bind( bind_parameter_index( name ), value );
198
* Bind a string value to the SQL statement via a named parameter where the
199
* string value will not change for the duration of the statement. This
200
* prevents a copy of the string being taken.
201
* @param name the named parameter to bind to
202
* @param value the invariant string value
203
* @returns an sqlite error code
204
* @see sqlite3_bind_text()
207
const std::string &name,
209
unsigned int value_length );
212
* Bind a string value to the SQL statement via a named parameter where the
213
* string value will not change for the duration of the statement. This
214
* prevents a copy of the string being taken.
215
* @param name the named parameter to bind to
216
* @param value the invariant string value
217
* @returns an sqlite error code
218
* @see sqlite3_bind_text()
221
const std::string &name,
225
* Bind a string value to the SQL statement via a named parameter where the
226
* string value will not change for the duration of the statement. This
227
* prevents a copy of the string being taken.
228
* @param name the named parameter to bind to
229
* @param value the invariant string value
230
* @returns an sqlite error code
231
* @see sqlite3_bind_text()
234
const std::string &name,
235
const std::string &value );
238
269
* Bind a NULL value to the SQL statement via a named parameter.
239
271
* @param name the named parameter to bind to
240
272
* @returns an sqlite error code
241
273
* @see sqlite3_bind_null()
244
276
const std::string &name );
279
* Bind a string value as a blob to the SQL statement via its index.
281
* @param name the named parameter to bind to
282
* @param value the blob data value
283
* @param value_length the length of the blob data
284
* @returns an sqlite error code
285
* @see sqlite3_bind_blob()
290
unsigned int value_length );
293
* Bind a string value as a blob to the SQL statement via its index.
295
* @param name the named parameter to bind to
296
* @param value the blob data value
297
* @returns an sqlite error code
298
* @see sqlite3_bind_blob()
302
const std::string &value );
305
* Bind a string value as a blob to the SQL statement via a named parameter.
307
* @param name the named parameter to bind to
308
* @param value the blob data value
309
* @param value_length the length of the blob data
310
* @returns an sqlite error code
311
* @see sqlite3_bind_blob()
314
const std::string &name,
316
unsigned int value_length );
319
* Bind a string value as a blob to the SQL statement via a named parameter.
321
* @param name the named parameter to bind to
322
* @param value the blob data value
323
* @returns an sqlite error code
324
* @see sqlite3_bind_blob()
327
const std::string &name,
328
const std::string &value );
246
330
//______________________________________________________________________________
247
331
// implementation