Bug 490833 - mozIStorageStatement::getParameterIndex should only accept :-named parameters

This makes getParameterIndex only work properly with :-named parameters.  This
simplifies the work callers actually have to do.
r=asuth
This commit is contained in:
Shawn Wilsher 2009-05-08 20:29:57 -04:00
parent 1d46153736
commit 7c59976c6e
4 changed files with 17 additions and 15 deletions

View File

@ -41,11 +41,10 @@
#include "mozIStorageValueArray.idl"
interface mozIStorageConnection;
interface nsISimpleEnumerator;
interface mozIStorageStatementCallback;
interface mozIStoragePendingStatement;
[scriptable, uuid(471e161a-af46-4eb3-adf6-f6b0c41fe81c)]
[scriptable, uuid(52d740cd-1c25-471f-a848-98d1a00a2963)]
interface mozIStorageStatement : mozIStorageValueArray {
/**
* Finalizes a statement so you can successfully close a database connection.
@ -76,8 +75,10 @@ interface mozIStorageStatement : mozIStorageValueArray {
/**
* Returns the index of the named parameter.
*
* @param aName The name of the parameter you want the index for.
* @return The index of the named parameter.
* @param aName
* The name of the parameter you want the index for. This does not
* include the leading ':'.
* @returns the index of the named parameter.
*/
unsigned long getParameterIndex(in AUTF8String aName);

View File

@ -345,8 +345,12 @@ Statement::GetParameterIndex(const nsACString &aName,
if (!mDBStatement)
return NS_ERROR_NOT_INITIALIZED;
// We do not accept any forms of names other than ":name", but we need to add
// the colon for SQLite.
nsCAutoString name(":");
name.Append(aName);
int ind = ::sqlite3_bind_parameter_index(mDBStatement,
PromiseFlatCString(aName).get());
PromiseFlatCString(name).get());
if (ind == 0) // Named parameter not found.
return NS_ERROR_INVALID_ARG;

View File

@ -91,9 +91,8 @@ StatementParams::SetProperty(nsIXPConnectWrappedNative *aWrapper,
}
else if (JSVAL_IS_STRING(aId)) {
JSString *str = JSVAL_TO_STRING(aId);
nsCAutoString name(":");
name.Append(NS_ConvertUTF16toUTF8(::JS_GetStringChars(str),
::JS_GetStringLength(str)));
NS_ConvertUTF16toUTF8 name(::JS_GetStringChars(str),
::JS_GetStringLength(str));
// check to see if there's a parameter with this name
PRUint32 index;
@ -206,11 +205,9 @@ StatementParams::NewResolve(nsIXPConnectWrappedNative *aWrapper,
jschar *nameChars = ::JS_GetStringChars(str);
size_t nameLength = ::JS_GetStringLength(str);
nsCAutoString name(":");
name.Append(NS_ConvertUTF16toUTF8(nameChars, nameLength));
// Check to see if there's a parameter with this name, and if not, let
// the rest of the prototype chain be checked.
NS_ConvertUTF16toUTF8 name(nameChars, nameLength);
nsresult rv = mStatement->GetParameterIndex(name, &idx);
if (NS_FAILED(rv))
return NS_OK;

View File

@ -69,16 +69,16 @@ function test_getParameterName()
function test_getParameterIndex_different()
{
var stmt = createStatement("SELECT * FROM test WHERE id = :id OR name = :name");
do_check_eq(0, stmt.getParameterIndex(":id"));
do_check_eq(1, stmt.getParameterIndex(":name"));
do_check_eq(0, stmt.getParameterIndex("id"));
do_check_eq(1, stmt.getParameterIndex("name"));
stmt.reset();
stmt.finalize();
}
function test_getParameterIndex_same()
{
var stmt = createStatement("SELECT * FROM test WHERE id = @test OR name = @test");
do_check_eq(0, stmt.getParameterIndex("@test"));
var stmt = createStatement("SELECT * FROM test WHERE id = :test OR name = :test");
do_check_eq(0, stmt.getParameterIndex("test"));
stmt.reset();
stmt.finalize();
}