/sqlite3cc

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

« back to all changes in this revision

Viewing changes to include/sqlite3cc/query.h

  • Committer: edam
  • Date: 2010-07-27 15:46:42 UTC
  • Revision ID: edam@waxworlds.org-20100727154642-1uxrjkpxhp7xl6hq
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
- added immediate transation

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * query.hpp
 
2
 * query.h
3
3
 *
4
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
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.
 
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.
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 QUERY_HPP_
24
 
#define QUERY_HPP_
25
 
 
26
 
 
27
 
#include <sqlitepp/statement.hpp>
 
23
#ifndef SQLITE3CC_QUERY_H_
 
24
#define SQLITE3CC_QUERY_H_
 
25
 
 
26
 
 
27
#include <boost/iterator/iterator_facade.hpp>
 
28
#include <sqlite3cc/basic_statement.h>
 
29
#include <sqlite3cc/row.h>
28
30
 
29
31
 
30
32
namespace sqlite
33
35
 
34
36
class query
35
37
        :
36
 
        public statement
 
38
        public basic_statement
37
39
{
 
40
//______________________________________________________________________________
 
41
//                                                                 instantiation
 
42
public:
 
43
 
 
44
        /**
 
45
         * Constructor that provides a database upon which to act and the SQL
 
46
         * query to execute.
 
47
         * @param database a reference to a database
 
48
         * @param sql an SQL statement in UTF-8
 
49
         */
 
50
        explicit query(
 
51
                database &database,
 
52
                const std::string &sql );
 
53
 
 
54
        /**
 
55
         * Constructor that provides a database upon which to act.
 
56
         * @param database a reference to a database
 
57
         * @param sql an SQL statement in UTF-8
 
58
         */
 
59
        explicit query(
 
60
                database &database );
 
61
 
 
62
//______________________________________________________________________________
 
63
//                                                              public interface
 
64
public:
 
65
 
 
66
        /**
 
67
         * Prepare an SQL statement.
 
68
         * @param sql an SQL statement in UTF-8
 
69
         * @returns an sqlite error code
 
70
         * @see sqlite3_prepare_v2()
 
71
         */
 
72
        virtual int prepare(
 
73
                const std::string &sql );
 
74
 
 
75
        /**
 
76
         * Peform a step() and return row object that can be used to retrieve the
 
77
         * results.
 
78
         * @return a row object
 
79
         */
 
80
        row step();
 
81
 
 
82
        /**
 
83
         * Get the number of columns in the results
 
84
         * @see sqlite3_column_count()
 
85
         */
 
86
        unsigned int column_count();
 
87
 
 
88
        /**
 
89
         * Get the name of a column in the results
 
90
         * @param index column index
 
91
         * @see sqlite3_column_name()
 
92
         */
 
93
        const std::string column_name(
 
94
                unsigned int index );
 
95
 
 
96
        /**
 
97
         * Query iterator which can be used to obtain rows
 
98
         */
 
99
        class iterator
 
100
                :
 
101
                public boost::iterator_facade< iterator, row,
 
102
                        boost::single_pass_traversal_tag, row >
 
103
        {
 
104
        public:
 
105
                iterator();
 
106
                explicit iterator( query &query, bool valid );
 
107
 
 
108
        private:
 
109
                friend class boost::iterator_core_access;
 
110
 
 
111
                row dereference() const;
 
112
                void increment();
 
113
                bool equal( iterator const &other ) const;
 
114
 
 
115
                /** is this iterator still pointing to a valid row? */
 
116
                bool _valid;
 
117
 
 
118
                /** the query */
 
119
                query &_query;
 
120
        };
 
121
 
 
122
        /**
 
123
         * Return an iterator to the initial row. Note that creating an iterator
 
124
         * causes step() to be called, so it should only be called to begin
 
125
         * iterating over the rows and not for comparison.
 
126
         * @return a query iterator
 
127
         */
 
128
        iterator begin();
 
129
 
 
130
        /**
 
131
         * Return an iterator to after the last row (i.e., an invalid row).
 
132
         */
 
133
        iterator end();
 
134
 
 
135
//______________________________________________________________________________
 
136
//                                                                implementation
 
137
private:
 
138
 
 
139
        /** next row index */
 
140
        unsigned long long _next_row;
38
141
 
39
142
};
40
143
 
41
144
 
42
 
}
43
 
 
44
 
 
45
 
#endif /* QUERY_HPP_ */
 
145
} // namespace sqlite
 
146
 
 
147
 
 
148
#endif /* SQLITE3CC_QUERY_H_ */