зеркало из https://github.com/mozilla/pjs.git
Bug 388059 - Add a getColumnIndex method to mozIStorageStatement. r=sspitzer
This commit is contained in:
Родитель
83e6f33379
Коммит
510bb3d6b4
|
@ -45,7 +45,7 @@ interface nsISimpleEnumerator;
|
||||||
|
|
||||||
[ptr] native sqlite3stmtptr(struct sqlite3_stmt);
|
[ptr] native sqlite3stmtptr(struct sqlite3_stmt);
|
||||||
|
|
||||||
[scriptable, uuid(76b1b70e-5740-488d-b910-3f0f02ab2fd1)]
|
[scriptable, uuid(98379cef-b1da-4731-8556-402f0e55eb9f)]
|
||||||
interface mozIStorageStatement : mozIStorageValueArray {
|
interface mozIStorageStatement : mozIStorageValueArray {
|
||||||
/**
|
/**
|
||||||
* Initialize this query with the given SQL statement.
|
* Initialize this query with the given SQL statement.
|
||||||
|
@ -91,6 +91,14 @@ interface mozIStorageStatement : mozIStorageValueArray {
|
||||||
*/
|
*/
|
||||||
AUTF8String getColumnName(in unsigned long aColumnIndex);
|
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
|
* Reset parameters/statement execution
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -141,10 +141,9 @@ mozStorageStatement::Initialize(mozIStorageConnection *aDBConnection, const nsAC
|
||||||
mResultColumnCount = sqlite3_column_count (mDBStatement);
|
mResultColumnCount = sqlite3_column_count (mDBStatement);
|
||||||
mColumnNames.Clear();
|
mColumnNames.Clear();
|
||||||
|
|
||||||
for (unsigned int i = 0; i < mResultColumnCount; i++) {
|
for (PRUint32 i = 0; i < mResultColumnCount; i++) {
|
||||||
const void *name = sqlite3_column_name16 (mDBStatement, i);
|
const char *name = sqlite3_column_name(mDBStatement, i);
|
||||||
mColumnNames.AppendString(
|
mColumnNames.AppendCString(nsDependentCString(name));
|
||||||
nsDependentString(static_cast<const PRUnichar*>(name)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// doing a sqlite3_prepare sets up the execution engine
|
// doing a sqlite3_prepare sets up the execution engine
|
||||||
|
@ -257,6 +256,24 @@ mozStorageStatement::GetColumnName(PRUint32 aColumnIndex, nsACString & _retval)
|
||||||
return NS_OK;
|
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 (); */
|
/* void reset (); */
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
mozStorageStatement::Reset()
|
mozStorageStatement::Reset()
|
||||||
|
|
|
@ -68,7 +68,7 @@ protected:
|
||||||
sqlite3_stmt *mDBStatement;
|
sqlite3_stmt *mDBStatement;
|
||||||
PRUint32 mParamCount;
|
PRUint32 mParamCount;
|
||||||
PRUint32 mResultColumnCount;
|
PRUint32 mResultColumnCount;
|
||||||
nsStringArray mColumnNames;
|
nsCStringArray mColumnNames;
|
||||||
PRBool mExecuting;
|
PRBool mExecuting;
|
||||||
|
|
||||||
// recreate the statement, and transfer bindings
|
// recreate the statement, and transfer bindings
|
||||||
|
|
|
@ -86,6 +86,30 @@ function test_getColumnName()
|
||||||
do_check_eq("name", stmt.getColumnName(0));
|
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()
|
function test_state_ready()
|
||||||
{
|
{
|
||||||
var stmt = createStatement("SELECT name, id FROM test");
|
var stmt = createStatement("SELECT name, id FROM test");
|
||||||
|
@ -112,7 +136,9 @@ function test_state_executing()
|
||||||
var tests = [test_parameterCount_none, test_parameterCount_one,
|
var tests = [test_parameterCount_none, test_parameterCount_one,
|
||||||
test_getParameterName, test_getParameterIndex_different,
|
test_getParameterName, test_getParameterIndex_different,
|
||||||
test_getParameterIndex_same, test_columnCount,
|
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()
|
function run_test()
|
||||||
{
|
{
|
||||||
|
|
Загрузка…
Ссылка в новой задаче