| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | /****************************************************************************
 | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:55:33 +01:00
										 |  |  | ** Copyright (C) 2016 The Qt Company Ltd. | 
					
						
							|  |  |  | ** Contact: https://www.qt.io/licensing/
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | ** | 
					
						
							|  |  |  | ** This file is part of Qt Creator. | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** Commercial License Usage | 
					
						
							|  |  |  | ** Licensees holding valid commercial Qt licenses may use this file in | 
					
						
							|  |  |  | ** accordance with the commercial license agreement provided with the | 
					
						
							|  |  |  | ** Software or, alternatively, in accordance with the terms contained in | 
					
						
							| 
									
										
										
										
											2016-01-15 14:55:33 +01:00
										 |  |  | ** a written agreement between you and The Qt Company. For licensing terms | 
					
						
							|  |  |  | ** and conditions see https://www.qt.io/terms-conditions. For further
 | 
					
						
							|  |  |  | ** information use the contact form at https://www.qt.io/contact-us.
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:55:33 +01:00
										 |  |  | ** GNU General Public License Usage | 
					
						
							|  |  |  | ** Alternatively, this file may be used under the terms of the GNU | 
					
						
							|  |  |  | ** General Public License version 3 as published by the Free Software | 
					
						
							|  |  |  | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
					
						
							|  |  |  | ** included in the packaging of this file. Please review the following | 
					
						
							|  |  |  | ** information to ensure the GNU General Public License requirements will | 
					
						
							|  |  |  | ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | ** | 
					
						
							|  |  |  | ****************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 17:41:41 +02:00
										 |  |  | #include "googletest.h"
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:59 +02:00
										 |  |  | #include "mocksqlitestatement.h"
 | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  | #include "sqliteteststatement.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-15 17:41:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-26 18:43:07 +02:00
										 |  |  | #include <sqlitedatabase.h>
 | 
					
						
							| 
									
										
										
										
											2015-06-16 12:38:04 +02:00
										 |  |  | #include <sqlitereadstatement.h>
 | 
					
						
							|  |  |  | #include <sqlitereadwritestatement.h>
 | 
					
						
							|  |  |  | #include <sqlitewritestatement.h>
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | #include <utils/smallstringio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-16 12:38:04 +02:00
										 |  |  | #include <QDir>
 | 
					
						
							| 
									
										
										
										
											2017-07-27 15:59:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							| 
									
										
										
										
											2017-07-27 15:59:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | using Sqlite::JournalMode; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  | using Sqlite::Exception; | 
					
						
							|  |  |  | using Sqlite::Database; | 
					
						
							|  |  |  | using Sqlite::ReadStatement; | 
					
						
							|  |  |  | using Sqlite::ReadWriteStatement; | 
					
						
							|  |  |  | using Sqlite::WriteStatement; | 
					
						
							| 
									
										
										
										
											2017-07-26 16:02:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
											
												Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
                                WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
                                WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
											
										 
											2017-07-31 19:44:39 +02:00
										 |  |  | MATCHER_P3(HasValues, value1, value2, rowid, | 
					
						
							|  |  |  |            std::string(negation ? "isn't" : "is") | 
					
						
							|  |  |  |            + PrintToString(value1) | 
					
						
							|  |  |  |            + ", " + PrintToString(value2) | 
					
						
							|  |  |  |            + " and " + PrintToString(rowid) | 
					
						
							|  |  |  |            ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     Database &database = arg.database(); | 
					
						
							| 
									
										
										
											
												Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
                                WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
                                WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
											
										 
											2017-07-31 19:44:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test WHERE rowid=?", database); | 
					
						
							| 
									
										
										
											
												Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
                                WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
                                WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
											
										 
											2017-07-31 19:44:39 +02:00
										 |  |  |     statement.bind(1, rowid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     statement.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 13:37:34 +02:00
										 |  |  |     return statement.fetchSmallStringViewValue(0) == value1 | 
					
						
							|  |  |  |         && statement.fetchSmallStringViewValue(1) == value2; | 
					
						
							| 
									
										
										
											
												Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
                                WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
                                WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
											
										 
											2017-07-31 19:44:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | class SqliteStatement : public ::testing::Test | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |      void SetUp() override; | 
					
						
							|  |  |  |      void TearDown() override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
                                WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
                                WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
											
										 
											2017-07-31 19:44:39 +02:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2017-10-18 13:34:05 +02:00
										 |  |  |      Database database{":memory:", Sqlite::JournalMode::Memory}; | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | struct Output | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-18 13:37:34 +02:00
										 |  |  |     Output(Utils::SmallStringView name, Utils::SmallStringView number, long long value) | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |         : name(name), number(number), value(value) | 
					
						
							|  |  |  |     {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  |     Utils::SmallString name; | 
					
						
							|  |  |  |     Utils::SmallString number; | 
					
						
							|  |  |  |     long long value; | 
					
						
							|  |  |  |     friend bool operator==(const Output &f, const Output &s) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return f.name == s.name && f.number == s.number && f.value == s.value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     friend std::ostream &operator<<(std::ostream &out, const Output &o) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return out << "(" << o.name << ", " << ", " << o.number<< ", " << o.value<< ")"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | TEST_F(SqliteStatement, ThrowsStatementHasErrorForWrongSqlStatement) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  |     ASSERT_THROW(ReadStatement("blah blah blah", database), Sqlite::StatementHasError); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, ThrowsNotReadOnlySqlStatementForWritableSqlStatementInReadStatement) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT_THROW(ReadStatement("INSERT INTO test(name, number) VALUES (?, ?)", database), | 
					
						
							|  |  |  |                  Sqlite::NotReadOnlySqlStatement); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, ThrowsNotReadonlySqlStatementForWritableSqlStatementInReadStatement) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT_THROW(WriteStatement("SELECT name, number FROM test", database), | 
					
						
							|  |  |  |                  Sqlite::NotWriteSqlStatement); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, CountRows) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT * FROM test", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     int nextCount = 0; | 
					
						
							|  |  |  |     while (statement.next()) | 
					
						
							|  |  |  |         ++nextCount; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     int sqlCount = ReadStatement::toValue<int>("SELECT count(*) FROM test", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(nextCount, sqlCount); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, Value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test ORDER BY name", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     statement.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     statement.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchValue<int>(0), 0); | 
					
						
							|  |  |  |     ASSERT_THAT(statement.fetchValue<int64_t>(0), 0); | 
					
						
							|  |  |  |     ASSERT_THAT(statement.fetchValue<double>(0), 0.0); | 
					
						
							| 
									
										
										
										
											2017-10-18 13:37:34 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchValue<Utils::SmallString>(0), "foo"); | 
					
						
							|  |  |  |     ASSERT_THAT(statement.fetchValue<Utils::PathString>(0), "foo"); | 
					
						
							|  |  |  |     ASSERT_THAT(statement.fetchSmallStringViewValue(0), "foo"); | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchValue<int>(1), 23); | 
					
						
							|  |  |  |     ASSERT_THAT(statement.fetchValue<int64_t>(1), 23); | 
					
						
							|  |  |  |     ASSERT_THAT(statement.fetchValue<double>(1), 23.3); | 
					
						
							| 
									
										
										
										
											2017-10-18 13:37:34 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchValue<Utils::SmallString>(1), "23.3"); | 
					
						
							|  |  |  |     ASSERT_THAT(statement.fetchValue<Utils::PathString>(1), "23.3"); | 
					
						
							|  |  |  |     ASSERT_THAT(statement.fetchSmallStringViewValue(1), "23.3"); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | TEST_F(SqliteStatement, ThrowNoValuesToFetchForNotSteppedStatement) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     ASSERT_THROW(statement.fetchValue<int>(0), Sqlite::NoValuesToFetch); | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | TEST_F(SqliteStatement, ThrowNoValuesToFetchForDoneStatement) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     while (statement.next()) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     ASSERT_THROW(statement.fetchValue<int>(0), Sqlite::NoValuesToFetch); | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | TEST_F(SqliteStatement, ThrowInvalidColumnFetchedForNegativeColumn) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     statement.next(); | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     ASSERT_THROW(statement.fetchValue<int>(-1), Sqlite::InvalidColumnFetched); | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, ThrowInvalidColumnFetchedForNotExistingColumn) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test", database); | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  |     statement.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     ASSERT_THROW(statement.fetchValue<int>(2), Sqlite::InvalidColumnFetched); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, ToIntergerValue) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     auto value = ReadStatement::toValue<int>("SELECT number FROM test WHERE name='foo'", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(value, 23); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, ToLongIntergerValue) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ASSERT_THAT(ReadStatement::toValue<qint64>("SELECT number FROM test WHERE name='foo'", database), Eq(23)); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, ToDoubleValue) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ASSERT_THAT(ReadStatement::toValue<double>("SELECT number FROM test WHERE name='foo'", database), 23.3); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 15:59:54 +02:00
										 |  |  | TEST_F(SqliteStatement, ToStringValue) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ASSERT_THAT(ReadStatement::toValue<Utils::SmallString>("SELECT name FROM test WHERE name='foo'", database), "foo"); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, ColumnNames) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 15:59:54 +02:00
										 |  |  |     auto columnNames = statement.columnNames(); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 15:59:54 +02:00
										 |  |  |     ASSERT_THAT(columnNames, ElementsAre("name", "number")); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 15:59:54 +02:00
										 |  |  | TEST_F(SqliteStatement, BindString) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test WHERE name=?", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 15:59:54 +02:00
										 |  |  |     statement.bind(1, "foo"); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     statement.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 13:37:34 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchSmallStringViewValue(0), "foo"); | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchValue<double>(1), 23.3); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, BindInteger) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test WHERE number=?", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     statement.bind(1, 40); | 
					
						
							|  |  |  |     statement.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 13:37:34 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchSmallStringViewValue(0),"poo"); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, BindLongInteger) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test WHERE number=?", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  |     statement.bind(1, int64_t(40)); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     statement.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 13:37:34 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchSmallStringViewValue(0), "poo"); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, BindDouble) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test WHERE number=?", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     statement.bind(1, 23.3); | 
					
						
							|  |  |  |     statement.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 13:37:34 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchSmallStringViewValue(0), "foo"); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, BindIntegerByParameter) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test WHERE number=@number", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 15:59:54 +02:00
										 |  |  |     statement.bind("@number", 40); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     statement.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 13:37:34 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchSmallStringViewValue(0), "poo"); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, BindLongIntegerByParameter) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test WHERE number=@number", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  |     statement.bind("@number", int64_t(40)); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     statement.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 13:37:34 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchSmallStringViewValue(0), "poo"); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, BindDoubleByIndex) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test WHERE number=@number", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 15:59:54 +02:00
										 |  |  |     statement.bind(statement.bindingIndexForName("@number"), 23.3); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     statement.next(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 13:37:34 +02:00
										 |  |  |     ASSERT_THAT(statement.fetchSmallStringViewValue(0), "foo"); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | TEST_F(SqliteStatement, BindIndexIsZeroIsThrowingBindingIndexIsOutOfBound) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test WHERE number=$1", database); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  |     ASSERT_THROW(statement.bind(0, 40), Sqlite::BindingIndexIsOutOfRange); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, BindIndexIsTpLargeIsThrowingBindingIndexIsOutOfBound) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test WHERE number=$1", database); | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THROW(statement.bind(2, 40), Sqlite::BindingIndexIsOutOfRange); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, WrongBindingNameThrowingBindingIndexIsOutOfBound) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("SELECT name, number FROM test WHERE number=@name", database); | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:59 +02:00
										 |  |  |     ASSERT_THROW(statement.bind("@name2", 40), Sqlite::WrongBindingName); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
                                WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
                                WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
											
										 
											2017-07-31 19:44:39 +02:00
										 |  |  | TEST_F(SqliteStatement, BindValues) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("UPDATE test SET name=?, number=? WHERE rowid=?", database); | 
					
						
							| 
									
										
										
											
												Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
                                WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
                                WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
											
										 
											2017-07-31 19:44:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     statement.bindValues("see", 7.23, 1); | 
					
						
							|  |  |  |     statement.execute(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(statement, HasValues("see", "7.23", 1)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, WriteValues) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     WriteStatement statement("UPDATE test SET name=?, number=? WHERE rowid=?", database); | 
					
						
							| 
									
										
										
											
												Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
                                WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
                                WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
											
										 
											2017-07-31 19:44:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     statement.write("see", 7.23, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(statement, HasValues("see", "7.23", 1)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, BindNamedValues) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 15:59:08 +02:00
										 |  |  |     SqliteTestStatement statement("UPDATE test SET name=@name, number=@number WHERE rowid=@id", database); | 
					
						
							| 
									
										
										
											
												Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
                                WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
                                WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
											
										 
											2017-07-31 19:44:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     statement.bindNameValues("@name", "see", "@number", 7.23, "@id", 1); | 
					
						
							|  |  |  |     statement.execute(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(statement, HasValues("see", "7.23", 1)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, WriteNamedValues) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     WriteStatement statement("UPDATE test SET name=@name, number=@number WHERE rowid=@id", database); | 
					
						
							| 
									
										
										
											
												Sqlite: Add variadic bind and write functions
You can now write
SqliteWriteStatement statement("UPDATE test SET name=?, number=?
                                WHERE rowid=?", database);
statement.write("see", 7.23, 1);
and
SqliteWriteStatement statement("UPDATE test SET name=@name, number=@number
                                WHERE rowid=@id", database);
statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1);
This is more type safe than using variants and performant too.
Change-Id: Ie1ed2a6d326b956be5c4ec056214f3f5b1531f45
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
											
										 
											2017-07-31 19:44:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     statement.writeNamed("@name", "see", "@number", 7.23, "@id", 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(statement, HasValues("see", "7.23", 1)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | TEST_F(SqliteStatement, CannotWriteToClosedDatabase) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     database.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THROW(WriteStatement("INSERT INTO test(name, number) VALUES (?, ?)", database), | 
					
						
							|  |  |  |                  Sqlite::DatabaseIsNotOpen); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, CannotReadFromClosedDatabase) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-07-26 18:43:07 +02:00
										 |  |  |     database.close(); | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THROW(ReadStatement("SELECT * FROM test", database), | 
					
						
							|  |  |  |                  Sqlite::DatabaseIsNotOpen); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | TEST_F(SqliteStatement, GetTupleValuesWithoutArguments) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using Tuple = std::tuple<Utils::SmallString, double, int>; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name, number, value FROM test", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     auto values = statement.values<Tuple, 3>(3); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre(Tuple{"bar", 0, 1}, | 
					
						
							|  |  |  |                                     Tuple{"foo", 23.3, 2}, | 
					
						
							|  |  |  |                                     Tuple{"poo", 40.0, 3})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetSingleValuesWithoutArguments) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name FROM test", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     std::vector<Utils::SmallString> values = statement.values<Utils::SmallString>(3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre("bar", "foo", "poo")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetStructValuesWithoutArguments) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name, number, value FROM test", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     auto values = statement.values<Output, 3>(3); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre(Output{"bar", "blah", 1}, | 
					
						
							|  |  |  |                                     Output{"foo", "23.3", 2}, | 
					
						
							|  |  |  |                                     Output{"poo", "40", 3})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesForSingleOutputWithBindingMultipleTimes) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name FROM test WHERE number=?", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  |     statement.values<Utils::SmallString>(3, 40); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::vector<Utils::SmallString> values = statement.values<Utils::SmallString>(3, 40); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre("poo")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesForMultipleOutputValuesAndContainerQueryValues) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using Tuple = std::tuple<Utils::SmallString, double, double>; | 
					
						
							|  |  |  |     std::vector<double> queryValues = {40, 23.3}; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name, number, value FROM test WHERE number=?", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     auto values = statement.values<Tuple, 3>(3, queryValues); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre(Tuple{"poo", 40, 3.}, | 
					
						
							|  |  |  |                                     Tuple{"foo", 23.3, 2.})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesForSingleOutputValuesAndContainerQueryValues) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     std::vector<double> queryValues = {40, 23.3}; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name, number FROM test WHERE number=?", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     std::vector<Utils::SmallString> values = statement.values<Utils::SmallString>(3, queryValues); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre("poo", "foo")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesForMultipleOutputValuesAndContainerQueryTupleValues) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using Tuple = std::tuple<Utils::SmallString, Utils::SmallString, int>; | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     using ResultTuple = std::tuple<Utils::SmallString, double, int>; | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  |     std::vector<Tuple> queryValues = {{"poo", "40", 3}, {"bar", "blah", 1}}; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name, number, value FROM test WHERE name= ? AND number=? AND value=?", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     auto values = statement.values<ResultTuple, 3>(3, queryValues); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     ASSERT_THAT(values, ElementsAre(ResultTuple{"poo", 40, 3}, | 
					
						
							|  |  |  |                                     ResultTuple{"bar", 0, 1})); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesForSingleOutputValuesAndContainerQueryTupleValues) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using Tuple = std::tuple<Utils::SmallString, Utils::SmallString>; | 
					
						
							|  |  |  |     std::vector<Tuple> queryValues = {{"poo", "40"}, {"bar", "blah"}}; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name, number FROM test WHERE name= ? AND number=?", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     std::vector<Utils::SmallString> values = statement.values<Utils::SmallString>(3, queryValues); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre("poo", "bar")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesForMultipleOutputValuesAndMultipleQueryValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using Tuple = std::tuple<Utils::SmallString, Utils::SmallString, long long>; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name, number, value FROM test WHERE name=? AND number=? AND value=?", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     auto values = statement.values<Tuple, 3>(3, "bar", "blah", 1); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre(Tuple{"bar", "blah", 1})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, CallGetValuesForMultipleOutputValuesAndMultipleQueryValueMultipleTimes) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using Tuple = std::tuple<Utils::SmallString, Utils::SmallString, long long>; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name, number, value FROM test WHERE name=? AND number=?", database); | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     statement.values<Tuple, 3>(3, "bar", "blah"); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     auto values = statement.values<Tuple, 3>(3, "bar", "blah"); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre(Tuple{"bar", "blah", 1})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetStructOutputValuesAndMultipleQueryValue) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name, number, value FROM test WHERE name=? AND number=? AND value=?", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     auto values = statement.values<Output, 3>(3, "bar", "blah", 1); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre(Output{"bar", "blah", 1})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetStructOutputValuesAndContainerQueryValues) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     std::vector<double> queryValues = {40, 23.3}; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name, number, value FROM test WHERE number=?", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     auto values = statement.values<Output, 3>(3, queryValues); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre(Output{"poo", "40", 3}, | 
					
						
							|  |  |  |                                     Output{"foo", "23.3", 2})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetStructOutputValuesAndContainerQueryTupleValues) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using Tuple = std::tuple<Utils::SmallString, Utils::SmallString, int>; | 
					
						
							|  |  |  |     std::vector<Tuple> queryValues = {{"poo", "40", 3}, {"bar", "blah", 1}}; | 
					
						
							| 
									
										
										
										
											2017-09-18 10:21:45 +02:00
										 |  |  |     ReadStatement statement("SELECT name, number, value FROM test WHERE name= ? AND number=? AND value=?", database); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  |     auto values = statement.values<Output, 3>(3, queryValues); | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(values, ElementsAre(Output{"poo", "40", 3}, | 
					
						
							|  |  |  |                                     Output{"bar", "blah", 1})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  | TEST_F(SqliteStatement, GetOptionalSingleValueAndMultipleQueryValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ReadStatement statement("SELECT name FROM test WHERE name=? AND number=? AND value=?", database); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto value = statement.value<Utils::SmallString>("bar", "blah", 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(value.value(), Eq("bar")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetOptionalOutputValueAndMultipleQueryValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ReadStatement statement("SELECT name, number, value FROM test WHERE name=? AND number=? AND value=?", database); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto value = statement.value<Output, 3>("bar", "blah", 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(value.value(), Eq(Output{"bar", "blah", 1})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetOptionalTupleValueAndMultipleQueryValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using Tuple = std::tuple<Utils::SmallString, Utils::SmallString, long long>; | 
					
						
							|  |  |  |     ReadStatement statement("SELECT name, number, value FROM test WHERE name=? AND number=? AND value=?", database); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto value = statement.value<Tuple, 3>("bar", "blah", 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(value.value(), Eq(Tuple{"bar", "blah", 1})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:59 +02:00
										 |  |  | TEST_F(SqliteStatement, GetOptionalValueCallsReset) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mockStatement.value<int>("bar"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetOptionalValueCallsResetIfExceptionIsThrown) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  |     ON_CALL(mockStatement, next()).WillByDefault(Throw(Sqlite::StatementHasError(""))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_THROW(mockStatement.value<int>("bar"), Sqlite::StatementHasError); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesWithoutArgumentsCallsReset) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mockStatement.values<int>(3); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesWithoutArgumentsCallsResetIfExceptionIsThrown) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  |     ON_CALL(mockStatement, next()).WillByDefault(Throw(Sqlite::StatementHasError(""))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_THROW(mockStatement.values<int>(3), Sqlite::StatementHasError); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesWithSimpleArgumentsCallsReset) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mockStatement.values<int>(3, "foo", "bar"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesWithSimpleArgumentsCallsResetIfExceptionIsThrown) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  |     ON_CALL(mockStatement, next()).WillByDefault(Throw(Sqlite::StatementHasError(""))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_THROW(mockStatement.values<int>(3, "foo", "bar"), Sqlite::StatementHasError); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesWithVectorArgumentsCallsReset) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()).Times(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mockStatement.values<int>(3, std::vector<Utils::SmallString>{"bar", "foo"}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesWithVectorArgumentCallsResetIfExceptionIsThrown) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  |     ON_CALL(mockStatement, next()).WillByDefault(Throw(Sqlite::StatementHasError(""))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_THROW(mockStatement.values<int>(3, std::vector<Utils::SmallString>{"bar", "foo"}), | 
					
						
							|  |  |  |                  Sqlite::StatementHasError); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesWithTupleArgumentsCallsReset) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()).Times(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mockStatement.values<int>(3, std::vector<std::tuple<int>>{{1}, {2}}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, GetValuesWithTupleArgumentsCallsResetIfExceptionIsThrown) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  |     ON_CALL(mockStatement, next()).WillByDefault(Throw(Sqlite::StatementHasError(""))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_THROW(mockStatement.values<int>(3, std::vector<std::tuple<int>>{{1}, {2}}), | 
					
						
							|  |  |  |                  Sqlite::StatementHasError); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-23 13:26:32 +02:00
										 |  |  | TEST_F(SqliteStatement, DoubleThrowExceptionsInReset) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  |     ON_CALL(mockStatement, next()).WillByDefault(Throw(Sqlite::StatementHasError(""))); | 
					
						
							|  |  |  |     ON_CALL(mockStatement, reset()).WillByDefault(Throw(Sqlite::StatementHasError(""))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THROW(mockStatement.values<int>(3, std::vector<std::tuple<int>>{{1}, {2}}), | 
					
						
							|  |  |  |                  Sqlite::StatementHasError); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, ThrowExceptionOnlyInReset) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  |     ON_CALL(mockStatement, reset()).WillByDefault(Throw(Sqlite::StatementHasError(""))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THROW(mockStatement.values<int>(3, std::vector<std::tuple<int>>{{1}, {2}}), | 
					
						
							|  |  |  |                  Sqlite::StatementHasError); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 10:41:11 +02:00
										 |  |  | TEST_F(SqliteStatement, ResetIfWriteIsThrowingException) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, bind(1, TypedEq<Utils::SmallStringView>("bar"))) | 
					
						
							|  |  |  |             .WillOnce(Throw(Sqlite::StatementIsBusy(""))); | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_ANY_THROW(mockStatement.write("bar")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, ResetIfWriteNamedIsThrowingException) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, bindingIndexForName(TypedEq<Utils::SmallStringView>("@foo"))) | 
					
						
							|  |  |  |             .WillOnce(Return(1)); | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, bind(1, TypedEq<Utils::SmallStringView>("bar"))) | 
					
						
							|  |  |  |             .WillOnce(Throw(Sqlite::StatementIsBusy(""))); | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_ANY_THROW(mockStatement.writeNamed("@foo", "bar")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SqliteStatement, ResetIfExecuteThrowsException) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MockSqliteStatement mockStatement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, next()).WillOnce(Throw(Sqlite::StatementIsBusy(""))); | 
					
						
							|  |  |  |     EXPECT_CALL(mockStatement, reset()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_ANY_THROW(mockStatement.execute()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | void SqliteStatement::SetUp() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-17 15:33:25 +02:00
										 |  |  |     database.execute("CREATE TABLE test(name TEXT UNIQUE, number NUMERIC, value NUMERIC)"); | 
					
						
							|  |  |  |     database.execute("INSERT INTO  test VALUES ('bar', 'blah', 1)"); | 
					
						
							|  |  |  |     database.execute("INSERT INTO  test VALUES ('foo', 23.3, 2)"); | 
					
						
							|  |  |  |     database.execute("INSERT INTO  test VALUES ('poo', 40, 3)"); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SqliteStatement::TearDown() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-09-18 11:23:09 +02:00
										 |  |  |     if (database.isOpen()) | 
					
						
							|  |  |  |         database.close(); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |