/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 09:16:26 UTC
  • Revision ID: edam@waxworlds.org-20100729091626-h8fmg0r74eyfo5ae
- fixed error caused by finialising in-progress queries during rollback that were later finaliased by RAII.

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