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/. */
7 #include "mozilla/dom/quota/StorageHelpers.h"
9 #include "mozilla/dom/quota/QuotaCommon.h"
10 #include "mozilla/dom/quota/ResultExtensions.h"
12 namespace mozilla::dom::quota
{
14 AutoDatabaseAttacher::AutoDatabaseAttacher(
15 nsCOMPtr
<mozIStorageConnection
> aConnection
,
16 nsCOMPtr
<nsIFile
> aDatabaseFile
, const nsLiteralCString
& aSchemaName
)
17 : mConnection(std::move(aConnection
)),
18 mDatabaseFile(std::move(aDatabaseFile
)),
19 mSchemaName(aSchemaName
),
22 AutoDatabaseAttacher::~AutoDatabaseAttacher() {
24 QM_WARNONLY_TRY(MOZ_TO_RESULT(Detach()));
28 nsresult
AutoDatabaseAttacher::Attach() {
29 MOZ_ASSERT(mConnection
);
30 MOZ_ASSERT(mDatabaseFile
);
31 MOZ_ASSERT(!mAttached
);
35 QM_TRY_INSPECT(const bool& exists
,
36 MOZ_TO_RESULT_INVOKE_MEMBER(mDatabaseFile
, Exists
));
42 QM_TRY_INSPECT(const auto& path
, MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
43 nsString
, mDatabaseFile
, GetPath
));
47 MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
48 nsCOMPtr
<mozIStorageStatement
>, mConnection
, CreateStatement
,
49 "ATTACH DATABASE :path AS "_ns
+ mSchemaName
+ ";"_ns
));
51 QM_TRY(MOZ_TO_RESULT(stmt
->BindStringByName("path"_ns
, path
)));
52 QM_TRY(MOZ_TO_RESULT(stmt
->Execute()));
59 nsresult
AutoDatabaseAttacher::Detach() {
60 MOZ_ASSERT(mConnection
);
61 MOZ_ASSERT(mAttached
);
64 mConnection
->ExecuteSimpleSQL("DETACH DATABASE "_ns
+ mSchemaName
)));
70 } // namespace mozilla::dom::quota