Commit 0a3df845 authored by Marco Bubke's avatar Marco Bubke Committed by Tim Jenssen

Sqlite: Use string view as result value

String view is returning simply the string pointer and the size from the
database. In that way we remove useless copies to an intermediate data
type.

Change-Id: I3354061938c52df585e91054a97c900ae4cd39b3
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 507d99f0
......@@ -436,10 +436,10 @@ static StringType convertToTextForColumn(sqlite3_stmt *sqlStatment, int column)
case SQLITE_FLOAT:
case SQLITE3_TEXT: return textForColumn<StringType>(sqlStatment, column);
case SQLITE_BLOB:
case SQLITE_NULL: return {};
case SQLITE_NULL: break;
}
Q_UNREACHABLE();
return StringType{"", 0};
}
int BaseStatement::fetchIntValue(int column) const
......@@ -500,17 +500,12 @@ StringType BaseStatement::fetchValue(int column) const
return convertToTextForColumn<StringType>(m_compiledStatement.get(), column);
}
Utils::SmallString BaseStatement::fetchSmallStringValue(int column) const
Utils::SmallStringView BaseStatement::fetchSmallStringViewValue(int column) const
{
return fetchValue<Utils::SmallString>(column);
}
Utils::PathString BaseStatement::fetchPathStringValue(int column) const
{
return fetchValue<Utils::PathString>(column);
return fetchValue<Utils::SmallStringView>(column);
}
template SQLITE_EXPORT Utils::SmallStringView BaseStatement::fetchValue<Utils::SmallStringView>(int column) const;
template SQLITE_EXPORT Utils::SmallString BaseStatement::fetchValue<Utils::SmallString>(int column) const;
template SQLITE_EXPORT Utils::PathString BaseStatement::fetchValue<Utils::PathString>(int column) const;
} // namespace Sqlite
......@@ -64,8 +64,7 @@ public:
long fetchLongValue(int column) const;
long long fetchLongLongValue(int column) const;
double fetchDoubleValue(int column) const;
Utils::SmallString fetchSmallStringValue(int column) const;
Utils::PathString fetchPathStringValue(int column) const;
Utils::SmallStringView fetchSmallStringViewValue(int column) const;
template<typename Type>
Type fetchValue(int column) const;
int columnCount() const;
......@@ -142,6 +141,7 @@ template <> SQLITE_EXPORT int BaseStatement::fetchValue<int>(int column) const;
template <> SQLITE_EXPORT long BaseStatement::fetchValue<long>(int column) const;
template <> SQLITE_EXPORT long long BaseStatement::fetchValue<long long>(int column) const;
template <> SQLITE_EXPORT double BaseStatement::fetchValue<double>(int column) const;
extern template SQLITE_EXPORT Utils::SmallStringView BaseStatement::fetchValue<Utils::SmallStringView>(int column) const;
extern template SQLITE_EXPORT Utils::SmallString BaseStatement::fetchValue<Utils::SmallString>(int column) const;
extern template SQLITE_EXPORT Utils::PathString BaseStatement::fetchValue<Utils::PathString>(int column) const;
......@@ -323,14 +323,9 @@ private:
return statement.fetchDoubleValue(column);
}
operator Utils::SmallString()
operator Utils::SmallStringView()
{
return statement.fetchSmallStringValue(column);
}
operator Utils::PathString()
{
return statement.fetchPathStringValue(column);
return statement.fetchSmallStringViewValue(column);
}
StatementImplementation &statement;
......
......@@ -61,7 +61,8 @@ MATCHER_P3(HasValues, value1, value2, rowid,
statement.next();
return statement.fetchPathStringValue(0) == value1 && statement.fetchPathStringValue(1) == value2;
return statement.fetchSmallStringViewValue(0) == value1
&& statement.fetchSmallStringViewValue(1) == value2;
}
class SqliteStatement : public ::testing::Test
......@@ -76,7 +77,7 @@ protected:
struct Output
{
Output(Utils::SmallString name, Utils::SmallString number, long long value)
Output(Utils::SmallStringView name, Utils::SmallStringView number, long long value)
: name(name), number(number), value(value)
{}
......@@ -132,13 +133,15 @@ TEST_F(SqliteStatement, Value)
ASSERT_THAT(statement.fetchValue<int>(0), 0);
ASSERT_THAT(statement.fetchValue<int64_t>(0), 0);
ASSERT_THAT(statement.fetchValue<double>(0), 0.0);
ASSERT_THAT(statement.fetchSmallStringValue(0), "foo");
ASSERT_THAT(statement.fetchPathStringValue(0), "foo");
ASSERT_THAT(statement.fetchValue<Utils::SmallString>(0), "foo");
ASSERT_THAT(statement.fetchValue<Utils::PathString>(0), "foo");
ASSERT_THAT(statement.fetchSmallStringViewValue(0), "foo");
ASSERT_THAT(statement.fetchValue<int>(1), 23);
ASSERT_THAT(statement.fetchValue<int64_t>(1), 23);
ASSERT_THAT(statement.fetchValue<double>(1), 23.3);
ASSERT_THAT(statement.fetchSmallStringValue(1), "23.3");
ASSERT_THAT(statement.fetchPathStringValue(1), "23.3");
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");
}
TEST_F(SqliteStatement, ThrowNoValuesToFetchForNotSteppedStatement)
......@@ -212,7 +215,7 @@ TEST_F(SqliteStatement, BindString)
statement.next();
ASSERT_THAT(statement.fetchSmallStringValue(0), "foo");
ASSERT_THAT(statement.fetchSmallStringViewValue(0), "foo");
ASSERT_THAT(statement.fetchValue<double>(1), 23.3);
}
......@@ -223,7 +226,7 @@ TEST_F(SqliteStatement, BindInteger)
statement.bind(1, 40);
statement.next();
ASSERT_THAT(statement.fetchSmallStringValue(0),"poo");
ASSERT_THAT(statement.fetchSmallStringViewValue(0),"poo");
}
TEST_F(SqliteStatement, BindLongInteger)
......@@ -233,7 +236,7 @@ TEST_F(SqliteStatement, BindLongInteger)
statement.bind(1, int64_t(40));
statement.next();
ASSERT_THAT(statement.fetchSmallStringValue(0), "poo");
ASSERT_THAT(statement.fetchSmallStringViewValue(0), "poo");
}
TEST_F(SqliteStatement, BindDouble)
......@@ -243,7 +246,7 @@ TEST_F(SqliteStatement, BindDouble)
statement.bind(1, 23.3);
statement.next();
ASSERT_THAT(statement.fetchSmallStringValue(0), "foo");
ASSERT_THAT(statement.fetchSmallStringViewValue(0), "foo");
}
TEST_F(SqliteStatement, BindIntegerByParameter)
......@@ -253,7 +256,7 @@ TEST_F(SqliteStatement, BindIntegerByParameter)
statement.bind("@number", 40);
statement.next();
ASSERT_THAT(statement.fetchSmallStringValue(0), "poo");
ASSERT_THAT(statement.fetchSmallStringViewValue(0), "poo");
}
TEST_F(SqliteStatement, BindLongIntegerByParameter)
......@@ -263,7 +266,7 @@ TEST_F(SqliteStatement, BindLongIntegerByParameter)
statement.bind("@number", int64_t(40));
statement.next();
ASSERT_THAT(statement.fetchSmallStringValue(0), "poo");
ASSERT_THAT(statement.fetchSmallStringViewValue(0), "poo");
}
TEST_F(SqliteStatement, BindDoubleByIndex)
......@@ -273,7 +276,7 @@ TEST_F(SqliteStatement, BindDoubleByIndex)
statement.bind(statement.bindingIndexForName("@number"), 23.3);
statement.next();
ASSERT_THAT(statement.fetchSmallStringValue(0), "foo");
ASSERT_THAT(statement.fetchSmallStringViewValue(0), "foo");
}
TEST_F(SqliteStatement, BindIndexIsZeroIsThrowingBindingIndexIsOutOfBound)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment