/sqlite3cc

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

« back to all changes in this revision

Viewing changes to include/sqlitepp/command.hpp

  • Committer: edam
  • Date: 2009-11-25 20:32:34 UTC
  • Revision ID: edam@waxworlds.org-20091125203234-24i7mzd60c7o46py
- initial commit
- basic support for: database, statements, commands, transactions, exceptions
- initial test app
- initial liscence and readme

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * command.h
 
2
 * database.hpp
3
3
 *
4
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
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.
 
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.
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 SQLITE3CC_COMMAND_H_
24
 
#define SQLITE3CC_COMMAND_H_
25
 
 
26
 
 
27
 
#include <sqlite3cc/basic_statement.h>
28
 
#include <boost/thread/recursive_mutex.hpp>
 
23
#ifndef COMMAND_HPP_
 
24
#define COMMAND_HPP_
 
25
 
 
26
 
 
27
#include "statement.hpp"
29
28
 
30
29
 
31
30
namespace sqlite
40
39
 */
41
40
class command
42
41
        :
43
 
        public basic_statement
 
42
        public statement
44
43
{
45
44
//______________________________________________________________________________
46
45
//                                                                 instantiation
47
46
public:
48
47
 
49
48
        /**
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
 
49
         * Constructor that provides the SQL statement to execute
53
50
         * @param sql an SQL statement in UTF-8
54
51
         */
55
 
        explicit command(
 
52
        command(
56
53
                database &database,
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 );
 
54
                const std::string &sql )
 
55
                :
 
56
                statement( database, sql )
 
57
        { }
65
58
 
66
59
//______________________________________________________________________________
67
60
//                                                              public interface
68
61
public:
69
62
 
70
63
        /**
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
 
        /**
89
64
         * Execute the command. This is the same as doing a step().
90
 
         * @return an sqlite error code
91
65
         * @see sqlite3_step()
92
66
         */
93
67
        inline int exec()
95
69
                return step();
96
70
        }
97
71
 
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
 
 
146
72
};
147
73
 
148
74
 
149
 
} // namespace sqlite
150
 
 
151
 
 
152
 
#endif /* SQLITE3CC_COMMAND_H_ */
 
75
}
 
76
 
 
77
 
 
78
#endif /* COMMAND_HPP_ */