/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc

« back to all changes in this revision

Viewing changes to src/statement.cpp

  • Committer: edam
  • Date: 2009-11-25 20:32:34 UTC
  • Revision ID: edam@waxworlds.org-20091125203234-24i7mzd60c7o46py
- initial commit
- basic support for: database, statements, commands, transactions, exceptions
- initial test app
- initial liscence and readme

Show diffs side-by-side

added added

removed removed

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