Bug 1326565 - Add `mozIStorageRow.getColumnName`. r?mak
MozReview-Commit-ID: CfgzOR2kpoe
--- a/storage/mozIStorageRow.idl
+++ b/storage/mozIStorageRow.idl
@@ -25,9 +25,18 @@ interface mozIStorageRow : mozIStorageVa
* @param aName
* Name of the result to get from the tuple.
* @returns the result of the specified column.
* @note The name of a result column is the value of the "AS" clause for that
* column. If there is no AS clause then the name of the column is
* unspecified and may change from one release to the next.
*/
nsIVariant getResultByName(in AUTF8String aName);
+
+ /**
+ * Obtains the name of the column specified by aIndex.
+ *
+ * @param aIndex
+ * Zero-based index of the column.
+ * @returns The name of the result column.
+ */
+ AUTF8String getColumnName(in unsigned long aIndex);
};
--- a/storage/mozStorageRow.cpp
+++ b/storage/mozStorageRow.cpp
@@ -97,16 +97,31 @@ NS_IMETHODIMP
Row::GetResultByName(const nsACString &aName,
nsIVariant **_result)
{
uint32_t index;
NS_ENSURE_TRUE(mNameHashtable.Get(aName, &index), NS_ERROR_NOT_AVAILABLE);
return GetResultByIndex(index, _result);
}
+NS_IMETHODIMP
+Row::GetColumnName(uint32_t aIndex,
+ nsACString &_name)
+{
+ ENSURE_INDEX_VALUE(aIndex, mNumCols);
+ _name.Truncate();
+ for (auto iter = mNameHashtable.ConstIter(); !iter.Done(); iter.Next()) {
+ if (iter.Data() == aIndex) {
+ _name.Assign(iter.Key());
+ break;
+ }
+ }
+ return NS_OK;
+}
+
////////////////////////////////////////////////////////////////////////////////
//// mozIStorageValueArray
NS_IMETHODIMP
Row::GetNumEntries(uint32_t *_entries)
{
*_entries = mNumCols;
return NS_OK;
--- a/storage/test/unit/test_statement_executeAsync.js
+++ b/storage/test/unit/test_statement_executeAsync.js
@@ -230,28 +230,31 @@ function test_get_data()
do_check_neq(null, tuple);
// Check that it's what we expect
do_check_false(tuple.getIsNull(0));
do_check_eq(tuple.getResultByName("string"), tuple.getResultByIndex(0));
do_check_eq(TEXT, tuple.getResultByName("string"));
do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_TEXT,
tuple.getTypeOfIndex(0));
+ do_check_eq(tuple.getColumnName(0), "string");
do_check_false(tuple.getIsNull(1));
do_check_eq(tuple.getResultByName("number"), tuple.getResultByIndex(1));
do_check_eq(REAL, tuple.getResultByName("number"));
do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_FLOAT,
tuple.getTypeOfIndex(1));
+ do_check_eq(tuple.getColumnName(1), "number");
do_check_true(tuple.getIsNull(2));
do_check_eq(tuple.getResultByName("nuller"), tuple.getResultByIndex(2));
do_check_eq(null, tuple.getResultByName("nuller"));
do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_NULL,
tuple.getTypeOfIndex(2));
+ do_check_eq(tuple.getColumnName(2), "nuller");
do_check_false(tuple.getIsNull(3));
var blobByName = tuple.getResultByName("blober");
do_check_eq(BLOB.length, blobByName.length);
var blobByIndex = tuple.getResultByIndex(3);
do_check_eq(BLOB.length, blobByIndex.length);
for (let i = 0; i < BLOB.length; i++) {
do_check_eq(BLOB[i], blobByName[i]);
@@ -260,22 +263,24 @@ function test_get_data()
var count = { value: 0 };
var blob = { value: null };
tuple.getBlob(3, count, blob);
do_check_eq(BLOB.length, count.value);
for (let i = 0; i < BLOB.length; i++)
do_check_eq(BLOB[i], blob.value[i]);
do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_BLOB,
tuple.getTypeOfIndex(3));
+ do_check_eq(tuple.getColumnName(3), "blober");
do_check_false(tuple.getIsNull(4));
do_check_eq(tuple.getResultByName("id"), tuple.getResultByIndex(4));
do_check_eq(INTEGER, tuple.getResultByName("id"));
do_check_eq(Ci.mozIStorageValueArray.VALUE_TYPE_INTEGER,
tuple.getTypeOfIndex(4));
+ do_check_eq(tuple.getColumnName(4), "id");
}]);
stmt.finalize();
run_next_test();
}
function test_tuple_out_of_bounds()
{
var stmt = makeTestStatement(