2009-05-09 04:29:56 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
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/. */
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
#include <limits.h>
|
2004-10-09 04:04:10 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "nsError.h"
|
2004-12-03 19:56:57 +03:00
|
|
|
#include "nsMemory.h"
|
2009-07-28 21:21:03 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2008-09-13 01:30:41 +04:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2010-03-24 10:32:40 +03:00
|
|
|
#include "Variant.h"
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-07-28 21:21:03 +04:00
|
|
|
#include "mozIStorageError.h"
|
|
|
|
|
|
|
|
#include "mozStorageBindingParams.h"
|
2004-10-09 18:33:01 +04:00
|
|
|
#include "mozStorageConnection.h"
|
2008-09-13 01:30:41 +04:00
|
|
|
#include "mozStorageStatementJSHelper.h"
|
2008-10-31 01:50:00 +03:00
|
|
|
#include "mozStoragePrivateHelpers.h"
|
2008-11-19 22:33:00 +03:00
|
|
|
#include "mozStorageStatementParams.h"
|
|
|
|
#include "mozStorageStatementRow.h"
|
2009-04-08 20:24:25 +04:00
|
|
|
#include "mozStorageStatement.h"
|
2013-03-18 18:25:50 +04:00
|
|
|
#include "GeckoProfiler.h"
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2016-12-09 23:27:17 +03:00
|
|
|
#include "mozilla/Printf.h"
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2016-01-28 21:35:00 +03:00
|
|
|
extern mozilla::LazyLogModule gStorageLog;
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
2009-04-08 20:24:25 +04:00
|
|
|
|
2008-09-13 01:30:41 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIClassInfo
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(Statement, mozIStorageStatement,
|
|
|
|
mozIStorageBaseStatement, mozIStorageBindingParams,
|
|
|
|
mozIStorageValueArray,
|
|
|
|
mozilla::storage::StorageBaseStatementInternal)
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
class StatementClassInfo : public nsIClassInfo {
|
2008-09-13 01:30:41 +04:00
|
|
|
public:
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr StatementClassInfo() {}
|
2013-11-22 23:45:50 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2009-05-09 04:29:56 +04:00
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2019-02-14 00:42:08 +03:00
|
|
|
GetInterfaces(nsTArray<nsIID>& _array) override {
|
|
|
|
return NS_CI_INTERFACE_GETTER_NAME(Statement)(_array);
|
2009-05-09 04:29:56 +04:00
|
|
|
}
|
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2015-03-29 17:52:54 +03:00
|
|
|
GetScriptableHelper(nsIXPCScriptable** _helper) override {
|
|
|
|
static StatementJSHelper sJSHelper;
|
|
|
|
*_helper = &sJSHelper;
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2017-10-18 05:17:26 +03:00
|
|
|
GetContractID(nsACString& aContractID) override {
|
|
|
|
aContractID.SetIsVoid(true);
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2017-10-18 05:17:26 +03:00
|
|
|
GetClassDescription(nsACString& aDesc) override {
|
|
|
|
aDesc.SetIsVoid(true);
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
GetClassID(nsCID** _id) override {
|
2012-07-30 18:20:58 +04:00
|
|
|
*_id = nullptr;
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
GetFlags(uint32_t* _flags) override {
|
2012-07-20 15:16:17 +04:00
|
|
|
*_flags = 0;
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
GetClassIDNoAlloc(nsCID* _cid) override { return NS_ERROR_NOT_AVAILABLE; }
|
2004-10-09 04:04:10 +04:00
|
|
|
};
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) StatementClassInfo::AddRef() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) StatementClassInfo::Release() {
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(StatementClassInfo, nsIClassInfo)
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
static StatementClassInfo sStatementClassInfo;
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Statement
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
Statement::Statement()
|
2018-04-13 16:01:28 +03:00
|
|
|
: StorageBaseStatementInternal(),
|
|
|
|
mDBStatement(nullptr),
|
2018-06-15 10:42:19 +03:00
|
|
|
mParamCount(0),
|
|
|
|
mResultColumnCount(0),
|
2018-04-13 16:01:28 +03:00
|
|
|
mColumnNames(),
|
|
|
|
mExecuting(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
nsresult Statement::initialize(Connection* aDBConnection,
|
2014-04-24 13:54:09 +04:00
|
|
|
sqlite3* aNativeConnection,
|
2009-05-09 04:29:56 +04:00
|
|
|
const nsACString& aSQLStatement) {
|
2014-04-24 13:54:09 +04:00
|
|
|
MOZ_ASSERT(aDBConnection, "No database connection given!");
|
2017-06-16 18:43:23 +03:00
|
|
|
MOZ_ASSERT(aDBConnection->isConnectionReadyOnThisThread(),
|
|
|
|
"Database connection should be valid");
|
2014-04-24 13:54:09 +04:00
|
|
|
MOZ_ASSERT(!mDBStatement, "Statement already initialized!");
|
|
|
|
MOZ_ASSERT(aNativeConnection, "No native connection given!");
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2014-04-24 13:54:12 +04:00
|
|
|
int srv = aDBConnection->prepareStatement(
|
|
|
|
aNativeConnection, PromiseFlatCString(aSQLStatement), &mDBStatement);
|
2009-05-09 04:29:56 +04:00
|
|
|
if (srv != SQLITE_OK) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Error,
|
2009-05-09 04:29:56 +04:00
|
|
|
("Sqlite statement prepare error: %d '%s'", srv,
|
2014-04-24 13:54:09 +04:00
|
|
|
::sqlite3_errmsg(aNativeConnection)));
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Error,
|
2009-05-09 04:29:56 +04:00
|
|
|
("Statement was: '%s'", PromiseFlatCString(aSQLStatement).get()));
|
|
|
|
return NS_ERROR_FAILURE;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Debug,
|
|
|
|
("Initialized statement '%s' (0x%p)",
|
2009-05-09 04:29:56 +04:00
|
|
|
PromiseFlatCString(aSQLStatement).get(), mDBStatement));
|
2009-03-20 23:28:16 +03:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
mDBConnection = aDBConnection;
|
2014-04-24 13:54:09 +04:00
|
|
|
mNativeConnection = aNativeConnection;
|
2009-05-09 04:29:56 +04:00
|
|
|
mParamCount = ::sqlite3_bind_parameter_count(mDBStatement);
|
|
|
|
mResultColumnCount = ::sqlite3_column_count(mDBStatement);
|
|
|
|
mColumnNames.Clear();
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2015-11-18 18:25:19 +03:00
|
|
|
nsCString* columnNames = mColumnNames.AppendElements(mResultColumnCount);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mResultColumnCount; i++) {
|
2009-05-09 04:29:56 +04:00
|
|
|
const char* name = ::sqlite3_column_name(mDBStatement, i);
|
2015-11-18 18:25:19 +03:00
|
|
|
columnNames[i].Assign(name);
|
2009-05-09 04:29:56 +04:00
|
|
|
}
|
2004-12-03 19:56:57 +03:00
|
|
|
|
2007-08-30 05:56:14 +04:00
|
|
|
#ifdef DEBUG
|
2009-05-09 04:29:56 +04:00
|
|
|
// We want to try and test for LIKE and that consumers are using
|
|
|
|
// escapeStringForLIKE instead of just trusting user input. The idea to
|
|
|
|
// check to see if they are binding a parameter after like instead of just
|
|
|
|
// using a string. We only do this in debug builds because it's expensive!
|
2020-05-27 21:11:12 +03:00
|
|
|
auto c = nsCaseInsensitiveCStringComparator;
|
2009-05-09 04:29:56 +04:00
|
|
|
nsACString::const_iterator start, end, e;
|
|
|
|
aSQLStatement.BeginReading(start);
|
|
|
|
aSQLStatement.EndReading(end);
|
|
|
|
e = end;
|
2020-07-01 11:29:29 +03:00
|
|
|
while (::FindInReadable(" LIKE"_ns, start, e, c)) {
|
2009-05-09 04:29:56 +04:00
|
|
|
// We have a LIKE in here, so we perform our tests
|
|
|
|
// FindInReadable moves the iterator, so we have to get a new one for
|
|
|
|
// each test we perform.
|
|
|
|
nsACString::const_iterator s1, s2, s3;
|
|
|
|
s1 = s2 = s3 = start;
|
|
|
|
|
2020-07-01 11:29:29 +03:00
|
|
|
if (!(::FindInReadable(" LIKE ?"_ns, s1, end, c) ||
|
|
|
|
::FindInReadable(" LIKE :"_ns, s2, end, c) ||
|
|
|
|
::FindInReadable(" LIKE @"_ns, s3, end, c))) {
|
2009-05-09 04:29:56 +04:00
|
|
|
// At this point, we didn't find a LIKE statement followed by ?, :,
|
|
|
|
// or @, all of which are valid characters for binding a parameter.
|
|
|
|
// We will warn the consumer that they may not be safely using LIKE.
|
|
|
|
NS_WARNING(
|
|
|
|
"Unsafe use of LIKE detected! Please ensure that you "
|
2009-11-14 12:15:12 +03:00
|
|
|
"are using mozIStorageStatement::escapeStringForLIKE "
|
2009-05-09 04:29:56 +04:00
|
|
|
"and that you are binding that result to the statement "
|
|
|
|
"to prevent SQL injection attacks.");
|
2007-08-30 05:56:14 +04:00
|
|
|
}
|
2009-05-09 04:29:56 +04:00
|
|
|
|
|
|
|
// resetting start and e
|
|
|
|
start = e;
|
|
|
|
e = end;
|
|
|
|
}
|
2007-08-30 05:56:14 +04:00
|
|
|
#endif
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
mozIStorageBindingParams* Statement::getParams() {
|
2009-07-28 21:21:03 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// If we do not have an array object yet, make it.
|
|
|
|
if (!mParamsArray) {
|
|
|
|
nsCOMPtr<mozIStorageBindingParamsArray> array;
|
|
|
|
rv = NewBindingParamsArray(getter_AddRefs(array));
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-07-28 21:21:03 +04:00
|
|
|
|
|
|
|
mParamsArray = static_cast<BindingParamsArray*>(array.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there isn't already any rows added, we'll have to add one to use.
|
|
|
|
if (mParamsArray->length() == 0) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BindingParams> params(new BindingParams(mParamsArray, this));
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(params, nullptr);
|
2009-07-28 21:21:03 +04:00
|
|
|
|
|
|
|
rv = mParamsArray->AddParams(params);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-07-28 21:21:03 +04:00
|
|
|
|
|
|
|
// We have to unlock our params because AddParams locks them. This is safe
|
|
|
|
// because no reference to the params object was, or ever will be given out.
|
2010-03-24 10:32:40 +03:00
|
|
|
params->unlock(this);
|
2009-07-28 21:21:03 +04:00
|
|
|
|
|
|
|
// We also want to lock our array at this point - we don't want anything to
|
|
|
|
// be added to it. Nothing has, or will ever get a reference to it, but we
|
|
|
|
// will get additional safety checks via assertions by doing this.
|
|
|
|
mParamsArray->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return *mParamsArray->begin();
|
|
|
|
}
|
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
Statement::~Statement() { (void)internalFinalize(true); }
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsISupports
|
|
|
|
|
2013-07-19 06:24:15 +04:00
|
|
|
NS_IMPL_ADDREF(Statement)
|
|
|
|
NS_IMPL_RELEASE(Statement)
|
2009-05-09 04:29:56 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(Statement)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageStatement)
|
2010-03-24 10:32:40 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageBaseStatement)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageBindingParams)
|
2009-05-09 04:29:56 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageValueArray)
|
2010-03-24 10:32:40 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(mozilla::storage::StorageBaseStatementInternal)
|
2009-05-09 04:29:56 +04:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIClassInfo))) {
|
|
|
|
foundInterface = static_cast<nsIClassInfo*>(&sStatementClassInfo);
|
|
|
|
} else
|
2010-03-24 10:32:40 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageStatement)
|
2009-05-09 04:29:56 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// StorageBaseStatementInternal
|
|
|
|
|
|
|
|
Connection* Statement::getOwner() { return mDBConnection; }
|
|
|
|
|
|
|
|
int Statement::getAsyncStatement(sqlite3_stmt** _stmt) {
|
|
|
|
// If we have no statement, we shouldn't be calling this method!
|
2013-07-31 19:44:58 +04:00
|
|
|
NS_ASSERTION(mDBStatement != nullptr, "We have no statement to clone!");
|
2010-03-24 10:32:40 +03:00
|
|
|
|
|
|
|
// If we do not yet have a cached async statement, clone our statement now.
|
|
|
|
if (!mAsyncStatement) {
|
2010-06-22 01:23:00 +04:00
|
|
|
nsDependentCString sql(::sqlite3_sql(mDBStatement));
|
2014-04-24 13:54:12 +04:00
|
|
|
int rc = mDBConnection->prepareStatement(mNativeConnection, sql,
|
|
|
|
&mAsyncStatement);
|
2010-03-24 10:32:40 +03:00
|
|
|
if (rc != SQLITE_OK) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*_stmt = nullptr;
|
2010-03-24 10:32:40 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Debug,
|
2010-03-24 10:32:40 +03:00
|
|
|
("Cloned statement 0x%p to 0x%p", mDBStatement, mAsyncStatement));
|
|
|
|
}
|
|
|
|
|
|
|
|
*_stmt = mAsyncStatement;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Statement::getAsynchronousStatementData(StatementData& _data) {
|
|
|
|
if (!mDBStatement) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
sqlite3_stmt* stmt;
|
|
|
|
int rc = getAsyncStatement(&stmt);
|
|
|
|
if (rc != SQLITE_OK) return convertResultCode(rc);
|
|
|
|
|
|
|
|
_data = StatementData(stmt, bindingParamsArray(), this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<mozIStorageBindingParams> Statement::newBindingParams(
|
|
|
|
mozIStorageBindingParamsArray* aOwner) {
|
|
|
|
nsCOMPtr<mozIStorageBindingParams> params = new BindingParams(aOwner, this);
|
|
|
|
return params.forget();
|
|
|
|
}
|
|
|
|
|
2008-09-13 01:30:41 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageStatement
|
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
// proxy to StorageBaseStatementInternal using its define helper.
|
|
|
|
MIXIN_IMPL_STORAGEBASESTATEMENTINTERNAL(Statement, (void)0;)
|
|
|
|
|
2004-10-09 04:04:10 +04:00
|
|
|
NS_IMETHODIMP
|
2009-05-09 04:29:56 +04:00
|
|
|
Statement::Clone(mozIStorageStatement** _statement) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Statement> statement(new Statement());
|
2009-05-09 04:29:56 +04:00
|
|
|
NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString sql(::sqlite3_sql(mDBStatement));
|
2014-04-24 13:54:09 +04:00
|
|
|
nsresult rv = statement->initialize(mDBConnection, mNativeConnection, sql);
|
2009-05-09 04:29:56 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
statement.forget(_statement);
|
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
2007-09-19 07:26:51 +04:00
|
|
|
NS_IMETHODIMP
|
2010-03-24 10:32:40 +03:00
|
|
|
Statement::Finalize() { return internalFinalize(false); }
|
|
|
|
|
|
|
|
nsresult Statement::internalFinalize(bool aDestructing) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement) return NS_OK;
|
2008-11-19 22:33:00 +03:00
|
|
|
|
2013-09-04 20:51:45 +04:00
|
|
|
int srv = SQLITE_OK;
|
|
|
|
|
2017-06-16 18:43:23 +03:00
|
|
|
{
|
|
|
|
// If the statement ends up being finalized twice, the second finalization
|
|
|
|
// would apply to a dangling pointer and may cause unexpected consequences.
|
|
|
|
// Thus we must be sure that the connection state won't change during this
|
|
|
|
// operation, to avoid racing with finalizations made by the closing
|
|
|
|
// connection. See Connection::internalClose().
|
|
|
|
MutexAutoLock lockedScope(mDBConnection->sharedAsyncExecutionMutex);
|
|
|
|
if (!mDBConnection->isClosed(lockedScope)) {
|
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Debug,
|
|
|
|
("Finalizing statement '%s' during garbage-collection",
|
|
|
|
::sqlite3_sql(mDBStatement)));
|
|
|
|
srv = ::sqlite3_finalize(mDBStatement);
|
|
|
|
}
|
2013-09-04 20:51:45 +04:00
|
|
|
#ifdef DEBUG
|
2017-06-16 18:43:23 +03:00
|
|
|
else {
|
|
|
|
// The database connection is closed. The sqlite
|
|
|
|
// statement has either been finalized already by the connection
|
|
|
|
// or is about to be finalized by the connection.
|
|
|
|
//
|
|
|
|
// Finalizing it here would be useless and segfaultish.
|
|
|
|
//
|
|
|
|
// Note that we can't display the statement itself, as the data structure
|
|
|
|
// is not valid anymore. However, the address shown here should help
|
|
|
|
// developers correlate with the more complete debug message triggered
|
|
|
|
// by AsyncClose().
|
|
|
|
|
|
|
|
SmprintfPointer msg = ::mozilla::Smprintf(
|
|
|
|
"SQL statement (%p) should have been finalized"
|
|
|
|
" before garbage-collection. For more details on this statement, set"
|
|
|
|
" NSPR_LOG_MESSAGES=mozStorage:5 .",
|
|
|
|
mDBStatement);
|
|
|
|
NS_WARNING(msg.get());
|
|
|
|
|
|
|
|
// Use %s so we aren't exposing random strings to printf interpolation.
|
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Warning, ("%s", msg.get()));
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
2013-09-04 20:51:45 +04:00
|
|
|
}
|
2013-09-19 17:19:02 +04:00
|
|
|
|
2013-07-31 19:44:58 +04:00
|
|
|
mDBStatement = nullptr;
|
2008-11-19 22:33:00 +03:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
if (mAsyncStatement) {
|
|
|
|
// If the destructor called us, there are no pending async statements (they
|
|
|
|
// hold a reference to us) and we can/must just kill the statement directly.
|
|
|
|
if (aDestructing)
|
2011-01-20 05:16:42 +03:00
|
|
|
destructorAsyncFinalize();
|
2010-03-24 10:32:40 +03:00
|
|
|
else
|
|
|
|
asyncFinalize();
|
2009-07-28 21:21:03 +04:00
|
|
|
}
|
|
|
|
|
2015-03-12 19:55:56 +03:00
|
|
|
// Release the holders, so they can release the reference to us.
|
|
|
|
mStatementParamsHolder = nullptr;
|
|
|
|
mStatementRowHolder = nullptr;
|
2008-11-19 22:33:00 +03:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
return convertResultCode(srv);
|
2007-09-19 07:26:51 +04:00
|
|
|
}
|
|
|
|
|
2004-10-09 04:04:10 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetParameterCount(uint32_t* _parameterCount) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2004-12-03 19:56:57 +03:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
*_parameterCount = mParamCount;
|
|
|
|
return NS_OK;
|
2004-12-03 19:56:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetParameterName(uint32_t aParamIndex, nsACString& _name) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
ENSURE_INDEX_VALUE(aParamIndex, mParamCount);
|
2007-09-21 22:13:47 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
const char* name =
|
|
|
|
::sqlite3_bind_parameter_name(mDBStatement, aParamIndex + 1);
|
2013-07-31 19:44:58 +04:00
|
|
|
if (name == nullptr) {
|
2009-05-09 04:29:56 +04:00
|
|
|
// this thing had no name, so fake one
|
2016-06-08 08:08:41 +03:00
|
|
|
nsAutoCString fakeName(":");
|
|
|
|
fakeName.AppendInt(aParamIndex);
|
|
|
|
_name.Assign(fakeName);
|
2009-05-09 04:29:56 +04:00
|
|
|
} else {
|
|
|
|
_name.Assign(nsDependentCString(name));
|
|
|
|
}
|
2004-12-03 19:56:57 +03:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
2004-12-03 19:56:57 +03:00
|
|
|
}
|
|
|
|
|
2005-04-18 01:00:50 +04:00
|
|
|
NS_IMETHODIMP
|
2009-05-09 04:29:56 +04:00
|
|
|
Statement::GetParameterIndex(const nsACString& aName, uint32_t* _index) {
|
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2005-04-18 01:00:50 +04:00
|
|
|
|
2009-05-09 04:29:57 +04:00
|
|
|
// We do not accept any forms of names other than ":name", but we need to add
|
|
|
|
// the colon for SQLite.
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString name(":");
|
2009-05-09 04:29:57 +04:00
|
|
|
name.Append(aName);
|
2011-09-15 12:36:07 +04:00
|
|
|
int ind = ::sqlite3_bind_parameter_index(mDBStatement, name.get());
|
2009-05-09 04:29:56 +04:00
|
|
|
if (ind == 0) // Named parameter not found.
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-06-19 03:42:23 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
*_index = ind - 1; // SQLite indexes are 1-based, we are 0-based.
|
|
|
|
|
|
|
|
return NS_OK;
|
2005-04-18 01:00:50 +04:00
|
|
|
}
|
|
|
|
|
2004-12-03 19:56:57 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetColumnCount(uint32_t* _columnCount) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2007-09-21 22:13:47 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
*_columnCount = mResultColumnCount;
|
|
|
|
return NS_OK;
|
2004-12-03 19:56:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetColumnName(uint32_t aColumnIndex, nsACString& _name) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
ENSURE_INDEX_VALUE(aColumnIndex, mResultColumnCount);
|
2004-12-03 19:56:57 +03:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
const char* cname = ::sqlite3_column_name(mDBStatement, aColumnIndex);
|
|
|
|
_name.Assign(nsDependentCString(cname));
|
2004-12-03 19:56:57 +03:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
2007-07-19 20:30:17 +04:00
|
|
|
NS_IMETHODIMP
|
2009-05-09 04:29:56 +04:00
|
|
|
Statement::GetColumnIndex(const nsACString& aName, uint32_t* _index) {
|
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2007-07-19 20:30:17 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
// Surprisingly enough, SQLite doesn't provide an API for this. We have to
|
|
|
|
// determine it ourselves sadly.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mResultColumnCount; i++) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (mColumnNames[i].Equals(aName)) {
|
|
|
|
*_index = i;
|
|
|
|
return NS_OK;
|
2007-07-19 20:30:17 +04:00
|
|
|
}
|
2009-05-09 04:29:56 +04:00
|
|
|
}
|
2007-07-19 20:30:17 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-07-19 20:30:17 +04:00
|
|
|
}
|
|
|
|
|
2004-10-09 04:04:10 +04:00
|
|
|
NS_IMETHODIMP
|
2009-05-09 04:29:56 +04:00
|
|
|
Statement::Reset() {
|
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-03-12 21:30:15 +03:00
|
|
|
#ifdef DEBUG
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Debug,
|
2009-05-09 04:29:56 +04:00
|
|
|
("Resetting statement: '%s'", ::sqlite3_sql(mDBStatement)));
|
2005-04-27 02:28:06 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
checkAndLogStatementPerformance(mDBStatement);
|
2009-03-12 21:30:15 +03:00
|
|
|
#endif
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mParamsArray = nullptr;
|
2009-05-09 04:29:56 +04:00
|
|
|
(void)sqlite3_reset(mDBStatement);
|
|
|
|
(void)sqlite3_clear_bindings(mDBStatement);
|
2005-06-10 05:31:48 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
mExecuting = false;
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Statement::BindParameters(mozIStorageBindingParamsArray* aParameters) {
|
2016-01-20 18:37:00 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aParameters);
|
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
BindingParamsArray* array = static_cast<BindingParamsArray*>(aParameters);
|
|
|
|
if (array->getOwner() != this) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2010-03-15 20:23:39 +03:00
|
|
|
if (array->length() == 0) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2009-06-17 23:12:51 +04:00
|
|
|
mParamsArray = array;
|
|
|
|
mParamsArray->lock();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-10-09 04:04:10 +04:00
|
|
|
NS_IMETHODIMP
|
2009-05-09 04:29:56 +04:00
|
|
|
Statement::Execute() {
|
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ret;
|
2009-05-09 04:29:56 +04:00
|
|
|
nsresult rv = ExecuteStep(&ret);
|
2010-06-08 02:06:56 +04:00
|
|
|
nsresult rv2 = Reset();
|
2007-06-13 02:01:05 +04:00
|
|
|
|
2010-06-08 02:06:56 +04:00
|
|
|
return NS_FAILED(rv) ? rv : rv2;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
Statement::ExecuteStep(bool* _moreResults) {
|
2018-05-19 00:55:18 +03:00
|
|
|
AUTO_PROFILER_LABEL("Statement::ExecuteStep", OTHER);
|
2014-05-24 01:12:29 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2007-09-21 22:13:47 +04:00
|
|
|
|
2009-07-28 21:21:03 +04:00
|
|
|
// Bind any parameters first before executing.
|
|
|
|
if (mParamsArray) {
|
|
|
|
// If we have more than one row of parameters to bind, they shouldn't be
|
|
|
|
// calling this method (and instead use executeAsync).
|
|
|
|
if (mParamsArray->length() != 1) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
BindingParamsArray::iterator row = mParamsArray->begin();
|
2017-06-16 18:43:23 +03:00
|
|
|
nsCOMPtr<IStorageBindingParamsInternal> bindingInternal =
|
2010-03-24 10:32:40 +03:00
|
|
|
do_QueryInterface(*row);
|
|
|
|
nsCOMPtr<mozIStorageError> error = bindingInternal->bind(mDBStatement);
|
2009-07-28 21:21:03 +04:00
|
|
|
if (error) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t srv;
|
2009-07-28 21:21:03 +04:00
|
|
|
(void)error->GetResult(&srv);
|
|
|
|
return convertResultCode(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have bound, so now we can clear our array.
|
2012-07-30 18:20:58 +04:00
|
|
|
mParamsArray = nullptr;
|
2009-07-28 21:21:03 +04:00
|
|
|
}
|
2014-04-24 13:54:12 +04:00
|
|
|
int srv = mDBConnection->stepStatement(mNativeConnection, mDBStatement);
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
if (srv != SQLITE_ROW && srv != SQLITE_DONE &&
|
|
|
|
MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString errStr;
|
2009-05-09 04:29:56 +04:00
|
|
|
(void)mDBConnection->GetLastErrorString(errStr);
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Debug,
|
2009-05-09 04:29:56 +04:00
|
|
|
("Statement::ExecuteStep error: %s", errStr.get()));
|
|
|
|
}
|
2007-07-10 21:23:36 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
// SQLITE_ROW and SQLITE_DONE are non-errors
|
|
|
|
if (srv == SQLITE_ROW) {
|
|
|
|
// we got a row back
|
|
|
|
mExecuting = true;
|
2011-10-17 18:59:28 +04:00
|
|
|
*_moreResults = true;
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
} else if (srv == SQLITE_DONE) {
|
|
|
|
// statement is done (no row returned)
|
|
|
|
mExecuting = false;
|
2011-10-17 18:59:28 +04:00
|
|
|
*_moreResults = false;
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
} else if (srv == SQLITE_BUSY || srv == SQLITE_MISUSE) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mExecuting = false;
|
2009-05-09 04:29:56 +04:00
|
|
|
} else if (mExecuting) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Error,
|
2009-05-09 04:29:56 +04:00
|
|
|
("SQLite error after mExecuting was true!"));
|
2011-10-17 18:59:28 +04:00
|
|
|
mExecuting = false;
|
2009-05-09 04:29:56 +04:00
|
|
|
}
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
return convertResultCode(srv);
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
2004-12-03 19:56:57 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetState(int32_t* _state) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement)
|
|
|
|
*_state = MOZ_STORAGE_STATEMENT_INVALID;
|
|
|
|
else if (mExecuting)
|
|
|
|
*_state = MOZ_STORAGE_STATEMENT_EXECUTING;
|
|
|
|
else
|
|
|
|
*_state = MOZ_STORAGE_STATEMENT_READY;
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2010-03-24 10:32:40 +03:00
|
|
|
//// mozIStorageValueArray (now part of mozIStorageStatement too)
|
2009-05-09 04:29:56 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetNumEntries(uint32_t* _length) {
|
2009-05-09 04:29:56 +04:00
|
|
|
*_length = mResultColumnCount;
|
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetTypeOfIndex(uint32_t aIndex, int32_t* _type) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2007-09-21 22:13:47 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mResultColumnCount);
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mExecuting) return NS_ERROR_UNEXPECTED;
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
int t = ::sqlite3_column_type(mDBStatement, aIndex);
|
|
|
|
switch (t) {
|
|
|
|
case SQLITE_INTEGER:
|
2010-03-24 10:32:40 +03:00
|
|
|
*_type = mozIStorageStatement::VALUE_TYPE_INTEGER;
|
2009-05-09 04:29:56 +04:00
|
|
|
break;
|
|
|
|
case SQLITE_FLOAT:
|
2010-03-24 10:32:40 +03:00
|
|
|
*_type = mozIStorageStatement::VALUE_TYPE_FLOAT;
|
2009-05-09 04:29:56 +04:00
|
|
|
break;
|
|
|
|
case SQLITE_TEXT:
|
2010-03-24 10:32:40 +03:00
|
|
|
*_type = mozIStorageStatement::VALUE_TYPE_TEXT;
|
2009-05-09 04:29:56 +04:00
|
|
|
break;
|
|
|
|
case SQLITE_BLOB:
|
2010-03-24 10:32:40 +03:00
|
|
|
*_type = mozIStorageStatement::VALUE_TYPE_BLOB;
|
2009-05-09 04:29:56 +04:00
|
|
|
break;
|
|
|
|
case SQLITE_NULL:
|
2010-03-24 10:32:40 +03:00
|
|
|
*_type = mozIStorageStatement::VALUE_TYPE_NULL;
|
2009-05-09 04:29:56 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetInt32(uint32_t aIndex, int32_t* _value) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2007-09-21 22:13:47 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mResultColumnCount);
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mExecuting) return NS_ERROR_UNEXPECTED;
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
*_value = ::sqlite3_column_int(mDBStatement, aIndex);
|
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetInt64(uint32_t aIndex, int64_t* _value) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2007-09-21 22:13:47 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mResultColumnCount);
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mExecuting) return NS_ERROR_UNEXPECTED;
|
2007-09-21 22:13:47 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
*_value = ::sqlite3_column_int64(mDBStatement, aIndex);
|
|
|
|
|
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetDouble(uint32_t aIndex, double* _value) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mResultColumnCount);
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mExecuting) return NS_ERROR_UNEXPECTED;
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
*_value = ::sqlite3_column_double(mDBStatement, aIndex);
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
2005-11-15 03:35:50 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetUTF8String(uint32_t aIndex, nsACString& _value) {
|
2009-05-09 04:29:56 +04:00
|
|
|
// Get type of Index will check aIndex for us, so we don't have to.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t type;
|
2009-05-09 04:29:56 +04:00
|
|
|
nsresult rv = GetTypeOfIndex(aIndex, &type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-03-24 10:32:40 +03:00
|
|
|
if (type == mozIStorageStatement::VALUE_TYPE_NULL) {
|
2010-07-29 23:17:42 +04:00
|
|
|
// NULL columns should have IsVoid set to distinguish them from the empty
|
2009-05-09 04:29:56 +04:00
|
|
|
// string.
|
2011-10-17 18:59:28 +04:00
|
|
|
_value.SetIsVoid(true);
|
2009-05-09 04:29:56 +04:00
|
|
|
} else {
|
|
|
|
const char* value = reinterpret_cast<const char*>(
|
|
|
|
::sqlite3_column_text(mDBStatement, aIndex));
|
|
|
|
_value.Assign(value, ::sqlite3_column_bytes(mDBStatement, aIndex));
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
2005-11-15 03:35:50 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetString(uint32_t aIndex, nsAString& _value) {
|
2009-05-09 04:29:56 +04:00
|
|
|
// Get type of Index will check aIndex for us, so we don't have to.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t type;
|
2009-05-09 04:29:56 +04:00
|
|
|
nsresult rv = GetTypeOfIndex(aIndex, &type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-03-24 10:32:40 +03:00
|
|
|
if (type == mozIStorageStatement::VALUE_TYPE_NULL) {
|
2010-07-29 23:17:42 +04:00
|
|
|
// NULL columns should have IsVoid set to distinguish them from the empty
|
2009-05-09 04:29:56 +04:00
|
|
|
// string.
|
2011-10-17 18:59:28 +04:00
|
|
|
_value.SetIsVoid(true);
|
2009-05-09 04:29:56 +04:00
|
|
|
} else {
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* value = static_cast<const char16_t*>(
|
|
|
|
::sqlite3_column_text16(mDBStatement, aIndex));
|
Bug 1543295 - Pass the text length when creating an `nsString` from a SQLite text result. r=mak
This commit updates mozStorage to always:
* Pass the length, using sqlite3_{column, value}_bytes16, when
creating an nsDependentString from a pointer.
* Call sqlite3_{column, value}_bytes{16} after
sqlite3_{column, value}_{text, blob, text16}, per the
recommendation in https://www.sqlite.org/c3ref/column_blob.html.
Some callers did this before, or in unclear order, since C++ doesn't
specify one for evaluating function arguments.
* Pass the byte length to sqlite3_result_text16.
Differential Revision: https://phabricator.services.mozilla.com/D26848
--HG--
extra : moz-landing-system : lando
2019-04-11 06:19:39 +03:00
|
|
|
_value.Assign(value, ::sqlite3_column_bytes16(mDBStatement, aIndex) /
|
|
|
|
sizeof(char16_t));
|
2009-05-09 04:29:56 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Statement::GetVariant(uint32_t aIndex, nsIVariant** _value) {
|
|
|
|
if (!mDBStatement) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
ENSURE_INDEX_VALUE(aIndex, mResultColumnCount);
|
|
|
|
|
|
|
|
if (!mExecuting) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIVariant> variant;
|
|
|
|
int type = ::sqlite3_column_type(mDBStatement, aIndex);
|
|
|
|
switch (type) {
|
|
|
|
case SQLITE_INTEGER:
|
|
|
|
variant =
|
|
|
|
new IntegerVariant(::sqlite3_column_int64(mDBStatement, aIndex));
|
|
|
|
break;
|
|
|
|
case SQLITE_FLOAT:
|
|
|
|
variant = new FloatVariant(::sqlite3_column_double(mDBStatement, aIndex));
|
|
|
|
break;
|
|
|
|
case SQLITE_TEXT: {
|
|
|
|
const char16_t* value = static_cast<const char16_t*>(
|
|
|
|
::sqlite3_column_text16(mDBStatement, aIndex));
|
Bug 1543295 - Pass the text length when creating an `nsString` from a SQLite text result. r=mak
This commit updates mozStorage to always:
* Pass the length, using sqlite3_{column, value}_bytes16, when
creating an nsDependentString from a pointer.
* Call sqlite3_{column, value}_bytes{16} after
sqlite3_{column, value}_{text, blob, text16}, per the
recommendation in https://www.sqlite.org/c3ref/column_blob.html.
Some callers did this before, or in unclear order, since C++ doesn't
specify one for evaluating function arguments.
* Pass the byte length to sqlite3_result_text16.
Differential Revision: https://phabricator.services.mozilla.com/D26848
--HG--
extra : moz-landing-system : lando
2019-04-11 06:19:39 +03:00
|
|
|
nsDependentString str(
|
|
|
|
value,
|
|
|
|
::sqlite3_column_bytes16(mDBStatement, aIndex) / sizeof(char16_t));
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
variant = new TextVariant(str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_NULL:
|
|
|
|
variant = new NullVariant();
|
|
|
|
break;
|
|
|
|
case SQLITE_BLOB: {
|
|
|
|
int size = ::sqlite3_column_bytes(mDBStatement, aIndex);
|
|
|
|
const void* data = ::sqlite3_column_blob(mDBStatement, aIndex);
|
|
|
|
variant = new BlobVariant(std::pair<const void*, int>(data, size));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
variant.forget(_value);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-10-09 04:04:10 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetBlob(uint32_t aIndex, uint32_t* _size, uint8_t** _blob) {
|
2009-05-09 04:29:56 +04:00
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mResultColumnCount);
|
|
|
|
|
|
|
|
if (!mExecuting) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
int size = ::sqlite3_column_bytes(mDBStatement, aIndex);
|
2012-07-30 18:20:58 +04:00
|
|
|
void* blob = nullptr;
|
2009-05-09 04:29:56 +04:00
|
|
|
if (size) {
|
2018-08-28 08:59:19 +03:00
|
|
|
blob = moz_xmemdup(::sqlite3_column_blob(mDBStatement, aIndex), size);
|
2009-05-09 04:29:56 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
*_blob = static_cast<uint8_t*>(blob);
|
2009-05-09 04:29:56 +04:00
|
|
|
*_size = size;
|
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
|
|
|
|
2015-04-29 19:03:15 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Statement::GetBlobAsString(uint32_t aIndex, nsAString& aValue) {
|
|
|
|
return DoGetBlobAsString(this, aIndex, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Statement::GetBlobAsUTF8String(uint32_t aIndex, nsACString& aValue) {
|
|
|
|
return DoGetBlobAsString(this, aIndex, aValue);
|
|
|
|
}
|
|
|
|
|
2004-10-09 04:04:10 +04:00
|
|
|
NS_IMETHODIMP
|
Bug 1543295 - Pass the text length when creating an `nsString` from a SQLite text result. r=mak
This commit updates mozStorage to always:
* Pass the length, using sqlite3_{column, value}_bytes16, when
creating an nsDependentString from a pointer.
* Call sqlite3_{column, value}_bytes{16} after
sqlite3_{column, value}_{text, blob, text16}, per the
recommendation in https://www.sqlite.org/c3ref/column_blob.html.
Some callers did this before, or in unclear order, since C++ doesn't
specify one for evaluating function arguments.
* Pass the byte length to sqlite3_result_text16.
Differential Revision: https://phabricator.services.mozilla.com/D26848
--HG--
extra : moz-landing-system : lando
2019-04-11 06:19:39 +03:00
|
|
|
Statement::GetSharedUTF8String(uint32_t aIndex, uint32_t* _byteLength,
|
2009-05-09 04:29:56 +04:00
|
|
|
const char** _value) {
|
|
|
|
*_value = reinterpret_cast<const char*>(
|
|
|
|
::sqlite3_column_text(mDBStatement, aIndex));
|
Bug 1543295 - Pass the text length when creating an `nsString` from a SQLite text result. r=mak
This commit updates mozStorage to always:
* Pass the length, using sqlite3_{column, value}_bytes16, when
creating an nsDependentString from a pointer.
* Call sqlite3_{column, value}_bytes{16} after
sqlite3_{column, value}_{text, blob, text16}, per the
recommendation in https://www.sqlite.org/c3ref/column_blob.html.
Some callers did this before, or in unclear order, since C++ doesn't
specify one for evaluating function arguments.
* Pass the byte length to sqlite3_result_text16.
Differential Revision: https://phabricator.services.mozilla.com/D26848
--HG--
extra : moz-landing-system : lando
2019-04-11 06:19:39 +03:00
|
|
|
if (_byteLength) {
|
|
|
|
*_byteLength = ::sqlite3_column_bytes(mDBStatement, aIndex);
|
|
|
|
}
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
2007-08-11 02:57:02 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
Bug 1543295 - Pass the text length when creating an `nsString` from a SQLite text result. r=mak
This commit updates mozStorage to always:
* Pass the length, using sqlite3_{column, value}_bytes16, when
creating an nsDependentString from a pointer.
* Call sqlite3_{column, value}_bytes{16} after
sqlite3_{column, value}_{text, blob, text16}, per the
recommendation in https://www.sqlite.org/c3ref/column_blob.html.
Some callers did this before, or in unclear order, since C++ doesn't
specify one for evaluating function arguments.
* Pass the byte length to sqlite3_result_text16.
Differential Revision: https://phabricator.services.mozilla.com/D26848
--HG--
extra : moz-landing-system : lando
2019-04-11 06:19:39 +03:00
|
|
|
Statement::GetSharedString(uint32_t aIndex, uint32_t* _byteLength,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t** _value) {
|
|
|
|
*_value = static_cast<const char16_t*>(
|
|
|
|
::sqlite3_column_text16(mDBStatement, aIndex));
|
Bug 1543295 - Pass the text length when creating an `nsString` from a SQLite text result. r=mak
This commit updates mozStorage to always:
* Pass the length, using sqlite3_{column, value}_bytes16, when
creating an nsDependentString from a pointer.
* Call sqlite3_{column, value}_bytes{16} after
sqlite3_{column, value}_{text, blob, text16}, per the
recommendation in https://www.sqlite.org/c3ref/column_blob.html.
Some callers did this before, or in unclear order, since C++ doesn't
specify one for evaluating function arguments.
* Pass the byte length to sqlite3_result_text16.
Differential Revision: https://phabricator.services.mozilla.com/D26848
--HG--
extra : moz-landing-system : lando
2019-04-11 06:19:39 +03:00
|
|
|
if (_byteLength) {
|
|
|
|
*_byteLength = ::sqlite3_column_bytes16(mDBStatement, aIndex);
|
|
|
|
}
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
2007-08-11 02:57:02 +04:00
|
|
|
}
|
2007-11-13 11:26:45 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
Bug 1543295 - Pass the text length when creating an `nsString` from a SQLite text result. r=mak
This commit updates mozStorage to always:
* Pass the length, using sqlite3_{column, value}_bytes16, when
creating an nsDependentString from a pointer.
* Call sqlite3_{column, value}_bytes{16} after
sqlite3_{column, value}_{text, blob, text16}, per the
recommendation in https://www.sqlite.org/c3ref/column_blob.html.
Some callers did this before, or in unclear order, since C++ doesn't
specify one for evaluating function arguments.
* Pass the byte length to sqlite3_result_text16.
Differential Revision: https://phabricator.services.mozilla.com/D26848
--HG--
extra : moz-landing-system : lando
2019-04-11 06:19:39 +03:00
|
|
|
Statement::GetSharedBlob(uint32_t aIndex, uint32_t* _byteLength,
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint8_t** _blob) {
|
|
|
|
*_blob =
|
|
|
|
static_cast<const uint8_t*>(::sqlite3_column_blob(mDBStatement, aIndex));
|
Bug 1543295 - Pass the text length when creating an `nsString` from a SQLite text result. r=mak
This commit updates mozStorage to always:
* Pass the length, using sqlite3_{column, value}_bytes16, when
creating an nsDependentString from a pointer.
* Call sqlite3_{column, value}_bytes{16} after
sqlite3_{column, value}_{text, blob, text16}, per the
recommendation in https://www.sqlite.org/c3ref/column_blob.html.
Some callers did this before, or in unclear order, since C++ doesn't
specify one for evaluating function arguments.
* Pass the byte length to sqlite3_result_text16.
Differential Revision: https://phabricator.services.mozilla.com/D26848
--HG--
extra : moz-landing-system : lando
2019-04-11 06:19:39 +03:00
|
|
|
if (_byteLength) {
|
|
|
|
*_byteLength = ::sqlite3_column_bytes(mDBStatement, aIndex);
|
|
|
|
}
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-11-13 11:26:45 +03:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetIsNull(uint32_t aIndex, bool* _isNull) {
|
2009-05-09 04:29:56 +04:00
|
|
|
// Get type of Index will check aIndex for us, so we don't have to.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t type;
|
2009-05-09 04:29:56 +04:00
|
|
|
nsresult rv = GetTypeOfIndex(aIndex, &type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-03-24 10:32:40 +03:00
|
|
|
*_isNull = (type == mozIStorageStatement::VALUE_TYPE_NULL);
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
2007-11-13 11:26:45 +03:00
|
|
|
}
|
2009-05-09 04:29:56 +04:00
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageBindingParams
|
|
|
|
|
|
|
|
BOILERPLATE_BIND_PROXIES(Statement,
|
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;)
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|