/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: Tim Marston
  • Date: 2014-01-03 12:50:54 UTC
  • Revision ID: tim@ed.am-20140103125054-lr8gbdeej98pcs5o
removed emake makefiles from project

Show diffs side-by-side

added added

removed removed

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