/sqlite3cc

To get this branch, use:
bzr branch http://bzr.ed.am/sqlite3cc
1 by edam
- initial commit
1
/*
2 by edam
- further initial development
2
 * command.h
1 by edam
- initial commit
3
 *
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
 *
2 by edam
- further initial development
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.
1 by edam
- initial commit
8
 *
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.
13
 *
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.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
2 by edam
- further initial development
23
#ifndef SQLITE3CC_COMMAND_H_
24
#define SQLITE3CC_COMMAND_H_
25
26
27
#include <sqlite3cc/basic_statement.h>
1 by edam
- initial commit
28
29
30
namespace sqlite
31
{
32
33
34
/**
35
 * The command class represents an SQL command. Since there is very little
36
 * difference between a command an a statement, it is basically a shim around
37
 * the statement class with the addition of an exec() method to execute the
38
 * command.
39
 */
40
class command
41
	:
2 by edam
- further initial development
42
	public basic_statement
1 by edam
- initial commit
43
{
44
//______________________________________________________________________________
45
//                                                                 instantiation
46
public:
47
48
	/**
2 by edam
- further initial development
49
	 * Constructor that provides a database upon which to act and the SQL
50
	 * command to execute.
51
	 * @param database a reference to a database
1 by edam
- initial commit
52
	 * @param sql an SQL statement in UTF-8
53
	 */
2 by edam
- further initial development
54
	explicit command(
1 by edam
- initial commit
55
		database &database,
2 by edam
- further initial development
56
		const std::string &sql );
57
58
	/**
59
	 * Constructor that provides a database upon which to act.
60
	 * @param database a reference to a database
61
	 */
62
	explicit command(
63
		database &database );
1 by edam
- initial commit
64
65
//______________________________________________________________________________
66
//                                                              public interface
67
public:
68
69
	/**
2 by edam
- further initial development
70
	 * Prepare an SQL statement.
71
	 * @param sql an SQL statement in UTF-8
72
	 * @returns an sqlite error code
73
	 * @see sqlite3_prepare_v2()
74
	 */
75
	virtual int prepare(
76
		const std::string &sql );
77
78
	/**
79
	 * Step through one execution cycle of the SQL statement. If this is an SQL
80
	 * statement that doesn't return any rows, only one cycle is required,
81
	 * otherwise, each cycle will return another row
82
	 * @return an sqlite error code
83
	 * @see sqlite3_step()
84
	 */
13 by edam
- made basic_statement::step() protected, for use by query and command only
85
	int step();
2 by edam
- further initial development
86
87
	/**
1 by edam
- initial commit
88
	 * Execute the command. This is the same as doing a step().
2 by edam
- further initial development
89
	 * @return an sqlite error code
1 by edam
- initial commit
90
	 * @see sqlite3_step()
91
	 */
92
	inline int exec()
93
	{
94
		return step();
95
	}
96
2 by edam
- further initial development
97
	/**
98
	 * Get the number of changes made by the last successful execution of this
99
	 * command. This doesn't include changes made in trigger subcontexts.
100
	 * @return the number of changed rows
101
	 * @see sqlite3_changes()
102
	 */
103
	inline int changes()
104
	{
105
		return _changes;
106
	}
107
108
	/**
109
	 * Get the number of changes made by the last successful execution of this
110
	 * command. This includes changes made in all trigger subcontexts.
111
	 * @return the number of changed rows
112
	 * @see sqlite3_total_changes()
113
	 */
114
	inline int total_changes()
115
	{
116
		return _total_changes;
117
	}
118
119
	/**
120
	 * Get the rowid of the last row inserted via a successful INSERT command
121
	 * (regardless of whether that is this command or not).
122
	 * @return the last inserted rowid
123
	 */
124
	inline long long last_insert_rowid()
125
	{
126
		return _last_insert_rowid;
127
	}
128
13 by edam
- made basic_statement::step() protected, for use by query and command only
129
	/**
130
	 * Stream operator is used to bind values to parameters automatically, in
131
	 * ascending order. In addition, the null, set_index() and execute auto-
132
	 * binding manipulators can be used.
133
	 * @param value a value to bind
134
	 */
135
	template< class T >
136
	command &operator <<(
137
		const T &value )
138
	{
139
		int code = bind( _bind_index, value );
140
		if( code != SQLITE_OK ) throw sqlite_error( _database, code );
141
		_bind_index++;
142
		return *this;
143
	}
144
2 by edam
- further initial development
145
//______________________________________________________________________________
146
//                                                                implementation
147
private:
148
149
	/** non-recursive number of changes made by the last execution */
150
	int _changes;
151
152
	/** recursive number of changes made by the last execution */
153
	int _total_changes;
154
155
	/** the rowid of the last successful insert command */
156
	long long _last_insert_rowid;
157
1 by edam
- initial commit
158
};
159
160
13 by edam
- made basic_statement::step() protected, for use by query and command only
161
// template specialisations for command::operator <<()
162
template< >
163
command &command::operator << < detail::null_t >(
164
	const detail::null_t & );
165
template< >
166
command &command::operator << < detail::exec_t >(
167
	const detail::exec_t & );
168
template< >
169
command &command::operator << < detail::set_index_t >(
170
	const detail::set_index_t &t );
171
172
2 by edam
- further initial development
173
} // namespace sqlite
174
175
176
#endif /* SQLITE3CC_COMMAND_H_ */