bzr branch
http://bzr.ed.am/sqlite3cc
44
by Tim Marston
updated tests for blob support |
1 |
/* |
2 |
* test-blob.cc |
|
3 |
* |
|
4 |
* Copyright (C) 2015 Tim Marston <tim@ed.am> |
|
5 |
* |
|
6 |
* This file is part of sqlite3cc (hereafter referred to as "this program"). |
|
7 |
* See http://ed.am/dev/sqlite3cc for more information. |
|
8 |
* |
|
9 |
* This program is free software: you can redistribute it and/or modify it under |
|
10 |
* the terms of the GNU Lesser General Public License as published by the Free |
|
11 |
* Software Foundation, either version 3 of the License, or (at your option) any |
|
12 |
* later version. |
|
13 |
* |
|
14 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
15 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
16 |
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
17 |
* details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU Lesser General Public License |
|
20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21 |
*/ |
|
22 |
||
23 |
#include <sqlite3cc.h> |
|
24 |
#include <boost/filesystem.hpp> |
|
25 |
||
26 |
||
27 |
#define DBFILE "blob.db" |
|
28 |
||
29 |
||
30 |
int main() |
|
31 |
{ |
|
32 |
// delete any existing test database |
|
33 |
if( boost::filesystem::exists( DBFILE ) ) |
|
34 |
boost::filesystem::remove( DBFILE ); |
|
35 |
||
36 |
// open database |
|
37 |
sqlite::connection conn( DBFILE ); |
|
38 |
||
39 |
std::cout << "SQLite threading mode: " << sqlite::threadsafe() << "\n"; |
|
40 |
||
41 |
// set up database |
|
42 |
sqlite::command c1( conn, |
|
43 |
"CREATE TABLE test ( " |
|
44 |
"name TEXT PRIMARY KEY, " |
|
45 |
"data BLOB NOT NULL " |
|
46 |
")" ); |
|
47 |
c1.exec(); |
|
48 |
||
49 |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
|
50 |
||
51 |
// insert data in to blob |
|
52 |
char data[] = { |
|
53 |
'h', 'e', 'l', 'l', 'o', 0, |
|
54 |
't', 'h', 'e', 'r', 'e' |
|
55 |
}; |
|
56 |
std::string str( data, sizeof( data ) ); |
|
57 |
size_t length = str.length(); |
|
58 |
std::cout << "data size: " << length << " chars\n"; |
|
59 |
sqlite::command( conn, "INSERT INTO test VALUES( ?, ? )" ) |
|
60 |
<< "test" << sqlite::blob( str ) << sqlite::exec; |
|
61 |
||
62 |
// check that it is stored in its entirety |
|
63 |
sqlite::query q1( conn, "SELECT length( data ) FROM test" ); |
|
64 |
assert( q1.step().column< int >( 0 ) == length ); |
|
65 |
||
66 |
// retrieve data from blob |
|
67 |
sqlite::query q2( conn, "SELECT data FROM test" ); |
|
68 |
std::string str2 = q2.step().column< std::string >( 0 ); |
|
69 |
assert( str2 == str ); |
|
70 |
||
71 |
return 0; |
|
72 |
} |