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 |
* |
4 |
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org> |
|
5 |
* |
|
2
by edam
- further initial development |
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. |
|
1
by edam
- initial commit |
8 |
* |
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. |
|
13 |
* |
|
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. |
|
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> |
|
25 |
#include <sqlite3cc/database.h> |
|
26 |
#include <sqlite3cc/manipulator.h> |
|
1
by edam
- initial commit |
27 |
#include <string.h> |
28 |
#include <iomanip> |
|
29 |
||
30 |
||
2
by edam
- further initial development |
31 |
sqlite::basic_statement::basic_statement( |
1
by edam
- initial commit |
32 |
database &database, |
33 |
const std::string &sql ) |
|
34 |
: |
|
35 |
_database( database ), |
|
36 |
_handle( NULL ), |
|
37 |
_bind_index( 1 ) |
|
38 |
{ |
|
39 |
int error_code = prepare( sql ); |
|
40 |
if( error_code != SQLITE_OK ) throw sqlite_error( error_code ); |
|
41 |
} |
|
42 |
||
43 |
||
2
by edam
- further initial development |
44 |
sqlite::basic_statement::basic_statement( |
45 |
database &database ) |
|
46 |
: |
|
47 |
_database( database ), |
|
48 |
_handle( NULL ), |
|
49 |
_bind_index( 1 ) |
|
50 |
{ |
|
51 |
} |
|
52 |
||
53 |
||
9
by edam
- added NEWS |
54 |
sqlite::basic_statement::~basic_statement() |
1
by edam
- initial commit |
55 |
{ |
56 |
finalize(); |
|
57 |
} |
|
58 |
||
59 |
||
2
by edam
- further initial development |
60 |
int sqlite::basic_statement::prepare( |
1
by edam
- initial commit |
61 |
const std::string &sql ) |
62 |
{ |
|
63 |
finalize(); |
|
64 |
return sqlite3_prepare_v2( _database._handle, sql.c_str(), |
|
65 |
sql.length() + 1, &_handle, NULL ); |
|
66 |
} |
|
67 |
||
68 |
||
2
by edam
- further initial development |
69 |
int sqlite::basic_statement::reset() |
1
by edam
- initial commit |
70 |
{ |
71 |
return sqlite3_reset( _handle ); |
|
72 |
} |
|
73 |
||
74 |
||
2
by edam
- further initial development |
75 |
int sqlite::basic_statement::clear_bindings() |
1
by edam
- initial commit |
76 |
{ |
77 |
_bind_index = 1; |
|
78 |
return sqlite3_clear_bindings( _handle ); |
|
79 |
} |
|
80 |
||
81 |
||
2
by edam
- further initial development |
82 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
83 |
unsigned int index, |
84 |
const char *value, |
|
85 |
unsigned int value_length ) |
|
86 |
{ |
|
87 |
return sqlite3_bind_text( _handle, index, value, value_length, |
|
88 |
SQLITE_STATIC ); |
|
89 |
} |
|
90 |
||
91 |
||
2
by edam
- further initial development |
92 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
93 |
unsigned int index, |
94 |
const char *value ) |
|
95 |
{ |
|
96 |
return bind_static( index, value, strlen( value ) ); |
|
97 |
} |
|
98 |
||
99 |
||
2
by edam
- further initial development |
100 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
101 |
unsigned int index, |
102 |
const std::string &value ) |
|
103 |
{ |
|
104 |
return bind_static( index, value.c_str(), value.length() ); |
|
105 |
} |
|
106 |
||
107 |
||
2
by edam
- further initial development |
108 |
int sqlite::basic_statement::bind_null( |
1
by edam
- initial commit |
109 |
unsigned int index ) |
110 |
{ |
|
111 |
return sqlite3_bind_null( _handle, index ); |
|
112 |
} |
|
113 |
||
114 |
||
2
by edam
- further initial development |
115 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
116 |
const std::string &name, |
117 |
const char *value, |
|
118 |
unsigned int value_length ) |
|
119 |
{ |
|
2
by edam
- further initial development |
120 |
return bind_static( bind_parameter_index( name ), value, value_length ); |
1
by edam
- initial commit |
121 |
} |
122 |
||
123 |
||
2
by edam
- further initial development |
124 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
125 |
const std::string &name, |
126 |
const char *value ) |
|
127 |
{ |
|
128 |
return bind_static( name, value, ::strlen( value ) ); |
|
129 |
} |
|
130 |
||
131 |
||
2
by edam
- further initial development |
132 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
133 |
const std::string &name, |
134 |
const std::string &value ) |
|
135 |
{ |
|
136 |
return bind_static( name, value.c_str(), value.length() ); |
|
137 |
} |
|
138 |
||
139 |
||
2
by edam
- further initial development |
140 |
int sqlite::basic_statement::bind_null( |
1
by edam
- initial commit |
141 |
const std::string &name ) |
142 |
{ |
|
2
by edam
- further initial development |
143 |
return bind_null( bind_parameter_index( name ) ); |
1
by edam
- initial commit |
144 |
} |
145 |
||
146 |
||
2
by edam
- further initial development |
147 |
int sqlite::basic_statement::finalize() |
1
by edam
- initial commit |
148 |
{ |
149 |
int error_code = SQLITE_OK; |
|
150 |
||
151 |
if( _handle ) { |
|
152 |
error_code = sqlite3_finalize( _handle ); |
|
153 |
_handle = NULL; |
|
154 |
} |
|
155 |
||
156 |
return error_code; |
|
157 |
} |
|
158 |
||
159 |
||
2
by edam
- further initial development |
160 |
int sqlite::basic_statement::step() |
161 |
{ |
|
162 |
return sqlite3_step( _handle ); |
|
163 |
} |
|
164 |
||
165 |
||
166 |
int sqlite::basic_statement::bind_parameter_index( |
|
167 |
const std::string &name ) |
|
168 |
{ |
|
169 |
unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() ); |
|
170 |
if( !index ) throw std::range_error( "named parameter not found" ); |
|
171 |
return index; |
|
172 |
} |
|
173 |
||
174 |
||
1
by edam
- initial commit |
175 |
template< > |
2
by edam
- further initial development |
176 |
sqlite::basic_statement &sqlite::basic_statement::operator << |
177 |
< sqlite::_null_t >( |
|
178 |
const sqlite::_null_t & ) |
|
1
by edam
- initial commit |
179 |
{ |
180 |
int error_code = bind_null( _bind_index ); |
|
181 |
if( error_code != SQLITE_OK ) throw sqlite_error( error_code ); |
|
182 |
_bind_index++; |
|
183 |
return *this; |
|
184 |
} |
|
185 |
||
2
by edam
- further initial development |
186 |
|
1
by edam
- initial commit |
187 |
template< > |
2
by edam
- further initial development |
188 |
sqlite::basic_statement &sqlite::basic_statement::operator << |
189 |
< sqlite::_exec_t >( |
|
190 |
const sqlite::_exec_t & ) |
|
1
by edam
- initial commit |
191 |
{ |
192 |
int error_code = step(); |
|
2
by edam
- further initial development |
193 |
if( error_code != SQLITE_DONE ) { |
1
by edam
- initial commit |
194 |
if( error_code == SQLITE_ROW ) |
195 |
throw sqlite_error( "statement returned results" ); |
|
196 |
else |
|
197 |
throw sqlite_error( error_code ); |
|
2
by edam
- further initial development |
198 |
} |
1
by edam
- initial commit |
199 |
return *this; |
200 |
} |
|
201 |
||
2
by edam
- further initial development |
202 |
|
1
by edam
- initial commit |
203 |
template< > |
2
by edam
- further initial development |
204 |
sqlite::basic_statement &sqlite::basic_statement::operator << |
205 |
< sqlite::_set_index_t >( |
|
206 |
const sqlite::_set_index_t &t ) |
|
1
by edam
- initial commit |
207 |
{ |
208 |
_bind_index = t._index; |
|
209 |
return *this; |
|
210 |
} |