/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: 2010-07-27 15:12:55 UTC
  • Revision ID: edam@waxworlds.org-20100727151255-goaqgdz4kj13q7gz
- update TODO
- added some missing includes for <string>
- changed usage of database::exec() to not require return code!
- prevented transaction_guard destructor from throwing an exception

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
#include <sqlite3cc/basic_statement.h>
24
24
#include <sqlite3cc/exception.h>
25
 
#include <sqlite3cc/connection.h>
 
25
#include <sqlite3cc/database.h>
26
26
#include <sqlite3cc/manipulator.h>
27
27
#include <string.h>
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(),
 
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(),
64
65
                sql.length() + 1, &_handle, NULL );
65
66
}
66
67
 
67
68
 
68
 
int sqlite::detail::basic_statement::reset()
 
69
int sqlite::basic_statement::reset()
69
70
{
70
71
        return sqlite3_reset( _handle );
71
72
}
72
73
 
73
74
 
74
 
int sqlite::detail::basic_statement::clear_bindings()
 
75
int sqlite::basic_statement::clear_bindings()
75
76
{
76
77
        _bind_index = 1;
77
78
        return sqlite3_clear_bindings( _handle );
78
79
}
79
80
 
80
81
 
81
 
int sqlite::detail::basic_statement::bind_static(
 
82
int sqlite::basic_statement::bind_static(
82
83
        unsigned int index,
83
84
        const char *value,
84
85
        unsigned int value_length )
88
89
}
89
90
 
90
91
 
91
 
int sqlite::detail::basic_statement::bind_static(
 
92
int sqlite::basic_statement::bind_static(
92
93
        unsigned int index,
93
94
        const char *value )
94
95
{
96
97
}
97
98
 
98
99
 
99
 
int sqlite::detail::basic_statement::bind_static(
 
100
int sqlite::basic_statement::bind_static(
100
101
        unsigned int index,
101
102
        const std::string &value )
102
103
{
104
105
}
105
106
 
106
107
 
107
 
int sqlite::detail::basic_statement::bind_null(
 
108
int sqlite::basic_statement::bind_null(
108
109
        unsigned int index )
109
110
{
110
111
        return sqlite3_bind_null( _handle, index );
111
112
}
112
113
 
113
114
 
114
 
int sqlite::detail::basic_statement::bind_static(
 
115
int sqlite::basic_statement::bind_static(
115
116
        const std::string &name,
116
117
        const char *value,
117
118
        unsigned int value_length )
120
121
}
121
122
 
122
123
 
123
 
int sqlite::detail::basic_statement::bind_static(
 
124
int sqlite::basic_statement::bind_static(
124
125
        const std::string &name,
125
126
        const char *value )
126
127
{
128
129
}
129
130
 
130
131
 
131
 
int sqlite::detail::basic_statement::bind_static(
 
132
int sqlite::basic_statement::bind_static(
132
133
        const std::string &name,
133
134
        const std::string &value )
134
135
{
136
137
}
137
138
 
138
139
 
139
 
int sqlite::detail::basic_statement::bind_null(
 
140
int sqlite::basic_statement::bind_null(
140
141
        const std::string &name )
141
142
{
142
143
        return bind_null( bind_parameter_index( name ) );
143
144
}
144
145
 
145
146
 
146
 
int sqlite::detail::basic_statement::finalize()
 
147
int sqlite::basic_statement::finalize()
147
148
{
148
 
        int code = SQLITE_OK;
 
149
        int error_code = SQLITE_OK;
149
150
 
150
151
        if( _handle ) {
151
 
                code = sqlite3_finalize( _handle );
 
152
                error_code = sqlite3_finalize( _handle );
152
153
                _handle = NULL;
153
154
        }
154
155
 
155
 
        return code;
156
 
}
157
 
 
158
 
 
159
 
int sqlite::detail::basic_statement::bind_parameter_index(
 
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(
160
167
        const std::string &name )
161
168
{
162
169
        unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
165
172
}
166
173
 
167
174
 
168
 
int sqlite::detail::basic_statement::step()
169
 
{
170
 
        return sqlite3_step( _handle );
171
 
}
172
 
 
173
 
 
174
 
 
175
175
template< >
176
 
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator <<
177
 
        < sqlite::detail::null_t >(
178
 
        const sqlite::detail::null_t & )
 
176
sqlite::basic_statement &sqlite::basic_statement::operator <<
 
177
        < sqlite::_null_t >(
 
178
        const sqlite::_null_t & )
179
179
{
180
 
        int code = bind_null( _bind_index );
181
 
        if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
 
180
        int error_code = bind_null( _bind_index );
 
181
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
182
182
        _bind_index++;
183
183
        return *this;
184
184
}
185
185
 
186
186
 
187
187
template< >
188
 
sqlite::detail::basic_statement &sqlite::detail::basic_statement::operator <<
189
 
        < sqlite::detail::set_index_t >(
190
 
        const sqlite::detail::set_index_t &t )
 
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 )
191
207
{
192
208
        _bind_index = t._index;
193
209
        return *this;