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> |
|
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 |
#include <string.h> |
28 |
||
29 |
||
2
by edam
- further initial development |
30 |
sqlite::basic_statement::basic_statement( |
1
by edam
- initial commit |
31 |
database &database, |
32 |
const std::string &sql ) |
|
33 |
: |
|
34 |
_database( database ), |
|
35 |
_handle( NULL ), |
|
36 |
_bind_index( 1 ) |
|
37 |
{ |
|
13
by edam
- made basic_statement::step() protected, for use by query and command only |
38 |
int code = prepare( sql ); |
39 |
if( code != SQLITE_OK ) throw sqlite_error( database, code ); |
|
1
by edam
- initial commit |
40 |
} |
41 |
||
42 |
||
2
by edam
- further initial development |
43 |
sqlite::basic_statement::basic_statement( |
44 |
database &database ) |
|
45 |
: |
|
46 |
_database( database ), |
|
47 |
_handle( NULL ), |
|
48 |
_bind_index( 1 ) |
|
49 |
{ |
|
50 |
} |
|
51 |
||
52 |
||
9
by edam
- added NEWS |
53 |
sqlite::basic_statement::~basic_statement() |
1
by edam
- initial commit |
54 |
{ |
55 |
finalize(); |
|
56 |
} |
|
57 |
||
58 |
||
2
by edam
- further initial development |
59 |
int sqlite::basic_statement::prepare( |
1
by edam
- initial commit |
60 |
const std::string &sql ) |
61 |
{ |
|
62 |
finalize(); |
|
63 |
return sqlite3_prepare_v2( _database._handle, sql.c_str(), |
|
64 |
sql.length() + 1, &_handle, NULL ); |
|
65 |
} |
|
66 |
||
67 |
||
2
by edam
- further initial development |
68 |
int sqlite::basic_statement::reset() |
1
by edam
- initial commit |
69 |
{ |
70 |
return sqlite3_reset( _handle ); |
|
71 |
} |
|
72 |
||
73 |
||
2
by edam
- further initial development |
74 |
int sqlite::basic_statement::clear_bindings() |
1
by edam
- initial commit |
75 |
{ |
76 |
_bind_index = 1; |
|
77 |
return sqlite3_clear_bindings( _handle ); |
|
78 |
} |
|
79 |
||
80 |
||
2
by edam
- further initial development |
81 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
82 |
unsigned int index, |
83 |
const char *value, |
|
84 |
unsigned int value_length ) |
|
85 |
{ |
|
86 |
return sqlite3_bind_text( _handle, index, value, value_length, |
|
87 |
SQLITE_STATIC ); |
|
88 |
} |
|
89 |
||
90 |
||
2
by edam
- further initial development |
91 |
int sqlite::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 |
||
2
by edam
- further initial development |
99 |
int sqlite::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 |
||
2
by edam
- further initial development |
107 |
int sqlite::basic_statement::bind_null( |
1
by edam
- initial commit |
108 |
unsigned int index ) |
109 |
{ |
|
110 |
return sqlite3_bind_null( _handle, index ); |
|
111 |
} |
|
112 |
||
113 |
||
2
by edam
- further initial development |
114 |
int sqlite::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 |
||
2
by edam
- further initial development |
123 |
int sqlite::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 |
||
2
by edam
- further initial development |
131 |
int sqlite::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 |
||
2
by edam
- further initial development |
139 |
int sqlite::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 |
||
2
by edam
- further initial development |
146 |
int sqlite::basic_statement::finalize() |
1
by edam
- initial commit |
147 |
{ |
13
by edam
- made basic_statement::step() protected, for use by query and command only |
148 |
int code = SQLITE_OK; |
1
by edam
- initial commit |
149 |
|
150 |
if( _handle ) { |
|
13
by edam
- made basic_statement::step() protected, for use by query and command only |
151 |
code = sqlite3_finalize( _handle ); |
1
by edam
- initial commit |
152 |
_handle = NULL; |
153 |
} |
|
154 |
||
13
by edam
- made basic_statement::step() protected, for use by query and command only |
155 |
return code; |
2
by edam
- further initial development |
156 |
} |
157 |
||
158 |
||
159 |
int sqlite::basic_statement::bind_parameter_index( |
|
160 |
const std::string &name ) |
|
161 |
{ |
|
162 |
unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() ); |
|
163 |
if( !index ) throw std::range_error( "named parameter not found" ); |
|
164 |
return index; |
|
165 |
} |
|
166 |
||
167 |
||
13
by edam
- made basic_statement::step() protected, for use by query and command only |
168 |
int sqlite::basic_statement::step() |
169 |
{ |
|
170 |
return sqlite3_step( _handle ); |
|
1
by edam
- initial commit |
171 |
} |
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 |
172 |
|
173 |
||
174 |
||
175 |
template< > |
|
176 |
sqlite::basic_statement &sqlite::basic_statement::operator << |
|
177 |
< sqlite::detail::null_t >( |
|
178 |
const sqlite::detail::null_t & ) |
|
179 |
{ |
|
180 |
int code = bind_null( _bind_index ); |
|
181 |
if( code != SQLITE_OK ) throw sqlite_error( _database, code ); |
|
182 |
_bind_index++; |
|
183 |
return *this; |
|
184 |
} |
|
185 |
||
186 |
||
187 |
template< > |
|
188 |
sqlite::basic_statement &sqlite::basic_statement::operator << |
|
189 |
< sqlite::detail::set_index_t >( |
|
190 |
const sqlite::detail::set_index_t &t ) |
|
191 |
{ |
|
192 |
_bind_index = t._index; |
|
193 |
return *this; |
|
194 |
} |