Mercurial > hg > orthanc
annotate OrthancFramework/UnitTestsSources/SQLiteTests.cpp @ 4083:26efd0404d97 framework
integration mainline->framework
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 23 Jun 2020 07:39:42 +0200 |
parents | e00f3d089991 |
children | bf7b9edf6b81 |
rev | line source |
---|---|
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
1 /** |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
6 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
8 * modify it under the terms of the GNU General Public License as |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
9 * published by the Free Software Foundation, either version 3 of the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
10 * License, or (at your option) any later version. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
11 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
12 * In addition, as a special exception, the copyright holders of this |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
13 * program give permission to link the code of its release with the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
15 * that use the same license as the "OpenSSL" library), and distribute |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
16 * the linked executables. You must obey the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
17 * in all respects for all of the code used other than "OpenSSL". If you |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
18 * modify file(s) with this exception, you may extend this exception to |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
19 * your version of the file(s), but you are not obligated to do so. If |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
20 * you do not wish to do so, delete this exception statement from your |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
21 * version. If you delete this exception statement from all source files |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
22 * in the program, then also delete it here. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
23 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
24 * This program is distributed in the hope that it will be useful, but |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
25 * WITHOUT ANY WARRANTY; without even the implied warranty of |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
27 * General Public License for more details. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
28 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
29 * You should have received a copy of the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
31 **/ |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
32 |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
803
diff
changeset
|
33 |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
34 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1 |
4063
e00f3d089991
shared library of orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4062
diff
changeset
|
35 // Must be the first to be sure to use the Orthanc framework shared library |
4014
27628b0f6ada
merging logging code for plugins and files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3992
diff
changeset
|
36 # include <OrthancFramework.h> |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
37 #endif |
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
38 |
4062 | 39 #include <gtest/gtest.h> |
0 | 40 |
4045 | 41 #include "../Sources/SystemToolbox.h" |
42 #include "../Sources/SQLite/Connection.h" | |
43 #include "../Sources/SQLite/Statement.h" | |
44 #include "../Sources/SQLite/Transaction.h" | |
0 | 45 |
46 #include <sqlite3.h> | |
47 | |
63 | 48 using namespace Orthanc; |
0 | 49 |
50 | |
51 TEST(SQLite, Configuration) | |
52 { | |
1476 | 53 /** |
54 * The system-wide version of SQLite under OS X uses | |
55 * SQLITE_THREADSAFE==2 (SQLITE_CONFIG_SERIALIZED), whereas the | |
56 * static builds of Orthanc use SQLITE_THREADSAFE==1 | |
57 * (SQLITE_CONFIG_MULTITHREAD). In any case, we wish to ensure that | |
58 * SQLITE_THREADSAFE!=0 (SQLITE_CONFIG_SINGLETHREAD). | |
59 **/ | |
60 ASSERT_NE(0, sqlite3_threadsafe()); | |
0 | 61 } |
62 | |
63 | |
64 TEST(SQLite, Connection) | |
65 { | |
2140 | 66 SystemToolbox::RemoveFile("UnitTestsResults/coucou"); |
0 | 67 SQLite::Connection c; |
803
4689e400e0fa
directory to store the results of the unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
724
diff
changeset
|
68 c.Open("UnitTestsResults/coucou"); |
0 | 69 c.Execute("CREATE TABLE c(k INTEGER PRIMARY KEY AUTOINCREMENT, v INTEGER)"); |
70 c.Execute("INSERT INTO c VALUES(NULL, 42);"); | |
71 } | |
72 | |
73 | |
74 TEST(SQLite, StatementReferenceBasic) | |
75 { | |
76 sqlite3* db; | |
77 sqlite3_open(":memory:", &db); | |
78 | |
79 { | |
80 SQLite::StatementReference r(db, "SELECT * FROM sqlite_master"); | |
81 ASSERT_EQ(0u, r.GetReferenceCount()); | |
82 | |
83 { | |
84 SQLite::StatementReference r1(r); | |
85 ASSERT_EQ(1u, r.GetReferenceCount()); | |
86 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
87 | |
88 { | |
89 SQLite::StatementReference r2(r); | |
90 ASSERT_EQ(2u, r.GetReferenceCount()); | |
91 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
92 ASSERT_EQ(0u, r2.GetReferenceCount()); | |
93 | |
94 SQLite::StatementReference r3(r2); | |
95 ASSERT_EQ(3u, r.GetReferenceCount()); | |
96 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
97 ASSERT_EQ(0u, r2.GetReferenceCount()); | |
98 ASSERT_EQ(0u, r3.GetReferenceCount()); | |
99 } | |
100 | |
101 ASSERT_EQ(1u, r.GetReferenceCount()); | |
102 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
103 | |
104 { | |
105 SQLite::StatementReference r2(r); | |
106 ASSERT_EQ(2u, r.GetReferenceCount()); | |
107 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
108 ASSERT_EQ(0u, r2.GetReferenceCount()); | |
109 } | |
110 | |
111 ASSERT_EQ(1u, r.GetReferenceCount()); | |
112 ASSERT_EQ(0u, r1.GetReferenceCount()); | |
113 } | |
114 | |
115 ASSERT_EQ(0u, r.GetReferenceCount()); | |
116 } | |
117 | |
118 sqlite3_close(db); | |
119 } | |
120 | |
121 TEST(SQLite, StatementBasic) | |
122 { | |
123 SQLite::Connection c; | |
124 c.OpenInMemory(); | |
125 | |
126 SQLite::Statement s(c, "SELECT * from sqlite_master"); | |
127 s.Run(); | |
128 | |
129 for (unsigned int i = 0; i < 5; i++) | |
130 { | |
131 SQLite::Statement cs(c, SQLITE_FROM_HERE, "SELECT * from sqlite_master"); | |
132 cs.Step(); | |
133 } | |
134 } | |
135 | |
136 | |
137 namespace | |
138 { | |
139 static bool destroyed; | |
140 | |
141 class MyFunc : public SQLite::IScalarFunction | |
142 { | |
143 public: | |
144 MyFunc() | |
145 { | |
146 destroyed = false; | |
147 } | |
148 | |
2924
22524fd06225
macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
149 virtual ~MyFunc() ORTHANC_OVERRIDE |
0 | 150 { |
151 destroyed = true; | |
152 } | |
153 | |
2924
22524fd06225
macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
154 virtual const char* GetName() const ORTHANC_OVERRIDE |
0 | 155 { |
156 return "MYFUNC"; | |
157 } | |
158 | |
2924
22524fd06225
macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
159 virtual unsigned int GetCardinality() const ORTHANC_OVERRIDE |
0 | 160 { |
161 return 2; | |
162 } | |
163 | |
2924
22524fd06225
macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
164 virtual void Compute(SQLite::FunctionContext& context) ORTHANC_OVERRIDE |
0 | 165 { |
166 context.SetIntResult(1000 + context.GetIntValue(0) * context.GetIntValue(1)); | |
167 } | |
168 }; | |
169 | |
170 class MyDelete : public SQLite::IScalarFunction | |
171 { | |
172 public: | |
173 std::set<int> deleted_; | |
174 | |
2924
22524fd06225
macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
175 virtual const char* GetName() const ORTHANC_OVERRIDE |
0 | 176 { |
177 return "MYDELETE"; | |
178 } | |
179 | |
2924
22524fd06225
macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
180 virtual unsigned int GetCardinality() const ORTHANC_OVERRIDE |
0 | 181 { |
182 return 1; | |
183 } | |
184 | |
2924
22524fd06225
macros ORTHANC_OVERRIDE and ORTHANC_FINAL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
185 virtual void Compute(SQLite::FunctionContext& context) ORTHANC_OVERRIDE |
0 | 186 { |
187 deleted_.insert(context.GetIntValue(0)); | |
188 context.SetNullResult(); | |
189 } | |
190 }; | |
191 } | |
192 | |
193 TEST(SQLite, ScalarFunction) | |
194 { | |
195 { | |
196 SQLite::Connection c; | |
197 c.OpenInMemory(); | |
198 c.Register(new MyFunc()); | |
199 c.Execute("CREATE TABLE t(id INTEGER PRIMARY KEY, v1 INTEGER, v2 INTEGER);"); | |
200 c.Execute("INSERT INTO t VALUES(NULL, 2, 3);"); | |
201 c.Execute("INSERT INTO t VALUES(NULL, 4, 4);"); | |
202 c.Execute("INSERT INTO t VALUES(NULL, 6, 5);"); | |
203 SQLite::Statement t(c, "SELECT MYFUNC(v1, v2), v1, v2 FROM t"); | |
204 int i = 0; | |
205 while (t.Step()) | |
206 { | |
207 ASSERT_EQ(t.ColumnInt(0), 1000 + t.ColumnInt(1) * t.ColumnInt(2)); | |
208 i++; | |
209 } | |
210 ASSERT_EQ(3, i); | |
211 ASSERT_FALSE(destroyed); | |
212 } | |
213 ASSERT_TRUE(destroyed); | |
214 } | |
215 | |
216 TEST(SQLite, CascadedDeleteCallback) | |
217 { | |
218 SQLite::Connection c; | |
219 c.OpenInMemory(); | |
220 MyDelete *func = new MyDelete(); | |
221 c.Register(func); | |
222 c.Execute("CREATE TABLE parent(id INTEGER PRIMARY KEY, dummy INTEGER);"); | |
223 c.Execute("CREATE TABLE child(" | |
224 " id INTEGER PRIMARY KEY, " | |
225 " parent INTEGER REFERENCES parent(id) ON DELETE CASCADE, " | |
226 " value INTEGER);"); | |
227 c.Execute("CREATE TRIGGER childRemoved " | |
228 "AFTER DELETE ON child " | |
229 "FOR EACH ROW BEGIN " | |
230 " SELECT MYDELETE(old.value); " | |
231 "END;"); | |
232 | |
233 c.Execute("INSERT INTO parent VALUES(42, 100);"); | |
234 c.Execute("INSERT INTO parent VALUES(43, 101);"); | |
235 | |
236 c.Execute("INSERT INTO child VALUES(NULL, 42, 4200);"); | |
237 c.Execute("INSERT INTO child VALUES(NULL, 42, 4201);"); | |
238 | |
239 c.Execute("INSERT INTO child VALUES(NULL, 43, 4300);"); | |
240 c.Execute("INSERT INTO child VALUES(NULL, 43, 4301);"); | |
241 | |
242 // The following command deletes "parent(43, 101)", then in turns | |
243 // "child(NULL, 43, 4300/4301)", then calls the MyDelete on 4300 and | |
244 // 4301 | |
245 c.Execute("DELETE FROM parent WHERE dummy=101"); | |
246 | |
247 ASSERT_EQ(2u, func->deleted_.size()); | |
248 ASSERT_TRUE(func->deleted_.find(4300) != func->deleted_.end()); | |
249 ASSERT_TRUE(func->deleted_.find(4301) != func->deleted_.end()); | |
250 } | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
251 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
252 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
253 TEST(SQLite, EmptyTransactions) |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
254 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
255 try |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
256 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
257 SQLite::Connection c; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
258 c.OpenInMemory(); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
259 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
260 c.Execute("CREATE TABLE a(id INTEGER PRIMARY KEY);"); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
261 c.Execute("INSERT INTO a VALUES(NULL)"); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
262 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
263 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
264 SQLite::Transaction t(c); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
265 t.Begin(); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
266 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
267 SQLite::Statement s(c, SQLITE_FROM_HERE, "SELECT * FROM a"); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
268 s.Step(); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
269 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
270 //t.Commit(); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
271 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
272 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
273 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
274 SQLite::Statement s(c, SQLITE_FROM_HERE, "SELECT * FROM a"); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
275 s.Step(); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
276 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
277 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
278 catch (OrthancException& e) |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
279 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
280 fprintf(stderr, "Exception: [%s]\n", e.What()); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
281 throw e; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
282 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
283 } |
724 | 284 |
285 | |
286 TEST(SQLite, Types) | |
287 { | |
288 SQLite::Connection c; | |
289 c.OpenInMemory(); | |
290 c.Execute("CREATE TABLE a(id INTEGER PRIMARY KEY, value)"); | |
291 | |
292 { | |
293 SQLite::Statement s(c, std::string("SELECT * FROM a")); | |
294 ASSERT_EQ(2, s.ColumnCount()); | |
295 ASSERT_FALSE(s.Step()); | |
296 } | |
297 | |
298 { | |
299 SQLite::Statement s(c, SQLITE_FROM_HERE, std::string("SELECT * FROM a")); | |
300 ASSERT_FALSE(s.Step()); | |
301 ASSERT_EQ("SELECT * FROM a", s.GetOriginalSQLStatement()); | |
302 } | |
303 | |
304 { | |
305 SQLite::Statement s(c, SQLITE_FROM_HERE, "INSERT INTO a VALUES(NULL, ?);"); | |
306 s.BindNull(0); ASSERT_TRUE(s.Run()); s.Reset(); | |
307 s.BindBool(0, true); ASSERT_TRUE(s.Run()); s.Reset(); | |
308 s.BindInt(0, 42); ASSERT_TRUE(s.Run()); s.Reset(); | |
309 s.BindInt64(0, 42ll); ASSERT_TRUE(s.Run()); s.Reset(); | |
310 s.BindDouble(0, 42.5); ASSERT_TRUE(s.Run()); s.Reset(); | |
311 s.BindCString(0, "Hello"); ASSERT_TRUE(s.Run()); s.Reset(); | |
312 s.BindBlob(0, "Hello", 5); ASSERT_TRUE(s.Run()); s.Reset(); | |
313 } | |
314 | |
315 { | |
316 SQLite::Statement s(c, SQLITE_FROM_HERE, std::string("SELECT * FROM a")); | |
317 ASSERT_TRUE(s.Step()); | |
318 ASSERT_EQ(SQLite::COLUMN_TYPE_NULL, s.GetColumnType(1)); | |
319 ASSERT_TRUE(s.ColumnIsNull(1)); | |
320 ASSERT_TRUE(s.Step()); | |
321 ASSERT_EQ(SQLite::COLUMN_TYPE_INTEGER, s.GetColumnType(1)); | |
322 ASSERT_TRUE(s.ColumnBool(1)); | |
323 ASSERT_TRUE(s.Step()); | |
324 ASSERT_EQ(SQLite::COLUMN_TYPE_INTEGER, s.GetColumnType(1)); | |
325 ASSERT_EQ(42, s.ColumnInt(1)); | |
326 ASSERT_TRUE(s.Step()); | |
327 ASSERT_EQ(SQLite::COLUMN_TYPE_INTEGER, s.GetColumnType(1)); | |
328 ASSERT_EQ(42ll, s.ColumnInt64(1)); | |
329 ASSERT_TRUE(s.Step()); | |
330 ASSERT_EQ(SQLite::COLUMN_TYPE_FLOAT, s.GetColumnType(1)); | |
876 | 331 ASSERT_DOUBLE_EQ(42.5, s.ColumnDouble(1)); |
724 | 332 ASSERT_TRUE(s.Step()); |
333 ASSERT_EQ(SQLite::COLUMN_TYPE_TEXT, s.GetColumnType(1)); | |
334 ASSERT_EQ("Hello", s.ColumnString(1)); | |
335 ASSERT_TRUE(s.Step()); | |
336 ASSERT_EQ(SQLite::COLUMN_TYPE_BLOB, s.GetColumnType(1)); | |
337 ASSERT_EQ(5, s.ColumnByteLength(1)); | |
338 ASSERT_TRUE(!memcmp("Hello", s.ColumnBlob(1), 5)); | |
339 | |
340 std::string t; | |
341 ASSERT_TRUE(s.ColumnBlobAsString(1, &t)); | |
342 ASSERT_EQ("Hello", t); | |
343 | |
344 ASSERT_FALSE(s.Step()); | |
345 } | |
346 } |