4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
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.
 
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.
 
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.
 
 
4
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
 
 
6
 * This file is part of sqlite3cc (hereafter referred to as "this program").
 
 
7
 * See http://ed.am/dev/sqlite3cc for more information.
 
 
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
 
 
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
 
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/>.
 
23
 
#ifndef STATEMENT_HPP_
 
24
 
#define STATEMENT_HPP_
 
 
23
#ifndef SQLITE3CC_BASIC_STATEMENT_H_
 
 
24
#define SQLITE3CC_BASIC_STATEMENT_H_
 
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>
 
42
 
struct _set_index_t { unsigned int _index; };
 
45
 
 * Auto-binding manipulator, for use with a statment's stream operator. This
 
46
 
 * specifies a NULL value to bind to a parameter.
 
48
 
extern const _null_t null;
 
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.
 
56
 
extern const _exec_t exec;
 
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
 
63
 
_set_index_t set_index(
 
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.
 
74
 
        private boost::noncopyable
 
 
51
 * The statement class represents an SQL statement.  It is the base class for
 
 
52
 * both the command and the query classes, which should be used for those
 
 
53
 * purposes.  The basic_statement class its self has protected instantiation.
 
76
57
//______________________________________________________________________________
 
81
 
         * Constructor that provides a database upon which to act and the SQL
 
 
62
         * Constructor that provides a connection upon which to act and the SQL
 
83
 
         * @param database a reference to the database
 
 
65
         * @param connection a reference to a connection
 
84
66
         * @param sql an SQL statement in UTF-8
 
 
68
        explicit basic_statement(
 
 
69
                connection &connection,
 
88
70
                const std::string &sql );
 
90
 
        virtual ~statement() throw( );
 
 
73
         * Constructor that provides a connection upon which to act.
 
 
75
         * @param connection a reference to a connection
 
 
77
        explicit basic_statement(
 
 
78
                connection &connection );
 
 
80
        virtual ~basic_statement();
 
92
82
//______________________________________________________________________________
 
93
83
//                                                              public interface
 
 
97
87
         * Prepare an SQL statement.
 
98
89
         * @param sql an SQL statement in UTF-8
 
99
90
         * @returns an sqlite error code
 
100
91
         * @see sqlite3_prepare_v2()
 
103
94
                const std::string &sql );
 
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()
 
115
 
         * Reset the statement, ready to re-execute it. This does not clear any of
 
 
97
         * Reset the statement, ready to re-execute it.  This does not clear any of
 
116
98
         * the values bound to the statement.
 
117
100
         * @returns an sqlite error code
 
118
101
         * @see sqlite3_reset()
 
123
106
         * Clears the values bound to a statement to NULL.
 
124
108
         * @returns an sqlite error code
 
125
109
         * @see sqlite3_clear_bindings()
 
127
111
        int clear_bindings();
 
130
 
         * Bind a value to the SQL statement via it's index. This template will take
 
131
 
         * a variety of data types and bind them as text. This is how sqlite
 
 
114
         * Bind a value to the SQL statement via it's index.  This template will
 
 
115
         * take a variety of data types and bind them as text.  This is how sqlite
 
132
116
         * internally stores the data anyway, so always binding as text just means
 
133
117
         * we do the conversion instead of sqlite and is no less efficient.
 
134
119
         * @param index the index of the parameter to bind to
 
135
120
         * @param value the value to bind
 
136
121
         * @returns an sqlite error code
 
 
213
204
                const std::string &name,
 
217
 
                        sqlite3_bind_parameter_index( _handle, name.c_str() );
 
218
 
                return bind( index, value );
 
 
207
                return bind( bind_parameter_index( name ), value );
 
222
211
         * Bind a string value to the SQL statement via a named parameter where the
 
223
 
         * string value will not change for the duration of the statement. This
 
224
 
         * prevents a copy of the string being taken.
 
 
212
         * string value will not change for the duration of the statement.  This
 
 
213
         * prevents sqlite from taking its own copy of the string.
 
225
215
         * @param name the named parameter to bind to
 
226
216
         * @param value the invariant string value
 
 
217
         * @param value_length the length of the string including zero-terminator
 
227
218
         * @returns an sqlite error code
 
228
219
         * @see sqlite3_bind_text()
 
 
271
265
         * Stream operator is used to bind values to parameters automatically, in
 
272
 
         * ascending order. In addition, the null, execute and set_index() auto-
 
273
 
         * binding manipulators can be used.
 
 
266
         * ascending order.  In addition, the null and set_index() auto-binding
 
 
267
         * manipulators can be used.
 
274
269
         * @param value a value to bind
 
276
271
        template< class T >
 
277
 
        statement &operator <<( T value )
 
 
272
        basic_statement &operator <<(
 
279
 
                int error_code = bind( _bind_index, value );
 
280
 
                if( error_code != SQLITE_OK ) throw sqlite_error( error_code );
 
 
275
                int code = bind( _bind_index, value );
 
 
276
                if( code != SQLITE_OK ) throw sqlite_error( _connection, code );
 
 
310
 
// template specialisations for statement::operator <<()
 
312
 
statement& statement::operator << < _null_t >(
 
315
 
statement& statement::operator << < _exec_t >(
 
318
 
statement& statement::operator << < _set_index_t >(
 
325
 
#endif /* STATEMENT_HPP_ */
 
 
324
// template specialisations for basic_statement::operator <<()
 
 
326
basic_statement &basic_statement::operator << < detail::null_t >(
 
 
327
        const detail::null_t & );
 
 
329
basic_statement &basic_statement::operator << < detail::set_index_t >(
 
 
330
        const detail::set_index_t &t );
 
 
333
} // namespace detail
 
 
336
} // namespace sqlite
 
 
339
#endif /* SQLITE3CC_BASIC_STATEMENT_H_ */