/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:46:42 UTC
  • Revision ID: edam@waxworlds.org-20100727154642-1uxrjkpxhp7xl6hq
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
- added immediate transation

Show diffs side-by-side

added added

removed removed

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