2009-05-08 17:29:56 -07: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 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
#include <limits.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsMemory.h"
|
2009-07-28 10:21:03 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2008-09-12 14:30:41 -07:00
|
|
|
#include "nsIClassInfoImpl.h"
|
|
|
|
#include "nsIProgrammingLanguage.h"
|
2010-03-24 00:32:40 -07:00
|
|
|
#include "Variant.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-28 10:21:03 -07:00
|
|
|
#include "mozIStorageError.h"
|
|
|
|
|
|
|
|
#include "mozStorageBindingParams.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "mozStorageConnection.h"
|
2008-09-12 14:30:41 -07:00
|
|
|
#include "mozStorageStatementJSHelper.h"
|
2008-10-30 15:50:00 -07:00
|
|
|
#include "mozStoragePrivateHelpers.h"
|
2008-11-19 11:33:00 -08:00
|
|
|
#include "mozStorageStatementParams.h"
|
|
|
|
#include "mozStorageStatementRow.h"
|
2009-04-08 09:24:25 -07:00
|
|
|
#include "mozStorageStatement.h"
|
2013-03-18 07:25:50 -07:00
|
|
|
#include "GeckoProfiler.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "prlog.h"
|
|
|
|
|
2010-05-19 16:22:19 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
extern PRLogModuleInfo* gStorageLog;
|
|
|
|
#endif
|
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
2009-04-08 09:24:25 -07:00
|
|
|
|
2008-09-12 14:30:41 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIClassInfo
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER5(
|
2009-05-08 17:29:56 -07:00
|
|
|
Statement,
|
|
|
|
mozIStorageStatement,
|
2010-03-24 00:32:40 -07:00
|
|
|
mozIStorageBaseStatement,
|
|
|
|
mozIStorageBindingParams,
|
|
|
|
mozIStorageValueArray,
|
|
|
|
mozilla::storage::StorageBaseStatementInternal
|
2008-09-12 14:30:41 -07:00
|
|
|
)
|
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
class StatementClassInfo : public nsIClassInfo
|
2008-09-12 14:30:41 -07:00
|
|
|
{
|
|
|
|
public:
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
GetInterfaces(uint32_t *_count, nsIID ***_array)
|
2009-05-08 17:29:56 -07:00
|
|
|
{
|
|
|
|
return NS_CI_INTERFACE_GETTER_NAME(Statement)(_count, _array);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
GetHelperForLanguage(uint32_t aLanguage, nsISupports **_helper)
|
2009-05-08 17:29:56 -07:00
|
|
|
{
|
|
|
|
if (aLanguage == nsIProgrammingLanguage::JAVASCRIPT) {
|
|
|
|
static StatementJSHelper sJSHelper;
|
|
|
|
*_helper = &sJSHelper;
|
|
|
|
return NS_OK;
|
2008-09-12 14:30:41 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*_helper = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-12 14:30:41 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
GetContractID(char **_contractID)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*_contractID = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-12 14:30:41 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
GetClassDescription(char **_desc)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*_desc = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
GetClassID(nsCID **_id)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*_id = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
GetImplementationLanguage(uint32_t *_language)
|
2009-05-08 17:29:56 -07:00
|
|
|
{
|
|
|
|
*_language = nsIProgrammingLanguage::CPLUSPLUS;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
GetFlags(uint32_t *_flags)
|
2009-05-08 17:29:56 -07:00
|
|
|
{
|
2012-07-20 04:16:17 -07:00
|
|
|
*_flags = 0;
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
GetClassIDNoAlloc(nsCID *_cid)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_IMETHODIMP_(nsrefcnt) StatementClassInfo::AddRef() { return 2; }
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) StatementClassInfo::Release() { return 1; }
|
|
|
|
NS_IMPL_QUERY_INTERFACE1(StatementClassInfo, nsIClassInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
static StatementClassInfo sStatementClassInfo;
|
2008-09-12 14:30:41 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Statement
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
Statement::Statement()
|
2010-03-24 00:32:40 -07:00
|
|
|
: StorageBaseStatementInternal()
|
2009-05-08 17:29:56 -07:00
|
|
|
, mDBStatement(NULL)
|
|
|
|
, mColumnNames()
|
|
|
|
, mExecuting(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-06-11 08:00:09 -07:00
|
|
|
nsresult
|
2009-05-08 17:29:56 -07:00
|
|
|
Statement::initialize(Connection *aDBConnection,
|
|
|
|
const nsACString &aSQLStatement)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_ASSERTION(aDBConnection, "No database connection given!");
|
|
|
|
NS_ASSERTION(!mDBStatement, "Statement already initialized!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
sqlite3 *db = aDBConnection->GetNativeConnection();
|
|
|
|
NS_ASSERTION(db, "We should never be called with a null sqlite3 database!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-06 12:12:55 -08:00
|
|
|
int srv = aDBConnection->prepareStatement(PromiseFlatCString(aSQLStatement),
|
|
|
|
&mDBStatement);
|
2009-05-08 17:29:56 -07:00
|
|
|
if (srv != SQLITE_OK) {
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef PR_LOGGING
|
2009-05-08 17:29:56 -07:00
|
|
|
PR_LOG(gStorageLog, PR_LOG_ERROR,
|
|
|
|
("Sqlite statement prepare error: %d '%s'", srv,
|
|
|
|
::sqlite3_errmsg(db)));
|
|
|
|
PR_LOG(gStorageLog, PR_LOG_ERROR,
|
|
|
|
("Statement was: '%s'", PromiseFlatCString(aSQLStatement).get()));
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-03-20 13:28:16 -07:00
|
|
|
#ifdef PR_LOGGING
|
2009-05-08 17:29:56 -07:00
|
|
|
PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Initialized statement '%s' (0x%p)",
|
|
|
|
PromiseFlatCString(aSQLStatement).get(),
|
|
|
|
mDBStatement));
|
2009-03-20 13:28:16 -07:00
|
|
|
#endif
|
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
mDBConnection = aDBConnection;
|
|
|
|
mParamCount = ::sqlite3_bind_parameter_count(mDBStatement);
|
|
|
|
mResultColumnCount = ::sqlite3_column_count(mDBStatement);
|
|
|
|
mColumnNames.Clear();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mResultColumnCount; i++) {
|
2009-05-08 17:29:56 -07:00
|
|
|
const char *name = ::sqlite3_column_name(mDBStatement, i);
|
|
|
|
(void)mColumnNames.AppendElement(nsDependentCString(name));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-29 18:56:14 -07:00
|
|
|
#ifdef DEBUG
|
2009-05-08 17:29:56 -07: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 01:15:12 -08:00
|
|
|
"are using mozIStorageStatement::escapeStringForLIKE "
|
2009-05-08 17:29:56 -07:00
|
|
|
"and that you are binding that result to the statement "
|
|
|
|
"to prevent SQL injection attacks.");
|
2007-08-29 18:56:14 -07:00
|
|
|
}
|
2009-05-08 17:29:56 -07:00
|
|
|
|
|
|
|
// resetting start and e
|
|
|
|
start = e;
|
|
|
|
e = end;
|
|
|
|
}
|
2007-08-29 18:56:14 -07:00
|
|
|
#endif
|
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
mozIStorageBindingParams *
|
2009-07-28 10:21:03 -07: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 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-07-28 10:21:03 -07: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) {
|
|
|
|
nsRefPtr<BindingParams> params(new BindingParams(mParamsArray, this));
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(params, nullptr);
|
2009-07-28 10:21:03 -07:00
|
|
|
|
|
|
|
rv = mParamsArray->AddParams(params);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2009-07-28 10:21:03 -07: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 00:32:40 -07:00
|
|
|
params->unlock(this);
|
2009-07-28 10:21:03 -07: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-08 17:29:56 -07:00
|
|
|
Statement::~Statement()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-24 00:32:40 -07:00
|
|
|
(void)internalFinalize(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsISupports
|
|
|
|
|
2013-07-18 19:24:15 -07:00
|
|
|
NS_IMPL_ADDREF(Statement)
|
|
|
|
NS_IMPL_RELEASE(Statement)
|
2009-05-08 17:29:56 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(Statement)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageStatement)
|
2010-03-24 00:32:40 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageBaseStatement)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageBindingParams)
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(mozIStorageValueArray)
|
2010-03-24 00:32:40 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(mozilla::storage::StorageBaseStatementInternal)
|
2009-05-08 17:29:56 -07:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIClassInfo))) {
|
|
|
|
foundInterface = static_cast<nsIClassInfo *>(&sStatementClassInfo);
|
|
|
|
}
|
|
|
|
else
|
2010-03-24 00:32:40 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageStatement)
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2010-03-24 00:32:40 -07: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!
|
|
|
|
NS_ASSERTION(mDBStatement != NULL, "We have no statement to clone!");
|
|
|
|
|
|
|
|
// If we do not yet have a cached async statement, clone our statement now.
|
|
|
|
if (!mAsyncStatement) {
|
2010-06-21 14:23:00 -07:00
|
|
|
nsDependentCString sql(::sqlite3_sql(mDBStatement));
|
2011-12-06 12:12:55 -08:00
|
|
|
int rc = mDBConnection->prepareStatement(sql, &mAsyncStatement);
|
2010-03-24 00:32:40 -07:00
|
|
|
if (rc != SQLITE_OK) {
|
2012-07-30 07:20:58 -07:00
|
|
|
*_stmt = nullptr;
|
2010-03-24 00:32:40 -07:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PR_LOG(gStorageLog, PR_LOG_NOTICE,
|
|
|
|
("Cloned statement 0x%p to 0x%p", mDBStatement, mAsyncStatement));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
*_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-12 14:30:41 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageStatement
|
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
// proxy to StorageBaseStatementInternal using its define helper.
|
|
|
|
MIXIN_IMPL_STORAGEBASESTATEMENTINTERNAL(Statement, (void)0;)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2009-05-08 17:29:56 -07:00
|
|
|
Statement::Clone(mozIStorageStatement **_statement)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
nsRefPtr<Statement> statement(new Statement());
|
|
|
|
NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString sql(::sqlite3_sql(mDBStatement));
|
2009-05-08 17:29:56 -07:00
|
|
|
nsresult rv = statement->initialize(mDBConnection, sql);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
statement.forget(_statement);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-09-18 20:26:51 -07:00
|
|
|
NS_IMETHODIMP
|
2009-05-08 17:29:56 -07:00
|
|
|
Statement::Finalize()
|
2010-03-24 00:32:40 -07:00
|
|
|
{
|
|
|
|
return internalFinalize(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Statement::internalFinalize(bool aDestructing)
|
2007-09-18 20:26:51 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_OK;
|
2008-11-19 11:33:00 -08:00
|
|
|
|
2008-07-11 12:00:58 -07:00
|
|
|
#ifdef PR_LOGGING
|
2009-05-08 17:29:56 -07:00
|
|
|
PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Finalizing statement '%s'",
|
|
|
|
::sqlite3_sql(mDBStatement)));
|
2008-07-11 12:00:58 -07:00
|
|
|
#endif
|
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
int srv = ::sqlite3_finalize(mDBStatement);
|
|
|
|
mDBStatement = NULL;
|
2008-11-19 11:33:00 -08:00
|
|
|
|
2010-03-24 00:32:40 -07: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-19 18:16:42 -08:00
|
|
|
destructorAsyncFinalize();
|
2010-03-24 00:32:40 -07:00
|
|
|
else
|
|
|
|
asyncFinalize();
|
2009-07-28 10:21:03 -07:00
|
|
|
}
|
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
// We are considered dead at this point, so any wrappers for row or params
|
|
|
|
// need to lose their reference to us.
|
|
|
|
if (mStatementParamsHolder) {
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrapper =
|
|
|
|
do_QueryInterface(mStatementParamsHolder);
|
|
|
|
nsCOMPtr<mozIStorageStatementParams> iParams =
|
|
|
|
do_QueryWrappedNative(wrapper);
|
|
|
|
StatementParams *params = static_cast<StatementParams *>(iParams.get());
|
2012-07-30 07:20:58 -07:00
|
|
|
params->mStatement = nullptr;
|
|
|
|
mStatementParamsHolder = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mStatementRowHolder) {
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrapper =
|
|
|
|
do_QueryInterface(mStatementRowHolder);
|
|
|
|
nsCOMPtr<mozIStorageStatementRow> iRow =
|
|
|
|
do_QueryWrappedNative(wrapper);
|
|
|
|
StatementRow *row = static_cast<StatementRow *>(iRow.get());
|
2012-07-30 07:20:58 -07:00
|
|
|
row->mStatement = nullptr;
|
|
|
|
mStatementRowHolder = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
}
|
2008-11-19 11:33:00 -08:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2007-09-18 20:26:51 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetParameterCount(uint32_t *_parameterCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
*_parameterCount = mParamCount;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetParameterName(uint32_t aParamIndex,
|
2009-05-08 17:29:56 -07:00
|
|
|
nsACString &_name)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
ENSURE_INDEX_VALUE(aParamIndex, mParamCount);
|
2007-09-21 11:13:47 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
const char *name = ::sqlite3_bind_parameter_name(mDBStatement,
|
|
|
|
aParamIndex + 1);
|
|
|
|
if (name == NULL) {
|
|
|
|
// this thing had no name, so fake one
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString name(":");
|
2009-05-08 17:29:56 -07:00
|
|
|
name.AppendInt(aParamIndex);
|
|
|
|
_name.Assign(name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_name.Assign(nsDependentCString(name));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-05-08 17:29:56 -07:00
|
|
|
Statement::GetParameterIndex(const nsACString &aName,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t *_index)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:57 -07:00
|
|
|
// We do not accept any forms of names other than ":name", but we need to add
|
|
|
|
// the colon for SQLite.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString name(":");
|
2009-05-08 17:29:57 -07:00
|
|
|
name.Append(aName);
|
2011-09-15 01:36:07 -07:00
|
|
|
int ind = ::sqlite3_bind_parameter_index(mDBStatement, name.get());
|
2009-05-08 17:29:56 -07:00
|
|
|
if (ind == 0) // Named parameter not found.
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-06-18 16:42:23 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
*_index = ind - 1; // SQLite indexes are 1-based, we are 0-based.
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetColumnCount(uint32_t *_columnCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-09-21 11:13:47 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
*_columnCount = mResultColumnCount;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetColumnName(uint32_t aColumnIndex,
|
2009-05-08 17:29:56 -07:00
|
|
|
nsACString &_name)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
ENSURE_INDEX_VALUE(aColumnIndex, mResultColumnCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
const char *cname = ::sqlite3_column_name(mDBStatement, aColumnIndex);
|
|
|
|
_name.Assign(nsDependentCString(cname));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-07-19 09:30:17 -07:00
|
|
|
NS_IMETHODIMP
|
2009-05-08 17:29:56 -07:00
|
|
|
Statement::GetColumnIndex(const nsACString &aName,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t *_index)
|
2007-07-19 09:30:17 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-07-19 09:30:17 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
// Surprisingly enough, SQLite doesn't provide an API for this. We have to
|
|
|
|
// determine it ourselves sadly.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mResultColumnCount; i++) {
|
2009-05-08 17:29:56 -07:00
|
|
|
if (mColumnNames[i].Equals(aName)) {
|
|
|
|
*_index = i;
|
|
|
|
return NS_OK;
|
2007-07-19 09:30:17 -07:00
|
|
|
}
|
2009-05-08 17:29:56 -07:00
|
|
|
}
|
2007-07-19 09:30:17 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2007-07-19 09:30:17 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2009-05-08 17:29:56 -07:00
|
|
|
Statement::Reset()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-03-12 11:30:15 -07:00
|
|
|
#ifdef DEBUG
|
2009-05-08 17:29:56 -07:00
|
|
|
PR_LOG(gStorageLog, PR_LOG_DEBUG, ("Resetting statement: '%s'",
|
|
|
|
::sqlite3_sql(mDBStatement)));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
checkAndLogStatementPerformance(mDBStatement);
|
2009-03-12 11:30:15 -07:00
|
|
|
#endif
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mParamsArray = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
(void)sqlite3_reset(mDBStatement);
|
|
|
|
(void)sqlite3_clear_bindings(mDBStatement);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
mExecuting = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-17 12:12:51 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Statement::BindParameters(mozIStorageBindingParamsArray *aParameters)
|
|
|
|
{
|
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
BindingParamsArray *array = static_cast<BindingParamsArray *>(aParameters);
|
|
|
|
if (array->getOwner() != this)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2010-03-15 10:23:39 -07:00
|
|
|
if (array->length() == 0)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2009-06-17 12:12:51 -07:00
|
|
|
mParamsArray = array;
|
|
|
|
mParamsArray->lock();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2009-05-08 17:29:56 -07:00
|
|
|
Statement::Execute()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool ret;
|
2009-05-08 17:29:56 -07:00
|
|
|
nsresult rv = ExecuteStep(&ret);
|
2010-06-07 15:06:56 -07:00
|
|
|
nsresult rv2 = Reset();
|
2007-06-12 15:01:05 -07:00
|
|
|
|
2010-06-07 15:06:56 -07:00
|
|
|
return NS_FAILED(rv) ? rv : rv2;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
Statement::ExecuteStep(bool *_moreResults)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-03-15 21:47:02 -07:00
|
|
|
PROFILER_LABEL("storage", "Statement::ExecuteStep");
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-09-21 11:13:47 -07:00
|
|
|
|
2009-07-28 10:21:03 -07: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 00:32:40 -07:00
|
|
|
nsCOMPtr<IStorageBindingParamsInternal> bindingInternal =
|
|
|
|
do_QueryInterface(*row);
|
|
|
|
nsCOMPtr<mozIStorageError> error = bindingInternal->bind(mDBStatement);
|
2009-07-28 10:21:03 -07:00
|
|
|
if (error) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t srv;
|
2009-07-28 10:21:03 -07:00
|
|
|
(void)error->GetResult(&srv);
|
|
|
|
return convertResultCode(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have bound, so now we can clear our array.
|
2012-07-30 07:20:58 -07:00
|
|
|
mParamsArray = nullptr;
|
2009-07-28 10:21:03 -07:00
|
|
|
}
|
2011-12-06 12:12:55 -08:00
|
|
|
int srv = mDBConnection->stepStatement(mDBStatement);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-10 09:58:17 -07:00
|
|
|
#ifdef PR_LOGGING
|
2009-05-08 17:29:56 -07:00
|
|
|
if (srv != SQLITE_ROW && srv != SQLITE_DONE) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString errStr;
|
2009-05-08 17:29:56 -07:00
|
|
|
(void)mDBConnection->GetLastErrorString(errStr);
|
|
|
|
PR_LOG(gStorageLog, PR_LOG_DEBUG,
|
|
|
|
("Statement::ExecuteStep error: %s", errStr.get()));
|
|
|
|
}
|
2007-07-10 09:58:17 -07:00
|
|
|
#endif
|
2007-07-10 10:23:36 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
// SQLITE_ROW and SQLITE_DONE are non-errors
|
|
|
|
if (srv == SQLITE_ROW) {
|
|
|
|
// we got a row back
|
|
|
|
mExecuting = true;
|
2011-10-17 07:59:28 -07:00
|
|
|
*_moreResults = true;
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else if (srv == SQLITE_DONE) {
|
|
|
|
// statement is done (no row returned)
|
|
|
|
mExecuting = false;
|
2011-10-17 07:59:28 -07:00
|
|
|
*_moreResults = false;
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else if (srv == SQLITE_BUSY || srv == SQLITE_MISUSE) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mExecuting = false;
|
2009-05-08 17:29:56 -07:00
|
|
|
}
|
|
|
|
else if (mExecuting) {
|
2007-07-11 15:20:39 -07:00
|
|
|
#ifdef PR_LOGGING
|
2009-05-08 17:29:56 -07:00
|
|
|
PR_LOG(gStorageLog, PR_LOG_ERROR,
|
|
|
|
("SQLite error after mExecuting was true!"));
|
2007-07-11 15:20:39 -07:00
|
|
|
#endif
|
2011-10-17 07:59:28 -07:00
|
|
|
mExecuting = false;
|
2009-05-08 17:29:56 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
return convertResultCode(srv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetState(int32_t *_state)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
*_state = MOZ_STORAGE_STATEMENT_INVALID;
|
|
|
|
else if (mExecuting)
|
|
|
|
*_state = MOZ_STORAGE_STATEMENT_EXECUTING;
|
|
|
|
else
|
|
|
|
*_state = MOZ_STORAGE_STATEMENT_READY;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetColumnDecltype(uint32_t aParamIndex,
|
2009-05-08 17:29:56 -07:00
|
|
|
nsACString &_declType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-09-21 11:13:47 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
ENSURE_INDEX_VALUE(aParamIndex, mResultColumnCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
_declType.Assign(::sqlite3_column_decltype(mDBStatement, aParamIndex));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2010-03-24 00:32:40 -07:00
|
|
|
//// mozIStorageValueArray (now part of mozIStorageStatement too)
|
2009-05-08 17:29:56 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetNumEntries(uint32_t *_length)
|
2009-05-08 17:29:56 -07:00
|
|
|
{
|
|
|
|
*_length = mResultColumnCount;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetTypeOfIndex(uint32_t aIndex,
|
|
|
|
int32_t *_type)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-09-21 11:13:47 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mResultColumnCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mExecuting)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
int t = ::sqlite3_column_type(mDBStatement, aIndex);
|
|
|
|
switch (t) {
|
|
|
|
case SQLITE_INTEGER:
|
2010-03-24 00:32:40 -07:00
|
|
|
*_type = mozIStorageStatement::VALUE_TYPE_INTEGER;
|
2009-05-08 17:29:56 -07:00
|
|
|
break;
|
|
|
|
case SQLITE_FLOAT:
|
2010-03-24 00:32:40 -07:00
|
|
|
*_type = mozIStorageStatement::VALUE_TYPE_FLOAT;
|
2009-05-08 17:29:56 -07:00
|
|
|
break;
|
|
|
|
case SQLITE_TEXT:
|
2010-03-24 00:32:40 -07:00
|
|
|
*_type = mozIStorageStatement::VALUE_TYPE_TEXT;
|
2009-05-08 17:29:56 -07:00
|
|
|
break;
|
|
|
|
case SQLITE_BLOB:
|
2010-03-24 00:32:40 -07:00
|
|
|
*_type = mozIStorageStatement::VALUE_TYPE_BLOB;
|
2009-05-08 17:29:56 -07:00
|
|
|
break;
|
|
|
|
case SQLITE_NULL:
|
2010-03-24 00:32:40 -07:00
|
|
|
*_type = mozIStorageStatement::VALUE_TYPE_NULL;
|
2009-05-08 17:29:56 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetInt32(uint32_t aIndex,
|
|
|
|
int32_t *_value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-09-21 11:13:47 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mResultColumnCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mExecuting)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
*_value = ::sqlite3_column_int(mDBStatement, aIndex);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetInt64(uint32_t aIndex,
|
|
|
|
int64_t *_value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-09-21 11:13:47 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mResultColumnCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mExecuting)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-09-21 11:13:47 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
*_value = ::sqlite3_column_int64(mDBStatement, aIndex);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetDouble(uint32_t aIndex,
|
2009-05-08 17:29:56 -07:00
|
|
|
double *_value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mResultColumnCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mExecuting)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
*_value = ::sqlite3_column_double(mDBStatement, aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetUTF8String(uint32_t aIndex,
|
2009-05-08 17:29:56 -07:00
|
|
|
nsACString &_value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
// Get type of Index will check aIndex for us, so we don't have to.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t type;
|
2009-05-08 17:29:56 -07:00
|
|
|
nsresult rv = GetTypeOfIndex(aIndex, &type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-03-24 00:32:40 -07:00
|
|
|
if (type == mozIStorageStatement::VALUE_TYPE_NULL) {
|
2010-07-29 12:17:42 -07:00
|
|
|
// NULL columns should have IsVoid set to distinguish them from the empty
|
2009-05-08 17:29:56 -07:00
|
|
|
// string.
|
|
|
|
_value.Truncate(0);
|
2011-10-17 07:59:28 -07:00
|
|
|
_value.SetIsVoid(true);
|
2009-05-08 17:29:56 -07: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;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetString(uint32_t aIndex,
|
2009-05-08 17:29:56 -07:00
|
|
|
nsAString &_value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
// Get type of Index will check aIndex for us, so we don't have to.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t type;
|
2009-05-08 17:29:56 -07:00
|
|
|
nsresult rv = GetTypeOfIndex(aIndex, &type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-03-24 00:32:40 -07:00
|
|
|
if (type == mozIStorageStatement::VALUE_TYPE_NULL) {
|
2010-07-29 12:17:42 -07:00
|
|
|
// NULL columns should have IsVoid set to distinguish them from the empty
|
2009-05-08 17:29:56 -07:00
|
|
|
// string.
|
|
|
|
_value.Truncate(0);
|
2011-10-17 07:59:28 -07:00
|
|
|
_value.SetIsVoid(true);
|
2009-05-08 17:29:56 -07:00
|
|
|
} else {
|
|
|
|
const PRUnichar *value =
|
|
|
|
static_cast<const PRUnichar *>(::sqlite3_column_text16(mDBStatement,
|
|
|
|
aIndex));
|
|
|
|
_value.Assign(value, ::sqlite3_column_bytes16(mDBStatement, aIndex) / 2);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetBlob(uint32_t aIndex,
|
|
|
|
uint32_t *_size,
|
|
|
|
uint8_t **_blob)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (!mDBStatement)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mResultColumnCount);
|
|
|
|
|
|
|
|
if (!mExecuting)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
int size = ::sqlite3_column_bytes(mDBStatement, aIndex);
|
2012-07-30 07:20:58 -07:00
|
|
|
void *blob = nullptr;
|
2009-05-08 17:29:56 -07:00
|
|
|
if (size) {
|
|
|
|
blob = nsMemory::Clone(::sqlite3_column_blob(mDBStatement, aIndex), size);
|
|
|
|
NS_ENSURE_TRUE(blob, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
*_blob = static_cast<uint8_t *>(blob);
|
2009-05-08 17:29:56 -07:00
|
|
|
*_size = size;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetSharedUTF8String(uint32_t aIndex,
|
|
|
|
uint32_t *_length,
|
2009-05-08 17:29:56 -07:00
|
|
|
const char **_value)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (_length)
|
|
|
|
*_length = ::sqlite3_column_bytes(mDBStatement, aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
*_value = reinterpret_cast<const char *>(::sqlite3_column_text(mDBStatement,
|
|
|
|
aIndex));
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-08-10 15:57:02 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetSharedString(uint32_t aIndex,
|
|
|
|
uint32_t *_length,
|
2009-05-08 17:29:56 -07:00
|
|
|
const PRUnichar **_value)
|
2007-08-10 15:57:02 -07:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
if (_length)
|
|
|
|
*_length = ::sqlite3_column_bytes16(mDBStatement, aIndex);
|
2007-08-10 15:57:02 -07:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
*_value = static_cast<const PRUnichar *>(::sqlite3_column_text16(mDBStatement,
|
|
|
|
aIndex));
|
|
|
|
return NS_OK;
|
2007-08-10 15:57:02 -07:00
|
|
|
}
|
2007-11-13 00:26:45 -08:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetSharedBlob(uint32_t aIndex,
|
|
|
|
uint32_t *_size,
|
|
|
|
const uint8_t **_blob)
|
2007-11-13 00:26:45 -08:00
|
|
|
{
|
2009-05-08 17:29:56 -07:00
|
|
|
*_size = ::sqlite3_column_bytes(mDBStatement, aIndex);
|
2012-08-22 08:56:38 -07:00
|
|
|
*_blob = static_cast<const uint8_t *>(::sqlite3_column_blob(mDBStatement,
|
2009-05-08 17:29:56 -07:00
|
|
|
aIndex));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-11-13 00:26:45 -08:00
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
Statement::GetIsNull(uint32_t aIndex,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *_isNull)
|
2009-05-08 17:29:56 -07:00
|
|
|
{
|
|
|
|
// Get type of Index will check aIndex for us, so we don't have to.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t type;
|
2009-05-08 17:29:56 -07:00
|
|
|
nsresult rv = GetTypeOfIndex(aIndex, &type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-03-24 00:32:40 -07:00
|
|
|
*_isNull = (type == mozIStorageStatement::VALUE_TYPE_NULL);
|
2009-05-08 17:29:56 -07:00
|
|
|
return NS_OK;
|
2007-11-13 00:26:45 -08:00
|
|
|
}
|
2009-05-08 17:29:56 -07:00
|
|
|
|
2010-03-24 00:32:40 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageBindingParams
|
|
|
|
|
|
|
|
BOILERPLATE_BIND_PROXIES(
|
|
|
|
Statement,
|
|
|
|
if (!mDBStatement) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
)
|
|
|
|
|
2009-05-08 17:29:56 -07:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|