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-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>
# include <utf8string.h>
2015-06-01 18:51:55 +02:00
# include <QByteArray>
2015-06-16 12:38:04 +02:00
# include <QDir>
2015-06-01 18:51:55 +02:00
# include <QMap>
# include <QString>
2015-06-16 12:38:04 +02:00
# include <QStringList>
# include <QVariant>
2015-06-01 18:51:55 +02:00
namespace {
2017-07-26 16:02:24 +02:00
using Sqlite : : SqliteException ;
2017-07-26 18:43:07 +02:00
using Sqlite : : SqliteDatabase ;
2017-07-26 16:02:24 +02:00
using Sqlite : : SqliteReadStatement ;
using Sqlite : : SqliteReadWriteStatement ;
using Sqlite : : SqliteWriteStatement ;
2015-06-01 18:51:55 +02:00
class SqliteStatement : public : : testing : : Test
{
protected :
void SetUp ( ) override ;
void TearDown ( ) override ;
2017-07-26 18:43:07 +02:00
SqliteDatabase database ;
2015-06-01 18:51:55 +02:00
} ;
TEST_F ( SqliteStatement , PrepareFailure )
{
2017-07-26 18:43:07 +02:00
ASSERT_THROW ( SqliteReadStatement ( Utf8StringLiteral ( " blah blah blah " ) , database ) , SqliteException ) ;
ASSERT_THROW ( SqliteWriteStatement ( Utf8StringLiteral ( " blah blah blah " ) , database ) , SqliteException ) ;
ASSERT_THROW ( SqliteReadStatement ( Utf8StringLiteral ( " INSERT INTO test(name, number) VALUES (?, ?) " ) , database ) , SqliteException ) ;
ASSERT_THROW ( SqliteWriteStatement ( Utf8StringLiteral ( " SELECT name, number FROM test ' " ) , database ) , SqliteException ) ;
2015-06-01 18:51:55 +02:00
}
TEST_F ( SqliteStatement , CountRows )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT * FROM test " ) , database ) ;
2015-06-01 18:51:55 +02:00
int nextCount = 0 ;
while ( statement . next ( ) )
+ + nextCount ;
2017-07-26 18:43:07 +02:00
int sqlCount = SqliteReadStatement : : toValue < int > ( Utf8StringLiteral ( " SELECT count(*) FROM test " ) , database ) ;
2015-06-01 18:51:55 +02:00
ASSERT_THAT ( nextCount , sqlCount ) ;
}
TEST_F ( SqliteStatement , Value )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test ORDER BY name " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . next ( ) ;
ASSERT_THAT ( statement . value < QVariant > ( 1 ) . type ( ) , QVariant : : ByteArray ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < int > ( 0 ) , 0 ) ;
ASSERT_THAT ( statement . value < qint64 > ( 0 ) , 0 ) ;
ASSERT_THAT ( statement . value < double > ( 0 ) , 0.0 ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " foo " ) ) ;
ASSERT_THAT ( statement . value < Utf8String > ( 0 ) , Utf8StringLiteral ( " foo " ) ) ;
ASSERT_THAT ( statement . value < QVariant > ( 0 ) , QVariant : : fromValue ( QStringLiteral ( " foo " ) ) ) ;
ASSERT_THAT ( statement . value < QVariant > ( 0 ) . type ( ) , QVariant : : String ) ;
ASSERT_THAT ( statement . value < int > ( 1 ) , 23 ) ;
ASSERT_THAT ( statement . value < qint64 > ( 1 ) , 23 ) ;
ASSERT_THAT ( statement . value < double > ( 1 ) , 23.3 ) ;
ASSERT_THAT ( statement . value < QString > ( 1 ) , QStringLiteral ( " 23.3 " ) ) ;
ASSERT_THAT ( statement . value < Utf8String > ( 1 ) , Utf8StringLiteral ( " 23.3 " ) ) ;
ASSERT_THAT ( statement . value < QVariant > ( 1 ) , QVariant : : fromValue ( 23.3 ) ) ;
ASSERT_THAT ( statement . value < QVariant > ( 1 ) . type ( ) , QVariant : : Double ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QVariant > ( 1 ) . type ( ) , QVariant : : LongLong ) ;
}
TEST_F ( SqliteStatement , ValueFailure )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test " ) , database ) ;
2015-06-01 18:51:55 +02:00
ASSERT_THROW ( statement . value < int > ( 0 ) , SqliteException ) ;
statement . reset ( ) ;
while ( statement . next ( ) ) { }
ASSERT_THROW ( statement . value < int > ( 0 ) , SqliteException ) ;
statement . reset ( ) ;
statement . next ( ) ;
ASSERT_THROW ( statement . value < int > ( - 1 ) , SqliteException ) ;
ASSERT_THROW ( statement . value < int > ( 2 ) , SqliteException ) ;
}
TEST_F ( SqliteStatement , ToIntergerValue )
{
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( SqliteReadStatement : : toValue < int > ( Utf8StringLiteral ( " SELECT number FROM test WHERE name='foo' " ) , database ) , 23 ) ;
2015-06-01 18:51:55 +02:00
}
TEST_F ( SqliteStatement , ToLongIntergerValue )
{
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( SqliteReadStatement : : toValue < qint64 > ( Utf8StringLiteral ( " SELECT number FROM test WHERE name='foo' " ) , database ) , 23LL ) ;
2015-06-01 18:51:55 +02:00
}
TEST_F ( SqliteStatement , ToDoubleValue )
{
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( SqliteReadStatement : : toValue < double > ( Utf8StringLiteral ( " SELECT number FROM test WHERE name='foo' " ) , database ) , 23.3 ) ;
2015-06-01 18:51:55 +02:00
}
TEST_F ( SqliteStatement , ToQStringValue )
{
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( SqliteReadStatement : : toValue < QString > ( Utf8StringLiteral ( " SELECT name FROM test WHERE name='foo' " ) , database ) , QStringLiteral ( " foo " ) ) ;
2015-06-01 18:51:55 +02:00
}
TEST_F ( SqliteStatement , ToUtf8StringValue )
{
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( SqliteReadStatement : : toValue < Utf8String > ( Utf8StringLiteral ( " SELECT name FROM test WHERE name='foo' " ) , database ) , Utf8StringLiteral ( " foo " ) ) ;
2015-06-01 18:51:55 +02:00
}
TEST_F ( SqliteStatement , ToQByteArrayValueIsNull )
{
2017-07-26 18:43:07 +02:00
ASSERT_TRUE ( SqliteReadStatement : : toValue < QByteArray > ( Utf8StringLiteral ( " SELECT name FROM test WHERE name='foo' " ) , database ) . isNull ( ) ) ;
2015-06-01 18:51:55 +02:00
}
TEST_F ( SqliteStatement , ToQVariantValue )
{
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( SqliteReadStatement : : toValue < QVariant > ( Utf8StringLiteral ( " SELECT name FROM test WHERE name='foo' " ) , database ) , QVariant : : fromValue ( QStringLiteral ( " foo " ) ) ) ;
2015-06-01 18:51:55 +02:00
}
TEST_F ( SqliteStatement , Utf8Values )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test ORDER by name " ) , database ) ;
2015-06-01 18:51:55 +02:00
Utf8StringVector values = statement . values < Utf8StringVector > ( ) ;
ASSERT_THAT ( values . count ( ) , 3 ) ;
ASSERT_THAT ( values . at ( 0 ) , Utf8StringLiteral ( " bar " ) ) ;
ASSERT_THAT ( values . at ( 1 ) , Utf8StringLiteral ( " foo " ) ) ;
ASSERT_THAT ( values . at ( 2 ) , Utf8StringLiteral ( " poo " ) ) ;
}
TEST_F ( SqliteStatement , DoubleValues )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test ORDER by name " ) , database ) ;
2015-06-01 18:51:55 +02:00
QVector < double > values = statement . values < QVector < double > > ( 1 ) ;
ASSERT_THAT ( values . count ( ) , 3 ) ;
ASSERT_THAT ( values . at ( 0 ) , 0.0 ) ;
ASSERT_THAT ( values . at ( 1 ) , 23.3 ) ;
ASSERT_THAT ( values . at ( 2 ) , 40.0 ) ;
}
TEST_F ( SqliteStatement , QVariantValues )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test ORDER by name " ) , database ) ;
2015-06-01 18:51:55 +02:00
QVector < QVariant > values = statement . values < QVector < QVariant > > ( QVector < int > ( ) < < 0 < < 1 ) ;
ASSERT_THAT ( values . count ( ) , 6 ) ;
ASSERT_THAT ( values . at ( 0 ) , QVariant : : fromValue ( QStringLiteral ( " bar " ) ) ) ;
ASSERT_THAT ( values . at ( 1 ) , QVariant : : fromValue ( QByteArray : : fromHex ( " 0500 " ) ) ) ;
ASSERT_THAT ( values . at ( 2 ) , QVariant : : fromValue ( QStringLiteral ( " foo " ) ) ) ;
ASSERT_THAT ( values . at ( 3 ) , QVariant : : fromValue ( 23.3 ) ) ;
ASSERT_THAT ( values . at ( 4 ) , QVariant : : fromValue ( QStringLiteral ( " poo " ) ) ) ;
ASSERT_THAT ( values . at ( 5 ) , QVariant : : fromValue ( 40 ) ) ;
}
TEST_F ( SqliteStatement , RowColumnValueMapCountForValidRow )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE rowid=1 " ) , database ) ;
2015-06-01 18:51:55 +02:00
QMap < QString , QVariant > values = statement . rowColumnValueMap ( ) ;
ASSERT_THAT ( values . count ( ) , 2 ) ;
}
TEST_F ( SqliteStatement , RowColumnValueMapCountForInvalidRow )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE rowid=100 " ) , database ) ;
2015-06-01 18:51:55 +02:00
QMap < QString , QVariant > values = statement . rowColumnValueMap ( ) ;
ASSERT_THAT ( values . count ( ) , 0 ) ;
}
TEST_F ( SqliteStatement , RowColumnValueMapValues )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE rowid=2 " ) , database ) ;
2015-06-01 18:51:55 +02:00
QMap < QString , QVariant > values = statement . rowColumnValueMap ( ) ;
ASSERT_THAT ( values . value ( QStringLiteral ( " name " ) ) . toString ( ) , QStringLiteral ( " foo " ) ) ;
ASSERT_THAT ( values . value ( QStringLiteral ( " number " ) ) . toDouble ( ) , 23.3 ) ;
}
TEST_F ( SqliteStatement , TwoColumnValueMapCount )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test " ) , database ) ;
2015-06-01 18:51:55 +02:00
QMap < QString , QVariant > values = statement . twoColumnValueMap ( ) ;
ASSERT_THAT ( values . count ( ) , 3 ) ;
}
TEST_F ( SqliteStatement , TwoColumnValueMapValues )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test " ) , database ) ;
2015-06-01 18:51:55 +02:00
QMap < QString , QVariant > values = statement . twoColumnValueMap ( ) ;
ASSERT_THAT ( values . value ( QStringLiteral ( " foo " ) ) . toDouble ( ) , 23.3 ) ;
}
TEST_F ( SqliteStatement , ValuesFailure )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test " ) , database ) ;
2015-06-01 18:51:55 +02:00
ASSERT_THROW ( statement . values < QVector < QVariant > > ( QVector < int > ( ) < < 1 < < 2 ) ; , SqliteException ) ;
ASSERT_THROW ( statement . values < QVector < QVariant > > ( QVector < int > ( ) < < - 1 < < 1 ) ; , SqliteException ) ;
}
TEST_F ( SqliteStatement , ColumnNames )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test " ) , database ) ;
2015-06-01 18:51:55 +02:00
Utf8StringVector columnNames = statement . columnNames ( ) ;
ASSERT_THAT ( columnNames . count ( ) , statement . columnCount ( ) ) ;
ASSERT_THAT ( columnNames . at ( 0 ) , Utf8StringLiteral ( " name " ) ) ;
ASSERT_THAT ( columnNames . at ( 1 ) , Utf8StringLiteral ( " number " ) ) ;
}
TEST_F ( SqliteStatement , BindQString )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE name=? " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( 1 , QStringLiteral ( " foo " ) ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " foo " ) ) ;
ASSERT_THAT ( statement . value < double > ( 1 ) , 23.3 ) ;
}
TEST_F ( SqliteStatement , BindInteger )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=? " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( 1 , 40 ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " poo " ) ) ;
}
TEST_F ( SqliteStatement , BindLongInteger )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=? " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( 1 , qint64 ( 40 ) ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " poo " ) ) ;
}
TEST_F ( SqliteStatement , BindByteArray )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=? " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( 1 , QByteArray : : fromHex ( " 0500 " ) ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " bar " ) ) ;
}
TEST_F ( SqliteStatement , BindDouble )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=? " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( 1 , 23.3 ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " foo " ) ) ;
}
TEST_F ( SqliteStatement , BindIntergerQVariant )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=? " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( 1 , QVariant : : fromValue ( 40 ) ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " poo " ) ) ;
}
TEST_F ( SqliteStatement , BindLongIntergerQVariant )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=? " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( 1 , QVariant : : fromValue ( qint64 ( 40 ) ) ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " poo " ) ) ;
}
TEST_F ( SqliteStatement , BindDoubleQVariant )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=? " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( 1 , QVariant : : fromValue ( 23.3 ) ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " foo " ) ) ;
}
TEST_F ( SqliteStatement , BindByteArrayQVariant )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=? " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( 1 , QVariant : : fromValue ( QByteArray : : fromHex ( " 0500 " ) ) ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " bar " ) ) ;
}
TEST_F ( SqliteStatement , BindIntegerByParameter )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=@number " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( Utf8StringLiteral ( " @number " ) , 40 ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " poo " ) ) ;
}
TEST_F ( SqliteStatement , BindLongIntegerByParameter )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=@number " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( Utf8StringLiteral ( " @number " ) , qint64 ( 40 ) ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " poo " ) ) ;
}
TEST_F ( SqliteStatement , BindByteArrayByParameter )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=@number " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( Utf8StringLiteral ( " @number " ) , QByteArray : : fromHex ( " 0500 " ) ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " bar " ) ) ;
}
TEST_F ( SqliteStatement , BindDoubleByIndex )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=@number " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( statement . bindingIndexForName ( Utf8StringLiteral ( " @number " ) ) , 23.3 ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " foo " ) ) ;
}
TEST_F ( SqliteStatement , BindQVariantByIndex )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=@number " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . bind ( statement . bindingIndexForName ( Utf8StringLiteral ( " @number " ) ) , QVariant : : fromValue ( ( 40 ) ) ) ;
statement . next ( ) ;
ASSERT_THAT ( statement . value < QString > ( 0 ) , QStringLiteral ( " poo " ) ) ;
}
TEST_F ( SqliteStatement , BindFailure )
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE number=@number " ) , database ) ;
2015-06-01 18:51:55 +02:00
ASSERT_THROW ( statement . bind ( 0 , 40 ) , SqliteException ) ;
ASSERT_THROW ( statement . bind ( 2 , 40 ) , SqliteException ) ;
ASSERT_THROW ( statement . bind ( Utf8StringLiteral ( " @name " ) , 40 ) , SqliteException ) ;
}
TEST_F ( SqliteStatement , RequestBindingNamesFromStatement )
{
Utf8StringVector expectedValues ( { Utf8StringLiteral ( " name " ) , Utf8StringLiteral ( " number " ) , Utf8StringLiteral ( " id " ) } ) ;
2017-07-26 18:43:07 +02:00
SqliteWriteStatement statement ( Utf8StringLiteral ( " UPDATE test SET name=@name, number=@number WHERE rowid=@id " ) , database ) ;
2015-06-01 18:51:55 +02:00
ASSERT_THAT ( statement . bindingColumnNames ( ) , expectedValues ) ;
}
TEST_F ( SqliteStatement , WriteUpdateWidthUnamedParameter )
{
{
2017-07-26 18:43:07 +02:00
int startTotalCount = database . totalChangesCount ( ) ;
2015-06-01 18:51:55 +02:00
RowDictionary firstValueMap ;
firstValueMap . insert ( Utf8StringLiteral ( " name " ) , QStringLiteral ( " foo " ) ) ;
firstValueMap . insert ( Utf8StringLiteral ( " number " ) , 66.6 ) ;
RowDictionary secondValueMap ;
secondValueMap . insert ( Utf8StringLiteral ( " name " ) , QStringLiteral ( " bar " ) ) ;
secondValueMap . insert ( Utf8StringLiteral ( " number " ) , 77.7 ) ;
2017-07-26 18:43:07 +02:00
SqliteWriteStatement statement ( Utf8StringLiteral ( " UPDATE test SET number=? WHERE name=? " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . setBindingColumnNames ( Utf8StringVector ( ) < < Utf8StringLiteral ( " number " ) < < Utf8StringLiteral ( " name " ) ) ;
statement . write ( firstValueMap ) ;
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( database . totalChangesCount ( ) , startTotalCount + 1 ) ;
2015-06-01 18:51:55 +02:00
statement . write ( firstValueMap ) ;
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( database . totalChangesCount ( ) , startTotalCount + 2 ) ;
2015-06-01 18:51:55 +02:00
statement . write ( secondValueMap ) ;
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( database . totalChangesCount ( ) , startTotalCount + 3 ) ;
2015-06-01 18:51:55 +02:00
}
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE name='foo' " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . next ( ) ;
ASSERT_THAT ( statement . value < double > ( 1 ) , 66.6 ) ;
}
}
TEST_F ( SqliteStatement , WriteUpdateWidthNamedParameter )
{
{
2017-07-26 18:43:07 +02:00
int startTotalCount = database . totalChangesCount ( ) ;
2015-06-01 18:51:55 +02:00
RowDictionary firstValueMap ;
firstValueMap . insert ( Utf8StringLiteral ( " name " ) , QStringLiteral ( " foo " ) ) ;
firstValueMap . insert ( Utf8StringLiteral ( " number " ) , 99.9 ) ;
2017-07-26 18:43:07 +02:00
SqliteWriteStatement statement ( Utf8StringLiteral ( " UPDATE test SET number=@number WHERE name=@name " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . write ( firstValueMap ) ;
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( database . totalChangesCount ( ) , startTotalCount + 1 ) ;
2015-06-01 18:51:55 +02:00
}
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE name='foo' " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . next ( ) ;
ASSERT_THAT ( statement . value < double > ( 1 ) , 99.9 ) ;
}
}
TEST_F ( SqliteStatement , WriteUpdateWidthNamedParameterAndBindNotAllParameter )
{
{
2017-07-26 18:43:07 +02:00
int startTotalCount = database . totalChangesCount ( ) ;
2015-06-01 18:51:55 +02:00
RowDictionary firstValueMap ;
firstValueMap . insert ( Utf8StringLiteral ( " name " ) , QStringLiteral ( " foo " ) ) ;
2017-07-26 18:43:07 +02:00
SqliteWriteStatement statement ( Utf8StringLiteral ( " UPDATE test SET number=@number WHERE name=@name " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . writeUnchecked ( firstValueMap ) ;
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( database . totalChangesCount ( ) , startTotalCount + 1 ) ;
2015-06-01 18:51:55 +02:00
}
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE name='foo' " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . next ( ) ;
ASSERT_THAT ( statement . value < double > ( 1 ) , 0.0 ) ;
}
}
TEST_F ( SqliteStatement , WriteInsert )
{
{
2017-07-26 18:43:07 +02:00
int startTotalCount = database . totalChangesCount ( ) ;
2015-06-01 18:51:55 +02:00
RowDictionary valueMap ;
valueMap . insert ( Utf8StringLiteral ( " name " ) , QStringLiteral ( " jane " ) ) ;
valueMap . insert ( Utf8StringLiteral ( " number " ) , 232.3 ) ;
2017-07-26 18:43:07 +02:00
SqliteWriteStatement statement ( Utf8StringLiteral ( " INSERT OR IGNORE INTO test(name, number) VALUES ( ? , ? ) " ), database) ;
2015-06-01 18:51:55 +02:00
statement . setBindingColumnNames ( Utf8StringVector ( ) < < Utf8StringLiteral ( " name " ) < < Utf8StringLiteral ( " number " ) ) ;
statement . write ( valueMap ) ;
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( database . totalChangesCount ( ) , startTotalCount + 1 ) ;
2015-06-01 18:51:55 +02:00
statement . write ( valueMap ) ;
2017-07-26 18:43:07 +02:00
ASSERT_THAT ( database . totalChangesCount ( ) , startTotalCount + 1 ) ;
2015-06-01 18:51:55 +02:00
}
{
2017-07-26 18:43:07 +02:00
SqliteReadStatement statement ( Utf8StringLiteral ( " SELECT name, number FROM test WHERE name='jane' " ) , database ) ;
2015-06-01 18:51:55 +02:00
statement . next ( ) ;
ASSERT_THAT ( statement . value < double > ( 1 ) , 232.3 ) ;
}
}
TEST_F ( SqliteStatement , WriteFailure )
{
{
RowDictionary valueMap ;
valueMap . insert ( Utf8StringLiteral ( " name " ) , QStringLiteral ( " foo " ) ) ;
valueMap . insert ( Utf8StringLiteral ( " number " ) , 323.3 ) ;
2017-07-26 18:43:07 +02:00
SqliteWriteStatement statement ( Utf8StringLiteral ( " INSERT INTO test(name, number) VALUES ( ? , ? ) " ), database) ;
2015-06-01 18:51:55 +02:00
statement . setBindingColumnNames ( Utf8StringVector ( ) < < Utf8StringLiteral ( " name " ) < < Utf8StringLiteral ( " number " ) ) ;
ASSERT_THROW ( statement . write ( valueMap ) , SqliteException ) ;
}
{
RowDictionary valueMap ;
valueMap . insert ( Utf8StringLiteral ( " name " ) , QStringLiteral ( " bar " ) ) ;
2017-07-26 18:43:07 +02:00
SqliteWriteStatement statement ( Utf8StringLiteral ( " INSERT OR IGNORE INTO test(name, number) VALUES ( ? , ? ) " ), database) ;
2015-06-01 18:51:55 +02:00
statement . setBindingColumnNames ( Utf8StringVector ( ) < < Utf8StringLiteral ( " name " ) < < Utf8StringLiteral ( " number " ) ) ;
ASSERT_THROW ( statement . write ( valueMap ) , SqliteException ) ;
}
}
TEST_F ( SqliteStatement , ClosedDatabase )
{
2017-07-26 18:43:07 +02:00
database . close ( ) ;
ASSERT_THROW ( SqliteWriteStatement ( Utf8StringLiteral ( " INSERT INTO test(name, number) VALUES (?, ?) " ) , database ) , SqliteException ) ;
ASSERT_THROW ( SqliteReadStatement ( Utf8StringLiteral ( " SELECT * FROM test " ) , database ) , SqliteException ) ;
ASSERT_THROW ( SqliteReadWriteStatement ( Utf8StringLiteral ( " INSERT INTO test(name, number) VALUES (?, ?) " ) , database ) , SqliteException ) ;
database . open ( QDir : : tempPath ( ) + QStringLiteral ( " /SqliteStatementTest.db " ) ) ;
2015-06-01 18:51:55 +02:00
}
void SqliteStatement : : SetUp ( )
{
2017-07-26 18:43:07 +02:00
database . setJournalMode ( JournalMode : : Memory ) ;
database . open ( QStringLiteral ( " :memory: " ) ) ;
SqliteWriteStatement ( Utf8StringLiteral ( " CREATE TABLE test(name TEXT UNIQUE, number NUMERIC) " ) , database ) . step ( ) ;
SqliteWriteStatement ( Utf8StringLiteral ( " INSERT INTO test VALUES ('bar', x'0500') " ) , database ) . step ( ) ;
SqliteWriteStatement ( Utf8StringLiteral ( " INSERT INTO test VALUES ('foo', 23.3) " ) , database ) . step ( ) ;
SqliteWriteStatement ( Utf8StringLiteral ( " INSERT INTO test VALUES ('poo', 40) " ) , database ) . step ( ) ;
2015-06-01 18:51:55 +02:00
}
void SqliteStatement : : TearDown ( )
{
2017-07-26 18:43:07 +02:00
database . close ( ) ;
2015-06-01 18:51:55 +02:00
}
}