SQLiteDynamicStorage.cpp (2892B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "Public/winget/SQLiteDynamicStorage.h" 5 6 namespace AppInstaller::SQLite 7 { 8 SQLiteDynamicStorage::SQLiteDynamicStorage(const std::string& target, const Version& version) : SQLiteStorageBase(target, version) 9 { 10 version.SetSchemaVersion(m_dbconn); 11 } 12 13 SQLiteDynamicStorage::SQLiteDynamicStorage(const std::filesystem::path& target, const Version& version) : SQLiteDynamicStorage(target.u8string(), version) 14 {} 15 16 SQLiteDynamicStorage::SQLiteDynamicStorage( 17 const std::string& filePath, 18 SQLiteStorageBase::OpenDisposition disposition, 19 Utility::ManagedFile&& file) 20 : SQLiteStorageBase(filePath, disposition, std::move(file)) 21 {} 22 23 SQLiteDynamicStorage::SQLiteDynamicStorage( 24 const std::filesystem::path& filePath, 25 SQLiteStorageBase::OpenDisposition disposition, 26 Utility::ManagedFile&& file) 27 : SQLiteDynamicStorage(filePath.u8string(), disposition, std::move(file)) 28 {} 29 30 SQLiteDynamicStorage::operator Connection& () 31 { 32 return m_dbconn; 33 } 34 35 SQLiteDynamicStorage::operator const Connection& () const 36 { 37 return m_dbconn; 38 } 39 40 Connection& SQLiteDynamicStorage::GetConnection() 41 { 42 return m_dbconn; 43 } 44 45 const Connection& SQLiteDynamicStorage::GetConnection() const 46 { 47 return m_dbconn; 48 } 49 50 _Acquires_lock_(mutex) 51 SQLiteDynamicStorage::TransactionLock::TransactionLock(std::mutex& mutex) : 52 m_lock(mutex) 53 { 54 } 55 56 _Acquires_lock_(mutex) 57 SQLiteDynamicStorage::TransactionLock::TransactionLock(std::mutex& mutex, Connection& connection, std::string_view name, bool immediateWrite) : 58 m_lock(mutex) 59 { 60 m_transaction = Transaction::Create(connection, std::string{ name }, immediateWrite); 61 } 62 63 void SQLiteDynamicStorage::TransactionLock::Rollback(bool throwOnError) 64 { 65 m_transaction.Rollback(throwOnError); 66 } 67 68 void SQLiteDynamicStorage::TransactionLock::Commit() 69 { 70 m_transaction.Commit(); 71 } 72 73 std::unique_ptr<SQLiteDynamicStorage::TransactionLock> SQLiteDynamicStorage::TryBeginTransaction(std::string_view name, bool immediateWrite) 74 { 75 auto result = std::make_unique<TransactionLock>(*m_interfaceLock, m_dbconn, name, immediateWrite); 76 77 Version currentVersion = Version::GetSchemaVersion(m_dbconn); 78 if (currentVersion != m_version) 79 { 80 m_version = currentVersion; 81 result.reset(); 82 } 83 84 return result; 85 } 86 87 std::unique_ptr<SQLiteDynamicStorage::TransactionLock> SQLiteDynamicStorage::LockConnection() 88 { 89 return std::make_unique<TransactionLock>(*m_interfaceLock); 90 } 91 }