/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>
28
#include <boost/thread/recursive_mutex.hpp>
1 by edam
- initial commit
29
30
31
namespace sqlite
32
{
33
34
35
/**
36
 * The command class represents an SQL command. Since there is very little
37
 * difference between a command an a statement, it is basically a shim around
38
 * the statement class with the addition of an exec() method to execute the
39
 * command.
40
 */
41
class command
42
	:
2 by edam
- further initial development
43
	public basic_statement
1 by edam
- initial commit
44
{
45
//______________________________________________________________________________
46
//                                                                 instantiation
47
public:
48
49
	/**
2 by edam
- further initial development
50
	 * Constructor that provides a database upon which to act and the SQL
51
	 * command to execute.
52
	 * @param database a reference to a database
1 by edam
- initial commit
53
	 * @param sql an SQL statement in UTF-8
54
	 */
2 by edam
- further initial development
55
	explicit command(
1 by edam
- initial commit
56
		database &database,
2 by edam
- further initial development
57
		const std::string &sql );
58
59
	/**
60
	 * Constructor that provides a database upon which to act.
61
	 * @param database a reference to a database
62
	 */
63
	explicit command(
64
		database &database );
1 by edam
- initial commit
65
66
//______________________________________________________________________________
67
//                                                              public interface
68
public:
69
70
	/**
2 by edam
- further initial development
71
	 * Prepare an SQL statement.
72
	 * @param sql an SQL statement in UTF-8
73
	 * @returns an sqlite error code
74
	 * @see sqlite3_prepare_v2()
75
	 */
76
	virtual int prepare(
77
		const std::string &sql );
78
79
	/**
80
	 * Step through one execution cycle of the SQL statement. If this is an SQL
81
	 * statement that doesn't return any rows, only one cycle is required,
82
	 * otherwise, each cycle will return another row
83
	 * @return an sqlite error code
84
	 * @see sqlite3_step()
85
	 */
86
	virtual int step();
87
88
	/**
1 by edam
- initial commit
89
	 * Execute the command. This is the same as doing a step().
2 by edam
- further initial development
90
	 * @return an sqlite error code
1 by edam
- initial commit
91
	 * @see sqlite3_step()
92
	 */
93
	inline int exec()
94
	{
95
		return step();
96
	}
97
2 by edam
- further initial development
98
	/**
99
	 * Get the number of changes made by the last successful execution of this
100
	 * command. This doesn't include changes made in trigger subcontexts.
101
	 * @return the number of changed rows
102
	 * @see sqlite3_changes()
103
	 */
104
	inline int changes()
105
	{
106
		return _changes;
107
	}
108
109
	/**
110
	 * Get the number of changes made by the last successful execution of this
111
	 * command. This includes changes made in all trigger subcontexts.
112
	 * @return the number of changed rows
113
	 * @see sqlite3_total_changes()
114
	 */
115
	inline int total_changes()
116
	{
117
		return _total_changes;
118
	}
119
120
	/**
121
	 * Get the rowid of the last row inserted via a successful INSERT command
122
	 * (regardless of whether that is this command or not).
123
	 * @return the last inserted rowid
124
	 */
125
	inline long long last_insert_rowid()
126
	{
127
		return _last_insert_rowid;
128
	}
129
130
//______________________________________________________________________________
131
//                                                                implementation
132
private:
133
134
	/** non-recursive number of changes made by the last execution */
135
	int _changes;
136
137
	/** recursive number of changes made by the last execution */
138
	int _total_changes;
139
140
	/** the rowid of the last successful insert command */
141
	long long _last_insert_rowid;
142
143
	/** exclusive use during command mutex */
144
	static boost::recursive_mutex _command_mutex;
145
1 by edam
- initial commit
146
};
147
148
2 by edam
- further initial development
149
} // namespace sqlite
150
151
152
#endif /* SQLITE3CC_COMMAND_H_ */