Bug 388059 - Add a getColumnIndex method to mozIStorageStatement. r=sspitzer

This commit is contained in:
sdwilsh@shawnwilsher.com 2007-07-19 09:30:17 -07:00
parent 282070c07b
commit fba73878f4
4 changed files with 58 additions and 7 deletions

View File

@ -45,7 +45,7 @@ interface nsISimpleEnumerator;
[ptr] native sqlite3stmtptr(struct sqlite3_stmt);
[scriptable, uuid(76b1b70e-5740-488d-b910-3f0f02ab2fd1)]
[scriptable, uuid(98379cef-b1da-4731-8556-402f0e55eb9f)]
interface mozIStorageStatement : mozIStorageValueArray {
/**
* Initialize this query with the given SQL statement.
@ -91,6 +91,14 @@ interface mozIStorageStatement : mozIStorageValueArray {
*/
AUTF8String getColumnName(in unsigned long aColumnIndex);
/**
* 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);
/**
* Reset parameters/statement execution
*/

View File

@ -141,10 +141,9 @@ mozStorageStatement::Initialize(mozIStorageConnection *aDBConnection, const nsAC
mResultColumnCount = sqlite3_column_count (mDBStatement);
mColumnNames.Clear();
for (unsigned int i = 0; i < mResultColumnCount; i++) {
const void *name = sqlite3_column_name16 (mDBStatement, i);
mColumnNames.AppendString(
nsDependentString(static_cast<const PRUnichar*>(name)));
for (PRUint32 i = 0; i < mResultColumnCount; i++) {
const char *name = sqlite3_column_name(mDBStatement, i);
mColumnNames.AppendCString(nsDependentCString(name));
}
// doing a sqlite3_prepare sets up the execution engine
@ -257,6 +256,24 @@ mozStorageStatement::GetColumnName(PRUint32 aColumnIndex, nsACString & _retval)
return NS_OK;
}
/* unsigned long getColumnIndex(in AUTF8String aName); */
NS_IMETHODIMP
mozStorageStatement::GetColumnIndex(const nsACString &aName, PRUint32 *_retval)
{
NS_ASSERTION (mDBConnection && mDBStatement, "statement 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;
}
}
return NS_ERROR_INVALID_ARG;
}
/* void reset (); */
NS_IMETHODIMP
mozStorageStatement::Reset()

View File

@ -68,7 +68,7 @@ protected:
sqlite3_stmt *mDBStatement;
PRUint32 mParamCount;
PRUint32 mResultColumnCount;
nsStringArray mColumnNames;
nsCStringArray mColumnNames;
PRBool mExecuting;
// recreate the statement, and transfer bindings

View File

@ -86,6 +86,30 @@ function test_getColumnName()
do_check_eq("name", stmt.getColumnName(0));
}
function test_getColumnIndex_same_case()
{
var stmt = createStatement("SELECT name, id FROM test");
do_check_eq(0, stmt.getColumnIndex("name"));
do_check_eq(1, stmt.getColumnIndex("id"));
}
function test_getColumnIndex_different_case()
{
var stmt = createStatement("SELECT name, id FROM test");
try {
do_check_eq(0, stmt.getColumnIndex("NaMe"));
do_throw("should not get here");
} catch (e) {
do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
}
try {
do_check_eq(1, stmt.getColumnIndex("Id"));
do_throw("should not get here");
} catch (e) {
do_check_eq(Cr.NS_ERROR_INVALID_ARG, e.result);
}
}
function test_state_ready()
{
var stmt = createStatement("SELECT name, id FROM test");
@ -112,7 +136,9 @@ function test_state_executing()
var tests = [test_parameterCount_none, test_parameterCount_one,
test_getParameterName, test_getParameterIndex_different,
test_getParameterIndex_same, test_columnCount,
test_getColumnName, test_state_ready, test_state_executing];
test_getColumnName, test_getColumnIndex_same_case,
test_getColumnIndex_different_case, test_state_ready,
test_state_executing];
function run_test()
{