/sqlite3cc

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

« back to all changes in this revision

Viewing changes to test/test-main.cc

  • Committer: Tim Marston
  • Date: 2015-02-26 08:59:36 UTC
  • Revision ID: tim@ed.am-20150226085936-xe43in8bfz6f6nxb
fixed some missing/incorrect includes

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * test-database.cc
 
2
 * test-main.cc
3
3
 *
4
4
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
5
5
 *
34
34
                boost::filesystem::remove( DBFILE );
35
35
 
36
36
        // open database
37
 
        sqlite::connection db( DBFILE );
 
37
        sqlite::connection conn( DBFILE );
38
38
 
39
39
        std::cout << "SQLite threading mode: " << sqlite::threadsafe() << "\n";
40
40
 
41
41
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42
42
 
43
43
        // test basic statement, via command class
44
 
        sqlite::command c1( db,
 
44
        sqlite::command c1( conn,
45
45
                "CREATE TABLE pets ( "
46
46
                        "name TEXT PRIMARY KEY, "
47
47
                        "age INTEGER "
48
48
                ")" );
49
49
        c1.step();
50
50
 
51
 
        // test basic_statement bind() calls, via command
52
 
        sqlite::command c2( db, "INSERT INTO pets VALUES( ?, ? )" );
 
51
        // test basic_statement::bind() calls, via command
 
52
        sqlite::command c2( conn, "INSERT INTO pets VALUES( ?, ? )" );
53
53
        c2.bind( 1, "billy" ); c2.bind( 2, 12 ); c2.exec();
54
 
        sqlite::command c3( db, "INSERT INTO pets VALUES( ?6, ?9 )" );
 
54
        sqlite::command c3( conn, "INSERT INTO pets VALUES( ?6, ?9 )" );
55
55
        c3.bind( 9, 16 ); c3.bind( 6, "mopti" ); c3.exec();
56
 
        sqlite::command c6( db, "INSERT INTO pets VALUES( ?, ? )" );
 
56
        sqlite::command c6( conn, "INSERT INTO pets VALUES( ?, ? )" );
57
57
        c6.bind_null( 1 ); c6.bind( 2, 123 ); c6.exec();
58
 
        sqlite::command c7( db, "INSERT INTO pets VALUES( :foo, :bar )" );
59
 
        c7.bind( ":foo", "foocat" ); c7.bind( ":bar", 22 ); c7.exec();
 
58
        sqlite::command c7( conn, "INSERT INTO pets VALUES( :foo, :bar )" );
 
59
        c7.bind( ":foo", "foocat" ); c7.bind( ":bar", 22.5 ); c7.exec();
60
60
 
61
61
        // test command binding via stream operator
62
 
        sqlite::command( db, "INSERT INTO pets VALUES( ?, ? )" ) <<
 
62
        sqlite::command( conn, "INSERT INTO pets VALUES( ?, ? )" ) <<
63
63
                "tessa" << 16 << sqlite::exec;
64
 
        sqlite::command c4( db, "INSERT INTO pets VALUES( ?, ? )" );
 
64
        sqlite::command c4( conn, "INSERT INTO pets VALUES( ?, ? )" );
65
65
        c4 << sqlite::null << sqlite::null << sqlite::set_index( 1 ) <<
66
66
                "tamara" << sqlite::exec;
67
67
        assert( c4.changes() == 1 );
68
68
 
69
69
        // test query binding via stream operator
70
 
        sqlite::query( db, "SELECT * FROM pets WHERE name = ? OR age = ?" )
 
70
        sqlite::query( conn, "SELECT * FROM pets WHERE name = ? OR age = ?" )
71
71
                << "foo" << 16;
72
 
        sqlite::query( db, "SELECT * FROM pets WHERE name = ? OR name = ? OR "
 
72
        sqlite::query( conn, "SELECT * FROM pets WHERE name = ? OR name = ? OR "
73
73
                "name = ? OR name = ?" ) << "foo" << sqlite::null << sqlite::null
74
74
                << sqlite::set_index( 1 ) << "bar" << "baz";
75
75
 
 
76
        // test connection command and query factory methods
 
77
        conn.make_command( "PRAGMA user_version = 12")->step();
 
78
        assert( conn.make_query( "PRAGMA user_version" )->step()
 
79
                .column< int >( 0 ) == 12 );
 
80
 
76
81
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77
82
 
78
83
        // test basic queries
79
 
        sqlite::query q1( db, "SELECT age, age + 3, name "
 
84
        sqlite::query q1( conn, "SELECT age, age + 3, name "
80
85
                "FROM pets WHERE NOT age IS NULL "
81
86
                "ORDER BY age ASC" );
82
87
 
86
91
 
87
92
        // test rows
88
93
        unsigned int age;
 
94
        double dblage;
89
95
        std::string name;
90
96
        q1.step().column( 0, age ); assert( age == 12 );
91
97
        name = q1.step().column< std::string >( 2 ); assert( name == "mopti" );
95
101
        r1 >> age; assert( age == 16 );
96
102
        r1 >> age; assert( age == 19 );
97
103
        r1 >> name; assert( name == "tessa" );
98
 
        q1.step() >> age >> sqlite::null >> name;
99
 
        assert( age == 22 && name == "foocat" );
 
104
        q1.step() >> dblage >> sqlite::null >> name;
 
105
        assert( dblage == 22.5 && name == "foocat" );
100
106
        sqlite::row row = q1.step();
101
107
        row >> age >> sqlite::null >> sqlite::null; assert( age == 123 );
102
108
        row >> sqlite::set_index( 1 ) >> age; assert( age == 126 );
110
116
        assert( !q1.step() );
111
117
 
112
118
        // test num results
113
 
        sqlite::query q3( db, "SELECT name FROM pets WHERE age = 16" );
 
119
        sqlite::query q3( conn, "SELECT name FROM pets WHERE age = 16" );
114
120
        assert( q3.num_results() == 2 );
115
121
 
116
122
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
117
123
 
118
124
        // test query iterator
119
 
        sqlite::query q2( db, "SELECT * FROM pets ORDER BY age DESC" );
 
125
        sqlite::query q2( conn, "SELECT * FROM pets ORDER BY age DESC" );
120
126
        sqlite::query::iterator i1 = q2.begin();
121
127
        assert( i1 != q2.end() );
122
128
 
142
148
 
143
149
        // test transactions
144
150
        {
145
 
                sqlite::transaction_guard< sqlite::deferred_tranaction > t1( db );
146
 
        }
147
 
        {
148
 
                sqlite::transaction_guard< sqlite::immediate_transaction > t1( db );
149
 
        }
150
 
        {
151
 
                sqlite::transaction_guard< sqlite::exclusive_transaction > t1( db );
152
 
        }
153
 
        {
154
 
                sqlite::transaction_guard< sqlite::recursive_transaction > t1( db );
155
 
                sqlite::transaction_guard< sqlite::recursive_transaction > t2( db );
 
151
                sqlite::transaction_guard< sqlite::deferred_transaction > t1( conn );
 
152
        }
 
153
        {
 
154
                sqlite::transaction_guard< sqlite::immediate_transaction > t1( conn );
 
155
        }
 
156
        {
 
157
                sqlite::transaction_guard< sqlite::exclusive_transaction > t1( conn );
 
158
        }
 
159
        {
 
160
                sqlite::transaction_guard< sqlite::recursive_transaction > t1( conn );
 
161
                sqlite::transaction_guard< sqlite::recursive_transaction > t2( conn );
156
162
        }
157
163
 
158
164
        // test transaction guard rollback
159
165
        {
160
 
                sqlite::transaction_guard< > t1( db );
161
 
                db.exec( "UPDATE pets SET age = 99" );
 
166
                sqlite::transaction_guard< > t1( conn );
 
167
                conn.exec( "UPDATE pets SET age = 99" );
162
168
        }
163
 
        sqlite::query( db, "SELECT age FROM pets ORDER BY age DESC LIMIT 1" )
 
169
        sqlite::query( conn, "SELECT age FROM pets ORDER BY age DESC LIMIT 1" )
164
170
                .step() >> age; assert( age == 123 );
165
171
 
166
 
        // text recursive transactions
 
172
        // test recursive transactions
167
173
        {
168
 
                sqlite::transaction_guard< sqlite::recursive_transaction > t1( db );
169
 
                db.exec( "UPDATE pets SET age = 66" );
 
174
                sqlite::transaction_guard< sqlite::recursive_transaction > t1( conn );
 
175
                conn.exec( "UPDATE pets SET age = 66" );
170
176
 
171
177
                {
172
 
                        sqlite::transaction_guard< sqlite::recursive_transaction > t2( db );
173
 
                        db.exec( "UPDATE pets SET age = 1" );
 
178
                        sqlite::transaction_guard< sqlite::recursive_transaction > t2( conn );
 
179
                        conn.exec( "UPDATE pets SET age = 1" );
174
180
                }
175
181
 
176
 
                sqlite::query( db, "SELECT age FROM pets ORDER BY age DESC LIMIT 1" )
 
182
                sqlite::query( conn, "SELECT age FROM pets ORDER BY age DESC LIMIT 1" )
177
183
                        .step() >> age; assert( age == 66 );
178
184
        }
179
 
        sqlite::query( db, "SELECT age FROM pets ORDER BY age DESC LIMIT 1" )
 
185
        sqlite::query( conn, "SELECT age FROM pets ORDER BY age DESC LIMIT 1" )
180
186
                .step() >> age; assert( age == 123 );
181
187
 
 
188
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
189
 
182
190
        // ok
183
191
        return 0;
184
192
}