/sqlite3cc

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

« back to all changes in this revision

Viewing changes to src/query.cc

  • Committer: edam
  • Date: 2010-07-29 21:00:36 UTC
  • Revision ID: edam@waxworlds.org-20100729210036-7me19mqc1iqo34pm
- failed to update TODO in last commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
#include <sqlite3cc/query.h>
24
24
#include <sqlite3cc/row.h>
 
25
#include <sqlite3cc/connection.h>
25
26
#include <assert.h>
26
27
 
27
28
 
28
29
sqlite::query::query(
29
 
        database &database,
 
30
        connection &connection,
30
31
        const std::string &sql )
31
32
        :
32
 
        basic_statement( database, sql ),
33
 
        _next_row( 0 )
 
33
        basic_statement( connection, sql ),
 
34
        _next_row_number( 0 )
34
35
{
35
36
        assert( sqlite3_column_count( _handle ) > 0 );
36
37
}
37
38
 
38
39
 
39
40
sqlite::query::query(
40
 
        database &database )
 
41
        connection &connection )
41
42
        :
42
 
        basic_statement( database ),
43
 
        _next_row( 0 )
 
43
        basic_statement( connection ),
 
44
        _next_row_number( 0 )
44
45
{
45
46
}
46
47
 
48
49
int sqlite::query::prepare(
49
50
        const std::string &sql )
50
51
{
51
 
        _next_row = 0;
52
 
        int error_code = basic_statement::prepare( sql );
53
 
        assert( error_code != SQLITE_OK || sqlite3_column_count( _handle ) > 0 );
54
 
        return error_code;
 
52
        _next_row_number = 0;
 
53
        int code = basic_statement::prepare( sql );
 
54
        assert( code != SQLITE_OK || sqlite3_column_count( _handle ) > 0 );
 
55
        return code;
 
56
}
 
57
 
 
58
 
 
59
int sqlite::query::reset()
 
60
{
 
61
        _next_row_number = 0;
 
62
        return basic_statement::reset();
55
63
}
56
64
 
57
65
 
58
66
sqlite::row sqlite::query::step()
59
67
{
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
 
//      }
 
68
        connection::mutex_guard lock( _connection );
 
69
 
 
70
        int code = basic_statement::step();
 
71
        if( code == SQLITE_DONE ) return row();
 
72
        if( code == SQLITE_ROW ) return row( _handle, _next_row_number++ );
 
73
 
 
74
        throw sqlite_error( _connection, code );
77
75
}
78
76
 
79
77
 
92
90
}
93
91
 
94
92
 
 
93
unsigned long long sqlite::query::num_results()
 
94
{
 
95
        reset();
 
96
        unsigned long long count = 0;
 
97
        while( step() ) count++;
 
98
        reset();
 
99
        return count;
 
100
}
 
101
 
 
102
 
95
103
sqlite::query::iterator::iterator(
96
104
        sqlite::query &query,
97
 
        bool valid )
 
105
        bool step )
98
106
        :
99
107
        _query( query )
100
108
{
101
 
        if( valid )
102
 
                increment();
103
 
        else
104
 
                _valid = false;
 
109
        if( step ) increment();
105
110
}
106
111
 
107
112
 
108
113
sqlite::row sqlite::query::iterator::dereference() const
109
114
{
110
 
        return sqlite::row( _query._handle, _query._next_row++ );
 
115
        return _row;
111
116
}
112
117
 
113
118
 
114
119
void sqlite::query::iterator::increment()
115
120
{
116
 
        _valid = _query.step();
 
121
        _row = _query.step();
117
122
}
118
123
 
119
124
 
121
126
        sqlite::query::iterator const &other )
122
127
        const
123
128
{
124
 
        return _valid == other._valid;
 
129
        return _row == other._row;
125
130
}
126
131
 
127
132
 
128
133
sqlite::query::iterator sqlite::query::begin()
129
134
{
 
135
        reset();
130
136
        return sqlite::query::iterator( *this, true );
131
137
}
132
138
 
135
141
{
136
142
        return sqlite::query::iterator( *this, false );
137
143
}
 
144