/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-01-28 12:21:03 UTC
  • Revision ID: edam@waxworlds.org-20100128122103-v3gs6hro5h7ffnhb
- further initial development
- basic support for: queries, rows (query results)
- filename, comment header and project name consistency

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>
26
25
#include <assert.h>
27
26
 
28
27
 
30
29
        database &database,
31
30
        const std::string &sql )
32
31
        :
33
 
        basic_statement( database, sql ),
34
 
        _next_row( 0 )
 
32
        basic_statement( database, sql )
35
33
{
36
34
        assert( sqlite3_column_count( _handle ) > 0 );
37
35
}
40
38
sqlite::query::query(
41
39
        database &database )
42
40
        :
43
 
        basic_statement( database ),
44
 
        _next_row( 0 )
 
41
        basic_statement( database )
45
42
{
46
43
}
47
44
 
49
46
int sqlite::query::prepare(
50
47
        const std::string &sql )
51
48
{
52
 
        _next_row = 0;
53
 
        int code = basic_statement::prepare( sql );
54
 
        assert( code != SQLITE_OK || sqlite3_column_count( _handle ) > 0 );
55
 
        return code;
 
49
        int error_code = basic_statement::prepare( sql );
 
50
        assert( error_code != SQLITE_OK || sqlite3_column_count( _handle ) > 0 );
 
51
        return error_code;
56
52
}
57
53
 
58
54
 
59
55
sqlite::row sqlite::query::step()
60
56
{
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 );
 
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 );
68
63
}
69
64
 
70
65
 
72
67
{
73
68
        return sqlite3_column_count( _handle );
74
69
}
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