Bug 403068 - "Need a wrapper function for SQLite function sqlite3_column_decltype" [p=jzhang@aptana.com (john Zhang) r=sdwilsh a1.9=damons]
authorreed@reedloden.com
Tue, 13 Nov 2007 00:26:45 -0800
changeset 7926 cf417c3b48356633fa19b23f56215c98c073b728
parent 7925 f8980ff41ffbe82ebe365927208fa140b8828a57
child 7927 0f1bb27c8646c5255a5c2917ef11f9af9142a07b
push idunknown
push userunknown
push dateunknown
reviewerssdwilsh
bugs403068
milestone1.9b2pre
Bug 403068 - "Need a wrapper function for SQLite function sqlite3_column_decltype" [p=jzhang@aptana.com (john Zhang) r=sdwilsh a1.9=damons]
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(1a95a6ec-e4b0-4d0b-947b-d2f40b04d057)]
+[scriptable, uuid(42fad13e-c67d-4b2c-bd61-2c5b17186772)]
 interface mozIStorageStatement : mozIStorageValueArray {
   /**
    * Initialize this query with the given SQL statement.
    *
    */
   void initialize(in mozIStorageConnection aDBConnection,
                   in AUTF8String aSQLStatement);
 
@@ -102,16 +102,25 @@ interface mozIStorageStatement : mozISto
    * Obtains the index of the column with the specified name.
    *
    * @param aName The name of the column.
    * @return The index of the column with the specified name.
    */
   unsigned long getColumnIndex(in AUTF8String aName);
 
   /**
+   * Obtains the declared column type of a prepared statement.
+   *
+   * @param aParamIndex The zero-based index of the column who's declared type
+   *                    we are interested in.
+   * @returns the declared index type.
+   */
+  AUTF8String getColumnDecltype(in unsigned long aParamIndex);
+
+  /**
    * Reset parameters/statement execution
    */
   void reset();
 
   /**
    * Bind the given value to the parameter at aParamIndex. Index 0
    * denotes the first numbered argument or ?1.
    */
--- a/storage/src/mozStorageStatement.cpp
+++ b/storage/src/mozStorageStatement.cpp
@@ -18,16 +18,17 @@
  * The Initial Developer of the Original Code is
  *  Oracle Corporation
  * Portions created by the Initial Developer are Copyright (C) 2004
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
  *   Shawn Wilsher <me@shawnwilsher.com>
+ *   John Zhang <jzhang@aptana.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -800,8 +801,25 @@ mozStorageStatement::EscapeStringForLIKE
     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;
 }
+
+/* AString getColumnDecltype(in unsigned long aParamIndex); */
+NS_IMETHODIMP
+mozStorageStatement::GetColumnDecltype(PRUint32 aParamIndex,
+                                       nsACString& aDeclType)
+{
+    if (!mDBConnection || !mDBStatement)
+        return NS_ERROR_NOT_INITIALIZED;
+    
+    if (aParamIndex < 0 || aParamIndex >= mResultColumnCount)
+        return NS_ERROR_ILLEGAL_VALUE;
+
+    const char *declType = sqlite3_column_decltype(mDBStatement, aParamIndex);
+    aDeclType.Assign(declType);
+    
+    return NS_OK;
+}
--- a/storage/test/unit/test_storage_statement.js
+++ b/storage/test/unit/test_storage_statement.js
@@ -158,22 +158,37 @@ function test_state_executing()
 function test_state_after_finalize()
 {
   var stmt = createStatement("SELECT name, id FROM test");
   stmt.executeStep();
   stmt.finalize();
   do_check_eq(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_INVALID, stmt.state);
 }
 
+function test_getColumnDecltype()
+{
+  var stmt = createStatement("SELECT name, id FROM test");
+  do_check_eq("TEXT", stmt.getColumnDecltype(0));
+  do_check_eq("INTEGER", stmt.getColumnDecltype(1));
+  try {
+    do_check_eq("GARBAGE", stmt.getColumnDecltype(2));
+    do_throw("should not get here");
+  } catch (e) {
+    do_check_eq(Cr.NS_ERROR_ILLEGAL_VALUE, e.result);
+  }
+  stmt.finalize();
+}
+
 var tests = [test_parameterCount_none, test_parameterCount_one,
              test_getParameterName, test_getParameterIndex_different,
              test_getParameterIndex_same, test_columnCount,
              test_getColumnName, test_getColumnIndex_same_case,
              test_getColumnIndex_different_case, test_state_ready,
-             test_state_executing, test_state_after_finalize];
+             test_state_executing, test_state_after_finalize,
+             test_getColumnDecltype];
 
 function run_test()
 {
   setup();
 
   for (var i = 0; i < tests.length; i++)
     tests[i]();