scclib-sqlite
Stable Cloud Computing Sqlite Library
sqld.cc
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sqlite/sqld.h>
32 #include <gtest/gtest.h>
33 #include <string>
34 #include <iostream>
35 #include <system_error>
36 #include <util/fs.h>
37 
44 using std::cout;
45 using std::endl;
46 using std::string;
47 using std::vector;
48 using std::system_error;
49 using std::runtime_error;
50 using fs = scc::util::Filesystem;
51 using scc::sqld::Conn;
52 using scc::sqld::Req;
53 using scc::sqld::Trans;
54 
55 struct SqliteTest : public testing::Test
56 {
57  string curdir;
58  Conn db;
59 
60  SqliteTest()
61  {
62  curdir = fs::get_current_dir();
63 
64  system_error err;
65  fs::remove_all("sandbox", &err);
66  fs::create_dir("sandbox");
67  fs::change_dir("sandbox");
68  }
69  virtual ~SqliteTest()
70  {
71  fs::change_dir(curdir);
72  system_error err;
73  fs::remove_all("sandbox", &err);
74  }
75 
76  void reopen(const string& uri)
77  {
78  cout << "opening: " << uri << endl;
79  db.reopen(uri);
80  }
81 };
82 
83 TEST_F(SqliteTest, default_conn)
84 {
85  auto fs = fs::scan_dir(".");
86  cout << "filesystem:" << endl;
87  for (auto& d : fs)
88  {
89  cout << d.first << " " << d.second << endl;
90  }
91  ASSERT_EQ(fs.size(), 0);
92 }
93 
94 TEST_F(SqliteTest, memory_conn)
95 {
96  reopen("file::memory:"); // reopen the connection without the shared cache
97  auto fs = fs::scan_dir(".");
98  cout << "filesystem:" << endl;
99  for (auto& d : fs)
100  {
101  cout << d.first << " " << d.second << endl;
102  }
103  ASSERT_EQ(fs.size(), 0);
104 }
105 
106 TEST_F(SqliteTest, file_open)
107 {
108  reopen("file:dbfile?mode=rwc"); // open a read/write/create file backed connection
109  auto fs = fs::scan_dir(".");
110  cout << "filesystem:" << endl;
111  for (auto& d : fs)
112  {
113  cout << d.first << " " << d.second << endl;
114  }
115  ASSERT_EQ(fs.size(), 1);
116  system_error err;
117  ASSERT_EQ(fs::file_stat("dbfile", &err).type, scc::util::FileType::reg);
118 }
119 
120 TEST_F(SqliteTest, exec_select)
121 {
122  Req req(db);
123 
124  req.sql()
125  << "create table t(a TEXT, b INT) STRICT;"
126  << "insert into t values('hello!', 1);"
127  << "insert into t values('goodbye', 2);"
128  << "select * from t;";
129 
130  int r = req.exec_select();
131  ASSERT_EQ(r, 2);
132 
133  cout << "first col name: " << req.col_name(0) << endl;
134  ASSERT_EQ(req.col_name(0), "a");
135  string n;
136  req.col_name(1, n);
137  cout << "second col name: " << n << endl;
138  ASSERT_EQ(n, "b");
139 
140  ASSERT_EQ(req.col_text(0), "hello!");
141  req.col_text(0, n);
142  ASSERT_EQ(n, "hello!");
143 
144  ASSERT_EQ(req.col_int(1), 1);
145 
146  ASSERT_EQ(req.next_row(), 2);
147 
148  ASSERT_EQ(req.col_text(0), "goodbye");
149  req.col_text(0, n);
150  ASSERT_EQ(n, "goodbye");
151 
152  ASSERT_EQ(req.col_int(1), 2);
153 
154  ASSERT_EQ(req.next_row(), 0);
155 
156  ASSERT_EQ(req.exec_select(), 0);
157 }
158 
159 TEST_F(SqliteTest, exec)
160 {
161  reopen("file:dbfile?mode=rwc");
162  Req req(db);
163 
164  int64_t big = 1;
165  big <<= 48;
166 
167  cout << "big is " << big << endl;
168 
169  req.sql()
170  << "create table t(a VARCHAR PRIMARY KEY, b DOUBLE, c INTEGER DEFAULT 0);"
171  << "insert into t (a, b) values('hello!', 1.1);"
172  << "insert into t values('goodbye', 2.2, " << big << ");"
173  << "select * from t;"
174  << "insert into t (a, b) values('until we meet again', 3.3);";
175 
176  req.exec(); // should go through all the statements ignoring the select statement
177 
178  req.clear();
179  req.sql() << "select b,c from t where a is 'goodbye';";
180 
181  int r = req.exec_select();
182  ASSERT_EQ(r, 2);
183  ASSERT_EQ(req.col_real(0), 2.2);
184  ASSERT_EQ(req.col_int64(1), big);
185 }
186 
187 TEST_F(SqliteTest, blob)
188 {
189  Req req(db);
190 
191  req.sql()
192  << "create table t(a BLOB) STRICT;"
193  << "insert into t values(x'deadbeef');"
194  << "select * from t;";
195 
196  int r = req.exec_select();
197  ASSERT_EQ(r, 1);
198 
199  vector<char> t = {'\xde', '\xad', '\xbe', '\xef'}, v;
200  req.col_blob(0, v);
201  ASSERT_EQ(t, v);
202 }
203 
204 TEST_F(SqliteTest, two_conns_xact)
205 {
206  reopen("file:dbfile?mode=rwc"); // first connection is read/write/create
207 
208  Req r(db);
209 
210  r.sql() << "create table t(a ANY) STRICT;";
211  r.exec(); // create the table
212 
213  Trans x(db);
214 
215  ASSERT_FALSE(x.is_active());
216 
217  x.begin(); // begin a transaction
218  ASSERT_TRUE(x.is_active());
219  r.clear();
220  r.sql() << "insert into t values(12345);";
221  r.exec(); // insert a value
222 
223  Conn db2("file:dbfile?mode=ro"); // second connection is read-only
224 
225  Req r2(db2); // request on second connection
226 
227  r2.sql() << "select * from t;";
228 
229  ASSERT_EQ(r2.exec_select(), 0); // cannot see the value yet
230 
231  x.commit(); // first connection commits transaction
232 
233  r2.reset(); // reuse the sql stream request
234 
235  ASSERT_EQ(r2.exec_select(), 1); // now the value can be seen
236 
237  x.begin();
238  r.clear();
239  r.sql() << "insert into t values(45678);";
240  r.exec();
241  x.abort(); // throw away this transaction
242 
243  r.clear();
244  r.sql() << "select * from t where a is 45678;";
245  ASSERT_EQ(r.exec_select(), 0); // it was rolled back
246 
247 
248  r2.clear();
249  r2.sql() << "insert into t values(45678);";
250  ASSERT_THROW(r2.exec(), runtime_error); // illegal for read-only db connection
251 }
sqld.h
File descriptor.
scc::sqld::Conn
Database connection.
Definition: sqld.h:61
scc::sqld::Trans
Database transaction.
Definition: sqld.h:89
scc::sqld::Req
Database request.
Definition: sqld.h:128
scc::sqld::Conn::reopen
void reopen(const std::string &="file:mem?mode=memory&cache=shared")
Reopen the connection.
Definition: sqld.cc:69