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