Sqlite: Make the statement conversion operator id aware

Before you had to use an constructor which is has an integer as
parameter. Like

struct Foo
{
  Foo{long long id} : id{id} {}
  Foo{TypeId id} : id{id} {}

  TypeId id;
}

Now you can write:

struct Foo
{
  TypeId id;
}

With C++ 20 we can even remove more contructors.

Change-Id: I374505a037a71339b672f5f3a57b06dcf443b4bf
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marco Bubke
2022-07-19 14:33:00 +02:00
parent fdcb02d5e2
commit 93d0b5a1d3
15 changed files with 194 additions and 198 deletions

View File

@@ -96,10 +96,10 @@ public:
void bind(int index, ValueView value);
void bind(int index, BlobView blobView);
template<auto Type, typename InternalIntergerType>
void bind(int index, BasicId<Type, InternalIntergerType> id)
template<typename Type, typename = std::enable_if_t<Type::IsBasicId::value>>
void bind(int index, Type id)
{
bind(index, id.id);
bind(index, &id);
}
void bind(int index, uint value) { bind(index, static_cast<long long>(value)); }
@@ -452,14 +452,24 @@ private:
, column(column)
{}
operator int() { return statement.fetchIntValue(column); }
operator long() { return statement.fetchLongValue(column); }
operator long long() { return statement.fetchLongLongValue(column); }
operator double() { return statement.fetchDoubleValue(column); }
operator int() const { return statement.fetchIntValue(column); }
operator long() const { return statement.fetchLongValue(column); }
operator long long() const { return statement.fetchLongLongValue(column); }
operator double() const { return statement.fetchDoubleValue(column); }
operator Utils::SmallStringView() { return statement.fetchSmallStringViewValue(column); }
operator BlobView() { return statement.fetchBlobValue(column); }
operator ValueView() { return statement.fetchValueView(column); }
template<typename ConversionType,
typename = std::enable_if_t<ConversionType::IsBasicId::value>>
constexpr operator ConversionType()
{
if constexpr (std::is_same_v<typename ConversionType::DatabaseType, int>)
return ConversionType::create(statement.fetchIntValue(column));
else
return ConversionType::create(statement.fetchLongLongValue(column));
}
StatementImplementation &statement;
int column;
};
@@ -491,7 +501,7 @@ private:
template<typename ResultType, int... ColumnIndices>
ResultType createValue(std::integer_sequence<int, ColumnIndices...>)
{
return ResultType{ValueGetter(*this, ColumnIndices)...};
return ResultType(ValueGetter(*this, ColumnIndices)...);
}
template<typename ResultType>

View File

@@ -27,6 +27,7 @@
#include <utils/span.h>
#include <type_traits>
#include <vector>
namespace Sqlite {
@@ -35,15 +36,19 @@ template<auto Type, typename InternalIntegerType = long long>
class BasicId
{
public:
using IsBasicId = std::true_type;
using DatabaseType = InternalIntegerType;
constexpr explicit BasicId() = default;
constexpr BasicId(const char *) = delete;
constexpr explicit BasicId(InternalIntegerType id)
: id{id}
{}
static constexpr BasicId create(InternalIntegerType idNumber)
{
BasicId id;
id.id = idNumber;
return id;
}
constexpr friend bool operator==(BasicId first, BasicId second)
{
@@ -71,7 +76,7 @@ public:
InternalIntegerType operator&() const { return id; }
public:
private:
InternalIntegerType id = -1;
};

View File

@@ -41,12 +41,6 @@ public:
, lastModified{lastModified}
{}
explicit FileStatus(int sourceId, long long size, long long lastModified)
: sourceId{sourceId}
, size{size}
, lastModified{lastModified}
{}
friend bool operator==(const FileStatus &first, const FileStatus &second)
{
return first.sourceId == second.sourceId && first.size == second.size

View File

@@ -519,14 +519,6 @@ private:
, importedTypeNameId{std::move(importedTypeNameId)}
{}
explicit PropertyDeclaration(long long typeId,
long long propertyDeclarationId,
long long importedTypeNameId)
: typeId{typeId}
, propertyDeclarationId{propertyDeclarationId}
, importedTypeNameId{importedTypeNameId}
{}
friend bool operator<(const PropertyDeclaration &first, const PropertyDeclaration &second)
{
return std::tie(first.typeId, first.propertyDeclarationId)
@@ -876,15 +868,15 @@ private:
void handleAliasPropertyDeclarationsWithPropertyType(
TypeId typeId, AliasPropertyDeclarations &relinkableAliasPropertyDeclarations)
{
auto callback = [&](long long typeId,
long long propertyDeclarationId,
long long propertyImportedTypeNameId,
long long aliasPropertyDeclarationId,
long long aliasPropertyDeclarationTailId) {
auto callback = [&](TypeId typeId,
PropertyDeclarationId propertyDeclarationId,
ImportedTypeNameId propertyImportedTypeNameId,
PropertyDeclarationId aliasPropertyDeclarationId,
PropertyDeclarationId aliasPropertyDeclarationTailId) {
auto aliasPropertyName = selectPropertyNameStatement.template value<Utils::SmallString>(
aliasPropertyDeclarationId);
Utils::SmallString aliasPropertyNameTail;
if (aliasPropertyDeclarationTailId != -1)
if (aliasPropertyDeclarationTailId)
aliasPropertyNameTail = selectPropertyNameStatement.template value<Utils::SmallString>(
aliasPropertyDeclarationTailId);
@@ -901,7 +893,7 @@ private:
};
selectAliasPropertiesDeclarationForPropertiesWithTypeIdStatement.readCallback(callback,
&typeId);
typeId);
}
void handlePropertyDeclarationWithPropertyType(TypeId typeId,
@@ -913,8 +905,8 @@ private:
void handlePrototypes(TypeId prototypeId, Prototypes &relinkablePrototypes)
{
auto callback = [&](long long typeId, long long prototypeNameId) {
relinkablePrototypes.emplace_back(TypeId{typeId}, ImportedTypeNameId{prototypeNameId});
auto callback = [&](TypeId typeId, ImportedTypeNameId prototypeNameId) {
relinkablePrototypes.emplace_back(typeId, prototypeNameId);
return Sqlite::CallbackControl::Continue;
};
@@ -1017,9 +1009,9 @@ private:
Prototypes &relinkablePrototypes,
TypeIds &deletedTypeIds)
{
auto callback = [&](long long typeId) {
deletedTypeIds.push_back(TypeId{typeId});
deleteType(TypeId{typeId},
auto callback = [&](TypeId typeId) {
deletedTypeIds.push_back(typeId);
deleteType(typeId,
relinkableAliasPropertyDeclarations,
relinkablePropertyDeclarations,
relinkablePrototypes);
@@ -1030,7 +1022,7 @@ private:
toIntegers(updatedSourceIds),
toIntegers(updatedTypeIds));
for (TypeId typeIdToBeDeleted : typeIdsToBeDeleted)
callback(&typeIdToBeDeleted);
callback(typeIdToBeDeleted);
}
void relink(AliasPropertyDeclarations &relinkableAliasPropertyDeclarations,
@@ -1207,9 +1199,9 @@ private:
SourceId sourceId,
TypeId typeId)
{
auto callback = [&](long long propertyDeclarationId) {
auto callback = [&](PropertyDeclarationId propertyDeclarationId) {
insertedAliasPropertyDeclarations.emplace_back(typeId,
PropertyDeclarationId{propertyDeclarationId},
propertyDeclarationId,
fetchImportedTypeNameId(value.typeName,
sourceId),
value.aliasPropertyName,
@@ -1365,8 +1357,8 @@ private:
{
public:
explicit AliasPropertyDeclarationView(Utils::SmallStringView name,
long long id,
long long aliasId)
PropertyDeclarationId id,
PropertyDeclarationId aliasId)
: name{name}
, id{id}
, aliasId{aliasId}
@@ -1481,12 +1473,12 @@ private:
auto insert = [&](const Storage::Synchronization::Import &import) {
insertDocumentImport(import, importKind, import.moduleId, ModuleExportedImportId{});
auto callback = [&](int exportedModuleId,
auto callback = [&](ModuleId exportedModuleId,
int majorVersion,
int minorVersion,
long long moduleExportedImportId) {
ModuleExportedImportId moduleExportedImportId) {
Storage::Synchronization::Import additionImport{
ModuleId{exportedModuleId},
exportedModuleId,
Storage::Synchronization::Version{majorVersion, minorVersion},
import.sourceId};
@@ -1497,7 +1489,7 @@ private:
insertDocumentImport(additionImport,
exportedImportKind,
import.moduleId,
ModuleExportedImportId{moduleExportedImportId});
moduleExportedImportId);
return Sqlite::CallbackControl::Continue;
};
@@ -1812,7 +1804,7 @@ private:
class TypeWithDefaultPropertyView
{
public:
TypeWithDefaultPropertyView(long long typeId, long long defaultPropertyId)
TypeWithDefaultPropertyView(TypeId typeId, PropertyDeclarationId defaultPropertyId)
: typeId{typeId}
, defaultPropertyId{defaultPropertyId}
{}
@@ -1893,8 +1885,8 @@ private:
void checkForPrototypeChainCycle(TypeId typeId) const
{
auto callback = [=](long long currentTypeId) {
if (typeId == TypeId{currentTypeId})
auto callback = [=](TypeId currentTypeId) {
if (typeId == currentTypeId)
throw PrototypeChainCycle{};
return Sqlite::CallbackControl::Continue;
@@ -1905,8 +1897,8 @@ private:
void checkForAliasChainCycle(PropertyDeclarationId propertyDeclarationId) const
{
auto callback = [=](long long currentPropertyDeclarationId) {
if (propertyDeclarationId == PropertyDeclarationId{currentPropertyDeclarationId})
auto callback = [=](PropertyDeclarationId currentPropertyDeclarationId) {
if (propertyDeclarationId == currentPropertyDeclarationId)
throw AliasChainCycle{};
return Sqlite::CallbackControl::Continue;
@@ -2032,8 +2024,8 @@ private:
class FetchPropertyDeclarationResult
{
public:
FetchPropertyDeclarationResult(long long propertyTypeId,
long long propertyDeclarationId,
FetchPropertyDeclarationResult(TypeId propertyTypeId,
PropertyDeclarationId propertyDeclarationId,
long long propertyTraits)
: propertyTypeId{propertyTypeId}
, propertyDeclarationId{propertyDeclarationId}
@@ -2087,14 +2079,14 @@ private:
{
insertIntoSourceContextsStatement.write(sourceContextPath);
return SourceContextId(database.lastInsertedRowId());
return SourceContextId::create(database.lastInsertedRowId());
}
SourceId writeSourceId(SourceContextId sourceContextId, Utils::SmallStringView sourceName)
{
insertIntoSourcesStatement.write(sourceContextId, sourceName);
return SourceId(database.lastInsertedRowId());
return SourceId::create(database.lastInsertedRowId());
}
SourceId readSourceId(SourceContextId sourceContextId, Utils::SmallStringView sourceName)
@@ -2121,7 +2113,7 @@ private:
auto callback = [&](Utils::SmallStringView name,
Utils::SmallStringView returnType,
long long functionDeclarationId) {
FunctionDeclarationId functionDeclarationId) {
auto &functionDeclaration = functionDeclarations.emplace_back(name, returnType);
functionDeclaration.parameters = selectFunctionParameterDeclarationsStatement.template values<
Storage::Synchronization::ParameterDeclaration>(8, functionDeclarationId);
@@ -2138,7 +2130,7 @@ private:
{
Storage::Synchronization::SignalDeclarations signalDeclarations;
auto callback = [&](Utils::SmallStringView name, long long signalDeclarationId) {
auto callback = [&](Utils::SmallStringView name, SignalDeclarationId signalDeclarationId) {
auto &signalDeclaration = signalDeclarations.emplace_back(name);
signalDeclaration.parameters = selectSignalParameterDeclarationsStatement.template values<
Storage::Synchronization::ParameterDeclaration>(8, signalDeclarationId);
@@ -2155,7 +2147,8 @@ private:
{
Storage::Synchronization::EnumerationDeclarations enumerationDeclarations;
auto callback = [&](Utils::SmallStringView name, long long enumerationDeclarationId) {
auto callback = [&](Utils::SmallStringView name,
EnumerationDeclarationId enumerationDeclarationId) {
enumerationDeclarations.emplace_back(
name,
selectEnumeratorDeclarationStatement.template values<

View File

@@ -174,7 +174,7 @@ public:
, sourceId{sourceId}
{}
explicit Import(int moduleId, int majorVersion, int minorVersion, int sourceId)
explicit Import(ModuleId moduleId, int majorVersion, int minorVersion, SourceId sourceId)
: version{majorVersion, minorVersion}
, moduleId{moduleId}
, sourceId{sourceId}
@@ -205,7 +205,8 @@ class ImportView
public:
explicit ImportView() = default;
explicit ImportView(long long importId, int sourceId, int moduleId, int majorVersion, int minorVersion)
explicit ImportView(
ImportId importId, SourceId sourceId, ModuleId moduleId, int majorVersion, int minorVersion)
: importId{importId}
, sourceId{sourceId}
, moduleId{moduleId}
@@ -272,9 +273,9 @@ class ModuleExportedImportView
public:
explicit ModuleExportedImportView() = default;
explicit ModuleExportedImportView(long long moduleExportedImportId,
int moduleId,
int exportedModuleId,
explicit ModuleExportedImportView(ModuleExportedImportId moduleExportedImportId,
ModuleId moduleId,
ModuleId exportedModuleId,
int majorVersion,
int minorVersion,
int isAutoVersion)
@@ -360,7 +361,7 @@ public:
, moduleId{moduleId}
{}
explicit ExportedType(int moduleId, Utils::SmallStringView name, int majorVersion, int minorVersion)
explicit ExportedType(ModuleId moduleId, Utils::SmallStringView name, int majorVersion, int minorVersion)
: name{name}
, version{majorVersion, minorVersion}
, moduleId{moduleId}
@@ -395,12 +396,12 @@ public:
, version{version}
, moduleId{moduleId}
{}
explicit ExportedTypeView(int moduleId,
explicit ExportedTypeView(ModuleId moduleId,
Utils::SmallStringView name,
int majorVersion,
int minorVersion,
int typeId,
long long exportedTypeNameId)
TypeId typeId,
ExportedTypeNameId exportedTypeNameId)
: name{name}
, version{majorVersion, minorVersion}
, typeId{typeId}
@@ -475,7 +476,7 @@ public:
explicit EnumerationDeclarationView() = default;
explicit EnumerationDeclarationView(Utils::SmallStringView name,
Utils::SmallStringView enumeratorDeclarations,
long long id)
EnumerationDeclarationId id)
: name{name}
, enumeratorDeclarations{std::move(enumeratorDeclarations)}
, id{id}
@@ -550,7 +551,7 @@ public:
explicit SignalDeclarationView() = default;
explicit SignalDeclarationView(Utils::SmallStringView name,
Utils::SmallStringView signature,
long long id)
SignalDeclarationId id)
: name{name}
, signature{signature}
, id{id}
@@ -601,7 +602,7 @@ public:
explicit FunctionDeclarationView(Utils::SmallStringView name,
Utils::SmallStringView returnTypeName,
Utils::SmallStringView signature,
long long id)
FunctionDeclarationId id)
: name{name}
, returnTypeName{returnTypeName}
, signature{signature}
@@ -667,7 +668,7 @@ public:
{}
explicit PropertyDeclaration(Utils::SmallStringView name,
long long propertyTypeId,
TypeId propertyTypeId,
int traits,
Utils::SmallStringView aliasPropertyName,
Utils::SmallStringView aliasPropertyNameTail = {})
@@ -718,10 +719,10 @@ class PropertyDeclarationView
public:
explicit PropertyDeclarationView(Utils::SmallStringView name,
int traits,
long long typeId,
long long typeNameId,
long long id,
long long aliasId)
TypeId typeId,
ImportedTypeNameId typeNameId,
PropertyDeclarationId id,
PropertyDeclarationId aliasId)
: name{name}
, traits{static_cast<PropertyDeclarationTraits>(traits)}
, typeId{typeId}
@@ -794,7 +795,7 @@ public:
explicit Type(Utils::SmallStringView typeName,
Utils::SmallStringView prototype,
int accessSemantics,
int sourceId)
SourceId sourceId)
: typeName{typeName}
, prototype{ImportedType{prototype}}
, accessSemantics{static_cast<TypeAccessSemantics>(accessSemantics)}
@@ -802,10 +803,10 @@ public:
{}
explicit Type(int sourceId,
explicit Type(SourceId sourceId,
Utils::SmallStringView typeName,
long long typeId,
long long prototypeId,
TypeId typeId,
TypeId prototypeId,
int accessSemantics,
Utils::SmallStringView defaultPropertyName)
: typeName{typeName}
@@ -855,7 +856,7 @@ public:
, fileType{fileType}
{}
ProjectData(int projectSourceId, int sourceId, int moduleId, int fileType)
ProjectData(SourceId projectSourceId, SourceId sourceId, ModuleId moduleId, int fileType)
: projectSourceId{projectSourceId}
, sourceId{sourceId}
, moduleId{moduleId}
@@ -938,10 +939,7 @@ namespace QmlDesigner::Storage::Info {
class PropertyDeclaration
{
public:
PropertyDeclaration(long long typeId,
Utils::SmallStringView name,
long long traits,
long long propertyTypeId)
PropertyDeclaration(TypeId typeId, Utils::SmallStringView name, long long traits, TypeId propertyTypeId)
: typeId{typeId}
, name{name}
, traits{static_cast<PropertyDeclarationTraits>(traits)}
@@ -967,11 +965,6 @@ public:
class Type
{
public:
Type(long long defaultPropertyId)
: defaultPropertyId{defaultPropertyId}
{}
Type(PropertyDeclarationId defaultPropertyId)
: defaultPropertyId{defaultPropertyId}
{}

View File

@@ -57,11 +57,6 @@ public:
class SourceNameEntry
{
public:
SourceNameEntry(Utils::SmallStringView sourceName, int sourceContextId)
: sourceName(sourceName)
, sourceContextId(sourceContextId)
{}
SourceNameEntry(Utils::SmallStringView sourceName, SourceContextId sourceContextId)
: sourceName(sourceName)
, sourceContextId(sourceContextId)
@@ -129,10 +124,6 @@ public:
: Base{{sourceName, sourceContextId}, sourceId}
{}
Source(Utils::SmallStringView sourceName, int sourceContextId, int sourceId)
: Base{{sourceName, SourceContextId{sourceContextId}}, SourceId{sourceId}}
{}
friend bool operator==(const Source &first, const Source &second)
{
return first.id == second.id && first.value == second.value;
@@ -145,10 +136,7 @@ class SourceNameAndSourceContextId
{
public:
constexpr SourceNameAndSourceContextId() = default;
SourceNameAndSourceContextId(Utils::SmallStringView sourceName, int sourceContextId)
: sourceName(sourceName)
, sourceContextId(sourceContextId)
{}
SourceNameAndSourceContextId(Utils::SmallStringView sourceName, SourceContextId sourceContextId)
: sourceName{sourceName}
, sourceContextId{sourceContextId}

View File

@@ -269,7 +269,7 @@ public:
{
std::shared_lock<Mutex> sharedLock(m_mutex);
if (IndexType{static_cast<IndexDatabaseType>(m_indices.size())} > id) {
if (IndexType::create(static_cast<IndexDatabaseType>(m_indices.size())) > id) {
if (auto indirectionIndex = m_indices.at(static_cast<std::size_t>(id));
indirectionIndex.isValid())
return m_entries.at(static_cast<std::size_t>(indirectionIndex)).value;

View File

@@ -36,11 +36,6 @@ public:
, id(id)
{}
StorageCacheEntry(ViewType value, typename IndexType::DatabaseType id)
: value(value)
, id{id}
{}
operator ViewType() const noexcept { return value; }
friend bool operator==(const StorageCacheEntry &first, const StorageCacheEntry &second)
{