Sqlite: Fix handling of blob and blob views

Change-Id: I90c31307ff3299975f820e191085ba93ed8afe0f
Reviewed-by: Henning Gründl <henning.gruendl@qt.io>
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2021-04-15 14:18:26 +02:00
parent 040854c6f1
commit 2bf45ec998
5 changed files with 143 additions and 15 deletions

View File

@@ -277,6 +277,30 @@ void BaseStatement::bind(int index, const Value &value)
case ValueType::String:
bind(index, value.toStringView());
break;
case ValueType::Blob:
bind(index, value.toBlobView());
break;
case ValueType::Null:
bind(index, NullValue{});
break;
}
}
void BaseStatement::bind(int index, ValueView value)
{
switch (value.type()) {
case ValueType::Integer:
bind(index, value.toInteger());
break;
case ValueType::Float:
bind(index, value.toFloat());
break;
case ValueType::String:
bind(index, value.toStringView());
break;
case ValueType::Blob:
bind(index, value.toBlobView());
break;
case ValueType::Null:
bind(index, NullValue{});
break;

View File

@@ -91,6 +91,7 @@ public:
void bind(int index, Utils::span<const char *> values);
void bind(int index, Utils::SmallStringView value);
void bind(int index, const Value &value);
void bind(int index, ValueView value);
void bind(int index, BlobView blobView);
void bind(int index, uint value) { bind(index, static_cast<long long>(value)); }

View File

@@ -44,11 +44,11 @@ class NullValue
friend bool operator==(NullValue, NullValue) { return false; }
};
template<typename StringType>
template<typename StringType, typename BlobType>
class ValueBase
{
public:
using VariantType = Utils::variant<NullValue, long long, double, StringType, Blob>;
using VariantType = Utils::variant<NullValue, long long, double, StringType, BlobType>;
ValueBase() = default;
@@ -115,9 +115,12 @@ public:
BlobView toBlobView() const
{
const Blob &blob = Utils::get<int(ValueType::Blob)>(value);
return {blob.bytes};
const BlobType &blob = Utils::get<int(ValueType::Blob)>(value);
if constexpr (std::is_same_v<BlobType, Blob>) {
return {blob.bytes};
} else {
return blob;
}
}
explicit operator QVariant() const
{
@@ -245,7 +248,7 @@ public:
VariantType value;
};
class ValueView : public ValueBase<Utils::SmallStringView>
class ValueView : public ValueBase<Utils::SmallStringView, BlobView>
{
public:
explicit ValueView(ValueBase &&base)
@@ -259,9 +262,9 @@ public:
}
};
class Value : public ValueBase<Utils::SmallString>
class Value : public ValueBase<Utils::SmallString, Blob>
{
using Base = ValueBase<Utils::SmallString>;
using Base = ValueBase<Utils::SmallString, Blob>;
public:
using Base::Base;