summaryrefslogtreecommitdiffstats
path: root/dom/fs/parent/ResultStatement.h
blob: 3c492736e91568ce70c6f36c43f1697b159012dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef DOM_FS_PARENT_RESULTSTATEMENT_H_
#define DOM_FS_PARENT_RESULTSTATEMENT_H_

#include "mozIStorageStatement.h"
#include "mozilla/dom/FileSystemTypes.h"
#include "mozilla/dom/quota/QuotaCommon.h"
#include "mozilla/dom/quota/ResultExtensions.h"
#include "nsCOMPtr.h"
#include "nsString.h"

class mozIStorageConnection;

namespace mozilla::dom::fs {

using Column = uint32_t;

using ResultConnection = nsCOMPtr<mozIStorageConnection>;

/**
 * @brief ResultStatement
 * - provides error monad Result<T, E> compatible interface to the lower level
 * error code-based statement implementation in order to enable remote
 * debugging with error stack traces
 * - converts between OPFS internal data types and the generic data types of
 * the lower level implementation
 * - provides a customization point for requests aimed at the lower level
 * implementation allowing for example to remap errors or implement mocks
 */
class ResultStatement {
 public:
  using underlying_t = nsCOMPtr<mozIStorageStatement>;

  explicit ResultStatement(underlying_t aStmt) : mStmt(std::move(aStmt)) {}

  ResultStatement(const ResultStatement& aOther)
      : ResultStatement(aOther.mStmt) {}

  ResultStatement(ResultStatement&& aOther) noexcept
      : ResultStatement(std::move(aOther.mStmt)) {}

  ResultStatement& operator=(const ResultStatement& aOther) = default;

  ResultStatement& operator=(ResultStatement&& aOther) noexcept {
    mStmt = std::move(aOther.mStmt);
    return *this;
  }

  static Result<ResultStatement, QMResult> Create(
      const ResultConnection& aConnection, const nsACString& aSQLStatement);

  // XXX Consider moving all these "inline" methods into a separate file
  // called ResultStatementInlines.h. ResultStatement.h wouldn't have to then
  // include ResultExtensions.h, QuotaCommon.h and mozIStorageStatement.h
  // which are quite large and should be preferable only included from cpp
  // files or special headers like ResultStatementInlines.h. So in the end,
  // other headers would include ResultStatement.h only and other cpp files
  // would include ResultStatementInlines.h. See also IndedexDababase.h and
  // IndexedDatabaseInlines.h to see how it's done.

  inline nsresult BindEntryIdByName(const nsACString& aField,
                                    const EntryId& aValue) {
    return mStmt->BindUTF8StringAsBlobByName(aField, aValue);
  }

  inline nsresult BindContentTypeByName(const nsACString& aField,
                                        const ContentType& aValue) {
    if (0u == aValue.Length()) {  // Otherwise empty string becomes null
      return mStmt->BindUTF8StringByName(aField, aValue);
    }

    return mStmt->BindUTF8StringAsBlobByName(aField, aValue);
  }

  inline nsresult BindNameByName(const nsACString& aField, const Name& aValue) {
    return mStmt->BindStringAsBlobByName(aField, aValue);
  }

  inline nsresult BindPageNumberByName(const nsACString& aField,
                                       PageNumber aValue) {
    return mStmt->BindInt32ByName(aField, aValue);
  }

  inline nsresult BindUsageByName(const nsACString& aField, Usage aValue) {
    return mStmt->BindInt64ByName(aField, aValue);
  }

  inline nsresult BindBooleanByName(const nsACString& aField, bool aValue) {
    return mStmt->BindInt32ByName(aField, aValue ? 1 : 0);
  }

  inline Result<bool, QMResult> GetBooleanByColumn(Column aColumn) {
    int32_t value = 0;
    QM_TRY(QM_TO_RESULT(mStmt->GetInt32(aColumn, &value)));

    return 0 != value;
  }

  inline Result<ContentType, QMResult> GetContentTypeByColumn(Column aColumn) {
    ContentType value;
    QM_TRY(QM_TO_RESULT(mStmt->GetUTF8String(aColumn, value)));

    return value;
  }

  inline Result<EntryId, QMResult> GetEntryIdByColumn(Column aColumn) {
    EntryId value;
    QM_TRY(QM_TO_RESULT(mStmt->GetBlobAsUTF8String(aColumn, value)));

    return value;
  }

  inline Result<Name, QMResult> GetNameByColumn(Column aColumn) {
    Name value;
    QM_TRY(QM_TO_RESULT(mStmt->GetBlobAsString(aColumn, value)));

    return value;
  }

  inline Result<Usage, QMResult> GetUsageByColumn(Column aColumn) {
    Usage value = 0;
    QM_TRY(QM_TO_RESULT(mStmt->GetInt64(aColumn, &value)));

    return value;
  }

  inline bool IsNullByColumn(Column aColumn) const {
    bool value = mStmt->IsNull(aColumn);

    return value;
  }

  inline nsresult Execute() { return mStmt->Execute(); }

  inline Result<bool, QMResult> ExecuteStep() {
    bool hasEntries = false;
    QM_TRY(QM_TO_RESULT(mStmt->ExecuteStep(&hasEntries)));

    return hasEntries;
  }

  inline Result<bool, QMResult> YesOrNoQuery() {
    bool hasEntries = false;
    QM_TRY(QM_TO_RESULT(mStmt->ExecuteStep(&hasEntries)));
    MOZ_ALWAYS_TRUE(hasEntries);
    return GetBooleanByColumn(0u);
  }

 private:
  underlying_t mStmt;
};

}  // namespace mozilla::dom::fs

#endif  // DOM_FS_PARENT_RESULTSTATEMENT_H_