4
* Copyright (C) 2009 Tim Marston <tim@ed.am>
4
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
6
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
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.
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
23
#include <sqlite3cc/basic_statement.h>
24
24
#include <sqlite3cc/exception.h>
25
#include <sqlite3cc/connection.h>
25
#include <sqlite3cc/database.h>
26
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(),
31
sqlite::basic_statement::basic_statement(
33
const std::string &sql )
35
_database( database ),
39
int error_code = prepare( sql );
40
if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
44
sqlite::basic_statement::basic_statement(
47
_database( database ),
54
sqlite::basic_statement::~basic_statement()
60
int sqlite::basic_statement::prepare(
61
const std::string &sql )
64
return sqlite3_prepare_v2( _database._handle, sql.c_str(),
63
65
sql.length() + 1, &_handle, NULL );
67
int sqlite::detail::basic_statement::reset()
69
int sqlite::basic_statement::reset()
69
71
return sqlite3_reset( _handle );
73
int sqlite::detail::basic_statement::clear_bindings()
75
int sqlite::basic_statement::clear_bindings()
76
78
return sqlite3_clear_bindings( _handle );
80
int sqlite::detail::basic_statement::bind_static(
82
int sqlite::basic_statement::bind_static(
81
83
unsigned int index,
83
85
unsigned int value_length )
85
// bind as text (applying the type affinity of the underlying column)
86
87
return sqlite3_bind_text( _handle, index, value, value_length,
91
int sqlite::detail::basic_statement::bind_static(
92
int sqlite::basic_statement::bind_static(
92
93
unsigned int index,
93
94
const char *value )
107
int sqlite::detail::basic_statement::bind_null(
108
int sqlite::basic_statement::bind_null(
108
109
unsigned int index )
110
111
return sqlite3_bind_null( _handle, index );
114
int sqlite::detail::basic_statement::bind_static(
115
int sqlite::basic_statement::bind_static(
115
116
const std::string &name,
116
117
const char *value,
117
118
unsigned int value_length )
139
int sqlite::detail::basic_statement::bind_null(
140
int sqlite::basic_statement::bind_null(
140
141
const std::string &name )
142
143
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;
147
int sqlite::basic_statement::finalize()
149
int error_code = SQLITE_OK;
186
code = sqlite3_finalize( _handle );
152
error_code = sqlite3_finalize( _handle );
194
int sqlite::detail::basic_statement::bind_parameter_index(
160
int sqlite::basic_statement::step()
162
return sqlite3_step( _handle );
166
int sqlite::basic_statement::bind_parameter_index(
195
167
const std::string &name )
197
169
unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
203
int sqlite::detail::basic_statement::step()
205
return sqlite3_step( _handle );
210
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator <<
176
sqlite::basic_statement &sqlite::basic_statement::operator <<
211
177
< sqlite::detail::null_t >(
212
178
const sqlite::detail::null_t & )
214
int code = bind_null( _bind_index );
215
if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
180
int error_code = bind_null( _bind_index );
181
if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
222
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator <<
188
sqlite::basic_statement &sqlite::basic_statement::operator <<
189
< sqlite::detail::exec_t >(
190
const sqlite::detail::exec_t & )
192
int error_code = step();
193
if( error_code != SQLITE_DONE ) {
194
if( error_code == SQLITE_ROW )
195
throw sqlite_error( "statement returned results" );
197
throw sqlite_error( error_code );
204
sqlite::basic_statement &sqlite::basic_statement::operator <<
223
205
< sqlite::detail::set_index_t >(
224
206
const sqlite::detail::set_index_t &t )
226
208
_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 );