/sqlite3cc

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

« back to all changes in this revision

Viewing changes to include/sqlitepp/statement.hpp

  • 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.h
 
2
 * statement.hpp
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
 
#ifndef SQLITE3CC_BASIC_STATEMENT_H_
24
 
#define SQLITE3CC_BASIC_STATEMENT_H_
 
23
#ifndef STATEMENT_HPP_
 
24
#define STATEMENT_HPP_
25
25
 
26
26
 
27
27
#include <sqlite3.h>
28
28
#include <boost/utility.hpp>
29
29
#include <boost/lexical_cast.hpp>
30
 
#include <sqlite3cc/exception.h>
 
30
#include <sqlitepp/exception.hpp>
31
31
 
32
32
 
33
33
namespace sqlite
35
35
 
36
36
 
37
37
class database;
38
 
class row;
39
 
namespace detail {
40
 
        struct null_t;
41
 
        struct exec_t;
42
 
        struct set_index_t;
43
 
}
44
 
 
45
 
 
46
 
/**
47
 
 * The statement class represents an SQL statement. It is the base class for
48
 
 * both the command and the query classes, which should be used for those
49
 
 * purposes. The basic_statement class its self has protected instantiation.
50
 
 */
51
 
class basic_statement
 
38
 
 
39
 
 
40
struct _null_t { };
 
41
struct _exec_t { };
 
42
struct _set_index_t { unsigned int _index; };
 
43
 
 
44
/**
 
45
 * Auto-binding manipulator, for use with a statment's stream operator. This
 
46
 * specifies a NULL value to bind to a parameter.
 
47
 */
 
48
extern const _null_t null;
 
49
 
 
50
/**
 
51
 * Auto-binding manipulator, for use with a statment's stream operator. This
 
52
 * indicates that the statement should be executed immediately. Unlike a
 
53
 * statment's exec() method, this will throw on error. Also, it will throw if
 
54
 * the execution returns any result rows.
 
55
 */
 
56
extern const _exec_t exec;
 
57
 
 
58
/**
 
59
 * Auto-binding manipulator, for use with a statment's stream operator. This
 
60
 * manipulator sets the index used to automatically assign values to parameters.
 
61
 * @param index the new index for incremental assignment
 
62
 */
 
63
_set_index_t set_index(
 
64
        unsigned int index );
 
65
 
 
66
 
 
67
/**
 
68
 * The statement class represents an SQL statement. It is the bas class for both
 
69
 * the command and the query classes which should be used for those purposes.
 
70
 * The statement class its self has protected instantiation.
 
71
 */
 
72
class statement
52
73
        :
53
74
        private boost::noncopyable
54
75
{
59
80
        /**
60
81
         * Constructor that provides a database upon which to act and the SQL
61
82
         * statement.
62
 
         * @param database a reference to a database
 
83
         * @param database a reference to the database
63
84
         * @param sql an SQL statement in UTF-8
64
85
         */
65
 
        explicit basic_statement(
 
86
        statement(
66
87
                database &database,
67
88
                const std::string &sql );
68
89
 
69
 
        /**
70
 
         * Constructor that provides a database upon which to act.
71
 
         * @param database a reference to a database
72
 
         * @param sql an SQL statement in UTF-8
73
 
         */
74
 
        explicit basic_statement(
75
 
                database &database );
76
 
 
77
 
        virtual ~basic_statement();
 
90
        virtual ~statement() throw( );
78
91
 
79
92
//______________________________________________________________________________
80
93
//                                                              public interface
86
99
         * @returns an sqlite error code
87
100
         * @see sqlite3_prepare_v2()
88
101
         */
89
 
        virtual int prepare(
 
102
        int prepare(
90
103
                const std::string &sql );
91
104
 
92
105
        /**
 
106
         * Step through one execution cycle of the SQL statement. If this is an SQL
 
107
         * statement that doesn't return any rows, only one cycle is required,
 
108
         * otherwise, each cycle will return another row
 
109
         * @return an sqlite error code
 
110
         * @see sqlite3_step()
 
111
         */
 
112
        int step();
 
113
 
 
114
        /**
93
115
         * Reset the statement, ready to re-execute it. This does not clear any of
94
116
         * the values bound to the statement.
95
117
         * @returns an sqlite error code
191
213
                const std::string &name,
192
214
                T value )
193
215
        {
194
 
                return bind( bind_parameter_index( name ), value );
 
216
                unsigned int index =
 
217
                        sqlite3_bind_parameter_index( _handle, name.c_str() );
 
218
                return bind( index, value );
195
219
        }
196
220
 
197
221
        /**
245
269
 
246
270
        /**
247
271
         * Stream operator is used to bind values to parameters automatically, in
248
 
         * ascending order. In addition, the null, set_index() and execute auto-
 
272
         * ascending order. In addition, the null, execute and set_index() auto-
249
273
         * binding manipulators can be used.
250
274
         * @param value a value to bind
251
275
         */
252
276
        template< class T >
253
 
        basic_statement &operator <<(
254
 
                const T &value )
 
277
        statement &operator <<( T value )
255
278
        {
256
279
                int error_code = bind( _bind_index, value );
257
280
                if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
263
286
//                                                                implementation
264
287
protected:
265
288
 
266
 
        friend class row;
267
 
 
268
289
        /**
269
290
         * Finalise an SQL statement.
270
291
         * @returns an sqlite error code
272
293
         */
273
294
        int finalize();
274
295
 
275
 
        /**
276
 
         * Step through one execution cycle of the SQL statement. If this is an SQL
277
 
         * statement that doesn't return any rows, only one cycle is required,
278
 
         * otherwise, each cycle will return another row
279
 
         * @return an sqlite error code
280
 
         * @see sqlite3_step()
281
 
         */
282
 
        int step();
283
 
 
284
 
        /**
285
 
         * Get the index number of a named parameter
286
 
         * @param parameter name
287
 
         * @return index of named parameter
288
 
         */
289
 
        int bind_parameter_index(
290
 
                const std::string &name );
 
296
private:
291
297
 
292
298
        /** the database upon which to act */
293
299
        database &_database;
295
301
        /** the statement handle */
296
302
        sqlite3_stmt *_handle;
297
303
 
298
 
private:
299
 
 
300
304
        /** index used when auto-binding */
301
305
        unsigned int _bind_index;
302
306
 
305
309
 
306
310
// template specialisations for statement::operator <<()
307
311
template< >
308
 
basic_statement &basic_statement::operator << < detail::null_t >(
309
 
        const detail::null_t & );
310
 
template< >
311
 
basic_statement &basic_statement::operator << < detail::exec_t >(
312
 
        const detail::exec_t & );
313
 
template< >
314
 
basic_statement &basic_statement::operator << < detail::set_index_t >(
315
 
        const detail::set_index_t &t );
316
 
 
317
 
 
318
 
} // namespace sqlite
319
 
 
320
 
 
321
 
#endif /* SQLITE3CC_BASIC_STATEMENT_H_ */
 
312
statement& statement::operator << < _null_t >(
 
313
        _null_t );
 
314
template< >
 
315
statement& statement::operator << < _exec_t >(
 
316
        _exec_t );
 
317
template< >
 
318
statement& statement::operator << < _set_index_t >(
 
319
        _set_index_t t );
 
320
 
 
321
 
 
322
}
 
323
 
 
324
 
 
325
#endif /* STATEMENT_HPP_ */