2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2020 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2020-05-27 23:48:03 +02:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2022-06-21 15:42:18 +02:00
|
|
|
#include "sqlite3_fwd.h"
|
2020-09-16 13:44:43 +02:00
|
|
|
#include "sqliteblob.h"
|
2020-05-27 23:48:03 +02:00
|
|
|
#include "sqliteglobal.h"
|
2020-10-27 15:09:25 +01:00
|
|
|
#include "sqlitevalue.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/smallstring.h>
|
2020-05-27 23:48:03 +02:00
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <iosfwd>
|
|
|
|
|
|
|
|
|
|
namespace Sqlite {
|
|
|
|
|
|
|
|
|
|
class Sessions;
|
|
|
|
|
|
2020-10-27 15:09:25 +01:00
|
|
|
enum class Operation : char { Invalid, Insert, Update, Delete };
|
|
|
|
|
|
2020-11-05 18:12:26 +01:00
|
|
|
namespace SessionChangeSetInternal {
|
|
|
|
|
|
2020-10-27 15:09:25 +01:00
|
|
|
class SentinelIterator
|
|
|
|
|
{};
|
|
|
|
|
|
|
|
|
|
class ValueViews
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ValueView newValue;
|
|
|
|
|
ValueView oldValue;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-05 18:12:26 +01:00
|
|
|
class SQLITE_EXPORT ConstTupleIterator
|
2020-10-27 15:09:25 +01:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
using difference_type = int;
|
2020-11-13 11:28:42 +01:00
|
|
|
using value_type = ValueViews;
|
|
|
|
|
using pointer = const ValueViews *;
|
|
|
|
|
using reference = const ValueViews &;
|
2020-10-27 15:09:25 +01:00
|
|
|
using iterator_category = std::forward_iterator_tag;
|
|
|
|
|
|
2020-11-05 18:12:26 +01:00
|
|
|
ConstTupleIterator(sqlite3_changeset_iter *sessionIterator,
|
|
|
|
|
int index,
|
|
|
|
|
Sqlite::Operation operation)
|
2020-10-27 15:09:25 +01:00
|
|
|
: m_sessionIterator{sessionIterator}
|
|
|
|
|
, m_column{index}
|
|
|
|
|
, m_operation{operation}
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
ConstTupleIterator operator++()
|
|
|
|
|
{
|
|
|
|
|
++m_column;
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend bool operator==(const ConstTupleIterator &first, const ConstTupleIterator &second)
|
|
|
|
|
{
|
|
|
|
|
return first.m_column == second.m_column;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend bool operator!=(const ConstTupleIterator &first, const ConstTupleIterator &second)
|
|
|
|
|
{
|
|
|
|
|
return !(first == second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ValueViews operator*() const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
sqlite3_changeset_iter *m_sessionIterator = {};
|
|
|
|
|
int m_column = 0;
|
2020-11-05 18:12:26 +01:00
|
|
|
Sqlite::Operation m_operation = Sqlite::Operation::Invalid;
|
2020-10-27 15:09:25 +01:00
|
|
|
};
|
|
|
|
|
|
2020-11-05 18:12:26 +01:00
|
|
|
class SQLITE_EXPORT Tuple
|
2020-10-27 15:09:25 +01:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
using difference_type = int;
|
|
|
|
|
using value_type = ValueView;
|
|
|
|
|
using reference = ValueView &;
|
|
|
|
|
using const_reference = const ValueView &;
|
|
|
|
|
using iterator = ConstTupleIterator;
|
|
|
|
|
using const_iterator = ConstTupleIterator;
|
|
|
|
|
using size_type = int;
|
|
|
|
|
|
|
|
|
|
Utils::SmallStringView table;
|
|
|
|
|
sqlite3_changeset_iter *sessionIterator = {};
|
|
|
|
|
int columnCount = 0;
|
2020-11-05 18:12:26 +01:00
|
|
|
Sqlite::Operation operation = Sqlite::Operation::Invalid;
|
2020-10-27 15:09:25 +01:00
|
|
|
|
|
|
|
|
ValueViews operator[](int column) const;
|
|
|
|
|
ConstTupleIterator begin() const { return {sessionIterator, 0, operation}; }
|
|
|
|
|
ConstTupleIterator end() const { return {sessionIterator, columnCount, operation}; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class State : char { Invalid, Row, Done };
|
|
|
|
|
|
2020-11-05 18:12:26 +01:00
|
|
|
class SQLITE_EXPORT ConstIterator
|
2020-10-27 15:09:25 +01:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
using difference_type = long;
|
|
|
|
|
using value_type = Tuple;
|
|
|
|
|
using pointer = const Tuple *;
|
|
|
|
|
using reference = const Tuple &;
|
|
|
|
|
using iterator_category = std::input_iterator_tag;
|
|
|
|
|
|
|
|
|
|
ConstIterator(sqlite3_changeset_iter *sessionIterator)
|
|
|
|
|
: m_sessionIterator(sessionIterator)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
ConstIterator(const ConstIterator &) = delete;
|
|
|
|
|
void operator=(const ConstIterator &) = delete;
|
|
|
|
|
|
|
|
|
|
ConstIterator(ConstIterator &&other)
|
|
|
|
|
: m_sessionIterator(other.m_sessionIterator)
|
|
|
|
|
, m_state(other.m_state)
|
|
|
|
|
{
|
|
|
|
|
other.m_sessionIterator = {};
|
|
|
|
|
other.m_state = State::Done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConstIterator &operator=(ConstIterator &&other)
|
|
|
|
|
{
|
|
|
|
|
auto tmp = std::move(other);
|
2020-11-11 08:23:26 +01:00
|
|
|
swap(tmp, *this);
|
2020-10-27 15:09:25 +01:00
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~ConstIterator();
|
|
|
|
|
|
2021-05-03 10:11:15 +02:00
|
|
|
friend void swap(ConstIterator &first, ConstIterator &second) noexcept
|
2020-11-11 08:23:26 +01:00
|
|
|
{
|
|
|
|
|
std::swap(first.m_sessionIterator, second.m_sessionIterator);
|
|
|
|
|
std::swap(first.m_state, second.m_state);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 15:09:25 +01:00
|
|
|
ConstIterator &operator++();
|
|
|
|
|
|
|
|
|
|
friend bool operator==(const ConstIterator &first, const ConstIterator &second)
|
|
|
|
|
{
|
|
|
|
|
return first.m_sessionIterator == second.m_sessionIterator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend bool operator!=(const ConstIterator &first, const ConstIterator &second)
|
|
|
|
|
{
|
|
|
|
|
return !(first == second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend bool operator==(const ConstIterator &first, SentinelIterator)
|
|
|
|
|
{
|
|
|
|
|
return first.m_state == State::Done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend bool operator!=(const ConstIterator &first, SentinelIterator)
|
|
|
|
|
{
|
|
|
|
|
return first.m_state == State::Row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend bool operator==(SentinelIterator first, const ConstIterator &second)
|
|
|
|
|
{
|
|
|
|
|
return second == first;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend bool operator!=(SentinelIterator first, const ConstIterator &second)
|
|
|
|
|
{
|
|
|
|
|
return second != first;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Tuple operator*() const;
|
|
|
|
|
|
|
|
|
|
State state() const { return m_state; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
sqlite3_changeset_iter *m_sessionIterator = {};
|
|
|
|
|
State m_state = State::Invalid;
|
|
|
|
|
};
|
|
|
|
|
} // namespace SessionChangeSetInternal
|
|
|
|
|
|
|
|
|
|
class SQLITE_EXPORT SessionChangeSet
|
2020-05-27 23:48:03 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2020-09-16 13:44:43 +02:00
|
|
|
SessionChangeSet(BlobView blob);
|
2020-05-27 23:48:03 +02:00
|
|
|
SessionChangeSet(Sessions &session);
|
|
|
|
|
~SessionChangeSet();
|
|
|
|
|
SessionChangeSet(const SessionChangeSet &) = delete;
|
|
|
|
|
void operator=(const SessionChangeSet &) = delete;
|
|
|
|
|
SessionChangeSet(SessionChangeSet &&other) noexcept
|
|
|
|
|
{
|
|
|
|
|
SessionChangeSet temp;
|
|
|
|
|
swap(temp, other);
|
|
|
|
|
swap(temp, *this);
|
|
|
|
|
}
|
|
|
|
|
void operator=(SessionChangeSet &);
|
|
|
|
|
|
2020-09-16 13:44:43 +02:00
|
|
|
BlobView asBlobView() const;
|
2020-05-27 23:48:03 +02:00
|
|
|
|
|
|
|
|
friend void swap(SessionChangeSet &first, SessionChangeSet &second) noexcept
|
|
|
|
|
{
|
|
|
|
|
SessionChangeSet temp;
|
2020-10-27 15:09:25 +01:00
|
|
|
std::swap(temp.m_data, first.m_data);
|
|
|
|
|
std::swap(temp.m_size, first.m_size);
|
|
|
|
|
std::swap(first.m_data, second.m_data);
|
|
|
|
|
std::swap(first.m_size, second.m_size);
|
|
|
|
|
std::swap(temp.m_data, second.m_data);
|
|
|
|
|
std::swap(temp.m_size, second.m_size);
|
2020-05-27 23:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-27 15:09:25 +01:00
|
|
|
SessionChangeSetInternal::ConstIterator begin() const;
|
|
|
|
|
SessionChangeSetInternal::SentinelIterator end() const { return {}; }
|
|
|
|
|
|
|
|
|
|
void *data() const { return m_data; }
|
|
|
|
|
|
|
|
|
|
int size() const { return m_size; }
|
|
|
|
|
|
2020-05-27 23:48:03 +02:00
|
|
|
private:
|
|
|
|
|
SessionChangeSet() = default;
|
|
|
|
|
|
2020-10-27 15:09:25 +01:00
|
|
|
private:
|
|
|
|
|
void *m_data = nullptr;
|
|
|
|
|
int m_size = {};
|
2020-05-27 23:48:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using SessionChangeSets = std::vector<SessionChangeSet>;
|
|
|
|
|
|
|
|
|
|
} // namespace Sqlite
|