/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:
29
29
        database &database,
30
30
        const std::string &sql )
31
31
        :
32
 
        basic_statement( database, sql ),
33
 
        _next_row( 0 )
 
32
        basic_statement( database, sql )
34
33
{
35
34
        assert( sqlite3_column_count( _handle ) > 0 );
36
35
}
39
38
sqlite::query::query(
40
39
        database &database )
41
40
        :
42
 
        basic_statement( database ),
43
 
        _next_row( 0 )
 
41
        basic_statement( database )
44
42
{
45
43
}
46
44
 
48
46
int sqlite::query::prepare(
49
47
        const std::string &sql )
50
48
{
51
 
        _next_row = 0;
52
49
        int error_code = basic_statement::prepare( sql );
53
50
        assert( error_code != SQLITE_OK || sqlite3_column_count( _handle ) > 0 );
54
51
        return error_code;
57
54
 
58
55
sqlite::row sqlite::query::step()
59
56
{
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
 
//      }
 
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 );
77
63
}
78
64
 
79
65
 
81
67
{
82
68
        return sqlite3_column_count( _handle );
83
69
}
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
 
}