Bug 1861751 Part 4: Add tests of invalid buffers in various usages. r=webgpu-reviewer...
[gecko.git] / storage / mozIStorageAsyncConnection.idl
blob6c053850076dbbc16e1585818afcc049cd9e1a42
1 /* -*- Mode: idl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsISupports.idl"
8 interface mozIStorageCompletionCallback;
9 interface mozIStorageFunction;
10 interface mozIStorageProgressHandler;
11 interface mozIStorageBaseStatement;
12 interface mozIStorageStatement;
13 interface mozIStorageAsyncStatement;
14 interface mozIStorageStatementCallback;
15 interface mozIStoragePendingStatement;
16 interface nsIFile;
18 /**
19 * mozIStorageAsyncConnection represents an asynchronous database
20 * connection attached to a specific file or to an in-memory data
21 * storage. It is the primary interface for interacting with a
22 * database from the main thread, including creating prepared
23 * statements, executing SQL, and examining database errors.
25 [scriptable, uuid(8bfd34d5-4ddf-4e4b-89dd-9b14f33534c6)]
26 interface mozIStorageAsyncConnection : nsISupports {
27 /**
28 * Transaction behavior constants.
30 const int32_t TRANSACTION_DEFAULT = -1;
31 const int32_t TRANSACTION_DEFERRED = 0;
32 const int32_t TRANSACTION_IMMEDIATE = 1;
33 const int32_t TRANSACTION_EXCLUSIVE = 2;
35 /**
36 * The default behavior for all transactions run on this connection. Defaults
37 * to `TRANSACTION_DEFERRED`, and can be overridden for individual
38 * transactions.
40 attribute int32_t defaultTransactionType;
42 /**
43 * The maximum number of bound parameters for statements executed on this
44 * connection. If your statement has more params than this limit, you'll
45 * need to chunk them into multiple statements. See `PlacesUtils.chunkArray`
46 * and its callers in Places for examples of how to do this, or read on for
47 * an overview.
49 * Keep in mind that the variable limit is for the _total_ number of
50 * parameters, including ones bound by name (using the `:VVV`, `@VVV`, or
51 * `?VVV` syntax) and index (`?` and `?NNN`).
53 * This means, when chunking:
55 * - If you're binding 1 param per 1 value per chunk (for example, if you
56 * have a list of GUIDs and a clause like `WHERE guid IN (?, ?, ?, ...)`,
57 * your chunk length is just `variableLimit`.
58 * - If you're binding 1 param per 1 value per chunk, but using that
59 * param in multiple positions in the query (for example, `WHERE url_hash
60 * IN (hash(?1), hash(?2), ...) AND url IN (?1, ?2, ...)`), you can use the
61 * `?NNN` syntax with a chunk length of `variableLimit`.
62 * - If you're binding N params per 1 value per chunk (for example, if you
63 * have a list of items with GUIDs and parent GUIDs, and you want to bind
64 * both), your chunk length is `variableLimit / N`, since you're binding
65 * two params for each element.
66 * - If you're binding K params per L values per chunk, plus M fixed ones
67 * (for example, `WHERE parentGuid = :parentGuid AND guid IN (?, ?, ...)`),
68 * your chunk length is `variableLimit - M`, to ensure there's space for the
69 * fixed variables.
71 * If you bind more params than this limit, `create{Async}Statement` will
72 * fail with a "too many SQL variables" error.
74 readonly attribute int32_t variableLimit;
76 /**
77 * Returns true if a transaction is active on this connection.
79 * Note that this is true if a transaction is active on the connection,
80 * regardless of how it was opened. There are several ways to open one:
82 * 1. Explicitly calling `beginTransaction` on a `mozIStorageConnection`.
83 * 2. Calling `executeSimpleSQL("BEGIN")` or
84 * `createStatement("BEGIN").execute()` on a `mozIStorageConnection`.
85 * 3. Executing an async statement, like
86 * `createAsyncStatement("BEGIN").executeAsync(...)`. This is what
87 * `Sqlite.sys.mjs` does under the hood.
89 * Because of this, it's important *not* to use this attribute to decide
90 * whether to *commit* the active transaction, because the caller that opened
91 * it may not expect that. This is why both `mozStorageTransaction` and
92 * `Sqlite.sys.mjs` use an internal variable (`mHasTransaction` for the former;
93 * `_hasInProgressTransaction` for the latter) to check if their transaction
94 * is already in progress, instead of just checking this attribute before
95 * committing. Otherwise, mozStorage might accidentally commit (or roll back!)
96 * a transaction started by `Sqlite.sys.mjs`, and vice versa.
98 readonly attribute boolean transactionInProgress;
101 * Close this database connection, allowing all pending statements
102 * to complete first.
104 * @param aCallback [optional]
105 * A callback that will be notified when the close is completed,
106 * with the following arguments:
107 * - status: the status of the call
108 * - value: |null|
110 * @throws NS_ERROR_NOT_SAME_THREAD
111 * If called on a thread other than the one that opened it. The
112 * callback will not be dispatched.
113 * @throws NS_ERROR_NOT_INITIALIZED
114 * If called on a connection that has already been closed or was
115 * never properly opened. The callback will still be dispatched
116 * to the main thread despite the returned error.
117 * @note If this call should fail, the callback won't be invoked.
119 void asyncClose([optional] in mozIStorageCompletionCallback aCallback);
122 * Forcibly closes a database connection synchronously.
123 * This should only be used when it's required to close and replace the
124 * database synchronously to return control to the consumer, for example in
125 * case of a detected corruption on database opening.
126 * Since this spins the events loop, it should be used only in very particular
127 * and rare situations, or it may cause unexpected consequences (crashes).
129 * @throws NS_ERROR_NOT_SAME_THREAD
130 * If called on a thread other than the one that opened it.
132 [noscript] void spinningSynchronousClose();
135 * Clone a database and make the clone read only if needed.
136 * SQL Functions and attached on-disk databases are applied to the new clone.
138 * @param aReadOnly
139 * If true, the returned database should be put into read-only mode.
141 * @param aCallback
142 * A callback that will be notified when the operation is complete,
143 * with the following arguments:
144 * - status: the status of the operation
145 * - value: in case of success, an intance of
146 * mozIStorageAsyncConnection cloned from this one.
148 * @throws NS_ERROR_NOT_SAME_THREAD
149 * If is called on a thread other than the one that opened it.
150 * @throws NS_ERROR_UNEXPECTED
151 * If this connection is a memory database.
153 * @note If your connection is already read-only, you will get a read-only
154 * clone.
155 * @note The resulting connection will implement `mozIStorageConnection`, but
156 * all synchronous methods will throw if called from the main thread.
157 * @note Due to a bug in SQLite, if you use the shared cache
158 * (see mozIStorageService), you end up with the same privileges as the
159 * first connection opened regardless of what is specified in aReadOnly.
160 * @note The following pragmas are copied over to a read-only clone:
161 * - cache_size
162 * - temp_store
163 * The following pragmas are copied over to a writeable clone:
164 * - cache_size
165 * - temp_store
166 * - foreign_keys
167 * - journal_size_limit
168 * - synchronous
169 * - wal_autocheckpoint
170 * All SQL functions are copied over to read-only and writeable clones.
171 * Additionally, all temporary tables, triggers, and views, as well as
172 * any indexes on temporary tables, are copied over to writeable clones.
173 * For temporary tables, only the schemas are copied, not their
174 * contents.
176 void asyncClone(in boolean aReadOnly,
177 in mozIStorageCompletionCallback aCallback);
180 * The current database nsIFile. Null if the database
181 * connection refers to an in-memory database.
183 readonly attribute nsIFile databaseFile;
186 * Causes any pending database operation to abort and return at the first
187 * opportunity.
188 * @note this cannot be used on mozIStorageConnection unless the connection is
189 * explicitly marked as `interruptible`. For more details, please
190 * refer to CONNECTION_INTERRUPTIBLE in mozIStorageService.
191 * @note operations that are nearly complete may still be able to complete.
192 * @throws if used on an unsupported connection type, or a closed connection.
194 void interrupt();
197 * Vacuum the main database plus all the attached one.
198 * If the database is in auto_vacuum = INCREMENTAL mode, this executes an
199 * incremental_vacuum, otherwise it will always execute a full vacuum.
201 * While it's possible to invoke this method directly, it's suggested, when
202 * possible, to use the VacuumManager instead.
203 * That means registering your component for the "vacuum-participant" XPCOM
204 * category, and implement the mozIStorageVacuumParticipant interface.
206 * @param [aCallback] Completion callback invoked once the operation is
207 * complete.
208 * @param [aUseIncremental] When set to true, this will try to convert the
209 * main schema to auto_vacuum = INCREMENTAL mode, if it's not set yet.
210 * When set to false, it will try to set auto_vacuum = NONE.
211 * Note a full vacuum will be executed if the auto_vacuum mode must be
212 * changed, otherwise an incremental vacuum will happen if the database
213 * is already in INCREMENTAL mode.
214 * @param [aSetPageSize] This can be used to change the database page_size, a
215 * full vacuum will be executed to persist the change. If the page
216 * size is already correct, or you pass 0, this will be a no-op.
217 * @throws If it's not possible to start the async vacuum operation, note in
218 * this case the callback won't be invoked.
219 * @note Vacuum will fail inside a transaction, or if there is an ongoing
220 * read statement.
222 void asyncVacuum(
223 [optional] in mozIStorageCompletionCallback aCallback,
224 [optional] in boolean aUseIncremental,
225 [optional] in long aSetPageSize
228 //////////////////////////////////////////////////////////////////////////////
229 //// Statement creation
232 * Create an asynchronous statement for the given SQL. An
233 * asynchronous statement can only be used to dispatch asynchronous
234 * requests to the asynchronous execution thread and cannot be used
235 * to take any synchronous actions on the database.
237 * The expression may use ? to indicate sequential numbered arguments,
238 * ?1, ?2 etc. to indicate specific numbered arguments or :name and
239 * $var to indicate named arguments.
241 * @param aSQLStatement
242 * The SQL statement to execute.
243 * @return a new mozIStorageAsyncStatement
244 * @note The statement is created lazily on first execution.
246 mozIStorageAsyncStatement createAsyncStatement(in AUTF8String aSQLStatement);
249 * Execute an array of statements created with this connection using
250 * any currently bound parameters. When the array contains multiple
251 * statements, the execution is wrapped in a single
252 * transaction. These statements can be reused immediately, and
253 * reset does not need to be called.
255 * @param aStatements
256 * The array of statements to execute asynchronously, in the order they
257 * are given in the array.
258 * @param aCallback [optional]
259 * The callback object that will be notified of progress, errors, and
260 * completion.
261 * @return an object that can be used to cancel the statements execution.
263 * @note If you have any custom defined functions, they must be
264 * re-entrant since they can be called on multiple threads.
266 mozIStoragePendingStatement executeAsync(
267 in Array<mozIStorageBaseStatement> aStatements,
268 [optional] in mozIStorageStatementCallback aCallback
272 * Execute asynchronously an SQL expression, expecting no arguments.
274 * @param aSQLStatement
275 * The SQL statement to execute
276 * @param aCallback [optional]
277 * The callback object that will be notified of progress, errors, and
278 * completion.
279 * @return an object that can be used to cancel the statement execution.
281 mozIStoragePendingStatement executeSimpleSQLAsync(
282 in AUTF8String aSQLStatement,
283 [optional] in mozIStorageStatementCallback aCallback);
285 //////////////////////////////////////////////////////////////////////////////
286 //// Functions
289 * Create a new SQL function. If you use your connection on multiple threads,
290 * your function needs to be threadsafe, or it should only be called on one
291 * thread.
293 * @param aFunctionName
294 * The name of function to create, as seen in SQL.
295 * @param aNumArguments
296 * The number of arguments the function takes. Pass -1 for
297 * variable-argument functions.
298 * @param aFunction
299 * The instance of mozIStorageFunction, which implements the function
300 * in question.
302 void createFunction(in AUTF8String aFunctionName,
303 in long aNumArguments,
304 in mozIStorageFunction aFunction);
307 * Delete custom SQL function.
309 * @param aFunctionName
310 * The name of function to remove.
312 void removeFunction(in AUTF8String aFunctionName);
315 * Sets a progress handler. Only one handler can be registered at a time.
316 * If you need more than one, you need to chain them yourself. This progress
317 * handler should be threadsafe if you use this connection object on more than
318 * one thread.
320 * @param aGranularity
321 * The number of SQL virtual machine steps between progress handler
322 * callbacks.
323 * @param aHandler
324 * The instance of mozIStorageProgressHandler.
325 * @return previous registered handler.
327 mozIStorageProgressHandler setProgressHandler(in int32_t aGranularity,
328 in mozIStorageProgressHandler aHandler);
331 * Remove a progress handler.
333 * @return previous registered handler.
335 mozIStorageProgressHandler removeProgressHandler();