1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef DOM_QUOTA_QUOTADATABASEHELPER_H 8 #define DOM_QUOTA_QUOTADATABASEHELPER_H 9 10 #include "mozilla/dom/quota/QuotaCommon.h" 11 #include "mozIStorageConnection.h" 12 #include "nsCOMPtr.h" 13 #include "nsString.h" 14 15 namespace mozilla::dom::quota { 16 17 /** 18 * This class provides a RAII wrap of attaching and detaching database 19 * in a given C++ scope. It is guaranteed that an attached database will 20 * be detached even if you have an exception or return early. 21 * 22 * @param aConnection 23 * The connection to attach a database to. 24 * @param aDatabaseFile 25 * The database file to attach. 26 * @param aSchemaName 27 * The schema-name. Can be any string literal which is supported by the 28 * underlying database. For more details about schema-name, see 29 * https://www.sqlite.org/lang_attach.html 30 */ 31 class MOZ_STACK_CLASS AutoDatabaseAttacher final { 32 public: 33 explicit AutoDatabaseAttacher(nsCOMPtr<mozIStorageConnection> aConnection, 34 nsCOMPtr<nsIFile> aDatabaseFile, 35 const nsLiteralCString& aSchemaName); 36 37 ~AutoDatabaseAttacher(); 38 39 AutoDatabaseAttacher() = delete; 40 41 [[nodiscard]] nsresult Attach(); 42 43 [[nodiscard]] nsresult Detach(); 44 45 private: 46 nsCOMPtr<mozIStorageConnection> mConnection; 47 nsCOMPtr<nsIFile> mDatabaseFile; 48 const nsLiteralCString mSchemaName; 49 bool mAttached; 50 }; 51 52 } // namespace mozilla::dom::quota 53 54 #endif // DOM_QUOTA_QUOTADATABASEHELPER_H 55