/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-27 15:46:42 UTC
  • Revision ID: edam@waxworlds.org-20100727154642-1uxrjkpxhp7xl6hq
- moved null_t, exec_t and set_index_t to detail namespace so only their extern instantiations are in the main namespace
- added immediate transation

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * query.cpp
 
2
 * query.cc
3
3
 *
4
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
6
 
 * This file is part of sqlitepp (hereafter referred to as "this program").
7
 
 * See http://www.waxworlds.org/edam/software/sqlitepp for more information.
 
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.
8
8
 *
9
9
 * This program is free software: you can redistribute it and/or modify
10
10
 * it under the terms of the GNU Lesser General Public License as published
20
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
21
 */
22
22
 
23
 
#include <sqlitepp/query.hpp>
24
 
 
25
 
 
 
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
        :
 
32
        basic_statement( database, sql ),
 
33
        _next_row( 0 )
 
34
{
 
35
        assert( sqlite3_column_count( _handle ) > 0 );
 
36
}
 
37
 
 
38
 
 
39
sqlite::query::query(
 
40
        database &database )
 
41
        :
 
42
        basic_statement( database ),
 
43
        _next_row( 0 )
 
44
{
 
45
}
 
46
 
 
47
 
 
48
int sqlite::query::prepare(
 
49
        const std::string &sql )
 
50
{
 
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;
 
55
}
 
56
 
 
57
 
 
58
sqlite::row sqlite::query::step()
 
59
{
 
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
//      }
 
77
}
 
78
 
 
79
 
 
80
unsigned int sqlite::query::column_count()
 
81
{
 
82
        return sqlite3_column_count( _handle );
 
83
}
 
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
}