/sqlite3cc

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

« back to all changes in this revision

Viewing changes to include/sqlite3cc/basic_statement.h

  • Committer: edam
  • Date: 2010-01-28 12:21:03 UTC
  • Revision ID: edam@waxworlds.org-20100128122103-v3gs6hro5h7ffnhb
- further initial development
- basic support for: queries, rows (query results)
- filename, comment header and project name consistency

Show diffs side-by-side

added added

removed removed

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