2010-03-24 10:32:40 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 sts=2 expandtab
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
#ifndef mozilla_storage_StorageBaseStatementInternal_h_
|
|
|
|
#define mozilla_storage_StorageBaseStatementInternal_h_
|
|
|
|
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2017-04-05 21:08:25 +03:00
|
|
|
#include "mozStorageHelper.h"
|
2010-03-24 10:32:40 +03:00
|
|
|
|
2014-04-24 13:54:09 +04:00
|
|
|
struct sqlite3;
|
2010-03-24 10:32:40 +03:00
|
|
|
struct sqlite3_stmt;
|
|
|
|
class mozIStorageBindingParamsArray;
|
|
|
|
class mozIStorageBindingParams;
|
|
|
|
class mozIStorageStatementCallback;
|
|
|
|
class mozIStoragePendingStatement;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
|
|
|
#define STORAGEBASESTATEMENTINTERNAL_IID \
|
|
|
|
{ \
|
|
|
|
0xd18856c9, 0xbf07, 0x4ae2, { \
|
|
|
|
0x94, 0x5b, 0x1a, 0xdd, 0x49, 0x19, 0x55, 0x2a \
|
|
|
|
} \
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
class Connection;
|
2011-01-21 06:49:09 +03:00
|
|
|
class StatementData;
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
class AsyncStatementFinalizer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation-only interface and shared logix mix-in corresponding to
|
|
|
|
* mozIStorageBaseStatement. Both Statement and AsyncStatement inherit from
|
|
|
|
* this. The interface aspect makes them look the same to implementation innards
|
|
|
|
* that aren't publicly accessible. The mix-in avoids code duplication in
|
|
|
|
* common implementations of mozIStorageBaseStatement, albeit with some minor
|
|
|
|
* performance/space overhead because we have to use defines to officially
|
|
|
|
* implement the methods on Statement/AsyncStatement (and proxy to this base
|
|
|
|
* class.)
|
|
|
|
*/
|
|
|
|
class StorageBaseStatementInternal : public nsISupports {
|
|
|
|
public:
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(STORAGEBASESTATEMENTINTERNAL_IID)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the connection that this statement belongs to.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
Connection* getOwner() { return mDBConnection; }
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the asynchronous statement, creating it if required.
|
|
|
|
*
|
|
|
|
* This is for use by the asynchronous execution code for StatementData
|
|
|
|
* created by AsyncStatements. Statement internally uses this method to
|
|
|
|
* prepopulate StatementData with the sqlite3_stmt.
|
|
|
|
*
|
|
|
|
* @param[out] stmt
|
|
|
|
* The sqlite3_stmt for asynchronous use.
|
|
|
|
* @return The SQLite result code for creating the statement if created,
|
|
|
|
* SQLITE_OK if creation was not required.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual int getAsyncStatement(sqlite3_stmt** _stmt) = 0;
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains the StatementData needed for asynchronous execution.
|
|
|
|
*
|
|
|
|
* This is for use by Connection to retrieve StatementData from statements
|
|
|
|
* when executeAsync is invoked.
|
|
|
|
*
|
|
|
|
* @param[out] _data
|
|
|
|
* A reference to a StatementData object that will be populated
|
|
|
|
* upon successful execution of this method.
|
|
|
|
* @return NS_OK if we were able to assemble the data, failure otherwise.
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual nsresult getAsynchronousStatementData(StatementData& _data) = 0;
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new BindingParams to be owned by the provided binding params
|
|
|
|
* array. This method exists so that BindingParamsArray does not need
|
|
|
|
* factory logic to determine what type of BindingParams to instantiate.
|
|
|
|
*
|
|
|
|
* @param aOwner
|
|
|
|
* The binding params array to own the newly created binding params.
|
|
|
|
* @return The new mozIStorageBindingParams instance appropriate to the
|
|
|
|
* underlying statement type.
|
|
|
|
*/
|
|
|
|
virtual already_AddRefed<mozIStorageBindingParams> newBindingParams(
|
2019-05-01 11:47:10 +03:00
|
|
|
mozIStorageBindingParamsArray* aOwner) = 0;
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
protected: // mix-in bits are protected
|
|
|
|
StorageBaseStatementInternal();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Connection> mDBConnection;
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3* mNativeConnection;
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Our asynchronous statement.
|
|
|
|
*
|
|
|
|
* For Statement this is populated by the first invocation to
|
|
|
|
* getAsyncStatement.
|
|
|
|
*
|
|
|
|
* For AsyncStatement, this is null at creation time and initialized by the
|
|
|
|
* async thread when it calls getAsyncStatement the first time the statement
|
|
|
|
* is executed. (Or in the event of badly formed SQL, every time.)
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
sqlite3_stmt* mAsyncStatement;
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiate asynchronous finalization by dispatching an event to the
|
|
|
|
* asynchronous thread to finalize mAsyncStatement. This acquires a reference
|
|
|
|
* to this statement and proxies it back to the connection's owning thread
|
|
|
|
* for release purposes.
|
|
|
|
*
|
|
|
|
* In the event the asynchronous thread is already gone or we otherwise fail
|
|
|
|
* to dispatch an event to it we failover to invoking internalAsyncFinalize
|
|
|
|
* directly. (That's what the asynchronous finalizer would have called.)
|
|
|
|
*
|
|
|
|
* @note You must not call this method from your destructor because its
|
|
|
|
* operation assumes we are still alive. Call internalAsyncFinalize
|
|
|
|
* directly in that case.
|
|
|
|
*/
|
|
|
|
void asyncFinalize();
|
|
|
|
|
|
|
|
/**
|
2011-01-20 05:16:42 +03:00
|
|
|
* Cleanup the async sqlite3_stmt stored in mAsyncStatement if it exists by
|
|
|
|
* attempting to dispatch to the asynchronous thread if available, finalizing
|
|
|
|
* on this thread if it is not.
|
2010-03-24 10:32:40 +03:00
|
|
|
*
|
|
|
|
* @note Call this from your destructor, call asyncFinalize otherwise.
|
|
|
|
*/
|
2011-01-20 05:16:42 +03:00
|
|
|
void destructorAsyncFinalize();
|
2010-03-24 10:32:40 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHOD NewBindingParamsArray(mozIStorageBindingParamsArray** _array);
|
|
|
|
NS_IMETHOD ExecuteAsync(mozIStorageStatementCallback* aCallback,
|
|
|
|
mozIStoragePendingStatement** _stmt);
|
|
|
|
NS_IMETHOD EscapeStringForLIKE(const nsAString& aValue, char16_t aEscapeChar,
|
|
|
|
nsAString& _escapedString);
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
// Needs access to internalAsyncFinalize
|
|
|
|
friend class AsyncStatementFinalizer;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(StorageBaseStatementInternal,
|
|
|
|
STORAGEBASESTATEMENTINTERNAL_IID)
|
|
|
|
|
|
|
|
#define NS_DECL_STORAGEBASESTATEMENTINTERNAL \
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual Connection* getOwner(); \
|
|
|
|
virtual int getAsyncStatement(sqlite3_stmt** _stmt) override; \
|
|
|
|
virtual nsresult getAsynchronousStatementData(StatementData& _data) \
|
2015-03-21 19:28:04 +03:00
|
|
|
override; \
|
2010-03-24 10:32:40 +03:00
|
|
|
virtual already_AddRefed<mozIStorageBindingParams> newBindingParams( \
|
2019-05-01 11:47:10 +03:00
|
|
|
mozIStorageBindingParamsArray* aOwner) override;
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper macro to implement the proxying implementations. Because we are
|
|
|
|
* implementing methods that are part of mozIStorageBaseStatement and the
|
|
|
|
* implementation classes already use NS_DECL_MOZISTORAGEBASESTATEMENT we don't
|
|
|
|
* need to provide declaration support.
|
|
|
|
*/
|
|
|
|
#define MIX_IMPL(_class, _optionalGuard, _method, _declArgs, _invokeArgs) \
|
|
|
|
NS_IMETHODIMP _class::_method _declArgs { \
|
|
|
|
_optionalGuard return StorageBaseStatementInternal::_method _invokeArgs; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define proxying implementation for the given _class. If a state invariant
|
|
|
|
* needs to be checked and an early return possibly performed, pass the clause
|
|
|
|
* to use as _optionalGuard.
|
|
|
|
*/
|
|
|
|
#define MIXIN_IMPL_STORAGEBASESTATEMENTINTERNAL(_class, _optionalGuard) \
|
|
|
|
MIX_IMPL(_class, _optionalGuard, NewBindingParamsArray, \
|
|
|
|
(mozIStorageBindingParamsArray * *_array), (_array)) \
|
|
|
|
MIX_IMPL(_class, _optionalGuard, ExecuteAsync, \
|
|
|
|
(mozIStorageStatementCallback * aCallback, \
|
|
|
|
mozIStoragePendingStatement * *_stmt), \
|
|
|
|
(aCallback, _stmt)) \
|
|
|
|
MIX_IMPL(_class, _optionalGuard, EscapeStringForLIKE, \
|
2019-05-01 11:47:10 +03:00
|
|
|
(const nsAString& aValue, char16_t aEscapeChar, \
|
|
|
|
nsAString& _escapedString), \
|
2010-03-24 10:32:40 +03:00
|
|
|
(aValue, aEscapeChar, _escapedString))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name-building helper for BIND_GEN_IMPL.
|
|
|
|
*/
|
|
|
|
#define BIND_NAME_CONCAT(_nameBit, _concatBit) Bind##_nameBit##_concatBit
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We have type-specific convenience methods for C++ implementations in
|
2017-08-06 03:17:50 +03:00
|
|
|
* two different forms; by index and by name. The following macro allows
|
2010-03-24 10:32:40 +03:00
|
|
|
* us to avoid having to define repetitive things by hand.
|
|
|
|
*
|
|
|
|
* Because of limitations of macros and our desire to avoid requiring special
|
|
|
|
* permutations for the null and blob cases (whose argument count varies),
|
|
|
|
* we require that the argument declarations and corresponding invocation
|
|
|
|
* usages are passed in.
|
|
|
|
*
|
|
|
|
* @param _class
|
|
|
|
* The class name.
|
|
|
|
* @param _guard
|
|
|
|
* The guard clause to inject.
|
|
|
|
* @param _declName
|
|
|
|
* The argument list (with parens) for the ByName variants.
|
|
|
|
* @param _declIndex
|
2017-08-06 03:17:50 +03:00
|
|
|
* The argument list (with parens) for the ByIndex variants.
|
2010-03-24 10:32:40 +03:00
|
|
|
* @param _invArgs
|
|
|
|
* The invocation argumment list.
|
|
|
|
*/
|
|
|
|
#define BIND_GEN_IMPL(_class, _guard, _name, _declName, _declIndex, _invArgs) \
|
|
|
|
NS_IMETHODIMP _class::BIND_NAME_CONCAT(_name, ByName) _declName { \
|
2019-05-01 11:47:10 +03:00
|
|
|
_guard mozIStorageBindingParams* params = getParams(); \
|
2010-03-24 10:32:40 +03:00
|
|
|
NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY); \
|
|
|
|
return params->BIND_NAME_CONCAT(_name, ByName) _invArgs; \
|
|
|
|
} \
|
|
|
|
NS_IMETHODIMP _class::BIND_NAME_CONCAT(_name, ByIndex) _declIndex { \
|
2019-05-01 11:47:10 +03:00
|
|
|
_guard mozIStorageBindingParams* params = getParams(); \
|
2010-03-24 10:32:40 +03:00
|
|
|
NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY); \
|
|
|
|
return params->BIND_NAME_CONCAT(_name, ByIndex) _invArgs; \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement BindByName/BindByIndex for the given class.
|
|
|
|
*
|
|
|
|
* @param _class The class name.
|
|
|
|
* @param _optionalGuard The guard clause to inject.
|
|
|
|
*/
|
|
|
|
#define BIND_BASE_IMPLS(_class, _optionalGuard) \
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _class::BindByName(const nsACString& aName, \
|
|
|
|
nsIVariant* aValue) { \
|
|
|
|
_optionalGuard mozIStorageBindingParams* params = getParams(); \
|
2010-03-24 10:32:40 +03:00
|
|
|
NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY); \
|
|
|
|
return params->BindByName(aName, aValue); \
|
|
|
|
} \
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _class::BindByIndex(uint32_t aIndex, nsIVariant* aValue) { \
|
|
|
|
_optionalGuard mozIStorageBindingParams* params = getParams(); \
|
2010-03-24 10:32:40 +03:00
|
|
|
NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY); \
|
|
|
|
return params->BindByIndex(aIndex, aValue); \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-06 03:17:50 +03:00
|
|
|
* Define the various Bind*ByIndex, Bind*ByName stubs that just end up proxying
|
|
|
|
* to the params object.
|
2010-03-24 10:32:40 +03:00
|
|
|
*/
|
|
|
|
#define BOILERPLATE_BIND_PROXIES(_class, _optionalGuard) \
|
|
|
|
BIND_BASE_IMPLS(_class, _optionalGuard) \
|
|
|
|
BIND_GEN_IMPL(_class, _optionalGuard, UTF8String, \
|
2019-05-01 11:47:10 +03:00
|
|
|
(const nsACString& aWhere, const nsACString& aValue), \
|
|
|
|
(uint32_t aWhere, const nsACString& aValue), (aWhere, aValue)) \
|
2010-03-24 10:32:40 +03:00
|
|
|
BIND_GEN_IMPL(_class, _optionalGuard, String, \
|
2019-05-01 11:47:10 +03:00
|
|
|
(const nsACString& aWhere, const nsAString& aValue), \
|
|
|
|
(uint32_t aWhere, const nsAString& aValue), (aWhere, aValue)) \
|
2010-03-24 10:32:40 +03:00
|
|
|
BIND_GEN_IMPL(_class, _optionalGuard, Double, \
|
2019-05-01 11:47:10 +03:00
|
|
|
(const nsACString& aWhere, double aValue), \
|
2012-08-22 19:56:38 +04:00
|
|
|
(uint32_t aWhere, double aValue), (aWhere, aValue)) \
|
2010-03-24 10:32:40 +03:00
|
|
|
BIND_GEN_IMPL(_class, _optionalGuard, Int32, \
|
2019-05-01 11:47:10 +03:00
|
|
|
(const nsACString& aWhere, int32_t aValue), \
|
2012-08-22 19:56:38 +04:00
|
|
|
(uint32_t aWhere, int32_t aValue), (aWhere, aValue)) \
|
2010-03-24 10:32:40 +03:00
|
|
|
BIND_GEN_IMPL(_class, _optionalGuard, Int64, \
|
2019-05-01 11:47:10 +03:00
|
|
|
(const nsACString& aWhere, int64_t aValue), \
|
2012-08-22 19:56:38 +04:00
|
|
|
(uint32_t aWhere, int64_t aValue), (aWhere, aValue)) \
|
2019-05-01 11:47:10 +03:00
|
|
|
BIND_GEN_IMPL(_class, _optionalGuard, Null, (const nsACString& aWhere), \
|
2012-08-22 19:56:38 +04:00
|
|
|
(uint32_t aWhere), (aWhere)) \
|
2010-03-24 10:32:40 +03:00
|
|
|
BIND_GEN_IMPL( \
|
|
|
|
_class, _optionalGuard, Blob, \
|
2019-05-01 11:47:10 +03:00
|
|
|
(const nsACString& aWhere, const uint8_t* aValue, uint32_t aValueSize), \
|
|
|
|
(uint32_t aWhere, const uint8_t* aValue, uint32_t aValueSize), \
|
2014-03-07 01:03:52 +04:00
|
|
|
(aWhere, aValue, aValueSize)) \
|
2019-05-20 19:30:12 +03:00
|
|
|
BIND_GEN_IMPL(_class, _optionalGuard, BlobArray, \
|
|
|
|
(const nsACString& aWhere, const nsTArray<uint8_t>& aValue), \
|
|
|
|
(uint32_t aWhere, const nsTArray<uint8_t>& aValue), \
|
|
|
|
(aWhere, aValue)) \
|
2015-04-29 19:03:15 +03:00
|
|
|
BIND_GEN_IMPL(_class, _optionalGuard, StringAsBlob, \
|
2019-05-01 11:47:10 +03:00
|
|
|
(const nsACString& aWhere, const nsAString& aValue), \
|
|
|
|
(uint32_t aWhere, const nsAString& aValue), (aWhere, aValue)) \
|
2015-04-29 19:03:15 +03:00
|
|
|
BIND_GEN_IMPL(_class, _optionalGuard, UTF8StringAsBlob, \
|
2019-05-01 11:47:10 +03:00
|
|
|
(const nsACString& aWhere, const nsACString& aValue), \
|
|
|
|
(uint32_t aWhere, const nsACString& aValue), (aWhere, aValue)) \
|
2014-03-07 01:03:52 +04:00
|
|
|
BIND_GEN_IMPL( \
|
|
|
|
_class, _optionalGuard, AdoptedBlob, \
|
2019-05-01 11:47:10 +03:00
|
|
|
(const nsACString& aWhere, uint8_t* aValue, uint32_t aValueSize), \
|
2014-03-07 01:03:52 +04:00
|
|
|
(uint32_t aWhere, uint8_t * aValue, uint32_t aValueSize), \
|
2010-03-24 10:32:40 +03:00
|
|
|
(aWhere, aValue, aValueSize))
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
#endif // mozilla_storage_StorageBaseStatementInternal_h_
|