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
#include <sqlite3cc/basic_statement.h>
24
#include <sqlite3cc/exception.h>
25
#include <sqlite3cc/database.h>
23
#include <sqlitepp/statement.hpp>
24
#include <sqlitepp/exception.hpp>
25
#include <sqlitepp/database.hpp>
26
26
#include <string.h>
29
sqlite::basic_statement::basic_statement(
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(
30
44
database &database,
31
45
const std::string &sql )
37
int code = prepare( sql );
38
if( code != SQLITE_OK ) throw sqlite_error( database, code );
42
sqlite::basic_statement::basic_statement(
45
_database( database ),
52
sqlite::basic_statement::~basic_statement()
51
int error_code = prepare( sql );
52
if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
56
sqlite::statement::~statement() throw( )
58
int sqlite::basic_statement::prepare(
62
int sqlite::statement::prepare(
59
63
const std::string &sql )
67
int sqlite::basic_statement::reset()
71
int sqlite::statement::step()
73
return sqlite3_step( _handle );
77
int sqlite::statement::reset()
69
79
return sqlite3_reset( _handle );
73
int sqlite::basic_statement::clear_bindings()
83
int sqlite::statement::clear_bindings()
76
86
return sqlite3_clear_bindings( _handle );
80
int sqlite::basic_statement::bind_static(
90
int sqlite::statement::bind_static(
81
91
unsigned int index,
83
93
unsigned int value_length )
106
int sqlite::basic_statement::bind_null(
116
int sqlite::statement::bind_null(
107
117
unsigned int index )
109
119
return sqlite3_bind_null( _handle, index );
113
int sqlite::basic_statement::bind_static(
123
int sqlite::statement::bind_static(
114
124
const std::string &name,
115
125
const char *value,
116
126
unsigned int value_length )
118
return bind_static( bind_parameter_index( name ), value, value_length );
128
unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
129
return bind_static( index, value, value_length );
122
int sqlite::basic_statement::bind_static(
133
int sqlite::statement::bind_static(
123
134
const std::string &name,
124
135
const char *value )
138
int sqlite::basic_statement::bind_null(
149
int sqlite::statement::bind_null(
139
150
const std::string &name )
141
return bind_null( bind_parameter_index( name ) );
152
unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
153
return bind_null( index );
145
int sqlite::basic_statement::finalize()
157
int sqlite::statement::finalize()
147
int code = SQLITE_OK;
159
int error_code = SQLITE_OK;
150
code = sqlite3_finalize( _handle );
162
error_code = sqlite3_finalize( _handle );
158
int sqlite::basic_statement::bind_parameter_index(
159
const std::string &name )
161
unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
162
if( !index ) throw std::range_error( "named parameter not found" );
167
int sqlite::basic_statement::step()
169
return sqlite3_step( _handle );
171
sqlite::statement& sqlite::statement::operator << < sqlite::_null_t >(
174
int error_code = bind_null( _bind_index );
175
if( error_code != SQLITE_OK ) throw sqlite_error( error_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 )
197
_bind_index = t._index;