bzr branch
http://bzr.ed.am/sqlite3cc
1
by edam
- initial commit |
1 |
/* |
2
by edam
- further initial development |
2 |
* basic_statement.cc |
1
by edam
- initial commit |
3 |
* |
22
by edam
updated email and web addresses |
4 |
* Copyright (C) 2009 Tim Marston <tim@ed.am> |
1
by edam
- initial commit |
5 |
* |
2
by edam
- further initial development |
6 |
* This file is part of sqlite3cc (hereafter referred to as "this program"). |
22
by edam
updated email and web addresses |
7 |
* See http://ed.am/dev/sqlite3cc for more information. |
8 |
* |
|
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 |
|
12 |
* later version. |
|
13 |
* |
|
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 |
|
17 |
* details. |
|
1
by edam
- initial commit |
18 |
* |
19 |
* You should have received a copy of the GNU Lesser General Public License |
|
20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21 |
*/ |
|
22 |
||
2
by edam
- further initial development |
23 |
#include <sqlite3cc/basic_statement.h> |
24 |
#include <sqlite3cc/exception.h> |
|
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
25 |
#include <sqlite3cc/connection.h> |
14
by edam
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec |
26 |
#include <sqlite3cc/manipulator.h> |
1
by edam
- initial commit |
27 |
|
28 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
29 |
sqlite::detail::basic_statement::basic_statement( |
30 |
connection &connection, |
|
1
by edam
- initial commit |
31 |
const std::string &sql ) |
32 |
: |
|
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
33 |
_connection( connection ), |
1
by edam
- initial commit |
34 |
_handle( NULL ), |
35 |
_bind_index( 1 ) |
|
36 |
{ |
|
13
by edam
- made basic_statement::step() protected, for use by query and command only |
37 |
int code = prepare( sql ); |
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
38 |
if( code != SQLITE_OK ) throw sqlite_error( connection, code ); |
1
by edam
- initial commit |
39 |
} |
40 |
||
41 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
42 |
sqlite::detail::basic_statement::basic_statement( |
43 |
connection &connection ) |
|
2
by edam
- further initial development |
44 |
: |
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
45 |
_connection( connection ), |
2
by edam
- further initial development |
46 |
_handle( NULL ), |
47 |
_bind_index( 1 ) |
|
48 |
{ |
|
49 |
} |
|
50 |
||
51 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
52 |
sqlite::detail::basic_statement::~basic_statement() |
1
by edam
- initial commit |
53 |
{ |
54 |
finalize(); |
|
55 |
} |
|
56 |
||
57 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
58 |
int sqlite::detail::basic_statement::prepare( |
1
by edam
- initial commit |
59 |
const std::string &sql ) |
60 |
{ |
|
61 |
finalize(); |
|
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
62 |
return sqlite3_prepare_v2( _connection._handle, sql.c_str(), |
1
by edam
- initial commit |
63 |
sql.length() + 1, &_handle, NULL ); |
64 |
} |
|
65 |
||
66 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
67 |
int sqlite::detail::basic_statement::reset() |
1
by edam
- initial commit |
68 |
{ |
69 |
return sqlite3_reset( _handle ); |
|
70 |
} |
|
71 |
||
72 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
73 |
int sqlite::detail::basic_statement::clear_bindings() |
1
by edam
- initial commit |
74 |
{ |
75 |
_bind_index = 1; |
|
76 |
return sqlite3_clear_bindings( _handle ); |
|
77 |
} |
|
78 |
||
79 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
80 |
int sqlite::detail::basic_statement::bind_static( |
1
by edam
- initial commit |
81 |
unsigned int index, |
82 |
const char *value, |
|
83 |
unsigned int value_length ) |
|
84 |
{ |
|
42
by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream |
85 |
// bind as text (applying the type affinity of the underlying column) |
1
by edam
- initial commit |
86 |
return sqlite3_bind_text( _handle, index, value, value_length, |
87 |
SQLITE_STATIC ); |
|
88 |
} |
|
89 |
||
90 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
91 |
int sqlite::detail::basic_statement::bind_static( |
1
by edam
- initial commit |
92 |
unsigned int index, |
93 |
const char *value ) |
|
94 |
{ |
|
95 |
return bind_static( index, value, strlen( value ) ); |
|
96 |
} |
|
97 |
||
98 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
99 |
int sqlite::detail::basic_statement::bind_static( |
1
by edam
- initial commit |
100 |
unsigned int index, |
101 |
const std::string &value ) |
|
102 |
{ |
|
103 |
return bind_static( index, value.c_str(), value.length() ); |
|
104 |
} |
|
105 |
||
106 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
107 |
int sqlite::detail::basic_statement::bind_null( |
1
by edam
- initial commit |
108 |
unsigned int index ) |
109 |
{ |
|
110 |
return sqlite3_bind_null( _handle, index ); |
|
111 |
} |
|
112 |
||
113 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
114 |
int sqlite::detail::basic_statement::bind_static( |
1
by edam
- initial commit |
115 |
const std::string &name, |
116 |
const char *value, |
|
117 |
unsigned int value_length ) |
|
118 |
{ |
|
2
by edam
- further initial development |
119 |
return bind_static( bind_parameter_index( name ), value, value_length ); |
1
by edam
- initial commit |
120 |
} |
121 |
||
122 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
123 |
int sqlite::detail::basic_statement::bind_static( |
1
by edam
- initial commit |
124 |
const std::string &name, |
125 |
const char *value ) |
|
126 |
{ |
|
127 |
return bind_static( name, value, ::strlen( value ) ); |
|
128 |
} |
|
129 |
||
130 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
131 |
int sqlite::detail::basic_statement::bind_static( |
1
by edam
- initial commit |
132 |
const std::string &name, |
133 |
const std::string &value ) |
|
134 |
{ |
|
135 |
return bind_static( name, value.c_str(), value.length() ); |
|
136 |
} |
|
137 |
||
138 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
139 |
int sqlite::detail::basic_statement::bind_null( |
1
by edam
- initial commit |
140 |
const std::string &name ) |
141 |
{ |
|
2
by edam
- further initial development |
142 |
return bind_null( bind_parameter_index( name ) ); |
1
by edam
- initial commit |
143 |
} |
144 |
||
145 |
||
42
by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream |
146 |
int sqlite::detail::basic_statement::bind_blob( |
147 |
unsigned int index, |
|
148 |
const char *value, |
|
149 |
unsigned int value_length ) |
|
150 |
{ |
|
151 |
return sqlite3_bind_blob( _handle, index, value, value_length, |
|
152 |
SQLITE_TRANSIENT ); |
|
153 |
} |
|
154 |
||
155 |
||
156 |
int sqlite::detail::basic_statement::bind_blob( |
|
157 |
unsigned int index, |
|
158 |
const std::string &value ) |
|
159 |
{ |
|
160 |
return bind_blob( index, value.c_str(), value.length() ); |
|
161 |
} |
|
162 |
||
163 |
||
164 |
int sqlite::detail::basic_statement::bind_blob( |
|
165 |
const std::string &name, |
|
166 |
const char *value, |
|
167 |
unsigned int value_length ) |
|
168 |
{ |
|
169 |
return bind_blob( bind_parameter_index( name ), value, value_length ); |
|
170 |
} |
|
171 |
||
172 |
||
173 |
int sqlite::detail::basic_statement::bind_blob( |
|
174 |
const std::string &name, |
|
175 |
const std::string &value ) |
|
176 |
{ |
|
177 |
return bind_blob( bind_parameter_index( name ), value ); |
|
178 |
} |
|
179 |
||
180 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
181 |
int sqlite::detail::basic_statement::finalize() |
1
by edam
- initial commit |
182 |
{ |
13
by edam
- made basic_statement::step() protected, for use by query and command only |
183 |
int code = SQLITE_OK; |
1
by edam
- initial commit |
184 |
|
185 |
if( _handle ) { |
|
13
by edam
- made basic_statement::step() protected, for use by query and command only |
186 |
code = sqlite3_finalize( _handle ); |
1
by edam
- initial commit |
187 |
_handle = NULL; |
188 |
} |
|
189 |
||
13
by edam
- made basic_statement::step() protected, for use by query and command only |
190 |
return code; |
2
by edam
- further initial development |
191 |
} |
192 |
||
193 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
194 |
int sqlite::detail::basic_statement::bind_parameter_index( |
2
by edam
- further initial development |
195 |
const std::string &name ) |
196 |
{ |
|
197 |
unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() ); |
|
198 |
if( !index ) throw std::range_error( "named parameter not found" ); |
|
199 |
return index; |
|
200 |
} |
|
201 |
||
202 |
||
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
203 |
int sqlite::detail::basic_statement::step() |
13
by edam
- made basic_statement::step() protected, for use by query and command only |
204 |
{ |
205 |
return sqlite3_step( _handle ); |
|
1
by edam
- initial commit |
206 |
} |
14
by edam
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec |
207 |
|
208 |
||
209 |
template< > |
|
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
210 |
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator << |
14
by edam
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec |
211 |
< sqlite::detail::null_t >( |
212 |
const sqlite::detail::null_t & ) |
|
213 |
{ |
|
214 |
int code = bind_null( _bind_index ); |
|
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
215 |
if( code != SQLITE_OK ) throw sqlite_error( _connection, code ); |
14
by edam
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec |
216 |
_bind_index++; |
217 |
return *this; |
|
218 |
} |
|
219 |
||
220 |
||
221 |
template< > |
|
16
by edam
- renamed database to connection to better identify what it is (would database_connection be better though?) |
222 |
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator << |
14
by edam
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec |
223 |
< sqlite::detail::set_index_t >( |
224 |
const sqlite::detail::set_index_t &t ) |
|
225 |
{ |
|
226 |
_bind_index = t._index; |
|
227 |
return *this; |
|
228 |
} |
|
42
by Tim Marston
improved support for blobs: added bind_blob(), added blob_t for binding stream |
229 |
|
230 |
||
231 |
template< > |
|
232 |
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator << |
|
233 |
< sqlite::detail::blob_t >( |
|
234 |
const sqlite::detail::blob_t &t ) |
|
235 |
{ |
|
236 |
int code = bind_blob( _bind_index, t.value, t.value_length ); |
|
237 |
if( code != SQLITE_OK ) throw sqlite_error( _connection, code ); |
|
238 |
_bind_index++; |
|
239 |
return *this; |
|
240 |
} |
|
241 |
||
242 |
||
243 |
template< > |
|
244 |
int sqlite::detail::basic_statement::bind< int >( |
|
245 |
unsigned int index, |
|
246 |
const int &value ) |
|
247 |
{ |
|
248 |
return sqlite3_bind_int( _handle, index, value ); |
|
249 |
} |
|
250 |
||
251 |
||
252 |
template< > |
|
253 |
int sqlite::detail::basic_statement::bind< double >( |
|
254 |
unsigned int index, |
|
255 |
const double &value ) |
|
256 |
{ |
|
257 |
return sqlite3_bind_double( _handle, index, value ); |
|
258 |
} |