Bug 388048 - mozIStorageStatement::getParameterIndexes is useless. r=sspitzer
authorsdwilsh@shawnwilsher.com
Sun, 15 Jul 2007 11:03:20 -0700
changeset 3498 d10224e9eb0313431b3011d9a674c67e4427e851
parent 3497 a95a13cdb9a2e65e780e42c8d6dcb35f280f4fbb
child 3499 7f4a5f44c2e09d63bbfa6f434374c28f69b52ca0
push idunknown
push userunknown
push dateunknown
reviewerssspitzer
bugs388048
milestone1.9a7pre
Bug 388048 - mozIStorageStatement::getParameterIndexes is useless. r=sspitzer
storage/public/mozIStorageStatement.idl
storage/src/mozStorageStatement.cpp
storage/test/unit/test_storage_statement.js
--- a/storage/public/mozIStorageStatement.idl
+++ b/storage/public/mozIStorageStatement.idl
@@ -40,17 +40,17 @@
 #include "mozIStorageValueArray.idl"
 
 interface mozIStorageConnection;
 interface mozIStorageDataSet;
 interface nsISimpleEnumerator;
 
 [ptr] native sqlite3stmtptr(struct sqlite3_stmt);
 
-[scriptable, uuid(1f39bc95-090d-40a5-9dee-6d5a591e48bf)]
+[scriptable, uuid(76b1b70e-5740-488d-b910-3f0f02ab2fd1)]
 interface mozIStorageStatement : mozIStorageValueArray {
   /**
    * Initialize this query with the given SQL statement.
    *
    */
   void initialize(in mozIStorageConnection aDBConnection,
                   in AUTF8String aSQLStatement);
 
@@ -69,22 +69,22 @@ interface mozIStorageStatement : mozISto
   readonly attribute unsigned long parameterCount;
 
   /**
    * Name of nth parameter, if given
    */
   AUTF8String getParameterName(in unsigned long aParamIndex);
 
   /**
-   * All indexes of a named parameter, if it's specified more than once
+   * 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.
    */
-  void getParameterIndexes
-    (in AUTF8String aParameterName,
-     out unsigned long aCount,
-     [array,size_is(aCount),retval] out unsigned long aIndexes);
+  unsigned long getParameterIndex(in AUTF8String aName);
 
   /**
    * Number of columns returned
    */
   readonly attribute unsigned long columnCount;
 
   /**
    * Name of nth column
--- a/storage/src/mozStorageStatement.cpp
+++ b/storage/src/mozStorageStatement.cpp
@@ -208,58 +208,29 @@ mozStorageStatement::GetParameterName(PR
         _retval.Assign(pname);
     } else {
         _retval.Assign(nsDependentCString(pname));
     }
 
     return NS_OK;
 }
 
-/* void getParameterIndexes(in AUTF8String aParameterName, out unsigned long aCount, [array,size_is(aCount),retval] out unsigned long aIndexes); */
+/* unsigned long getParameterIndex(in AUTF8String aParameterName); */
 NS_IMETHODIMP
-mozStorageStatement::GetParameterIndexes(const nsACString &aParameterName, PRUint32 *aCount, PRUint32 **aIndexes)
+mozStorageStatement::GetParameterIndex(const nsACString &aName,
+                                       PRUint32 *_retval)
 {
     NS_ASSERTION (mDBConnection && mDBStatement, "statement not initialized");
-    NS_ENSURE_ARG_POINTER(aCount);
-    NS_ENSURE_ARG_POINTER(aIndexes);
 
-    nsCAutoString name(":");
-    name.Append(aParameterName);
-
-    if (sqlite3_bind_parameter_index(mDBStatement, name.get()) == 0) {
-        // Named parameter not found
-        *aCount = 0;
-        *aIndexes = nsnull;
-        return NS_OK;
-    }
+    int ind = sqlite3_bind_parameter_index(mDBStatement,
+                                           nsPromiseFlatCString(aName).get());
+    if (ind  == 0) // Named parameter not found
+        return NS_ERROR_INVALID_ARG;
     
-    int count = sqlite3_bind_parameter_count(mDBStatement);
-    int *idxs = new int[count];
-    if (!idxs)
-        return NS_ERROR_OUT_OF_MEMORY;
-
-    int size = 0;
-    for (int i = 0; i < count; i++) {
-        // sqlite indices start at 1
-        const char *pName = sqlite3_bind_parameter_name(mDBStatement, i + 1);
-        if (name.Equals(pName))
-            idxs[size++] = i;
-    }
-
-    *aCount = size;
-    *aIndexes = (PRUint32*) NS_Alloc(sizeof(PRUint32) * size);
-    if (!aIndexes) {
-        delete[] idxs;
-        return NS_ERROR_OUT_OF_MEMORY;
-    }
-
-    for (int i = 0; i < size; i++)
-        (*aIndexes)[i] = idxs[i];
-
-    delete[] idxs;
+    *_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)
 {
--- a/storage/test/unit/test_storage_statement.js
+++ b/storage/test/unit/test_storage_statement.js
@@ -55,41 +55,27 @@ function test_parameterCount_one()
 }
 
 function test_getParameterName()
 {
   var stmt = createStatement("SELECT * FROM test WHERE id = :id");
   do_check_eq(":id", stmt.getParameterName(0));
 }
 
-function test_getParameterIndexes_different()
+function test_getParameterIndex_different()
 {
   var stmt = createStatement("SELECT * FROM test WHERE id = :id OR name = :name");
-  var count = { value: 0 };
-  var result = stmt.getParameterIndexes("id", count);
-  do_check_eq(1, count.value);
-  do_check_eq(1, result.length);
-  do_check_eq(0, result[0]);
-  
-  result = stmt.getParameterIndexes("name", count);
-  do_check_eq(1, count.value);
-  do_check_eq(1, result.length);
-  do_check_eq(1, result[0]);
+  do_check_eq(0, stmt.getParameterIndex(":id"));
+  do_check_eq(1, stmt.getParameterIndex(":name"));
 }
 
-function test_getParameterIndexes_same()
+function test_getParameterIndex_same()
 {
-  // XXX this looks like a bug in sqlite
-  var stmt = createStatement("SELECT * FROM test WHERE id = :test OR name = :test");
-
-  var count = { value: 0 };
-  var result = stmt.getParameterIndexes("test", count);
-  do_check_eq(1, count.value);
-  do_check_eq(1, result.length);
-  do_check_eq(0, result[0]);
+  var stmt = createStatement("SELECT * FROM test WHERE id = @test OR name = @test");
+  do_check_eq(0, stmt.getParameterIndex("@test"));
 }
 
 function test_columnCount()
 {
   var stmt = createStatement("SELECT * FROM test WHERE id = ?1 OR name = ?2");
   do_check_eq(2, stmt.columnCount);
 }
 
@@ -119,18 +105,18 @@ function test_state_executing()
   stmt.executeStep();
   do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_EXECUTING,
               stmt.state);
   stmt.reset();
   do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
 }
 
 var tests = [test_parameterCount_none, test_parameterCount_one,
-             test_getParameterName, test_getParameterIndexes_different,
-             test_getParameterIndexes_same, test_columnCount,
+             test_getParameterName, test_getParameterIndex_different,
+             test_getParameterIndex_same, test_columnCount,
              test_getColumnName, test_state_ready, test_state_executing];
 
 function run_test()
 {
   setup();
 
   for (var i = 0; i < tests.length; i++)
     tests[i]();