2020-05-12 12:54:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2020 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** 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
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "sqlitedatabaseinterface.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/smallstringio.h>
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
namespace Sqlite {
|
|
|
|
|
|
2020-06-05 19:55:10 +02:00
|
|
|
template<unsigned int TableCount = 0>
|
2020-05-12 12:54:18 +02:00
|
|
|
class LastChangedRowId
|
|
|
|
|
{
|
|
|
|
|
public:
|
2020-05-16 14:53:58 +02:00
|
|
|
LastChangedRowId(DatabaseInterface &database)
|
|
|
|
|
: database(database)
|
|
|
|
|
|
|
|
|
|
{
|
2020-06-05 19:55:10 +02:00
|
|
|
database.setUpdateHook(this, callbackOnlyRowId);
|
2020-05-16 14:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LastChangedRowId(DatabaseInterface &database, Utils::SmallStringView databaseName)
|
|
|
|
|
: database(database)
|
2020-06-05 19:55:10 +02:00
|
|
|
, databaseName(databaseName)
|
2020-05-16 14:53:58 +02:00
|
|
|
{
|
2020-06-05 19:55:10 +02:00
|
|
|
database.setUpdateHook(this, callbackWithDatabase);
|
2020-05-16 14:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-05 19:55:10 +02:00
|
|
|
template<typename... Tables>
|
2020-05-12 12:54:18 +02:00
|
|
|
LastChangedRowId(DatabaseInterface &database,
|
|
|
|
|
Utils::SmallStringView databaseName,
|
2020-06-05 19:55:10 +02:00
|
|
|
Tables... tableNames)
|
2020-05-12 12:54:18 +02:00
|
|
|
: database(database)
|
2020-06-05 19:55:10 +02:00
|
|
|
, databaseName(databaseName)
|
|
|
|
|
, tableNames({tableNames...})
|
2020-05-12 17:05:33 +02:00
|
|
|
{
|
2020-06-05 19:55:10 +02:00
|
|
|
database.setUpdateHook(this, callbackWithTables);
|
2020-05-12 17:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-05 19:55:10 +02:00
|
|
|
~LastChangedRowId() { database.resetUpdateHook(); }
|
2020-05-12 17:05:33 +02:00
|
|
|
|
2020-06-05 19:55:10 +02:00
|
|
|
void operator()(long long rowId) { lastRowId = rowId; }
|
|
|
|
|
|
|
|
|
|
static void callbackOnlyRowId(void *object, int, char const *, char const *, long long rowId)
|
2020-05-12 17:05:33 +02:00
|
|
|
{
|
2020-06-05 19:55:10 +02:00
|
|
|
(*static_cast<LastChangedRowId *>(object))(rowId);
|
|
|
|
|
}
|
2020-05-12 17:05:33 +02:00
|
|
|
|
2020-06-05 19:55:10 +02:00
|
|
|
bool containsTable(Utils::SmallStringView table)
|
|
|
|
|
{
|
|
|
|
|
return std::find(tableNames.begin(), tableNames.end(), table) != tableNames.end();
|
2020-05-12 17:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-05 19:55:10 +02:00
|
|
|
void operator()(Utils::SmallStringView database, long long rowId)
|
|
|
|
|
{
|
|
|
|
|
if (databaseName == database)
|
|
|
|
|
lastRowId = rowId;
|
|
|
|
|
}
|
2020-05-12 17:05:33 +02:00
|
|
|
|
2020-06-05 19:55:10 +02:00
|
|
|
static void callbackWithDatabase(
|
|
|
|
|
void *object, int, char const *database, char const *, long long rowId)
|
2020-05-12 12:54:18 +02:00
|
|
|
{
|
2020-06-05 19:55:10 +02:00
|
|
|
(*static_cast<LastChangedRowId *>(object))(Utils::SmallStringView{database}, rowId);
|
|
|
|
|
}
|
2020-05-12 12:54:18 +02:00
|
|
|
|
2020-06-05 19:55:10 +02:00
|
|
|
void operator()(Utils::SmallStringView database, Utils::SmallStringView table, long long rowId)
|
|
|
|
|
{
|
|
|
|
|
if (databaseName == database && containsTable(table))
|
|
|
|
|
lastRowId = rowId;
|
2020-05-12 12:54:18 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-05 19:55:10 +02:00
|
|
|
static void callbackWithTables(
|
|
|
|
|
void *object, int, char const *database, char const *table, long long rowId)
|
|
|
|
|
{
|
|
|
|
|
(*static_cast<LastChangedRowId *>(
|
|
|
|
|
object))(Utils::SmallStringView{database}, Utils::SmallStringView{table}, rowId);
|
|
|
|
|
}
|
2020-05-12 12:54:18 +02:00
|
|
|
|
|
|
|
|
long long takeLastRowId()
|
|
|
|
|
{
|
2020-06-05 19:55:10 +02:00
|
|
|
long long lastId = lastRowId;
|
|
|
|
|
|
2020-05-12 12:54:18 +02:00
|
|
|
lastRowId = -1;
|
2020-06-05 19:55:10 +02:00
|
|
|
|
|
|
|
|
return lastId;
|
2020-05-12 12:54:18 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-05 19:55:10 +02:00
|
|
|
bool lastRowIdIsValid() const { return lastRowId >= 0; }
|
2020-05-26 20:31:07 +02:00
|
|
|
|
2020-05-12 12:54:18 +02:00
|
|
|
public:
|
|
|
|
|
DatabaseInterface &database;
|
|
|
|
|
long long lastRowId = -1;
|
2020-06-05 19:55:10 +02:00
|
|
|
Utils::SmallStringView databaseName;
|
|
|
|
|
std::array<Utils::SmallStringView, TableCount> tableNames;
|
2020-05-12 12:54:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace Sqlite
|