Commit ef1f8c24 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

sqlite: SqliteConnection -> sqlite::Connection

parent ca24a466
......@@ -25,9 +25,13 @@
namespace medialibrary
{
class SqliteConnection;
namespace sqlite
{
class Connection;
}
class MediaLibrary;
typedef SqliteConnection* DBConnection;
using DBConnection = sqlite::Connection*;
using MediaLibraryPtr = const MediaLibrary*;
}
......@@ -161,9 +161,9 @@ bool MediaLibrary::createAllTables()
}
template <typename T>
static void propagateDeletionToCache( SqliteConnection::HookReason reason, int64_t rowId )
static void propagateDeletionToCache( sqlite::Connection::HookReason reason, int64_t rowId )
{
if ( reason != SqliteConnection::HookReason::Delete )
if ( reason != sqlite::Connection::HookReason::Delete )
return;
T::removeFromCache( rowId );
}
......@@ -174,36 +174,36 @@ void MediaLibrary::registerEntityHooks()
return;
m_dbConnection->registerUpdateHook( policy::MediaTable::Name,
[this]( SqliteConnection::HookReason reason, int64_t rowId ) {
if ( reason != SqliteConnection::HookReason::Delete )
[this]( sqlite::Connection::HookReason reason, int64_t rowId ) {
if ( reason != sqlite::Connection::HookReason::Delete )
return;
Media::removeFromCache( rowId );
m_modificationNotifier->notifyMediaRemoval( rowId );
});
m_dbConnection->registerUpdateHook( policy::ArtistTable::Name,
[this]( SqliteConnection::HookReason reason, int64_t rowId ) {
if ( reason != SqliteConnection::HookReason::Delete )
[this]( sqlite::Connection::HookReason reason, int64_t rowId ) {
if ( reason != sqlite::Connection::HookReason::Delete )
return;
Artist::removeFromCache( rowId );
m_modificationNotifier->notifyArtistRemoval( rowId );
});
m_dbConnection->registerUpdateHook( policy::AlbumTable::Name,
[this]( SqliteConnection::HookReason reason, int64_t rowId ) {
if ( reason != SqliteConnection::HookReason::Delete )
[this]( sqlite::Connection::HookReason reason, int64_t rowId ) {
if ( reason != sqlite::Connection::HookReason::Delete )
return;
Album::removeFromCache( rowId );
m_modificationNotifier->notifyAlbumRemoval( rowId );
});
m_dbConnection->registerUpdateHook( policy::AlbumTrackTable::Name,
[this]( SqliteConnection::HookReason reason, int64_t rowId ) {
if ( reason != SqliteConnection::HookReason::Delete )
[this]( sqlite::Connection::HookReason reason, int64_t rowId ) {
if ( reason != sqlite::Connection::HookReason::Delete )
return;
AlbumTrack::removeFromCache( rowId );
m_modificationNotifier->notifyAlbumTrackRemoval( rowId );
});
m_dbConnection->registerUpdateHook( policy::PlaylistTable::Name,
[this]( SqliteConnection::HookReason reason, int64_t rowId ) {
if ( reason != SqliteConnection::HookReason::Delete )
[this]( sqlite::Connection::HookReason reason, int64_t rowId ) {
if ( reason != sqlite::Connection::HookReason::Delete )
return;
Playlist::removeFromCache( rowId );
m_modificationNotifier->notifyPlaylistRemoval( rowId );
......@@ -257,7 +257,7 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& thu
}
m_thumbnailPath = thumbnailPath;
m_callback = mlCallback;
m_dbConnection = SqliteConnection::connect( dbPath );
m_dbConnection = sqlite::Connection::connect( dbPath );
// Give a chance to test overloads to reject the creation of a notifier
startDeletionNotifier();
......@@ -814,7 +814,7 @@ bool MediaLibrary::migrateModel3to4()
* Disable Foreign Keys & recursive triggers to avoid cascading deletion
* while remodeling the database into the transaction.
*/
SqliteConnection::WeakDbContext weakConnCtx{ getConn() };
sqlite::Connection::WeakDbContext weakConnCtx{ getConn() };
auto t = getConn()->newTransaction();
using namespace policy;
// As SQLite do not allow us to remove or add some constraints,
......
......@@ -36,7 +36,6 @@ class ModificationNotifier;
class DiscovererWorker;
class Parser;
class ParserService;
class SqliteConnection;
class Album;
class Artist;
......@@ -177,7 +176,7 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
virtual bool isDeviceKnown( const std::string& uuid ) const override;
protected:
std::shared_ptr<SqliteConnection> m_dbConnection;
std::shared_ptr<sqlite::Connection> m_dbConnection;
std::vector<std::shared_ptr<factory::IFileSystem>> m_fsFactories;
std::string m_thumbnailPath;
IMediaLibraryCb* m_callback;
......
......@@ -30,8 +30,10 @@
namespace medialibrary
{
namespace sqlite
{
SqliteConnection::SqliteConnection( const std::string &dbPath )
Connection::Connection( const std::string &dbPath )
: m_dbPath( dbPath )
, m_readLock( m_contextLock )
, m_writeLock( m_contextLock )
......@@ -42,12 +44,12 @@ SqliteConnection::SqliteConnection( const std::string &dbPath )
throw std::runtime_error( "Failed to enable sqlite multithreaded mode" );
}
SqliteConnection::~SqliteConnection()
Connection::~Connection()
{
sqlite::Statement::FlushStatementCache();
}
SqliteConnection::Handle SqliteConnection::getConn()
Connection::Handle Connection::getConn()
{
/**
* We need to have a single sqlite connection per thread, but we also need
......@@ -67,7 +69,7 @@ SqliteConnection::Handle SqliteConnection::getConn()
* Also note that we need to flush any potentially cached compiled statement
* when a thread gets terminated, as it would become unusable as well.
*
* \sa SqliteConnection::ThreadSpecificConnection::~ThreadSpecificConnection
* \sa sqlite::Connection::ThreadSpecificConnection::~ThreadSpecificConnection
* \sa sqlite::Statement::StatementsCache
*/
std::unique_lock<compat::Mutex> lock( m_connMutex );
......@@ -95,22 +97,22 @@ SqliteConnection::Handle SqliteConnection::getConn()
return it->second.get();
}
std::unique_ptr<sqlite::Transaction> SqliteConnection::newTransaction()
std::unique_ptr<sqlite::Transaction> Connection::newTransaction()
{
return std::unique_ptr<sqlite::Transaction>{ new sqlite::Transaction( this ) };
}
SqliteConnection::ReadContext SqliteConnection::acquireReadContext()
Connection::ReadContext Connection::acquireReadContext()
{
return ReadContext{ m_readLock };
}
SqliteConnection::WriteContext SqliteConnection::acquireWriteContext()
Connection::WriteContext Connection::acquireWriteContext()
{
return WriteContext{ m_writeLock };
}
void SqliteConnection::setPragmaEnabled( Handle conn,
void Connection::setPragmaEnabled( Handle conn,
const std::string& pragmaName,
bool value )
{
......@@ -131,7 +133,7 @@ void SqliteConnection::setPragmaEnabled( Handle conn,
throw std::runtime_error( "PRAGMA " + pragmaName + " value mismatch" );
}
void SqliteConnection::setForeignKeyEnabled( bool value )
void Connection::setForeignKeyEnabled( bool value )
{
// Ensure no transaction will be started during the pragma change
auto ctx = acquireWriteContext();
......@@ -141,7 +143,7 @@ void SqliteConnection::setForeignKeyEnabled( bool value )
setPragmaEnabled( getConn(), "foreign_keys", value );
}
void SqliteConnection::setRecursiveTriggers( bool value )
void Connection::setRecursiveTriggers( bool value )
{
// Ensure no request will run while we change this setting
auto ctx = acquireWriteContext();
......@@ -157,25 +159,25 @@ void SqliteConnection::setRecursiveTriggers( bool value )
setPragmaEnabled( getConn(), "recursive_triggers", value );
}
void SqliteConnection::registerUpdateHook( const std::string& table, SqliteConnection::UpdateHookCb cb )
void Connection::registerUpdateHook( const std::string& table, Connection::UpdateHookCb cb )
{
m_hooks.emplace( table, cb );
}
std::shared_ptr<SqliteConnection> SqliteConnection::connect( const std::string& dbPath )
std::shared_ptr<Connection> Connection::connect( const std::string& dbPath )
{
// Use a wrapper to allow make_shared to use the private SqliteConnection ctor
struct SqliteConnectionWrapper : public SqliteConnection
// Use a wrapper to allow make_shared to use the private Connection ctor
struct SqliteConnectionWrapper : public Connection
{
SqliteConnectionWrapper( const std::string& p ) : SqliteConnection( p ) {}
SqliteConnectionWrapper( const std::string& p ) : Connection( p ) {}
};
return std::make_shared<SqliteConnectionWrapper>( dbPath );
}
void SqliteConnection::updateHook( void* data, int reason, const char*,
void Connection::updateHook( void* data, int reason, const char*,
const char* table, sqlite_int64 rowId )
{
const auto self = reinterpret_cast<SqliteConnection*>( data );
const auto self = reinterpret_cast<Connection*>( data );
auto it = self->m_hooks.find( table );
if ( it == end( self->m_hooks ) )
return;
......@@ -193,26 +195,26 @@ void SqliteConnection::updateHook( void* data, int reason, const char*,
}
}
SqliteConnection::WeakDbContext::WeakDbContext( SqliteConnection* conn )
Connection::WeakDbContext::WeakDbContext( Connection* conn )
: m_conn( conn )
{
m_conn->setForeignKeyEnabled( false );
m_conn->setRecursiveTriggers( false );
}
SqliteConnection::WeakDbContext::~WeakDbContext()
Connection::WeakDbContext::~WeakDbContext()
{
m_conn->setForeignKeyEnabled( true );
m_conn->setRecursiveTriggers( true );
}
SqliteConnection::ThreadSpecificConnection::ThreadSpecificConnection(
std::shared_ptr<SqliteConnection> conn)
Connection::ThreadSpecificConnection::ThreadSpecificConnection(
std::shared_ptr<Connection> conn)
: m_conn( conn )
{
}
SqliteConnection::ThreadSpecificConnection::~ThreadSpecificConnection()
Connection::ThreadSpecificConnection::~ThreadSpecificConnection()
{
std::unique_lock<compat::Mutex> lock( m_conn->m_connMutex );
auto it = m_conn->m_conns.find( compat::this_thread::get_id() );
......@@ -228,3 +230,5 @@ SqliteConnection::ThreadSpecificConnection::~ThreadSpecificConnection()
}
}
}
......@@ -38,10 +38,10 @@ namespace medialibrary
namespace sqlite
{
class Transaction;
}
class SqliteConnection : public std::enable_shared_from_this<SqliteConnection>
class Transaction;
class Connection : public std::enable_shared_from_this<Connection>
{
public:
using ReadContext = std::unique_lock<utils::ReadLocker>;
......@@ -55,14 +55,14 @@ public:
};
struct WeakDbContext
{
WeakDbContext( SqliteConnection* conn );
WeakDbContext( Connection* conn );
~WeakDbContext();
WeakDbContext( const WeakDbContext& ) = delete;
WeakDbContext( WeakDbContext&& ) = delete;
WeakDbContext& operator=( const WeakDbContext& ) = delete;
WeakDbContext& operator=( WeakDbContext&& ) = delete;
private:
SqliteConnection* m_conn;
Connection* m_conn;
};
using UpdateHookCb = std::function<void(HookReason, int64_t)>;
......@@ -78,17 +78,17 @@ public:
void registerUpdateHook( const std::string& table, UpdateHookCb cb );
static std::shared_ptr<SqliteConnection> connect( const std::string& dbPath );
static std::shared_ptr<Connection> connect( const std::string& dbPath );
protected:
explicit SqliteConnection( const std::string& dbPath );
~SqliteConnection();
explicit Connection( const std::string& dbPath );
~Connection();
private:
SqliteConnection( const SqliteConnection& ) = delete;
SqliteConnection( SqliteConnection&& ) = delete;
SqliteConnection& operator=( const SqliteConnection& ) = delete;
SqliteConnection& operator=( SqliteConnection&& ) = delete;
Connection( const Connection& ) = delete;
Connection( Connection&& ) = delete;
Connection& operator=( const Connection& ) = delete;
Connection& operator=( Connection&& ) = delete;
void setPragmaEnabled( Handle conn, const std::string& pragmaName, bool value );
static void updateHook( void* data, int reason, const char* database,
......@@ -97,11 +97,11 @@ private:
private:
struct ThreadSpecificConnection
{
ThreadSpecificConnection( std::shared_ptr<SqliteConnection> conn );
ThreadSpecificConnection( std::shared_ptr<Connection> conn );
~ThreadSpecificConnection();
private:
std::shared_ptr<SqliteConnection> m_conn;
std::shared_ptr<Connection> m_conn;
};
using ConnPtr = std::unique_ptr<sqlite3, int(*)(sqlite3*)>;
......
......@@ -31,7 +31,7 @@ namespace medialibrary
namespace sqlite
{
std::unordered_map<SqliteConnection::Handle,
std::unordered_map<Connection::Handle,
std::unordered_map<std::string, Statement::CachedStmtPtr>> Statement::StatementsCache;
compat::Mutex Statement::StatementsCacheLock;
......
......@@ -118,7 +118,7 @@ private:
class Statement
{
public:
Statement( SqliteConnection::Handle dbConnection, const std::string& req )
Statement( Connection::Handle dbConnection, const std::string& req )
: m_stmt( nullptr, [](sqlite3_stmt* stmt) {
sqlite3_clear_bindings( stmt );
sqlite3_reset( stmt );
......@@ -187,7 +187,7 @@ public:
StatementsCache.clear();
}
static void FlushConnectionStatementCache( SqliteConnection::Handle h )
static void FlushConnectionStatementCache( Connection::Handle h )
{
std::lock_guard<compat::Mutex> lock( StatementsCacheLock );
auto it = StatementsCache.find( h );
......@@ -213,11 +213,11 @@ private:
// basically holds the state of the currently executed request.
using StatementPtr = std::unique_ptr<sqlite3_stmt, void(*)(sqlite3_stmt*)>;
StatementPtr m_stmt;
SqliteConnection::Handle m_dbConn;
Connection::Handle m_dbConn;
unsigned int m_bindIdx;
bool m_isCommit;
static compat::Mutex StatementsCacheLock;
static std::unordered_map<SqliteConnection::Handle,
static std::unordered_map<Connection::Handle,
std::unordered_map<std::string, CachedStmtPtr>> StatementsCache;
};
......@@ -235,7 +235,7 @@ class Tools
static std::vector<std::shared_ptr<INTF> > fetchAll( MediaLibraryPtr ml, const std::string& req, Args&&... args )
{
auto dbConnection = ml->getConn();
SqliteConnection::ReadContext ctx;
Connection::ReadContext ctx;
if (Transaction::transactionInProgress() == false)
ctx = dbConnection->acquireReadContext();
auto chrono = std::chrono::steady_clock::now();
......@@ -259,7 +259,7 @@ class Tools
static std::shared_ptr<T> fetchOne( MediaLibraryPtr ml, const std::string& req, Args&&... args )
{
auto dbConnection = ml->getConn();
SqliteConnection::ReadContext ctx;
Connection::ReadContext ctx;
if (Transaction::transactionInProgress() == false)
ctx = dbConnection->acquireReadContext();
auto chrono = std::chrono::steady_clock::now();
......@@ -279,7 +279,7 @@ class Tools
template <typename... Args>
static bool executeRequest( DBConnection dbConnection, const std::string& req, Args&&... args )
{
SqliteConnection::WriteContext ctx;
Connection::WriteContext ctx;
if (Transaction::transactionInProgress() == false)
ctx = dbConnection->acquireWriteContext();
return executeRequestLocked( dbConnection, req, std::forward<Args>( args )... );
......@@ -288,7 +288,7 @@ class Tools
template <typename... Args>
static bool executeDelete( DBConnection dbConnection, const std::string& req, Args&&... args )
{
SqliteConnection::WriteContext ctx;
Connection::WriteContext ctx;
if (Transaction::transactionInProgress() == false)
ctx = dbConnection->acquireWriteContext();
if ( executeRequestLocked( dbConnection, req, std::forward<Args>( args )... ) == false )
......@@ -310,7 +310,7 @@ class Tools
template <typename... Args>
static int64_t executeInsert( DBConnection dbConnection, const std::string& req, Args&&... args )
{
SqliteConnection::WriteContext ctx;
Connection::WriteContext ctx;
if (Transaction::transactionInProgress() == false)
ctx = dbConnection->acquireWriteContext();
if ( executeRequestLocked( dbConnection, req, std::forward<Args>( args )... ) == false )
......
......@@ -50,7 +50,7 @@ public:
private:
DBConnection m_dbConn;
SqliteConnection::WriteContext m_ctx;
Connection::WriteContext m_ctx;
std::vector<std::function<void()>> m_failureHandlers;
......
......@@ -53,7 +53,7 @@ public:
{
// Don't use our Sqlite wrapper to open a connection. We don't want
// to mess with per-thread connections.
medialibrary::SqliteConnection::Handle conn;
medialibrary::sqlite::Connection::Handle conn;
sqlite3_open( "test.db", &conn );
std::unique_ptr<sqlite3, int(*)(sqlite3*)> dbPtr{ conn, &sqlite3_close };
// The backup file already contains a transaction
......@@ -85,7 +85,7 @@ TEST_F( DbModel, Upgrade )
{
// All is done during the database initialization, we only care about no
// exception being thrown, and MediaLibrary::initialize() returning true
medialibrary::SqliteConnection::Handle conn;
medialibrary::sqlite::Connection::Handle conn;
sqlite3_open( "test.db", &conn );
std::unique_ptr<sqlite3, int(*)(sqlite3*)> dbPtr{ conn, &sqlite3_close };
medialibrary::sqlite::Statement stmt{ conn,
......
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