/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc

« back to all changes in this revision

Viewing changes to src/statement.cpp

  • Committer: edam
  • Date: 2009-11-25 20:32:34 UTC
  • Revision ID: edam@waxworlds.org-20091125203234-24i7mzd60c7o46py
- initial commit
- basic support for: database, statements, commands, transactions, exceptions
- initial test app
- initial liscence and readme

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * basic_statement.cc
3
 
 *
4
 
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
5
 
 *
6
 
 * This file is part of sqlite3cc (hereafter referred to as "this program").
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.
 
2
 * statement.cpp
 
3
 *
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
5
 *
 
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.
 
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
18
 *
19
19
 * You should have received a copy of the GNU Lesser General Public License
20
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
21
 */
22
22
 
23
 
#include <sqlite3cc/basic_statement.h>
24
 
#include <sqlite3cc/exception.h>
25
 
#include <sqlite3cc/connection.h>
26
 
#include <sqlite3cc/manipulator.h>
27
 
 
28
 
 
29
 
sqlite::detail::basic_statement::basic_statement(
30
 
        connection &connection,
31
 
        const std::string &sql )
32
 
        :
33
 
        _connection( connection ),
34
 
        _handle( NULL ),
35
 
        _bind_index( 1 )
36
 
{
37
 
        int code = prepare( sql );
38
 
        if( code != SQLITE_OK ) throw sqlite_error( connection, code );
39
 
}
40
 
 
41
 
 
42
 
sqlite::detail::basic_statement::basic_statement(
43
 
        connection &connection )
44
 
        :
45
 
        _connection( connection ),
46
 
        _handle( NULL ),
47
 
        _bind_index( 1 )
48
 
{
49
 
}
50
 
 
51
 
 
52
 
sqlite::detail::basic_statement::~basic_statement()
53
 
{
54
 
        finalize();
55
 
}
56
 
 
57
 
 
58
 
int sqlite::detail::basic_statement::prepare(
59
 
        const std::string &sql )
60
 
{
61
 
        finalize();
62
 
        return sqlite3_prepare_v2( _connection._handle, sql.c_str(),
 
23
#include <sqlitepp/statement.hpp>
 
24
#include <sqlitepp/exception.hpp>
 
25
#include <sqlitepp/database.hpp>
 
26
#include <string.h>
 
27
#include <iomanip>
 
28
 
 
29
 
 
30
const sqlite::_null_t sqlite::null = { };
 
31
 
 
32
const sqlite::_exec_t sqlite::exec = { };
 
33
 
 
34
 
 
35
sqlite::_set_index_t sqlite::set_index(
 
36
        unsigned int index )
 
37
{
 
38
        _set_index_t t = { index };
 
39
        return t;
 
40
}
 
41
 
 
42
 
 
43
sqlite::statement::statement(
 
44
        database &database,
 
45
        const std::string &sql )
 
46
        :
 
47
        _database( database ),
 
48
        _handle( NULL ),
 
49
        _bind_index( 1 )
 
50
{
 
51
        int error_code = prepare( sql );
 
52
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
53
}
 
54
 
 
55
 
 
56
sqlite::statement::~statement() throw( )
 
57
{
 
58
        finalize();
 
59
}
 
60
 
 
61
 
 
62
int sqlite::statement::prepare(
 
63
        const std::string &sql )
 
64
{
 
65
        finalize();
 
66
        return sqlite3_prepare_v2( _database._handle, sql.c_str(),
63
67
                sql.length() + 1, &_handle, NULL );
64
68
}
65
69
 
66
70
 
67
 
int sqlite::detail::basic_statement::reset()
 
71
int sqlite::statement::step()
 
72
{
 
73
        return sqlite3_step( _handle );
 
74
}
 
75
 
 
76
 
 
77
int sqlite::statement::reset()
68
78
{
69
79
        return sqlite3_reset( _handle );
70
80
}
71
81
 
72
82
 
73
 
int sqlite::detail::basic_statement::clear_bindings()
 
83
int sqlite::statement::clear_bindings()
74
84
{
75
85
        _bind_index = 1;
76
86
        return sqlite3_clear_bindings( _handle );
77
87
}
78
88
 
79
89
 
80
 
int sqlite::detail::basic_statement::bind_static(
 
90
int sqlite::statement::bind_static(
81
91
        unsigned int index,
82
92
        const char *value,
83
93
        unsigned int value_length )
84
94
{
85
 
                // bind as text (applying the type affinity of the underlying column)
86
95
        return sqlite3_bind_text( _handle, index, value, value_length,
87
96
                SQLITE_STATIC );
88
97
}
89
98
 
90
99
 
91
 
int sqlite::detail::basic_statement::bind_static(
 
100
int sqlite::statement::bind_static(
92
101
        unsigned int index,
93
102
        const char *value )
94
103
{
96
105
}
97
106
 
98
107
 
99
 
int sqlite::detail::basic_statement::bind_static(
 
108
int sqlite::statement::bind_static(
100
109
        unsigned int index,
101
110
        const std::string &value )
102
111
{
104
113
}
105
114
 
106
115
 
107
 
int sqlite::detail::basic_statement::bind_null(
 
116
int sqlite::statement::bind_null(
108
117
        unsigned int index )
109
118
{
110
119
        return sqlite3_bind_null( _handle, index );
111
120
}
112
121
 
113
122
 
114
 
int sqlite::detail::basic_statement::bind_static(
 
123
int sqlite::statement::bind_static(
115
124
        const std::string &name,
116
125
        const char *value,
117
126
        unsigned int value_length )
118
127
{
119
 
        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 );
120
130
}
121
131
 
122
132
 
123
 
int sqlite::detail::basic_statement::bind_static(
 
133
int sqlite::statement::bind_static(
124
134
        const std::string &name,
125
135
        const char *value )
126
136
{
128
138
}
129
139
 
130
140
 
131
 
int sqlite::detail::basic_statement::bind_static(
 
141
int sqlite::statement::bind_static(
132
142
        const std::string &name,
133
143
        const std::string &value )
134
144
{
136
146
}
137
147
 
138
148
 
139
 
int sqlite::detail::basic_statement::bind_null(
 
149
int sqlite::statement::bind_null(
140
150
        const std::string &name )
141
151
{
142
 
        return bind_null( bind_parameter_index( name ) );
143
 
}
144
 
 
145
 
 
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
 
 
181
 
int sqlite::detail::basic_statement::finalize()
182
 
{
183
 
        int code = SQLITE_OK;
 
152
        unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
 
153
        return bind_null( index );
 
154
}
 
155
 
 
156
 
 
157
int sqlite::statement::finalize()
 
158
{
 
159
        int error_code = SQLITE_OK;
184
160
 
185
161
        if( _handle ) {
186
 
                code = sqlite3_finalize( _handle );
 
162
                error_code = sqlite3_finalize( _handle );
187
163
                _handle = NULL;
188
164
        }
189
165
 
190
 
        return code;
191
 
}
192
 
 
193
 
 
194
 
int sqlite::detail::basic_statement::bind_parameter_index(
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
 
 
203
 
int sqlite::detail::basic_statement::step()
204
 
{
205
 
        return sqlite3_step( _handle );
 
166
        return error_code;
206
167
}
207
168
 
208
169
 
209
170
template< >
210
 
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator <<
211
 
        < sqlite::detail::null_t >(
212
 
        const sqlite::detail::null_t & )
 
171
sqlite::statement& sqlite::statement::operator << < sqlite::_null_t >(
 
172
        sqlite::_null_t )
213
173
{
214
 
        int code = bind_null( _bind_index );
215
 
        if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
 
174
        int error_code = bind_null( _bind_index );
 
175
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
216
176
        _bind_index++;
217
177
        return *this;
218
178
}
219
179
 
 
180
template< >
 
181
sqlite::statement& sqlite::statement::operator << < sqlite::_exec_t >(
 
182
        sqlite::_exec_t )
 
183
{
 
184
        int error_code = step();
 
185
        if( error_code != SQLITE_DONE )
 
186
                if( error_code == SQLITE_ROW )
 
187
                        throw sqlite_error( "statement returned results" );
 
188
                else
 
189
                        throw sqlite_error( error_code );
 
190
        return *this;
 
191
}
220
192
 
221
193
template< >
222
 
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator <<
223
 
        < sqlite::detail::set_index_t >(
224
 
        const sqlite::detail::set_index_t &t )
 
194
sqlite::statement& sqlite::statement::operator << < sqlite::_set_index_t >(
 
195
        sqlite::_set_index_t t )
225
196
{
226
197
        _bind_index = t._index;
227
198
        return *this;
228
199
}
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
 
}