/sqlite3cc

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

« back to all changes in this revision

Viewing changes to src/basic_statement.cc

  • Committer: edam
  • Date: 2012-04-23 18:51:11 UTC
  • Revision ID: tim@ed.am-20120423185111-0i7uc3yu6u5nhgkm
changed version to 0.2dev

Show diffs side-by-side

added added

removed removed

1
1
/*
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.
 
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.
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 <sqlitepp/statement.hpp>
24
 
#include <sqlitepp/exception.hpp>
25
 
#include <sqlitepp/database.hpp>
 
23
#include <sqlite3cc/basic_statement.h>
 
24
#include <sqlite3cc/exception.h>
 
25
#include <sqlite3cc/connection.h>
 
26
#include <sqlite3cc/manipulator.h>
26
27
#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(),
 
28
 
 
29
 
 
30
sqlite::detail::basic_statement::basic_statement(
 
31
        connection &connection,
 
32
        const std::string &sql )
 
33
        :
 
34
        _connection( connection ),
 
35
        _handle( NULL ),
 
36
        _bind_index( 1 )
 
37
{
 
38
        int code = prepare( sql );
 
39
        if( code != SQLITE_OK ) throw sqlite_error( connection, code );
 
40
}
 
41
 
 
42
 
 
43
sqlite::detail::basic_statement::basic_statement(
 
44
        connection &connection )
 
45
        :
 
46
        _connection( connection ),
 
47
        _handle( NULL ),
 
48
        _bind_index( 1 )
 
49
{
 
50
}
 
51
 
 
52
 
 
53
sqlite::detail::basic_statement::~basic_statement()
 
54
{
 
55
        finalize();
 
56
}
 
57
 
 
58
 
 
59
int sqlite::detail::basic_statement::prepare(
 
60
        const std::string &sql )
 
61
{
 
62
        finalize();
 
63
        return sqlite3_prepare_v2( _connection._handle, sql.c_str(),
67
64
                sql.length() + 1, &_handle, NULL );
68
65
}
69
66
 
70
67
 
71
 
int sqlite::statement::step()
72
 
{
73
 
        return sqlite3_step( _handle );
74
 
}
75
 
 
76
 
 
77
 
int sqlite::statement::reset()
 
68
int sqlite::detail::basic_statement::reset()
78
69
{
79
70
        return sqlite3_reset( _handle );
80
71
}
81
72
 
82
73
 
83
 
int sqlite::statement::clear_bindings()
 
74
int sqlite::detail::basic_statement::clear_bindings()
84
75
{
85
76
        _bind_index = 1;
86
77
        return sqlite3_clear_bindings( _handle );
87
78
}
88
79
 
89
80
 
90
 
int sqlite::statement::bind_static(
 
81
int sqlite::detail::basic_statement::bind_static(
91
82
        unsigned int index,
92
83
        const char *value,
93
84
        unsigned int value_length )
97
88
}
98
89
 
99
90
 
100
 
int sqlite::statement::bind_static(
 
91
int sqlite::detail::basic_statement::bind_static(
101
92
        unsigned int index,
102
93
        const char *value )
103
94
{
105
96
}
106
97
 
107
98
 
108
 
int sqlite::statement::bind_static(
 
99
int sqlite::detail::basic_statement::bind_static(
109
100
        unsigned int index,
110
101
        const std::string &value )
111
102
{
113
104
}
114
105
 
115
106
 
116
 
int sqlite::statement::bind_null(
 
107
int sqlite::detail::basic_statement::bind_null(
117
108
        unsigned int index )
118
109
{
119
110
        return sqlite3_bind_null( _handle, index );
120
111
}
121
112
 
122
113
 
123
 
int sqlite::statement::bind_static(
 
114
int sqlite::detail::basic_statement::bind_static(
124
115
        const std::string &name,
125
116
        const char *value,
126
117
        unsigned int value_length )
127
118
{
128
 
        unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
129
 
        return bind_static( index, value, value_length );
 
119
        return bind_static( bind_parameter_index( name ), value, value_length );
130
120
}
131
121
 
132
122
 
133
 
int sqlite::statement::bind_static(
 
123
int sqlite::detail::basic_statement::bind_static(
134
124
        const std::string &name,
135
125
        const char *value )
136
126
{
138
128
}
139
129
 
140
130
 
141
 
int sqlite::statement::bind_static(
 
131
int sqlite::detail::basic_statement::bind_static(
142
132
        const std::string &name,
143
133
        const std::string &value )
144
134
{
146
136
}
147
137
 
148
138
 
149
 
int sqlite::statement::bind_null(
 
139
int sqlite::detail::basic_statement::bind_null(
150
140
        const std::string &name )
151
141
{
152
 
        unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
153
 
        return bind_null( index );
 
142
        return bind_null( bind_parameter_index( name ) );
154
143
}
155
144
 
156
145
 
157
 
int sqlite::statement::finalize()
 
146
int sqlite::detail::basic_statement::finalize()
158
147
{
159
 
        int error_code = SQLITE_OK;
 
148
        int code = SQLITE_OK;
160
149
 
161
150
        if( _handle ) {
162
 
                error_code = sqlite3_finalize( _handle );
 
151
                code = sqlite3_finalize( _handle );
163
152
                _handle = NULL;
164
153
        }
165
154
 
166
 
        return error_code;
167
 
}
 
155
        return code;
 
156
}
 
157
 
 
158
 
 
159
int sqlite::detail::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
 
 
168
int sqlite::detail::basic_statement::step()
 
169
{
 
170
        return sqlite3_step( _handle );
 
171
}
 
172
 
168
173
 
169
174
 
170
175
template< >
171
 
sqlite::statement& sqlite::statement::operator << < sqlite::_null_t >(
172
 
        sqlite::_null_t )
 
176
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator <<
 
177
        < sqlite::detail::null_t >(
 
178
        const sqlite::detail::null_t & )
173
179
{
174
 
        int error_code = bind_null( _bind_index );
175
 
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
180
        int code = bind_null( _bind_index );
 
181
        if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
176
182
        _bind_index++;
177
183
        return *this;
178
184
}
179
185
 
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
 
}
192
186
 
193
187
template< >
194
 
sqlite::statement& sqlite::statement::operator << < sqlite::_set_index_t >(
195
 
        sqlite::_set_index_t t )
 
188
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator <<
 
189
        < sqlite::detail::set_index_t >(
 
190
        const sqlite::detail::set_index_t &t )
196
191
{
197
192
        _bind_index = t._index;
198
193
        return *this;