/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/row.h

  • Committer: edam
  • Date: 2012-01-23 13:47:08 UTC
  • Revision ID: edam@waxworlds.org-20120123134708-ol4tilkotsm3han0
updated build system

Show diffs side-by-side

added added

removed removed

 
1
/*
 
2
 * row.h
 
3
 *
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
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.
 
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
 
 
23
#ifndef SQLITE3CC_ROW_H_
 
24
#define SQLITE3CC_ROW_H_
 
25
 
 
26
 
 
27
#include <sqlite3.h>
 
28
#include <boost/utility.hpp>
 
29
#include <boost/lexical_cast.hpp>
 
30
#include <boost/utility/value_init.hpp>
 
31
#include <cassert>
 
32
#include <iostream>
 
33
 
 
34
 
 
35
namespace sqlite
 
36
{
 
37
 
 
38
 
 
39
class query;
 
40
namespace detail {
 
41
        struct null_t;
 
42
        struct set_index_t;
 
43
}
 
44
 
 
45
 
 
46
/**
 
47
 * A result row from a query.
 
48
 *
 
49
 * The row is only valid until the next call to step() or reset() on the parent
 
50
 * query object, or until the parent query object is destructed. This may change
 
51
 * in future versions.
 
52
 */
 
53
class row
 
54
{
 
55
//______________________________________________________________________________
 
56
//                                                                 instantiation
 
57
protected:
 
58
 
 
59
        friend class query;
 
60
 
 
61
        /**
 
62
         * Constructor that produces a valid row.
 
63
         * @param handle of the statement (query) that created this row
 
64
         * @oaram row_number the index of this row
 
65
         */
 
66
        explicit row(
 
67
                sqlite3_stmt *handle,
 
68
                unsigned long long row_number );
 
69
 
 
70
        /**
 
71
         * Constructor that produces an invalid row.
 
72
         */
 
73
        explicit row();
 
74
 
 
75
//______________________________________________________________________________
 
76
//                                                              public interface
 
77
public:
 
78
 
 
79
        /**
 
80
         * Determine if this row is valid or not. If it is not valid, there are no
 
81
         * more rows in the results of the query.
 
82
         */
 
83
        operator bool() const;
 
84
 
 
85
        /**
 
86
         * get the index in to the results that is this row
 
87
         * @return index
 
88
         */
 
89
        inline unsigned long long row_number()
 
90
        {
 
91
                return _row_number;
 
92
        }
 
93
 
 
94
        /**
 
95
         * Ascertain a column's type.
 
96
         * @param index column index
 
97
         * @return sqlite datatype code
 
98
         * @see sqlite3_column_type()
 
99
         */
 
100
        int column_type(
 
101
                unsigned int index );
 
102
 
 
103
        /**
 
104
         * get the number of bytes in the result for a given column.
 
105
         * @param index column index
 
106
         * @return number of bytes in result
 
107
         * @see sqlite3_column_bytes()
 
108
         */
 
109
        unsigned int column_bytes(
 
110
                unsigned int index );
 
111
 
 
112
        /**
 
113
         * Get a value from the row.
 
114
         * @param index column index
 
115
         * @param value reference to object to set with the value
 
116
         * @see sqlite3_column_*()
 
117
         */
 
118
        template< class T >
 
119
        void column(
 
120
                unsigned int index,
 
121
                T &value )
 
122
        {
 
123
                assert( index <
 
124
                        static_cast< unsigned int >( sqlite3_column_count( _handle ) ) );
 
125
                const char *text = reinterpret_cast< const char * >(
 
126
                        sqlite3_column_text( _handle, index ) );
 
127
                if( text )
 
128
                        value = boost::lexical_cast< T >( text );
 
129
                else
 
130
                        value = boost::get( boost::value_initialized< T >() );
 
131
        }
 
132
 
 
133
        /**
 
134
         * Get a value from the row and return it.
 
135
         * @param index column index
 
136
         * @return the value
 
137
         * @see sqlite3_column_*()
 
138
         */
 
139
        template< class T >
 
140
        T column(
 
141
                unsigned int index )
 
142
        {
 
143
                T value;
 
144
                column( index, value );
 
145
                return value;
 
146
        }
 
147
 
 
148
        /**
 
149
         * Stream operator is used to obtain values from a result row, fetching from
 
150
         * each column in turn. In addition, the null and set_index() auto-column-
 
151
         * getting manipulators can be used.
 
152
         * @param value is a variable to store the retrieved data in
 
153
         */
 
154
        template< class T >
 
155
        row &operator >>(
 
156
                T &value )
 
157
        {
 
158
                column( _column_index, value );
 
159
                _column_index++;
 
160
                return *this;
 
161
        }
 
162
 
 
163
        /**
 
164
         * Stream operator for use with set_index().
 
165
         */
 
166
        row &operator >>(
 
167
                detail::set_index_t t );
 
168
 
 
169
        /**
 
170
         * Test to see if two rows are the same
 
171
         * @param other the row to compare this one to
 
172
         * @return true if they are
 
173
         */
 
174
        bool operator ==(
 
175
                const row &other )
 
176
                const;
 
177
 
 
178
//______________________________________________________________________________
 
179
//                                                                implementation
 
180
protected:
 
181
 
 
182
        /** the query's handle, or NULL */
 
183
        sqlite3_stmt *_handle;
 
184
 
 
185
private:
 
186
 
 
187
        /** index used when auto-column-getting */
 
188
        unsigned int _column_index;
 
189
 
 
190
        /** the index of this row */
 
191
        unsigned long long _row_number;
 
192
 
 
193
};
 
194
 
 
195
 
 
196
// template specialisations
 
197
template< >
 
198
row &row::operator >> < detail::null_t >(
 
199
        detail::null_t & );
 
200
 
 
201
 
 
202
} // namespace sqlite
 
203
 
 
204
 
 
205
#endif /* SQLITE3CC_ROW_H_ */