/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-29 06:28:53 UTC
  • Revision ID: edam@waxworlds.org-20100729062853-4i2fec52m86mh724
- made basic_statement::step() protected, for use by query and command only
- moved basic_statement::operator<<() to command and query instead; one needs to accept sqlite::exec, the other doesn't
- added tests for query::operator<<()
- added code to invlaidate in-progress queries during any transaction rollbacks (currently segfaults in basic_statement::finalize())
- added new sqlite_error constructor that obtains a full error message
- implemented database::database_mutex_guard class
- swapped command's step mutex in favour of the database mutex

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * statement.cpp
 
2
 * basic_statement.cc
3
3
 *
4
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
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.
 
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
8
 *
9
9
 * This program is free software: you can redistribute it and/or modify
10
10
 * it under the terms of the GNU Lesser General Public License as published
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/database.h>
26
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(
 
27
 
 
28
 
 
29
sqlite::basic_statement::basic_statement(
44
30
        database &database,
45
31
        const std::string &sql )
46
32
        :
48
34
        _handle( NULL ),
49
35
        _bind_index( 1 )
50
36
{
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( )
 
37
        int code = prepare( sql );
 
38
        if( code != SQLITE_OK ) throw sqlite_error( database, code );
 
39
}
 
40
 
 
41
 
 
42
sqlite::basic_statement::basic_statement(
 
43
        database &database )
 
44
        :
 
45
        _database( database ),
 
46
        _handle( NULL ),
 
47
        _bind_index( 1 )
 
48
{
 
49
}
 
50
 
 
51
 
 
52
sqlite::basic_statement::~basic_statement()
57
53
{
58
54
        finalize();
59
55
}
60
56
 
61
57
 
62
 
int sqlite::statement::prepare(
 
58
int sqlite::basic_statement::prepare(
63
59
        const std::string &sql )
64
60
{
65
61
        finalize();
68
64
}
69
65
 
70
66
 
71
 
int sqlite::statement::step()
72
 
{
73
 
        return sqlite3_step( _handle );
74
 
}
75
 
 
76
 
 
77
 
int sqlite::statement::reset()
 
67
int sqlite::basic_statement::reset()
78
68
{
79
69
        return sqlite3_reset( _handle );
80
70
}
81
71
 
82
72
 
83
 
int sqlite::statement::clear_bindings()
 
73
int sqlite::basic_statement::clear_bindings()
84
74
{
85
75
        _bind_index = 1;
86
76
        return sqlite3_clear_bindings( _handle );
87
77
}
88
78
 
89
79
 
90
 
int sqlite::statement::bind_static(
 
80
int sqlite::basic_statement::bind_static(
91
81
        unsigned int index,
92
82
        const char *value,
93
83
        unsigned int value_length )
97
87
}
98
88
 
99
89
 
100
 
int sqlite::statement::bind_static(
 
90
int sqlite::basic_statement::bind_static(
101
91
        unsigned int index,
102
92
        const char *value )
103
93
{
105
95
}
106
96
 
107
97
 
108
 
int sqlite::statement::bind_static(
 
98
int sqlite::basic_statement::bind_static(
109
99
        unsigned int index,
110
100
        const std::string &value )
111
101
{
113
103
}
114
104
 
115
105
 
116
 
int sqlite::statement::bind_null(
 
106
int sqlite::basic_statement::bind_null(
117
107
        unsigned int index )
118
108
{
119
109
        return sqlite3_bind_null( _handle, index );
120
110
}
121
111
 
122
112
 
123
 
int sqlite::statement::bind_static(
 
113
int sqlite::basic_statement::bind_static(
124
114
        const std::string &name,
125
115
        const char *value,
126
116
        unsigned int value_length )
127
117
{
128
 
        unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
129
 
        return bind_static( index, value, value_length );
 
118
        return bind_static( bind_parameter_index( name ), value, value_length );
130
119
}
131
120
 
132
121
 
133
 
int sqlite::statement::bind_static(
 
122
int sqlite::basic_statement::bind_static(
134
123
        const std::string &name,
135
124
        const char *value )
136
125
{
138
127
}
139
128
 
140
129
 
141
 
int sqlite::statement::bind_static(
 
130
int sqlite::basic_statement::bind_static(
142
131
        const std::string &name,
143
132
        const std::string &value )
144
133
{
146
135
}
147
136
 
148
137
 
149
 
int sqlite::statement::bind_null(
 
138
int sqlite::basic_statement::bind_null(
150
139
        const std::string &name )
151
140
{
152
 
        unsigned int index = sqlite3_bind_parameter_index( _handle, name.c_str() );
153
 
        return bind_null( index );
 
141
        return bind_null( bind_parameter_index( name ) );
154
142
}
155
143
 
156
144
 
157
 
int sqlite::statement::finalize()
 
145
int sqlite::basic_statement::finalize()
158
146
{
159
 
        int error_code = SQLITE_OK;
 
147
        int code = SQLITE_OK;
160
148
 
161
149
        if( _handle ) {
162
 
                error_code = sqlite3_finalize( _handle );
 
150
                code = sqlite3_finalize( _handle );
163
151
                _handle = NULL;
164
152
        }
165
153
 
166
 
        return error_code;
167
 
}
168
 
 
169
 
 
170
 
template< >
171
 
sqlite::statement& sqlite::statement::operator << < sqlite::_null_t >(
172
 
        sqlite::_null_t )
173
 
{
174
 
        int error_code = bind_null( _bind_index );
175
 
        if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
176
 
        _bind_index++;
177
 
        return *this;
178
 
}
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
 
}
192
 
 
193
 
template< >
194
 
sqlite::statement& sqlite::statement::operator << < sqlite::_set_index_t >(
195
 
        sqlite::_set_index_t t )
196
 
{
197
 
        _bind_index = t._index;
198
 
        return *this;
 
154
        return code;
 
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
 
 
167
int sqlite::basic_statement::step()
 
168
{
 
169
        return sqlite3_step( _handle );
199
170
}