From 97575b657803c7bdc450614a271e97ffcd0f7620 Mon Sep 17 00:00:00 2001 From: Shawn Wilsher Date: Fri, 8 May 2009 20:29:56 -0400 Subject: [PATCH] Bug 489702 - Update mozStorageStatement.* to follow style guidelines Consistent spacing, consistent naming, and namespaces - OH MY! r=asuth --- storage/src/mozStorageArgValueArray.cpp | 3 +- storage/src/mozStorageConnection.cpp | 12 +- storage/src/mozStorageStatement.cpp | 1342 +++++++++---------- storage/src/mozStorageStatement.h | 69 +- storage/src/mozStorageStatementJSHelper.cpp | 10 +- storage/src/mozStorageStatementJSHelper.h | 6 +- storage/src/mozStorageStatementParams.h | 5 +- storage/src/mozStorageStatementRow.cpp | 4 +- storage/src/mozStorageStatementRow.h | 11 +- storage/src/mozStorageStatementWrapper.cpp | 2 +- storage/src/mozStorageStatementWrapper.h | 2 +- 11 files changed, 697 insertions(+), 769 deletions(-) diff --git a/storage/src/mozStorageArgValueArray.cpp b/storage/src/mozStorageArgValueArray.cpp index 74e17fe4523..78716d7ce71 100644 --- a/storage/src/mozStorageArgValueArray.cpp +++ b/storage/src/mozStorageArgValueArray.cpp @@ -171,7 +171,8 @@ ArgValueArray::GetString(PRUint32 aIndex, NS_IMETHODIMP ArgValueArray::GetBlob(PRUint32 aIndex, - PRUint32 *_size, PRUint8 **_blob) + PRUint32 *_size, + PRUint8 **_blob) { ENSURE_INDEX_VALUE(aIndex, mArgc); diff --git a/storage/src/mozStorageConnection.cpp b/storage/src/mozStorageConnection.cpp index c7347f8bbcf..4916c048a48 100644 --- a/storage/src/mozStorageConnection.cpp +++ b/storage/src/mozStorageConnection.cpp @@ -629,13 +629,13 @@ Connection::CreateStatement(const nsACString &aSQLStatement, NS_ENSURE_ARG_POINTER(_stmt); if (!mDBConn) return NS_ERROR_NOT_INITIALIZED; - nsRefPtr statement(new mozStorageStatement()); + nsRefPtr statement(new Statement()); NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY); - nsresult rv = statement->Initialize(this, aSQLStatement); + nsresult rv = statement->initialize(this, aSQLStatement); NS_ENSURE_SUCCESS(rv, rv); - mozStorageStatement *rawPtr; + Statement *rawPtr; statement.forget(&rawPtr); *_stmt = rawPtr; return NS_OK; @@ -661,7 +661,7 @@ Connection::ExecuteAsync(mozIStorageStatement **aStatements, nsTArray stmts(aNumStatements); for (PRUint32 i = 0; i < aNumStatements && rc == SQLITE_OK; i++) { sqlite3_stmt *old_stmt = - static_cast(aStatements[i])->nativeStatement(); + static_cast(aStatements[i])->nativeStatement(); if (!old_stmt) { rc = SQLITE_MISUSE; break; @@ -670,8 +670,8 @@ Connection::ExecuteAsync(mozIStorageStatement **aStatements, "Statement must be from this database connection!"); // Clone this statement. We only need a sqlite3_stmt object, so we can - // avoid all the extra work that making a new mozStorageStatement would - // normally involve and use the SQLite API. + // avoid all the extra work that making a new Statement would normally + // involve and use the SQLite API. sqlite3_stmt *new_stmt; rc = ::sqlite3_prepare_v2(mDBConn, ::sqlite3_sql(old_stmt), -1, &new_stmt, NULL); diff --git a/storage/src/mozStorageStatement.cpp b/storage/src/mozStorageStatement.cpp index 9533b05e2dd..df772dc2d55 100644 --- a/storage/src/mozStorageStatement.cpp +++ b/storage/src/mozStorageStatement.cpp @@ -1,5 +1,5 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- - * vim: sw=4 ts=4 sts=4 expandtab +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * @@ -41,9 +41,7 @@ #include -#include "nsAutoLock.h" #include "nsError.h" -#include "nsISimpleEnumerator.h" #include "nsMemory.h" #include "nsIClassInfoImpl.h" #include "nsIProgrammingLanguage.h" @@ -61,840 +59,772 @@ extern PRLogModuleInfo* gStorageLog; #endif -using namespace mozilla::storage; +namespace mozilla { +namespace storage { //////////////////////////////////////////////////////////////////////////////// //// nsIClassInfo NS_IMPL_CI_INTERFACE_GETTER2( - mozStorageStatement -, mozIStorageStatement -, mozIStorageValueArray + Statement, + mozIStorageStatement, + mozIStorageValueArray ) -class mozStorageStatementClassInfo : public nsIClassInfo +class StatementClassInfo : public nsIClassInfo { public: - NS_DECL_ISUPPORTS + NS_DECL_ISUPPORTS - NS_IMETHODIMP - GetInterfaces(PRUint32 *_count, nsIID ***_array) - { - return NS_CI_INTERFACE_GETTER_NAME(mozStorageStatement)(_count, _array); + NS_IMETHODIMP + GetInterfaces(PRUint32 *_count, nsIID ***_array) + { + return NS_CI_INTERFACE_GETTER_NAME(Statement)(_count, _array); + } + + NS_IMETHODIMP + GetHelperForLanguage(PRUint32 aLanguage, nsISupports **_helper) + { + if (aLanguage == nsIProgrammingLanguage::JAVASCRIPT) { + static StatementJSHelper sJSHelper; + *_helper = &sJSHelper; + return NS_OK; } - NS_IMETHODIMP - GetHelperForLanguage(PRUint32 aLanguage, nsISupports **_helper) - { - if (aLanguage == nsIProgrammingLanguage::JAVASCRIPT) { - static StatementJSHelper sJSHelper; - *_helper = &sJSHelper; - return NS_OK; - } + *_helper = nsnull; + return NS_OK; + } - *_helper = nsnull; - return NS_OK; - } + NS_IMETHODIMP + GetContractID(char **_contractID) + { + *_contractID = nsnull; + return NS_OK; + } - NS_IMETHODIMP - GetContractID(char **_contractID) - { - *_contractID = nsnull; - return NS_OK; - } + NS_IMETHODIMP + GetClassDescription(char **_desc) + { + *_desc = nsnull; + return NS_OK; + } - NS_IMETHODIMP - GetClassDescription(char **_desc) - { - *_desc = nsnull; - return NS_OK; - } + NS_IMETHODIMP + GetClassID(nsCID **_id) + { + *_id = nsnull; + return NS_OK; + } - NS_IMETHODIMP - GetClassID(nsCID **_id) - { - *_id = nsnull; - return NS_OK; - } + NS_IMETHODIMP + GetImplementationLanguage(PRUint32 *_language) + { + *_language = nsIProgrammingLanguage::CPLUSPLUS; + return NS_OK; + } - NS_IMETHODIMP - GetImplementationLanguage(PRUint32 *_language) - { - *_language = nsIProgrammingLanguage::CPLUSPLUS; - return NS_OK; - } + NS_IMETHODIMP + GetFlags(PRUint32 *_flags) + { + *_flags = nsnull; + return NS_OK; + } - NS_IMETHODIMP - GetFlags(PRUint32 *_flags) - { - *_flags = nsnull; - return NS_OK; - } - - NS_IMETHODIMP - GetClassIDNoAlloc(nsCID *_cid) - { - return NS_ERROR_NOT_AVAILABLE; - } + NS_IMETHODIMP + GetClassIDNoAlloc(nsCID *_cid) + { + return NS_ERROR_NOT_AVAILABLE; + } }; -NS_IMETHODIMP_(nsrefcnt) mozStorageStatementClassInfo::AddRef() { return 2; } -NS_IMETHODIMP_(nsrefcnt) mozStorageStatementClassInfo::Release() { return 1; } -NS_IMPL_QUERY_INTERFACE1(mozStorageStatementClassInfo, nsIClassInfo) +NS_IMETHODIMP_(nsrefcnt) StatementClassInfo::AddRef() { return 2; } +NS_IMETHODIMP_(nsrefcnt) StatementClassInfo::Release() { return 1; } +NS_IMPL_QUERY_INTERFACE1(StatementClassInfo, nsIClassInfo) -static mozStorageStatementClassInfo sStatementClassInfo; +static StatementClassInfo sStatementClassInfo; -/** - ** mozStorageStatementRowEnumerator - **/ -class mozStorageStatementRowEnumerator : public nsISimpleEnumerator { -public: - // this expects a statement that has NOT had step called on it yet - mozStorageStatementRowEnumerator (sqlite3_stmt *aDBStatement); +//////////////////////////////////////////////////////////////////////////////// +//// Statement - // nsISupports interface - NS_DECL_ISUPPORTS - - // nsISimpleEnumerator interface - NS_DECL_NSISIMPLEENUMERATOR - -private: - ~mozStorageStatementRowEnumerator (); -protected: - sqlite3_stmt *mDBStatement; - PRBool mHasMore; - PRBool mDidStep; - - void DoRealStep(); -}; - - -/** - ** mozStorageStatement - **/ - -NS_IMPL_THREADSAFE_ADDREF(mozStorageStatement) -NS_IMPL_THREADSAFE_RELEASE(mozStorageStatement) - -NS_INTERFACE_MAP_BEGIN(mozStorageStatement) - NS_INTERFACE_MAP_ENTRY(mozIStorageStatement) - NS_INTERFACE_MAP_ENTRY(mozIStorageValueArray) - if (aIID.Equals(NS_GET_IID(nsIClassInfo))) { - foundInterface = static_cast(&sStatementClassInfo); - } else - NS_INTERFACE_MAP_ENTRY(nsISupports) -NS_INTERFACE_MAP_END - -mozStorageStatement::mozStorageStatement() - : mDBConnection (nsnull), mDBStatement(nsnull), mColumnNames(nsnull), mExecuting(PR_FALSE) +Statement::Statement() +: mDBConnection(nsnull) +, mDBStatement(NULL) +, mColumnNames() +, mExecuting(false) { } nsresult -mozStorageStatement::Initialize(Connection *aDBConnection, - const nsACString & aSQLStatement) +Statement::initialize(Connection *aDBConnection, + const nsACString &aSQLStatement) { - NS_ASSERTION(aDBConnection, "No database connection given!"); - NS_ASSERTION(!mDBStatement, "Calling Initialize on an already initialized statement!"); + NS_ASSERTION(aDBConnection, "No database connection given!"); + NS_ASSERTION(!mDBStatement, "Statement already initialized!"); - sqlite3 *db = aDBConnection->GetNativeConnection(); - NS_ENSURE_TRUE(db != nsnull, NS_ERROR_NULL_POINTER); + sqlite3 *db = aDBConnection->GetNativeConnection(); + NS_ASSERTION(db, "We should never be called with a null sqlite3 database!"); - int srv = sqlite3_prepare_v2(db, PromiseFlatCString(aSQLStatement).get(), + int srv = ::sqlite3_prepare_v2(db, PromiseFlatCString(aSQLStatement).get(), -1, &mDBStatement, NULL); - if (srv != SQLITE_OK) { + if (srv != SQLITE_OK) { #ifdef PR_LOGGING - 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'", nsPromiseFlatCString(aSQLStatement).get())); + 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())); #endif - return NS_ERROR_FAILURE; + return NS_ERROR_FAILURE; } #ifdef PR_LOGGING - PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Initialized statement '%s' (0x%p)", - nsPromiseFlatCString(aSQLStatement).get(), - mDBStatement)); + PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Initialized statement '%s' (0x%p)", + PromiseFlatCString(aSQLStatement).get(), + mDBStatement)); #endif - mDBConnection = aDBConnection; - mParamCount = sqlite3_bind_parameter_count (mDBStatement); - mResultColumnCount = sqlite3_column_count (mDBStatement); - mColumnNames.Clear(); + mDBConnection = aDBConnection; + mParamCount = ::sqlite3_bind_parameter_count(mDBStatement); + mResultColumnCount = ::sqlite3_column_count(mDBStatement); + mColumnNames.Clear(); - for (PRUint32 i = 0; i < mResultColumnCount; i++) { - const char *name = sqlite3_column_name(mDBStatement, i); - mColumnNames.AppendElement(nsDependentCString(name)); - } + for (PRUint32 i = 0; i < mResultColumnCount; i++) { + const char *name = ::sqlite3_column_name(mDBStatement, i); + (void)mColumnNames.AppendElement(nsDependentCString(name)); + } #ifdef DEBUG - // 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; + // 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 " - "are using mozIStorageConnection::escapeStringForLIKE " - "and that you are binding that result to the statement " - "to prevent SQL injection attacks."); - } - - // resetting start and e - start = e; - e = end; + 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 " + "are using mozIStorageConnection::escapeStringForLIKE " + "and that you are binding that result to the statement " + "to prevent SQL injection attacks."); } + + // resetting start and e + start = e; + e = end; + } #endif - return NS_OK; + return NS_OK; } -mozStorageStatement::~mozStorageStatement() +Statement::~Statement() { - (void)Finalize(); + (void)Finalize(); } +NS_IMPL_THREADSAFE_ADDREF(Statement) +NS_IMPL_THREADSAFE_RELEASE(Statement) + +NS_INTERFACE_MAP_BEGIN(Statement) + NS_INTERFACE_MAP_ENTRY(mozIStorageStatement) + NS_INTERFACE_MAP_ENTRY(mozIStorageValueArray) + if (aIID.Equals(NS_GET_IID(nsIClassInfo))) { + foundInterface = static_cast(&sStatementClassInfo); + } + else + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + //////////////////////////////////////////////////////////////////////////////// //// mozIStorageStatement -/* mozIStorageStatement clone (); */ NS_IMETHODIMP -mozStorageStatement::Clone(mozIStorageStatement **_retval) +Statement::Clone(mozIStorageStatement **_statement) { - mozStorageStatement *mss = new mozStorageStatement(); - if (!mss) - return NS_ERROR_OUT_OF_MEMORY; + nsRefPtr statement(new Statement()); + NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY); - nsCAutoString sql(sqlite3_sql(mDBStatement)); - nsresult rv = mss->Initialize (mDBConnection, sql); - NS_ENSURE_SUCCESS(rv, rv); + nsCAutoString sql(::sqlite3_sql(mDBStatement)); + nsresult rv = statement->initialize(mDBConnection, sql); + NS_ENSURE_SUCCESS(rv, rv); - NS_ADDREF(*_retval = mss); - return NS_OK; + statement.forget(_statement); + return NS_OK; } -/* void finalize(); */ NS_IMETHODIMP -mozStorageStatement::Finalize() +Statement::Finalize() { - if (!mDBStatement) - return NS_OK; + if (!mDBStatement) + return NS_OK; #ifdef PR_LOGGING - PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Finalizing statement '%s'", - sqlite3_sql(mDBStatement))); + PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Finalizing statement '%s'", + ::sqlite3_sql(mDBStatement))); #endif - int srv = sqlite3_finalize(mDBStatement); - mDBStatement = NULL; + int srv = ::sqlite3_finalize(mDBStatement); + mDBStatement = NULL; - // We are considered dead at this point, so any wrappers for row or params - // need to lose their reference to us. - if (mStatementParamsHolder) { - nsCOMPtr wrapper = - do_QueryInterface(mStatementParamsHolder); - nsCOMPtr iParams = - do_QueryWrappedNative(wrapper); - StatementParams *params = static_cast(iParams.get()); - params->mStatement = nsnull; - mStatementParamsHolder = nsnull; - } + // We are considered dead at this point, so any wrappers for row or params + // need to lose their reference to us. + if (mStatementParamsHolder) { + nsCOMPtr wrapper = + do_QueryInterface(mStatementParamsHolder); + nsCOMPtr iParams = + do_QueryWrappedNative(wrapper); + StatementParams *params = static_cast(iParams.get()); + params->mStatement = nsnull; + mStatementParamsHolder = nsnull; + } - if (mStatementRowHolder) { - nsCOMPtr wrapper = - do_QueryInterface(mStatementRowHolder); - nsCOMPtr iRow = - do_QueryWrappedNative(wrapper); - StatementRow *row = static_cast(iRow.get()); - row->mStatement = nsnull; - mStatementRowHolder = nsnull; - } + if (mStatementRowHolder) { + nsCOMPtr wrapper = + do_QueryInterface(mStatementRowHolder); + nsCOMPtr iRow = + do_QueryWrappedNative(wrapper); + StatementRow *row = static_cast(iRow.get()); + row->mStatement = nsnull; + mStatementRowHolder = nsnull; + } - return convertResultCode(srv); + return convertResultCode(srv); } -/* readonly attribute unsigned long parameterCount; */ NS_IMETHODIMP -mozStorageStatement::GetParameterCount(PRUint32 *aParameterCount) +Statement::GetParameterCount(PRUint32 *_parameterCount) { - NS_ENSURE_ARG_POINTER(aParameterCount); + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - *aParameterCount = mParamCount; - return NS_OK; + *_parameterCount = mParamCount; + return NS_OK; } -/* AUTF8String getParameterName(in unsigned long aParamIndex); */ NS_IMETHODIMP -mozStorageStatement::GetParameterName(PRUint32 aParamIndex, nsACString & _retval) +Statement::GetParameterName(PRUint32 aParamIndex, + nsACString &_name) { - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + ENSURE_INDEX_VALUE(aParamIndex, mParamCount); - // We have to check this index because sqlite3_bind_parameter_name returns - // NULL if an error occurs, or if a column is unnamed. Since we handle - // unnamed columns, we won't be able to tell if it is an error not without - // checking ourselves. - if (aParamIndex < 0 || aParamIndex >= mParamCount) - return NS_ERROR_ILLEGAL_VALUE; + const char *name = ::sqlite3_bind_parameter_name(mDBStatement, + aParamIndex + 1); + if (name == NULL) { + // this thing had no name, so fake one + nsCAutoString name(":"); + name.AppendInt(aParamIndex); + _name.Assign(name); + } + else { + _name.Assign(nsDependentCString(name)); + } - const char *pname = sqlite3_bind_parameter_name(mDBStatement, aParamIndex + 1); - if (pname == NULL) { - // this thing had no name, so fake one - nsCAutoString pname(":"); - pname.AppendInt(aParamIndex); - _retval.Assign(pname); - } else { - _retval.Assign(nsDependentCString(pname)); - } - - return NS_OK; + return NS_OK; } -/* unsigned long getParameterIndex(in AUTF8String aParameterName); */ NS_IMETHODIMP -mozStorageStatement::GetParameterIndex(const nsACString &aName, - PRUint32 *_retval) +Statement::GetParameterIndex(const nsACString &aName, + PRUint32 *_index) { - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - int ind = sqlite3_bind_parameter_index(mDBStatement, - nsPromiseFlatCString(aName).get()); - if (ind == 0) // Named parameter not found - return NS_ERROR_INVALID_ARG; - - *_retval = ind - 1; // SQLite indexes are 1-based, we are 0-based - - return NS_OK; -} - -/* readonly attribute unsigned long columnCount; */ -NS_IMETHODIMP -mozStorageStatement::GetColumnCount(PRUint32 *aColumnCount) -{ - NS_ENSURE_ARG_POINTER(aColumnCount); - - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - *aColumnCount = mResultColumnCount; - return NS_OK; -} - -/* AUTF8String getColumnName(in unsigned long aColumnIndex); */ -NS_IMETHODIMP -mozStorageStatement::GetColumnName(PRUint32 aColumnIndex, nsACString & _retval) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - // We have to check this index because sqlite3_column_name returns - // NULL if an error occurs, or if a column is unnamed. - if (aColumnIndex < 0 || aColumnIndex >= mResultColumnCount) - return NS_ERROR_ILLEGAL_VALUE; - - const char *cname = sqlite3_column_name(mDBStatement, aColumnIndex); - _retval.Assign(nsDependentCString(cname)); - - return NS_OK; -} - -/* unsigned long getColumnIndex(in AUTF8String aName); */ -NS_IMETHODIMP -mozStorageStatement::GetColumnIndex(const nsACString &aName, PRUint32 *_retval) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - // Surprisingly enough, SQLite doesn't provide an API for this. We have to - // determine it ourselves sadly. - for (PRUint32 i = 0; i < mResultColumnCount; i++) { - if (mColumnNames[i].Equals(aName)) { - *_retval = i; - return NS_OK; - } - } + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + int ind = ::sqlite3_bind_parameter_index(mDBStatement, + PromiseFlatCString(aName).get()); + if (ind == 0) // Named parameter not found. return NS_ERROR_INVALID_ARG; + + *_index = ind - 1; // SQLite indexes are 1-based, we are 0-based. + + return NS_OK; } -/* void reset (); */ NS_IMETHODIMP -mozStorageStatement::Reset() +Statement::GetColumnCount(PRUint32 *_columnCount) { - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; -#ifdef DEBUG - PR_LOG(gStorageLog, PR_LOG_DEBUG, ("Resetting statement: '%s'", - sqlite3_sql(mDBStatement))); - - checkAndLogStatementPerformance(mDBStatement); -#endif - - sqlite3_reset(mDBStatement); - sqlite3_clear_bindings(mDBStatement); - - mExecuting = PR_FALSE; - - return NS_OK; + *_columnCount = mResultColumnCount; + return NS_OK; } -/* void bindUTF8StringParameter (in unsigned long aParamIndex, in AUTF8String aValue); */ NS_IMETHODIMP -mozStorageStatement::BindUTF8StringParameter(PRUint32 aParamIndex, const nsACString & aValue) +Statement::GetColumnName(PRUint32 aColumnIndex, + nsACString &_name) { - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + ENSURE_INDEX_VALUE(aColumnIndex, mResultColumnCount); - int srv = sqlite3_bind_text (mDBStatement, aParamIndex + 1, - nsPromiseFlatCString(aValue).get(), - aValue.Length(), SQLITE_TRANSIENT); + const char *cname = ::sqlite3_column_name(mDBStatement, aColumnIndex); + _name.Assign(nsDependentCString(cname)); - return convertResultCode(srv); + return NS_OK; } -/* void bindStringParameter (in unsigned long aParamIndex, in AString aValue); */ NS_IMETHODIMP -mozStorageStatement::BindStringParameter(PRUint32 aParamIndex, const nsAString & aValue) +Statement::GetColumnIndex(const nsACString &aName, + PRUint32 *_index) { - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; - int srv = sqlite3_bind_text16 (mDBStatement, aParamIndex + 1, - nsPromiseFlatString(aValue).get(), - aValue.Length() * 2, SQLITE_TRANSIENT); - - return convertResultCode(srv); -} - -/* void bindDoubleParameter (in unsigned long aParamIndex, in double aValue); */ -NS_IMETHODIMP -mozStorageStatement::BindDoubleParameter(PRUint32 aParamIndex, double aValue) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - int srv = sqlite3_bind_double (mDBStatement, aParamIndex + 1, aValue); - - return convertResultCode(srv); -} - -/* void bindInt32Parameter (in unsigned long aParamIndex, in long aValue); */ -NS_IMETHODIMP -mozStorageStatement::BindInt32Parameter(PRUint32 aParamIndex, PRInt32 aValue) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - int srv = sqlite3_bind_int (mDBStatement, aParamIndex + 1, aValue); - - return convertResultCode(srv); -} - -/* void bindInt64Parameter (in unsigned long aParamIndex, in long long aValue); */ -NS_IMETHODIMP -mozStorageStatement::BindInt64Parameter(PRUint32 aParamIndex, PRInt64 aValue) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - int srv = sqlite3_bind_int64 (mDBStatement, aParamIndex + 1, aValue); - - return convertResultCode(srv); -} - -/* void bindNullParameter (in unsigned long aParamIndex); */ -NS_IMETHODIMP -mozStorageStatement::BindNullParameter(PRUint32 aParamIndex) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - int srv = sqlite3_bind_null (mDBStatement, aParamIndex + 1); - - return convertResultCode(srv); -} - -/* void bindBlobParameter (in unsigned long aParamIndex, [array, const, size_is (aValueSize)] in octet aValue, in unsigned long aValueSize); */ -NS_IMETHODIMP -mozStorageStatement::BindBlobParameter(PRUint32 aParamIndex, const PRUint8 *aValue, PRUint32 aValueSize) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - int srv = sqlite3_bind_blob (mDBStatement, aParamIndex + 1, aValue, - aValueSize, SQLITE_TRANSIENT); - - return convertResultCode(srv); -} - -/* void execute (); */ -NS_IMETHODIMP -mozStorageStatement::Execute() -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - PRBool ret; - nsresult rv = ExecuteStep(&ret); - NS_ENSURE_SUCCESS(rv, rv); - - return Reset(); -} - -/* boolean executeStep (); */ -NS_IMETHODIMP -mozStorageStatement::ExecuteStep(PRBool *_retval) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - int srv = sqlite3_step (mDBStatement); - -#ifdef PR_LOGGING - if (srv != SQLITE_ROW && srv != SQLITE_DONE) - { - nsCAutoString errStr; - mDBConnection->GetLastErrorString(errStr); - PR_LOG(gStorageLog, PR_LOG_DEBUG, ("mozStorageStatement::ExecuteStep error: %s", errStr.get())); - } -#endif - - // SQLITE_ROW and SQLITE_DONE are non-errors - if (srv == SQLITE_ROW) { - // we got a row back - mExecuting = PR_TRUE; - *_retval = PR_TRUE; - return NS_OK; - } else if (srv == SQLITE_DONE) { - // statement is done (no row returned) - mExecuting = PR_FALSE; - *_retval = PR_FALSE; - return NS_OK; - } else if (srv == SQLITE_BUSY || srv == SQLITE_MISUSE) { - mExecuting = PR_FALSE; - } else if (mExecuting == PR_TRUE) { -#ifdef PR_LOGGING - PR_LOG(gStorageLog, PR_LOG_ERROR, ("SQLite error after mExecuting was true!")); -#endif - mExecuting = PR_FALSE; - } - - return convertResultCode(srv); -} - -/* nsICancelable executeAsync([optional] in storageIStatementCallback aCallback); */ -nsresult -mozStorageStatement::ExecuteAsync(mozIStorageStatementCallback *aCallback, - mozIStoragePendingStatement **_stmt) -{ - mozIStorageStatement * stmts[1] = {this}; - return mDBConnection->ExecuteAsync(stmts, 1, aCallback, _stmt); -} - -/* readonly attribute long state; */ -NS_IMETHODIMP -mozStorageStatement::GetState(PRInt32 *_retval) -{ - if (!mDBStatement) { - *_retval = MOZ_STORAGE_STATEMENT_INVALID; - } else if (mExecuting) { - *_retval = MOZ_STORAGE_STATEMENT_EXECUTING; - } else { - *_retval = MOZ_STORAGE_STATEMENT_READY; - } - - return NS_OK; -} - -/*** - *** mozIStorageValueArray - ***/ - -/* readonly attribute unsigned long numEntries; */ -NS_IMETHODIMP -mozStorageStatement::GetNumEntries(PRUint32 *aLength) -{ - *aLength = mResultColumnCount; - return NS_OK; -} - -/* long getTypeOfIndex (in unsigned long aIndex); */ -NS_IMETHODIMP -mozStorageStatement::GetTypeOfIndex(PRUint32 aIndex, PRInt32 *_retval) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - if (aIndex < 0 || aIndex >= mResultColumnCount) - return NS_ERROR_ILLEGAL_VALUE; - - if (!mExecuting) - return NS_ERROR_UNEXPECTED; - - int t = sqlite3_column_type (mDBStatement, aIndex); - switch (t) { - case SQLITE_INTEGER: - *_retval = VALUE_TYPE_INTEGER; - break; - case SQLITE_FLOAT: - *_retval = VALUE_TYPE_FLOAT; - break; - case SQLITE_TEXT: - *_retval = VALUE_TYPE_TEXT; - break; - case SQLITE_BLOB: - *_retval = VALUE_TYPE_BLOB; - break; - case SQLITE_NULL: - *_retval = VALUE_TYPE_NULL; - break; - default: - // ??? - return NS_ERROR_FAILURE; - } - - return NS_OK; -} - -/* long getInt32 (in unsigned long aIndex); */ -NS_IMETHODIMP -mozStorageStatement::GetInt32(PRUint32 aIndex, PRInt32 *_retval) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - if (aIndex < 0 || aIndex >= mResultColumnCount) - return NS_ERROR_ILLEGAL_VALUE; - - if (!mExecuting) - return NS_ERROR_UNEXPECTED; - - *_retval = sqlite3_column_int (mDBStatement, aIndex); - - return NS_OK; -} - -/* long long getInt64 (in unsigned long aIndex); */ -NS_IMETHODIMP -mozStorageStatement::GetInt64(PRUint32 aIndex, PRInt64 *_retval) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - if (aIndex < 0 || aIndex >= mResultColumnCount) - return NS_ERROR_ILLEGAL_VALUE; - - if (!mExecuting) - return NS_ERROR_UNEXPECTED; - - *_retval = sqlite3_column_int64 (mDBStatement, aIndex); - - return NS_OK; -} - -/* double getDouble (in unsigned long aIndex); */ -NS_IMETHODIMP -mozStorageStatement::GetDouble(PRUint32 aIndex, double *_retval) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - if (aIndex < 0 || aIndex >= mResultColumnCount) - return NS_ERROR_ILLEGAL_VALUE; - - if (!mExecuting) - return NS_ERROR_UNEXPECTED; - - *_retval = sqlite3_column_double (mDBStatement, aIndex); - - return NS_OK; -} - -/* AUTF8String getUTF8String (in unsigned long aIndex); */ -NS_IMETHODIMP -mozStorageStatement::GetUTF8String(PRUint32 aIndex, nsACString & _retval) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - // Get type of Index will check aIndex for us, so we don't have to. - PRInt32 t; - nsresult rv = GetTypeOfIndex (aIndex, &t); - NS_ENSURE_SUCCESS(rv, rv); - if (t == VALUE_TYPE_NULL) { - // null columns get IsVoid set to distinguish them from empty strings - _retval.Truncate(0); - _retval.SetIsVoid(PR_TRUE); - } else { - int slen = sqlite3_column_bytes (mDBStatement, aIndex); - const unsigned char *cstr = sqlite3_column_text (mDBStatement, aIndex); - _retval.Assign ((char *) cstr, slen); - } - return NS_OK; -} - -/* AString getString (in unsigned long aIndex); */ -NS_IMETHODIMP -mozStorageStatement::GetString(PRUint32 aIndex, nsAString & _retval) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - // Get type of Index will check aIndex for us, so we don't have to. - PRInt32 t; - nsresult rv = GetTypeOfIndex (aIndex, &t); - NS_ENSURE_SUCCESS(rv, rv); - if (t == VALUE_TYPE_NULL) { - // null columns get IsVoid set to distinguish them from empty strings - _retval.Truncate(0); - _retval.SetIsVoid(PR_TRUE); - } else { - int slen = sqlite3_column_bytes16 (mDBStatement, aIndex); - const void *text = sqlite3_column_text16 (mDBStatement, aIndex); - const PRUnichar *wstr = static_cast(text); - _retval.Assign (wstr, slen/2); - } - return NS_OK; -} - -/* void getBlob (in unsigned long aIndex, out unsigned long aDataSize, [array, size_is (aDataSize)] out octet aData); */ -NS_IMETHODIMP -mozStorageStatement::GetBlob(PRUint32 aIndex, PRUint32 *aDataSize, PRUint8 **aData) -{ - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - if (aIndex < 0 || aIndex >= mResultColumnCount) - return NS_ERROR_ILLEGAL_VALUE; - - if (!mExecuting) - return NS_ERROR_UNEXPECTED; - - int blobsize = sqlite3_column_bytes (mDBStatement, aIndex); - if (blobsize == 0) { - // empty column - *aData = nsnull; - *aDataSize = 0; + // Surprisingly enough, SQLite doesn't provide an API for this. We have to + // determine it ourselves sadly. + for (PRUint32 i = 0; i < mResultColumnCount; i++) { + if (mColumnNames[i].Equals(aName)) { + *_index = i; return NS_OK; } - const void *blob = sqlite3_column_blob (mDBStatement, aIndex); + } - void *blobcopy = nsMemory::Clone(blob, blobsize); - if (blobcopy == NULL) - return NS_ERROR_OUT_OF_MEMORY; - - *aData = (PRUint8*) blobcopy; - *aDataSize = blobsize; - - return NS_OK; + return NS_ERROR_INVALID_ARG; } -/* [noscript] void getSharedUTF8String(in unsigned long aIndex, out unsigned long aLength, [shared,retval] out string aResult); */ NS_IMETHODIMP -mozStorageStatement::GetSharedUTF8String(PRUint32 aIndex, PRUint32 *aLength, const char **_retval) +Statement::Reset() { - if (aLength) { - int slen = sqlite3_column_bytes (mDBStatement, aIndex); - *aLength = slen; - } + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; - *_retval = (const char *) sqlite3_column_text (mDBStatement, aIndex); - return NS_OK; +#ifdef DEBUG + PR_LOG(gStorageLog, PR_LOG_DEBUG, ("Resetting statement: '%s'", + ::sqlite3_sql(mDBStatement))); + + checkAndLogStatementPerformance(mDBStatement); +#endif + + (void)sqlite3_reset(mDBStatement); + (void)sqlite3_clear_bindings(mDBStatement); + + mExecuting = false; + + return NS_OK; } -/* [noscript] void getSharedString(in unsigned long aIndex, out unsigned long aLength, [shared,retval] out wstring aResult); */ NS_IMETHODIMP -mozStorageStatement::GetSharedString(PRUint32 aIndex, PRUint32 *aLength, const PRUnichar **_retval) +Statement::BindUTF8StringParameter(PRUint32 aParamIndex, + const nsACString &aValue) { - if (aLength) { - int slen = sqlite3_column_bytes16 (mDBStatement, aIndex); - *aLength = slen; - } + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; - *_retval = (const PRUnichar *) sqlite3_column_text16 (mDBStatement, aIndex); - return NS_OK; + int srv = ::sqlite3_bind_text(mDBStatement, aParamIndex + 1, + PromiseFlatCString(aValue).get(), + aValue.Length(), SQLITE_TRANSIENT); + return convertResultCode(srv); } -/* [noscript] void getSharedBlob(in unsigned long aIndex, out unsigned long aLength, [shared,retval] out octetPtr aResult); */ NS_IMETHODIMP -mozStorageStatement::GetSharedBlob(PRUint32 aIndex, PRUint32 *aDataSize, const PRUint8 **aData) +Statement::BindStringParameter(PRUint32 aParamIndex, + const nsAString &aValue) { - *aDataSize = sqlite3_column_bytes (mDBStatement, aIndex); - *aData = (const PRUint8*) sqlite3_column_blob (mDBStatement, aIndex); + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; - return NS_OK; + int srv = ::sqlite3_bind_text16(mDBStatement, aParamIndex + 1, + PromiseFlatString(aValue).get(), + aValue.Length() * 2, SQLITE_TRANSIENT); + return convertResultCode(srv); } -/* boolean getIsNull (in unsigned long aIndex); */ NS_IMETHODIMP -mozStorageStatement::GetIsNull(PRUint32 aIndex, PRBool *_retval) +Statement::BindDoubleParameter(PRUint32 aParamIndex, + double aValue) { - // Get type of Index will check aIndex for us, so we don't have to. - PRInt32 t; - nsresult rv = GetTypeOfIndex (aIndex, &t); - NS_ENSURE_SUCCESS(rv, rv); + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; - if (t == VALUE_TYPE_NULL) - *_retval = PR_TRUE; - else - *_retval = PR_FALSE; - - return NS_OK; + int srv = ::sqlite3_bind_double(mDBStatement, aParamIndex + 1, aValue); + return convertResultCode(srv); } -/* AString escapeStringForLIKE(in AString aValue, in char aEscapeChar); */ NS_IMETHODIMP -mozStorageStatement::EscapeStringForLIKE(const nsAString & aValue, - const PRUnichar aEscapeChar, - nsAString &aEscapedString) +Statement::BindInt32Parameter(PRUint32 aParamIndex, + PRInt32 aValue) { - const PRUnichar MATCH_ALL('%'); - const PRUnichar MATCH_ONE('_'); + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; - aEscapedString.Truncate(0); - - for (PRInt32 i = 0; i < aValue.Length(); i++) { - if (aValue[i] == aEscapeChar || aValue[i] == MATCH_ALL || - aValue[i] == MATCH_ONE) - aEscapedString += aEscapeChar; - aEscapedString += aValue[i]; - } - return NS_OK; + int srv = ::sqlite3_bind_int(mDBStatement, aParamIndex + 1, aValue); + return convertResultCode(srv); } -/* AString getColumnDecltype(in unsigned long aParamIndex); */ NS_IMETHODIMP -mozStorageStatement::GetColumnDecltype(PRUint32 aParamIndex, - nsACString& aDeclType) +Statement::BindInt64Parameter(PRUint32 aParamIndex, + PRInt64 aValue) { - if (!mDBStatement) - return NS_ERROR_NOT_INITIALIZED; - - if (aParamIndex < 0 || aParamIndex >= mResultColumnCount) - return NS_ERROR_ILLEGAL_VALUE; + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; - const char *declType = sqlite3_column_decltype(mDBStatement, aParamIndex); - aDeclType.Assign(declType); - - return NS_OK; + int srv = ::sqlite3_bind_int64(mDBStatement, aParamIndex + 1, aValue); + return convertResultCode(srv); } + +NS_IMETHODIMP +Statement::BindNullParameter(PRUint32 aParamIndex) +{ + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + + int srv = ::sqlite3_bind_null(mDBStatement, aParamIndex + 1); + return convertResultCode(srv); +} + +NS_IMETHODIMP +Statement::BindBlobParameter(PRUint32 aParamIndex, + const PRUint8 *aValue, + PRUint32 aValueSize) +{ + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + + int srv = ::sqlite3_bind_blob(mDBStatement, aParamIndex + 1, aValue, + aValueSize, SQLITE_TRANSIENT); + return convertResultCode(srv); +} + +NS_IMETHODIMP +Statement::Execute() +{ + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + + PRBool ret; + nsresult rv = ExecuteStep(&ret); + NS_ENSURE_SUCCESS(rv, rv); + + return Reset(); +} + +NS_IMETHODIMP +Statement::ExecuteStep(PRBool *_moreResults) +{ + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + + int srv = ::sqlite3_step(mDBStatement); + +#ifdef PR_LOGGING + if (srv != SQLITE_ROW && srv != SQLITE_DONE) { + nsCAutoString errStr; + (void)mDBConnection->GetLastErrorString(errStr); + PR_LOG(gStorageLog, PR_LOG_DEBUG, + ("Statement::ExecuteStep error: %s", errStr.get())); + } +#endif + + // SQLITE_ROW and SQLITE_DONE are non-errors + if (srv == SQLITE_ROW) { + // we got a row back + mExecuting = true; + *_moreResults = PR_TRUE; + return NS_OK; + } + else if (srv == SQLITE_DONE) { + // statement is done (no row returned) + mExecuting = false; + *_moreResults = PR_FALSE; + return NS_OK; + } + else if (srv == SQLITE_BUSY || srv == SQLITE_MISUSE) { + mExecuting = PR_FALSE; + } + else if (mExecuting) { +#ifdef PR_LOGGING + PR_LOG(gStorageLog, PR_LOG_ERROR, + ("SQLite error after mExecuting was true!")); +#endif + mExecuting = PR_FALSE; + } + + return convertResultCode(srv); +} + +nsresult +Statement::ExecuteAsync(mozIStorageStatementCallback *aCallback, + mozIStoragePendingStatement **_stmt) +{ + mozIStorageStatement *stmts[1] = {this}; + return mDBConnection->ExecuteAsync(stmts, 1, aCallback, _stmt); +} + +NS_IMETHODIMP +Statement::GetState(PRInt32 *_state) +{ + if (!mDBStatement) + *_state = MOZ_STORAGE_STATEMENT_INVALID; + else if (mExecuting) + *_state = MOZ_STORAGE_STATEMENT_EXECUTING; + else + *_state = MOZ_STORAGE_STATEMENT_READY; + + return NS_OK; +} + +NS_IMETHODIMP +Statement::EscapeStringForLIKE(const nsAString &aValue, + const PRUnichar aEscapeChar, + nsAString &_escapedString) +{ + const PRUnichar MATCH_ALL('%'); + const PRUnichar MATCH_ONE('_'); + + _escapedString.Truncate(0); + + for (PRInt32 i = 0; i < aValue.Length(); i++) { + if (aValue[i] == aEscapeChar || aValue[i] == MATCH_ALL || + aValue[i] == MATCH_ONE) + _escapedString += aEscapeChar; + _escapedString += aValue[i]; + } + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetColumnDecltype(PRUint32 aParamIndex, + nsACString &_declType) +{ + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + + ENSURE_INDEX_VALUE(aParamIndex, mResultColumnCount); + + _declType.Assign(::sqlite3_column_decltype(mDBStatement, aParamIndex)); + return NS_OK; +} + +//////////////////////////////////////////////////////////////////////////////// +//// mozIStorageValueArray + +NS_IMETHODIMP +Statement::GetNumEntries(PRUint32 *_length) +{ + *_length = mResultColumnCount; + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetTypeOfIndex(PRUint32 aIndex, + PRInt32 *_type) +{ + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + + ENSURE_INDEX_VALUE(aIndex, mResultColumnCount); + + if (!mExecuting) + return NS_ERROR_UNEXPECTED; + + int t = ::sqlite3_column_type(mDBStatement, aIndex); + switch (t) { + case SQLITE_INTEGER: + *_type = VALUE_TYPE_INTEGER; + break; + case SQLITE_FLOAT: + *_type = VALUE_TYPE_FLOAT; + break; + case SQLITE_TEXT: + *_type = VALUE_TYPE_TEXT; + break; + case SQLITE_BLOB: + *_type = VALUE_TYPE_BLOB; + break; + case SQLITE_NULL: + *_type = VALUE_TYPE_NULL; + break; + default: + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetInt32(PRUint32 aIndex, + PRInt32 *_value) +{ + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + + ENSURE_INDEX_VALUE(aIndex, mResultColumnCount); + + if (!mExecuting) + return NS_ERROR_UNEXPECTED; + + *_value = ::sqlite3_column_int(mDBStatement, aIndex); + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetInt64(PRUint32 aIndex, + PRInt64 *_value) +{ + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + + ENSURE_INDEX_VALUE(aIndex, mResultColumnCount); + + if (!mExecuting) + return NS_ERROR_UNEXPECTED; + + *_value = ::sqlite3_column_int64(mDBStatement, aIndex); + + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetDouble(PRUint32 aIndex, + double *_value) +{ + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + + ENSURE_INDEX_VALUE(aIndex, mResultColumnCount); + + if (!mExecuting) + return NS_ERROR_UNEXPECTED; + + *_value = ::sqlite3_column_double(mDBStatement, aIndex); + + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetUTF8String(PRUint32 aIndex, + nsACString &_value) +{ + // Get type of Index will check aIndex for us, so we don't have to. + PRInt32 type; + nsresult rv = GetTypeOfIndex(aIndex, &type); + NS_ENSURE_SUCCESS(rv, rv); + if (type == VALUE_TYPE_NULL) { + // NULL columns should have IsVod set to distinguis them from an empty + // string. + _value.Truncate(0); + _value.SetIsVoid(PR_TRUE); + } + else { + const char *value = + reinterpret_cast(::sqlite3_column_text(mDBStatement, + aIndex)); + _value.Assign(value, ::sqlite3_column_bytes(mDBStatement, aIndex)); + } + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetString(PRUint32 aIndex, + nsAString &_value) +{ + // Get type of Index will check aIndex for us, so we don't have to. + PRInt32 type; + nsresult rv = GetTypeOfIndex(aIndex, &type); + NS_ENSURE_SUCCESS(rv, rv); + if (type == VALUE_TYPE_NULL) { + // NULL columns should have IsVod set to distinguis them from an empty + // string. + _value.Truncate(0); + _value.SetIsVoid(PR_TRUE); + } else { + const PRUnichar *value = + static_cast(::sqlite3_column_text16(mDBStatement, + aIndex)); + _value.Assign(value, ::sqlite3_column_bytes16(mDBStatement, aIndex) / 2); + } + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetBlob(PRUint32 aIndex, + PRUint32 *_size, + PRUint8 **_blob) +{ + if (!mDBStatement) + return NS_ERROR_NOT_INITIALIZED; + + ENSURE_INDEX_VALUE(aIndex, mResultColumnCount); + + if (!mExecuting) + return NS_ERROR_UNEXPECTED; + + int size = ::sqlite3_column_bytes(mDBStatement, aIndex); + void *blob = nsnull; + if (size) { + blob = nsMemory::Clone(::sqlite3_column_blob(mDBStatement, aIndex), size); + NS_ENSURE_TRUE(blob, NS_ERROR_OUT_OF_MEMORY); + } + + *_blob = static_cast(blob); + *_size = size; + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetSharedUTF8String(PRUint32 aIndex, + PRUint32 *_length, + const char **_value) +{ + if (_length) + *_length = ::sqlite3_column_bytes(mDBStatement, aIndex); + + *_value = reinterpret_cast(::sqlite3_column_text(mDBStatement, + aIndex)); + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetSharedString(PRUint32 aIndex, + PRUint32 *_length, + const PRUnichar **_value) +{ + if (_length) + *_length = ::sqlite3_column_bytes16(mDBStatement, aIndex); + + *_value = static_cast(::sqlite3_column_text16(mDBStatement, + aIndex)); + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetSharedBlob(PRUint32 aIndex, + PRUint32 *_size, + const PRUint8 **_blob) +{ + *_size = ::sqlite3_column_bytes(mDBStatement, aIndex); + *_blob = static_cast(::sqlite3_column_blob(mDBStatement, + aIndex)); + return NS_OK; +} + +NS_IMETHODIMP +Statement::GetIsNull(PRUint32 aIndex, + PRBool *_isNull) +{ + // Get type of Index will check aIndex for us, so we don't have to. + PRInt32 type; + nsresult rv = GetTypeOfIndex(aIndex, &type); + NS_ENSURE_SUCCESS(rv, rv); + *_isNull = (type == VALUE_TYPE_NULL); + return NS_OK; +} + +} // namespace storage +} // namespace mozilla diff --git a/storage/src/mozStorageStatement.h b/storage/src/mozStorageStatement.h index f0627334621..4e9388df1c6 100644 --- a/storage/src/mozStorageStatement.h +++ b/storage/src/mozStorageStatement.h @@ -1,5 +1,6 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* ***** BEGIN LICENSE BLOCK ***** +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : + * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version @@ -36,8 +37,8 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef _MOZSTORAGESTATEMENT_H_ -#define _MOZSTORAGESTATEMENT_H_ +#ifndef _mozStorageStatement_h_ +#define _mozStorageStatement_h_ #include "nsAutoPtr.h" #include "nsString.h" @@ -46,55 +47,50 @@ #include "mozIStorageStatement.h" -#include - class nsIXPConnectJSObjectHolder; +struct sqlite3_stmt; namespace mozilla { namespace storage { class StatementJSHelper; class Connection; -} // storage -} // mozilla -class mozStorageStatement : public mozIStorageStatement +class Statement : public mozIStorageStatement { public: - mozStorageStatement(); + NS_DECL_ISUPPORTS + NS_DECL_MOZISTORAGESTATEMENT + NS_DECL_MOZISTORAGEVALUEARRAY - // interfaces - NS_DECL_ISUPPORTS - NS_DECL_MOZISTORAGESTATEMENT - NS_DECL_MOZISTORAGEVALUEARRAY + Statement(); - /** - * Initializes the object on aDBConnection by preparing the SQL statement - * given by aSQLStatement. - * - * @param aDBConnection - * The mozStorageConnection object this statement is associated with. - * @param aSQLStatement - * The SQL statement to prepare that this object will represent. - */ - nsresult Initialize(mozilla::storage::Connection *aDBConnection, - const nsACString &aSQLStatement); + /** + * Initializes the object on aDBConnection by preparing the SQL statement + * given by aSQLStatement. + * + * @param aDBConnection + * The Connection object this statement is associated with. + * @param aSQLStatement + * The SQL statement to prepare that this object will represent. + */ + nsresult initialize(Connection *aDBConnection, + const nsACString &aSQLStatement); - /** - * Obtains the native statement pointer. - */ - inline sqlite3_stmt *nativeStatement() { return mDBStatement; } + /** + * Obtains the native statement pointer. + */ + inline sqlite3_stmt *nativeStatement() { return mDBStatement; } private: - ~mozStorageStatement(); + ~Statement(); -protected: - nsRefPtr mDBConnection; + nsRefPtr mDBConnection; sqlite3_stmt *mDBStatement; PRUint32 mParamCount; PRUint32 mResultColumnCount; nsTArray mColumnNames; - PRBool mExecuting; + bool mExecuting; /** * The following two members are only used with the JS helper. They cache @@ -103,7 +99,10 @@ protected: nsCOMPtr mStatementParamsHolder; nsCOMPtr mStatementRowHolder; - friend class mozilla::storage::StatementJSHelper; + friend class StatementJSHelper; }; -#endif /* _MOZSTORAGESTATEMENT_H_ */ +} // storage +} // mozilla + +#endif // _mozStorageStatement_h_ diff --git a/storage/src/mozStorageStatementJSHelper.cpp b/storage/src/mozStorageStatementJSHelper.cpp index 76988db5bfd..986c5b91ebd 100644 --- a/storage/src/mozStorageStatementJSHelper.cpp +++ b/storage/src/mozStorageStatementJSHelper.cpp @@ -74,8 +74,7 @@ stepFunc(JSContext *aCtx, return JS_FALSE; } - mozStorageStatement *stmt = - static_cast(wrapper->Native()); + Statement *stmt = static_cast(wrapper->Native()); #ifdef DEBUG { @@ -105,7 +104,7 @@ stepFunc(JSContext *aCtx, //// StatementJSHelper nsresult -StatementJSHelper::getRow(mozStorageStatement *aStatement, +StatementJSHelper::getRow(Statement *aStatement, JSContext *aCtx, JSObject *aScopeObj, jsval *_row) @@ -141,7 +140,7 @@ StatementJSHelper::getRow(mozStorageStatement *aStatement, } nsresult -StatementJSHelper::getParams(mozStorageStatement *aStatement, +StatementJSHelper::getParams(Statement *aStatement, JSContext *aCtx, JSObject *aScopeObj, jsval *_params) @@ -205,8 +204,7 @@ StatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper, if (!JSVAL_IS_STRING(aId)) return NS_OK; - mozStorageStatement *stmt = - static_cast(aWrapper->Native()); + Statement *stmt = static_cast(aWrapper->Native()); #ifdef DEBUG { diff --git a/storage/src/mozStorageStatementJSHelper.h b/storage/src/mozStorageStatementJSHelper.h index 08f05c6e8a8..fe7caa54185 100644 --- a/storage/src/mozStorageStatementJSHelper.h +++ b/storage/src/mozStorageStatementJSHelper.h @@ -42,7 +42,7 @@ #include "nsIXPCScriptable.h" -class mozStorageStatement; +class Statement; namespace mozilla { namespace storage { @@ -54,8 +54,8 @@ public: NS_DECL_NSIXPCSCRIPTABLE private: - nsresult getRow(mozStorageStatement *, JSContext *, JSObject *, jsval *); - nsresult getParams(mozStorageStatement *, JSContext *, JSObject *, jsval *); + nsresult getRow(Statement *, JSContext *, JSObject *, jsval *); + nsresult getParams(Statement *, JSContext *, JSObject *, jsval *); }; } // namespace storage diff --git a/storage/src/mozStorageStatementParams.h b/storage/src/mozStorageStatementParams.h index fc75746e441..f80c291364b 100644 --- a/storage/src/mozStorageStatementParams.h +++ b/storage/src/mozStorageStatementParams.h @@ -47,11 +47,12 @@ #include "jsdate.h" class mozIStorageStatement; -class mozStorageStatement; namespace mozilla { namespace storage { +class Statement; + class StatementParams : public mozIStorageStatementParams , public nsIXPCScriptable { @@ -67,7 +68,7 @@ protected: mozIStorageStatement *mStatement; PRUint32 mParamCount; - friend class ::mozStorageStatement; + friend class Statement; }; static diff --git a/storage/src/mozStorageStatementRow.cpp b/storage/src/mozStorageStatementRow.cpp index bdbc03920c8..3390ddda7e0 100644 --- a/storage/src/mozStorageStatementRow.cpp +++ b/storage/src/mozStorageStatementRow.cpp @@ -53,8 +53,8 @@ namespace storage { //////////////////////////////////////////////////////////////////////////////// //// StatementRow -StatementRow::StatementRow(mozStorageStatement *aStatement) : - mStatement(aStatement) +StatementRow::StatementRow(Statement *aStatement) +: mStatement(aStatement) { } diff --git a/storage/src/mozStorageStatementRow.h b/storage/src/mozStorageStatementRow.h index 6b06580c770..b90c72dff8e 100644 --- a/storage/src/mozStorageStatementRow.h +++ b/storage/src/mozStorageStatementRow.h @@ -43,12 +43,11 @@ #include "mozIStorageStatementWrapper.h" #include "nsIXPCScriptable.h" -class mozStorageStatement; - - namespace mozilla { namespace storage { +class Statement; + class StatementRow : public mozIStorageStatementRow , public nsIXPCScriptable { @@ -57,12 +56,12 @@ public: NS_DECL_MOZISTORAGESTATEMENTROW NS_DECL_NSIXPCSCRIPTABLE - StatementRow(mozStorageStatement *aStatement); + StatementRow(Statement *aStatement); protected: - mozStorageStatement *mStatement; + Statement *mStatement; - friend class ::mozStorageStatement; + friend class Statement; }; } // namespace storage diff --git a/storage/src/mozStorageStatementWrapper.cpp b/storage/src/mozStorageStatementWrapper.cpp index 432b1879201..4009f1c6810 100644 --- a/storage/src/mozStorageStatementWrapper.cpp +++ b/storage/src/mozStorageStatementWrapper.cpp @@ -77,7 +77,7 @@ StatementWrapper::Initialize(mozIStorageStatement *aStatement) NS_ASSERTION(mStatement == nsnull, "StatementWrapper is already initialized"); NS_ENSURE_ARG_POINTER(aStatement); - mStatement = static_cast(aStatement); + mStatement = static_cast(aStatement); // fetch various things we care about (void)mStatement->GetParameterCount(&mParamCount); diff --git a/storage/src/mozStorageStatementWrapper.h b/storage/src/mozStorageStatementWrapper.h index 7f8a083ca31..b38fbea6776 100644 --- a/storage/src/mozStorageStatementWrapper.h +++ b/storage/src/mozStorageStatementWrapper.h @@ -67,7 +67,7 @@ private: return mStatement->nativeStatement(); } - nsRefPtr mStatement; + nsRefPtr mStatement; PRUint32 mParamCount; PRUint32 mResultColumnCount; nsTArray mColumnNames;