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> |
|
1
by edam
- initial commit |
26 |
#include <string.h> |
27 |
||
28 |
||
2
by edam
- further initial development |
29 |
sqlite::basic_statement::basic_statement( |
1
by edam
- initial commit |
30 |
database &database, |
31 |
const std::string &sql ) |
|
32 |
: |
|
33 |
_database( database ), |
|
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 ); |
38 |
if( code != SQLITE_OK ) throw sqlite_error( database, code ); |
|
1
by edam
- initial commit |
39 |
} |
40 |
||
41 |
||
2
by edam
- further initial development |
42 |
sqlite::basic_statement::basic_statement( |
43 |
database &database ) |
|
44 |
: |
|
45 |
_database( database ), |
|
46 |
_handle( NULL ), |
|
47 |
_bind_index( 1 ) |
|
48 |
{ |
|
49 |
} |
|
50 |
||
51 |
||
9
by edam
- added NEWS |
52 |
sqlite::basic_statement::~basic_statement() |
1
by edam
- initial commit |
53 |
{ |
54 |
finalize(); |
|
55 |
} |
|
56 |
||
57 |
||
2
by edam
- further initial development |
58 |
int sqlite::basic_statement::prepare( |
1
by edam
- initial commit |
59 |
const std::string &sql ) |
60 |
{ |
|
61 |
finalize(); |
|
62 |
return sqlite3_prepare_v2( _database._handle, sql.c_str(), |
|
63 |
sql.length() + 1, &_handle, NULL ); |
|
64 |
} |
|
65 |
||
66 |
||
2
by edam
- further initial development |
67 |
int sqlite::basic_statement::reset() |
1
by edam
- initial commit |
68 |
{ |
69 |
return sqlite3_reset( _handle ); |
|
70 |
} |
|
71 |
||
72 |
||
2
by edam
- further initial development |
73 |
int sqlite::basic_statement::clear_bindings() |
1
by edam
- initial commit |
74 |
{ |
75 |
_bind_index = 1; |
|
76 |
return sqlite3_clear_bindings( _handle ); |
|
77 |
} |
|
78 |
||
79 |
||
2
by edam
- further initial development |
80 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
81 |
unsigned int index, |
82 |
const char *value, |
|
83 |
unsigned int value_length ) |
|
84 |
{ |
|
85 |
return sqlite3_bind_text( _handle, index, value, value_length, |
|
86 |
SQLITE_STATIC ); |
|
87 |
} |
|
88 |
||
89 |
||
2
by edam
- further initial development |
90 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
91 |
unsigned int index, |
92 |
const char *value ) |
|
93 |
{ |
|
94 |
return bind_static( index, value, strlen( value ) ); |
|
95 |
} |
|
96 |
||
97 |
||
2
by edam
- further initial development |
98 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
99 |
unsigned int index, |
100 |
const std::string &value ) |
|
101 |
{ |
|
102 |
return bind_static( index, value.c_str(), value.length() ); |
|
103 |
} |
|
104 |
||
105 |
||
2
by edam
- further initial development |
106 |
int sqlite::basic_statement::bind_null( |
1
by edam
- initial commit |
107 |
unsigned int index ) |
108 |
{ |
|
109 |
return sqlite3_bind_null( _handle, index ); |
|
110 |
} |
|
111 |
||
112 |
||
2
by edam
- further initial development |
113 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
114 |
const std::string &name, |
115 |
const char *value, |
|
116 |
unsigned int value_length ) |
|
117 |
{ |
|
2
by edam
- further initial development |
118 |
return bind_static( bind_parameter_index( name ), value, value_length ); |
1
by edam
- initial commit |
119 |
} |
120 |
||
121 |
||
2
by edam
- further initial development |
122 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
123 |
const std::string &name, |
124 |
const char *value ) |
|
125 |
{ |
|
126 |
return bind_static( name, value, ::strlen( value ) ); |
|
127 |
} |
|
128 |
||
129 |
||
2
by edam
- further initial development |
130 |
int sqlite::basic_statement::bind_static( |
1
by edam
- initial commit |
131 |
const std::string &name, |
132 |
const std::string &value ) |
|
133 |
{ |
|
134 |
return bind_static( name, value.c_str(), value.length() ); |
|
135 |
} |
|
136 |
||
137 |
||
2
by edam
- further initial development |
138 |
int sqlite::basic_statement::bind_null( |
1
by edam
- initial commit |
139 |
const std::string &name ) |
140 |
{ |
|
2
by edam
- further initial development |
141 |
return bind_null( bind_parameter_index( name ) ); |
1
by edam
- initial commit |
142 |
} |
143 |
||
144 |
||
2
by edam
- further initial development |
145 |
int sqlite::basic_statement::finalize() |
1
by edam
- initial commit |
146 |
{ |
13
by edam
- made basic_statement::step() protected, for use by query and command only |
147 |
int code = SQLITE_OK; |
1
by edam
- initial commit |
148 |
|
149 |
if( _handle ) { |
|
13
by edam
- made basic_statement::step() protected, for use by query and command only |
150 |
code = sqlite3_finalize( _handle ); |
1
by edam
- initial commit |
151 |
_handle = NULL; |
152 |
} |
|
153 |
||
13
by edam
- made basic_statement::step() protected, for use by query and command only |
154 |
return code; |
2
by edam
- further initial development |
155 |
} |
156 |
||
157 |
||
158 |
int sqlite::basic_statement::bind_parameter_index( |
|
159 |
const std::string &name ) |
|
160 |
{ |
|
161 |
unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() ); |
|
162 |
if( !index ) throw std::range_error( "named parameter not found" ); |
|
163 |
return index; |
|
164 |
} |
|
165 |
||
166 |
||
13
by edam
- made basic_statement::step() protected, for use by query and command only |
167 |
int sqlite::basic_statement::step() |
168 |
{ |
|
169 |
return sqlite3_step( _handle ); |
|
1
by edam
- initial commit |
170 |
} |