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"
|
2013-09-10 00:37:37 +04:00
|
|
|
#include "nsDOMClassInfo.h"
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2010-05-20 03:22:19 +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:
|
2013-11-22 23:45:50 +04:00
|
|
|
MOZ_CONSTEXPR StatementClassInfo() {}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2009-05-09 04:29:56 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-03-21 19:28:04 +03:00
|
|
|
GetInterfaces(uint32_t *_count, nsIID ***_array) override
|
2009-05-09 04:29:56 +04:00
|
|
|
{
|
|
|
|
return NS_CI_INTERFACE_GETTER_NAME(Statement)(_count, _array);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-03-29 17:52:54 +03:00
|
|
|
GetScriptableHelper(nsIXPCScriptable **_helper) override
|
2009-05-09 04:29:56 +04:00
|
|
|
{
|
2015-03-29 17:52:54 +03:00
|
|
|
static StatementJSHelper sJSHelper;
|
|
|
|
*_helper = &sJSHelper;
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
NS_IMETHODIMP
|
2015-03-21 19:28:04 +03:00
|
|
|
GetContractID(char **_contractID) override
|
2009-05-09 04:29:56 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*_contractID = nullptr;
|
2009-05-09 04:29:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-13 01:30:41 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
NS_IMETHODIMP
|
2015-03-21 19:28:04 +03:00
|
|
|
GetClassDescription(char **_desc) override
|
2009-05-09 04:29:56 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*_desc = nullptr;
|
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
|
|
|
NS_IMETHODIMP
|
2015-03-21 19:28:04 +03:00
|
|
|
GetClassID(nsCID **_id) override
|
2009-05-09 04:29:56 +04:00
|
|
|
{
|
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
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
NS_IMETHODIMP
|
2015-03-21 19:28:04 +03:00
|
|
|
GetFlags(uint32_t *_flags) override
|
2009-05-09 04:29:56 +04:00
|
|
|
{
|
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
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
NS_IMETHODIMP
|
2015-03-21 19:28:04 +03:00
|
|
|
GetClassIDNoAlloc(nsCID *_cid) override
|
2009-05-09 04:29:56 +04:00
|
|
|
{
|
|
|
|
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()
|
2010-03-24 10:32:40 +03:00
|
|
|
: StorageBaseStatementInternal()
|
2013-07-31 19:44:58 +04:00
|
|
|
, mDBStatement(nullptr)
|
2009-05-09 04:29:56 +04:00
|
|
|
, mColumnNames()
|
|
|
|
, mExecuting(false)
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-06-11 19:00:09 +04:00
|
|
|
nsresult
|
2009-05-09 04:29:56 +04:00
|
|
|
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)
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
2014-04-24 13:54:09 +04:00
|
|
|
MOZ_ASSERT(aDBConnection, "No database connection given!");
|
2014-04-24 13:54:12 +04:00
|
|
|
MOZ_ASSERT(!aDBConnection->isClosed(), "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),
|
2011-12-07 00:12:55 +04:00
|
|
|
&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!
|
|
|
|
const nsCaseInsensitiveCStringComparator c;
|
|
|
|
nsACString::const_iterator start, end, e;
|
|
|
|
aSQLStatement.BeginReading(start);
|
|
|
|
aSQLStatement.EndReading(end);
|
|
|
|
e = end;
|
|
|
|
while (::FindInReadable(NS_LITERAL_CSTRING(" LIKE"), start, e, c)) {
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
if (!(::FindInReadable(NS_LITERAL_CSTRING(" LIKE ?"), s1, end, c) ||
|
|
|
|
::FindInReadable(NS_LITERAL_CSTRING(" LIKE :"), s2, end, c) ||
|
|
|
|
::FindInReadable(NS_LITERAL_CSTRING(" LIKE @"), s3, end, c))) {
|
|
|
|
// 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 *
|
2009-07-28 21:21:03 +04:00
|
|
|
Statement::getParams()
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
Statement::~Statement()
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
2010-03-24 10:32:40 +03:00
|
|
|
(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)
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
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
|
2009-05-09 04:29:56 +04:00
|
|
|
Statement::Finalize()
|
2010-03-24 10:32:40 +03:00
|
|
|
{
|
|
|
|
return internalFinalize(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Statement::internalFinalize(bool aDestructing)
|
2007-09-19 07:26:51 +04:00
|
|
|
{
|
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;
|
|
|
|
|
2014-04-24 13:54:12 +04:00
|
|
|
if (!mDBConnection->isClosed()) {
|
2013-09-04 20:51:45 +04:00
|
|
|
//
|
|
|
|
// The connection is still open. While statement finalization and
|
|
|
|
// closing may, in some cases, take place in two distinct threads,
|
|
|
|
// we have a guarantee that the connection will remain open until
|
|
|
|
// this method terminates:
|
|
|
|
//
|
|
|
|
// a. The connection will be closed synchronously. In this case,
|
|
|
|
// there is no race condition, as everything takes place on the
|
|
|
|
// same thread.
|
|
|
|
//
|
|
|
|
// b. The connection is closed asynchronously and this code is
|
|
|
|
// executed on the opener thread. In this case, asyncClose() has
|
|
|
|
// not been called yet and will not be called before we return
|
|
|
|
// from this function.
|
|
|
|
//
|
|
|
|
// c. The connection is closed asynchronously and this code is
|
|
|
|
// executed on the async execution thread. In this case,
|
|
|
|
// AsyncCloseConnection::Run() has not been called yet and will
|
|
|
|
// not be called before we return from this function.
|
|
|
|
//
|
|
|
|
// In either case, the connection is still valid, hence closing
|
|
|
|
// here is safe.
|
|
|
|
//
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Debug, ("Finalizing statement '%s' during garbage-collection",
|
2013-09-04 20:51:45 +04:00
|
|
|
::sqlite3_sql(mDBStatement)));
|
|
|
|
srv = ::sqlite3_finalize(mDBStatement);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else {
|
|
|
|
//
|
|
|
|
// The database connection is either closed or closing. 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.
|
|
|
|
//
|
|
|
|
|
|
|
|
char *msg = ::PR_smprintf("SQL statement (%x) should have been finalized"
|
2013-09-19 17:19:02 +04:00
|
|
|
" before garbage-collection. For more details on this statement, set"
|
|
|
|
" NSPR_LOG_MESSAGES=mozStorage:5 .",
|
2013-09-04 20:51:45 +04:00
|
|
|
mDBStatement);
|
2013-09-19 17:19:02 +04:00
|
|
|
|
2013-09-04 20:51:45 +04:00
|
|
|
//
|
|
|
|
// 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().
|
|
|
|
//
|
2013-09-19 17:19:02 +04:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
// Deactivate the warning until we have fixed the exising culprit
|
|
|
|
// (see bug 914070).
|
2013-09-04 20:51:45 +04:00
|
|
|
NS_WARNING(msg);
|
2013-09-19 17:19:02 +04:00
|
|
|
#endif // 0
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gStorageLog, LogLevel::Warning, (msg));
|
2013-09-19 17:19:02 +04:00
|
|
|
|
2013-09-04 20:51:45 +04:00
|
|
|
::PR_smprintf_free(msg);
|
|
|
|
}
|
2013-09-19 17:19:02 +04:00
|
|
|
|
|
|
|
#endif
|
2008-07-11 23:00:58 +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)
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
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,
|
2009-05-09 04:29:56 +04:00
|
|
|
nsACString &_name)
|
2004-12-03 19:56:57 +03:00
|
|
|
{
|
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
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString name(":");
|
2009-05-09 04:29:56 +04:00
|
|
|
name.AppendInt(aParamIndex);
|
|
|
|
_name.Assign(name);
|
|
|
|
}
|
|
|
|
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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t *_index)
|
2005-04-18 01:00:50 +04:00
|
|
|
{
|
2009-05-09 04:29:56 +04:00
|
|
|
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)
|
2004-12-03 19:56:57 +03: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-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,
|
2009-05-09 04:29:56 +04:00
|
|
|
nsACString &_name)
|
2004-12-03 19:56:57 +03:00
|
|
|
{
|
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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t *_index)
|
2007-07-19 20:30:17 +04:00
|
|
|
{
|
2009-05-09 04:29:56 +04:00
|
|
|
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()
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
2009-05-09 04:29:56 +04:00
|
|
|
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, ("Resetting statement: '%s'",
|
2009-05-09 04:29:56 +04:00
|
|
|
::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()
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
2009-05-09 04:29:56 +04:00
|
|
|
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)
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
2014-05-24 01:12:29 +04:00
|
|
|
PROFILER_LABEL("Statement", "ExecuteStep",
|
|
|
|
js::ProfileEntry::Category::STORAGE);
|
|
|
|
|
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();
|
2010-03-24 10:32:40 +03:00
|
|
|
nsCOMPtr<IStorageBindingParamsInternal> bindingInternal =
|
|
|
|
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)
|
2004-12-03 19:56:57 +03:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetColumnDecltype(uint32_t aParamIndex,
|
2009-05-09 04:29:56 +04:00
|
|
|
nsACString &_declType)
|
2004-10-09 04:04:10 +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-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aParamIndex, mResultColumnCount);
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
_declType.Assign(::sqlite3_column_decltype(mDBStatement, aParamIndex));
|
|
|
|
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)
|
2004-10-09 04:04:10 +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-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)
|
2004-10-09 04:04:10 +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-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)
|
2004-10-09 04:04:10 +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-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,
|
2009-05-09 04:29:56 +04:00
|
|
|
double *_value)
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
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,
|
2009-05-09 04:29:56 +04:00
|
|
|
nsACString &_value)
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
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.
|
|
|
|
_value.Truncate(0);
|
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,
|
2009-05-09 04:29:56 +04:00
|
|
|
nsAString &_value)
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
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.
|
|
|
|
_value.Truncate(0);
|
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,
|
2009-05-09 04:29:56 +04:00
|
|
|
aIndex));
|
|
|
|
_value.Assign(value, ::sqlite3_column_bytes16(mDBStatement, aIndex) / 2);
|
|
|
|
}
|
|
|
|
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)
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
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) {
|
|
|
|
blob = nsMemory::Clone(::sqlite3_column_blob(mDBStatement, aIndex), size);
|
|
|
|
NS_ENSURE_TRUE(blob, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
|
|
|
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetSharedUTF8String(uint32_t aIndex,
|
|
|
|
uint32_t *_length,
|
2009-05-09 04:29:56 +04:00
|
|
|
const char **_value)
|
2004-10-09 04:04:10 +04:00
|
|
|
{
|
2009-05-09 04:29:56 +04:00
|
|
|
if (_length)
|
|
|
|
*_length = ::sqlite3_column_bytes(mDBStatement, aIndex);
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
*_value = reinterpret_cast<const char *>(::sqlite3_column_text(mDBStatement,
|
|
|
|
aIndex));
|
|
|
|
return NS_OK;
|
2004-10-09 04:04:10 +04:00
|
|
|
}
|
2007-08-11 02:57:02 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetSharedString(uint32_t aIndex,
|
|
|
|
uint32_t *_length,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t **_value)
|
2007-08-11 02:57:02 +04:00
|
|
|
{
|
2009-05-09 04:29:56 +04:00
|
|
|
if (_length)
|
|
|
|
*_length = ::sqlite3_column_bytes16(mDBStatement, aIndex);
|
2007-08-11 02:57:02 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
*_value = static_cast<const char16_t *>(::sqlite3_column_text16(mDBStatement,
|
2009-05-09 04:29:56 +04:00
|
|
|
aIndex));
|
|
|
|
return NS_OK;
|
2007-08-11 02:57:02 +04:00
|
|
|
}
|
2007-11-13 11:26:45 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Statement::GetSharedBlob(uint32_t aIndex,
|
|
|
|
uint32_t *_size,
|
|
|
|
const uint8_t **_blob)
|
2007-11-13 11:26:45 +03:00
|
|
|
{
|
2009-05-09 04:29:56 +04:00
|
|
|
*_size = ::sqlite3_column_bytes(mDBStatement, aIndex);
|
2012-08-22 19:56:38 +04:00
|
|
|
*_blob = static_cast<const uint8_t *>(::sqlite3_column_blob(mDBStatement,
|
2009-05-09 04:29:56 +04:00
|
|
|
aIndex));
|
|
|
|
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,
|
2011-09-29 10:19:26 +04:00
|
|
|
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
|