4
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
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
* 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.
4
* Copyright (C) 2009 Tim Marston <tim@ed.am>
6
* This file is part of sqlite3cc (hereafter referred to as "this program").
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/>.
23
#include <sqlitepp/statement.hpp>
24
#include <sqlitepp/exception.hpp>
25
#include <sqlitepp/database.hpp>
30
const sqlite::_null_t sqlite::null = { };
32
const sqlite::_exec_t sqlite::exec = { };
35
sqlite::_set_index_t sqlite::set_index(
38
_set_index_t t = { index };
43
sqlite::statement::statement(
45
const std::string &sql )
47
_database( database ),
51
int error_code = prepare( sql );
52
if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
56
sqlite::statement::~statement() throw( )
62
int sqlite::statement::prepare(
63
const std::string &sql )
66
return sqlite3_prepare_v2( _database._handle, sql.c_str(),
23
#include <sqlite3cc/basic_statement.h>
24
#include <sqlite3cc/exception.h>
25
#include <sqlite3cc/connection.h>
26
#include <sqlite3cc/manipulator.h>
29
sqlite::detail::basic_statement::basic_statement(
30
connection &connection,
31
const std::string &sql )
33
_connection( connection ),
37
int code = prepare( sql );
38
if( code != SQLITE_OK ) throw sqlite_error( connection, code );
42
sqlite::detail::basic_statement::basic_statement(
43
connection &connection )
45
_connection( connection ),
52
sqlite::detail::basic_statement::~basic_statement()
58
int sqlite::detail::basic_statement::prepare(
59
const std::string &sql )
62
return sqlite3_prepare_v2( _connection._handle, sql.c_str(),
67
63
sql.length() + 1, &_handle, NULL );
71
int sqlite::statement::step()
73
return sqlite3_step( _handle );
77
int sqlite::statement::reset()
67
int sqlite::detail::basic_statement::reset()
79
69
return sqlite3_reset( _handle );
83
int sqlite::statement::clear_bindings()
73
int sqlite::detail::basic_statement::clear_bindings()
86
76
return sqlite3_clear_bindings( _handle );
90
int sqlite::statement::bind_static(
80
int sqlite::detail::basic_statement::bind_static(
91
81
unsigned int index,
93
83
unsigned int value_length )
85
// bind as text (applying the type affinity of the underlying column)
95
86
return sqlite3_bind_text( _handle, index, value, value_length,
100
int sqlite::statement::bind_static(
91
int sqlite::detail::basic_statement::bind_static(
101
92
unsigned int index,
102
93
const char *value )
116
int sqlite::statement::bind_null(
107
int sqlite::detail::basic_statement::bind_null(
117
108
unsigned int index )
119
110
return sqlite3_bind_null( _handle, index );
123
int sqlite::statement::bind_static(
114
int sqlite::detail::basic_statement::bind_static(
124
115
const std::string &name,
125
116
const char *value,
126
117
unsigned int value_length )
128
unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
129
return bind_static( index, value, value_length );
119
return bind_static( bind_parameter_index( name ), value, value_length );
133
int sqlite::statement::bind_static(
123
int sqlite::detail::basic_statement::bind_static(
134
124
const std::string &name,
135
125
const char *value )
149
int sqlite::statement::bind_null(
139
int sqlite::detail::basic_statement::bind_null(
150
140
const std::string &name )
152
unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
153
return bind_null( index );
157
int sqlite::statement::finalize()
159
int error_code = SQLITE_OK;
142
return bind_null( bind_parameter_index( name ) );
146
int sqlite::detail::basic_statement::bind_blob(
149
unsigned int value_length )
151
return sqlite3_bind_blob( _handle, index, value, value_length,
156
int sqlite::detail::basic_statement::bind_blob(
158
const std::string &value )
160
return bind_blob( index, value.c_str(), value.length() );
164
int sqlite::detail::basic_statement::bind_blob(
165
const std::string &name,
167
unsigned int value_length )
169
return bind_blob( bind_parameter_index( name ), value, value_length );
173
int sqlite::detail::basic_statement::bind_blob(
174
const std::string &name,
175
const std::string &value )
177
return bind_blob( bind_parameter_index( name ), value );
181
int sqlite::detail::basic_statement::finalize()
183
int code = SQLITE_OK;
162
error_code = sqlite3_finalize( _handle );
186
code = sqlite3_finalize( _handle );
194
int sqlite::detail::basic_statement::bind_parameter_index(
195
const std::string &name )
197
unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
198
if( !index ) throw std::range_error( "named parameter not found" );
203
int sqlite::detail::basic_statement::step()
205
return sqlite3_step( _handle );
171
sqlite::statement& sqlite::statement::operator << < sqlite::_null_t >(
210
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator <<
211
< sqlite::detail::null_t >(
212
const sqlite::detail::null_t & )
174
int error_code = bind_null( _bind_index );
175
if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
214
int code = bind_null( _bind_index );
215
if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
181
sqlite::statement& sqlite::statement::operator << < sqlite::_exec_t >(
184
int error_code = step();
185
if( error_code != SQLITE_DONE )
186
if( error_code == SQLITE_ROW )
187
throw sqlite_error( "statement returned results" );
189
throw sqlite_error( error_code );
194
sqlite::statement& sqlite::statement::operator << < sqlite::_set_index_t >(
195
sqlite::_set_index_t t )
222
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator <<
223
< sqlite::detail::set_index_t >(
224
const sqlite::detail::set_index_t &t )
197
226
_bind_index = t._index;
232
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator <<
233
< sqlite::detail::blob_t >(
234
const sqlite::detail::blob_t &t )
236
int code = bind_blob( _bind_index, t.value, t.value_length );
237
if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
244
int sqlite::detail::basic_statement::bind< int >(
248
return sqlite3_bind_int( _handle, index, value );
253
int sqlite::detail::basic_statement::bind< double >(
255
const double &value )
257
return sqlite3_bind_double( _handle, index, value );