4
4
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
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.
6
* This file is part of sqlitepp (hereafter referred to as "this program").
7
* See http://www.waxworlds.org/edam/software/sqlitepp for more information.
9
9
* This program is free software: you can redistribute it and/or modify
10
10
* it under the terms of the GNU Lesser General Public License as published
20
20
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#ifndef SQLITE3CC_BASIC_STATEMENT_H_
24
#define SQLITE3CC_BASIC_STATEMENT_H_
23
#ifndef STATEMENT_HPP_
24
#define STATEMENT_HPP_
27
27
#include <sqlite3.h>
28
28
#include <boost/utility.hpp>
29
29
#include <boost/lexical_cast.hpp>
30
#include <sqlite3cc/exception.h>
30
#include <sqlitepp/exception.hpp>
51
* The statement class represents an SQL statement. It is the base class for
52
* both the command and the query classes, which should be used for those
53
* purposes. The basic_statement class its self has protected instantiation.
42
struct _set_index_t { unsigned int _index; };
45
* Auto-binding manipulator, for use with a statment's stream operator. This
46
* specifies a NULL value to bind to a parameter.
48
extern const _null_t null;
51
* Auto-binding manipulator, for use with a statment's stream operator. This
52
* indicates that the statement should be executed immediately. Unlike a
53
* statment's exec() method, this will throw on error. Also, it will throw if
54
* the execution returns any result rows.
56
extern const _exec_t exec;
59
* Auto-binding manipulator, for use with a statment's stream operator. This
60
* manipulator sets the index used to automatically assign values to parameters.
61
* @param index the new index for incremental assignment
63
_set_index_t set_index(
68
* The statement class represents an SQL statement. It is the bas class for both
69
* the command and the query classes which should be used for those purposes.
70
* The statement class its self has protected instantiation.
57
74
private boost::noncopyable
64
* Constructor that provides a connection upon which to act and the SQL
81
* Constructor that provides a database upon which to act and the SQL
66
* @param connection a reference to a connection
83
* @param database a reference to the database
67
84
* @param sql an SQL statement in UTF-8
69
explicit basic_statement(
70
connection &connection,
71
88
const std::string &sql );
74
* Constructor that provides a connection upon which to act.
75
* @param connection a reference to a connection
77
explicit basic_statement(
78
connection &connection );
80
virtual ~basic_statement();
90
virtual ~statement() throw( );
82
92
//______________________________________________________________________________
83
93
// public interface
89
99
* @returns an sqlite error code
90
100
* @see sqlite3_prepare_v2()
93
103
const std::string &sql );
106
* Step through one execution cycle of the SQL statement. If this is an SQL
107
* statement that doesn't return any rows, only one cycle is required,
108
* otherwise, each cycle will return another row
109
* @return an sqlite error code
110
* @see sqlite3_step()
96
115
* Reset the statement, ready to re-execute it. This does not clear any of
97
116
* the values bound to the statement.
98
117
* @returns an sqlite error code
99
118
* @see sqlite3_reset()
104
123
* Clears the values bound to a statement to NULL.
131
150
* Bind a string value to the SQL statement via it's index where the value
132
151
* of that string will not change for the duration of the statement. This is
133
* more optimal because sqlite will not have to take it's own copy of the
152
* more optimal because sqlite will not have to make it's own copy of the
135
154
* @param index the index of the parameter to bind to
136
155
* @param value the invariant string value
137
* @param value_length the length of the string including zero-terminator
138
156
* @returns an sqlite error code
139
157
* @see sqlite3_bind_text()
147
165
* Bind a string value to the SQL statement via it's index where the value
148
166
* of that string will not change for the duration of the statement. This is
149
* more optimal because sqlite will not have to take it's own copy of the
167
* more optimal because sqlite will not have to make it's own copy of the
151
169
* @param index the index of the parameter to bind to
152
170
* @param value the invariant string value
161
179
* Bind a string value to the SQL statement via it's index where the value
162
180
* of that string will not change for the duration of the statement. This is
163
* more optimal because sqlite will not have to take it's own copy of the
181
* more optimal because sqlite will not have to make it's own copy of the
165
183
* @param index the index of the parameter to bind to
166
184
* @param value the invariant string value
195
213
const std::string &name,
198
return bind( bind_parameter_index( name ), value );
217
sqlite3_bind_parameter_index( _handle, name.c_str() );
218
return bind( index, value );
202
222
* Bind a string value to the SQL statement via a named parameter where the
203
223
* string value will not change for the duration of the statement. This
204
* prevents sqlite from taking its own copy of the string.
224
* prevents a copy of the string being taken.
205
225
* @param name the named parameter to bind to
206
226
* @param value the invariant string value
207
* @param value_length the length of the string including zero-terminator
208
227
* @returns an sqlite error code
209
228
* @see sqlite3_bind_text()
252
271
* Stream operator is used to bind values to parameters automatically, in
253
* ascending order. In addition, the null and set_index() auto-binding
254
* manipulators can be used.
272
* ascending order. In addition, the null, execute and set_index() auto-
273
* binding manipulators can be used.
255
274
* @param value a value to bind
257
276
template< class T >
258
basic_statement &operator <<(
277
statement &operator <<( T value )
261
int code = bind( _bind_index, value );
262
if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
279
int error_code = bind( _bind_index, value );
280
if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
281
* Get the index number of a named parameter
282
* @param parameter name
283
* @return index of named parameter
285
int bind_parameter_index(
286
const std::string &name );
290
* @return sqlite error code
291
* @see sqlite3_step()
295
/** the connection upon which to act */
296
connection &_connection;
298
/** the database upon which to act */
298
301
/** the statement handle */
299
302
sqlite3_stmt *_handle;
307
// template specialisations for basic_statement::operator <<()
309
basic_statement &basic_statement::operator << < detail::null_t >(
310
const detail::null_t & );
312
basic_statement &basic_statement::operator << < detail::set_index_t >(
313
const detail::set_index_t &t );
316
} // namespace detail
319
} // namespace sqlite
322
#endif /* SQLITE3CC_BASIC_STATEMENT_H_ */
310
// template specialisations for statement::operator <<()
312
statement& statement::operator << < _null_t >(
315
statement& statement::operator << < _exec_t >(
318
statement& statement::operator << < _set_index_t >(
325
#endif /* STATEMENT_HPP_ */