bzr branch
http://bzr.ed.am/sqlite3cc
1
by edam
- initial commit |
1 |
/* |
2
by edam
- further initial development |
2 |
* query.cc |
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 |
#include <sqlite3cc/query.h> |
24 |
#include <sqlite3cc/row.h> |
|
25 |
#include <assert.h> |
|
26 |
||
27 |
||
28 |
sqlite::query::query( |
|
29 |
database &database, |
|
30 |
const std::string &sql ) |
|
31 |
: |
|
9
by edam
- added NEWS |
32 |
basic_statement( database, sql ), |
33 |
_next_row( 0 ) |
|
2
by edam
- further initial development |
34 |
{ |
35 |
assert( sqlite3_column_count( _handle ) > 0 ); |
|
36 |
} |
|
37 |
||
38 |
||
39 |
sqlite::query::query( |
|
40 |
database &database ) |
|
41 |
: |
|
9
by edam
- added NEWS |
42 |
basic_statement( database ), |
43 |
_next_row( 0 ) |
|
2
by edam
- further initial development |
44 |
{ |
45 |
} |
|
46 |
||
47 |
||
48 |
int sqlite::query::prepare( |
|
49 |
const std::string &sql ) |
|
50 |
{ |
|
9
by edam
- added NEWS |
51 |
_next_row = 0; |
2
by edam
- further initial development |
52 |
int error_code = basic_statement::prepare( sql ); |
53 |
assert( error_code != SQLITE_OK || sqlite3_column_count( _handle ) > 0 ); |
|
54 |
return error_code; |
|
55 |
} |
|
56 |
||
57 |
||
58 |
sqlite::row sqlite::query::step() |
|
59 |
{ |
|
9
by edam
- added NEWS |
60 |
// sqlite3_mutex_enter( sqlite3_db_mutex( _database ) ); |
61 |
// try { |
|
62 |
int error_code = basic_statement::step(); |
|
63 |
if( error_code == SQLITE_ROW ) { |
|
64 |
// sqlite3_mutex_leave( sqlite3_db_mutex( _database ) ); |
|
65 |
return row( _handle, _next_row++ ); |
|
66 |
} |
|
67 |
if( error_code == SQLITE_DONE ) { |
|
68 |
// sqlite3_mutex_leave( sqlite3_db_mutex( _database ) ); |
|
69 |
return row(); |
|
70 |
} |
|
71 |
throw sqlite_error( error_code ); |
|
72 |
// } |
|
73 |
// catch( ... ) { |
|
74 |
// sqlite3_mutex_leave( sqlite3_db_mutex( _database ) ); |
|
75 |
// throw; |
|
76 |
// } |
|
2
by edam
- further initial development |
77 |
} |
78 |
||
79 |
||
80 |
unsigned int sqlite::query::column_count() |
|
81 |
{ |
|
82 |
return sqlite3_column_count( _handle ); |
|
83 |
} |
|
9
by edam
- added NEWS |
84 |
|
85 |
||
86 |
const std::string sqlite::query::column_name( |
|
87 |
unsigned int index ) |
|
88 |
{ |
|
89 |
assert( index < |
|
90 |
static_cast< unsigned int >( sqlite3_column_count( _handle ) ) ); |
|
91 |
return sqlite3_column_name( _handle, index ); |
|
92 |
} |
|
93 |
||
94 |
||
95 |
sqlite::query::iterator::iterator( |
|
96 |
sqlite::query &query, |
|
97 |
bool valid ) |
|
98 |
: |
|
99 |
_query( query ) |
|
100 |
{ |
|
101 |
if( valid ) |
|
102 |
increment(); |
|
103 |
else |
|
104 |
_valid = false; |
|
105 |
} |
|
106 |
||
107 |
||
108 |
sqlite::row sqlite::query::iterator::dereference() const |
|
109 |
{ |
|
110 |
return sqlite::row( _query._handle, _query._next_row++ ); |
|
111 |
} |
|
112 |
||
113 |
||
114 |
void sqlite::query::iterator::increment() |
|
115 |
{ |
|
116 |
_valid = _query.step(); |
|
117 |
} |
|
118 |
||
119 |
||
120 |
bool sqlite::query::iterator::equal( |
|
121 |
sqlite::query::iterator const &other ) |
|
122 |
const |
|
123 |
{ |
|
124 |
return _valid == other._valid; |
|
125 |
} |
|
126 |
||
127 |
||
128 |
sqlite::query::iterator sqlite::query::begin() |
|
129 |
{ |
|
130 |
return sqlite::query::iterator( *this, true ); |
|
131 |
} |
|
132 |
||
133 |
||
134 |
sqlite::query::iterator sqlite::query::end() |
|
135 |
{ |
|
136 |
return sqlite::query::iterator( *this, false ); |
|
137 |
} |