/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
	:
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
42
	public detail::basic_statement
1 by edam
- initial commit
43
{
44
//______________________________________________________________________________
45
//                                                                 instantiation
46
public:
47
48
	/**
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
49
	 * Constructor that provides a connection upon which to act and the SQL
2 by edam
- further initial development
50
	 * command to execute.
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
51
	 * @param connection a reference to a connection
1 by edam
- initial commit
52
	 * @param sql an SQL statement in UTF-8
53
	 */
2 by edam
- further initial development
54
	explicit command(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
55
		connection &connection,
2 by edam
- further initial development
56
		const std::string &sql );
57
58
	/**
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
59
	 * Constructor that provides a connection upon which to act.
60
	 * @param connection a reference to a connection
2 by edam
- further initial development
61
	 */
62
	explicit command(
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
63
		connection &connection );
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
129
//______________________________________________________________________________
130
//                                                                implementation
131
private:
132
133
	/** non-recursive number of changes made by the last execution */
134
	int _changes;
135
136
	/** recursive number of changes made by the last execution */
137
	int _total_changes;
138
139
	/** the rowid of the last successful insert command */
140
	long long _last_insert_rowid;
141
1 by edam
- initial commit
142
};
143
144
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
145
namespace detail
146
{
147
148
149
// template specialisations for detail::basic_statement::operator <<()
14 by edam
- moved basic_statement::operator <<() back to basic_statement and just create another specialisation in command so that it can use sqlite::exec
150
template< >
151
basic_statement &basic_statement::operator << < detail::exec_t >(
13 by edam
- made basic_statement::step() protected, for use by query and command only
152
	const detail::exec_t & );
153
154
16 by edam
- renamed database to connection to better identify what it is (would database_connection be better though?)
155
} // namespace detail
156
157
2 by edam
- further initial development
158
} // namespace sqlite
159
160
161
#endif /* SQLITE3CC_COMMAND_H_ */