2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-06-12 07:59:26 +04:00
|
|
|
|
|
|
|
// This file tests the functions of mozIStorageStatement
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function setup() {
|
2007-06-12 07:59:26 +04:00
|
|
|
getOpenedDatabase().createTable("test", "id INTEGER PRIMARY KEY, name TEXT");
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_parameterCount_none() {
|
2007-06-12 07:59:26 +04:00
|
|
|
var stmt = createStatement("SELECT * FROM test");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(0, stmt.parameterCount);
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.reset();
|
|
|
|
stmt.finalize();
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_parameterCount_one() {
|
2007-06-12 07:59:26 +04:00
|
|
|
var stmt = createStatement("SELECT * FROM test WHERE id = ?1");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(1, stmt.parameterCount);
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.reset();
|
|
|
|
stmt.finalize();
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_getParameterName() {
|
2007-06-12 07:59:26 +04:00
|
|
|
var stmt = createStatement("SELECT * FROM test WHERE id = :id");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(":id", stmt.getParameterName(0));
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.reset();
|
|
|
|
stmt.finalize();
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_getParameterIndex_different() {
|
2007-06-12 07:59:26 +04:00
|
|
|
var stmt = createStatement("SELECT * FROM test WHERE id = :id OR name = :name");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(0, stmt.getParameterIndex("id"));
|
|
|
|
Assert.equal(1, stmt.getParameterIndex("name"));
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.reset();
|
|
|
|
stmt.finalize();
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_getParameterIndex_same() {
|
2009-05-09 04:29:57 +04:00
|
|
|
var stmt = createStatement("SELECT * FROM test WHERE id = :test OR name = :test");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(0, stmt.getParameterIndex("test"));
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.reset();
|
|
|
|
stmt.finalize();
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_columnCount() {
|
2007-06-12 07:59:26 +04:00
|
|
|
var stmt = createStatement("SELECT * FROM test WHERE id = ?1 OR name = ?2");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(2, stmt.columnCount);
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.reset();
|
|
|
|
stmt.finalize();
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_getColumnName() {
|
2007-06-12 07:59:26 +04:00
|
|
|
var stmt = createStatement("SELECT name, id FROM test");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal("id", stmt.getColumnName(1));
|
|
|
|
Assert.equal("name", stmt.getColumnName(0));
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.reset();
|
|
|
|
stmt.finalize();
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_getColumnIndex_same_case() {
|
2007-07-19 20:30:17 +04:00
|
|
|
var stmt = createStatement("SELECT name, id FROM test");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(0, stmt.getColumnIndex("name"));
|
|
|
|
Assert.equal(1, stmt.getColumnIndex("id"));
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.reset();
|
|
|
|
stmt.finalize();
|
2007-07-19 20:30:17 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_getColumnIndex_different_case() {
|
2007-07-19 20:30:17 +04:00
|
|
|
var stmt = createStatement("SELECT name, id FROM test");
|
|
|
|
try {
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(0, stmt.getColumnIndex("NaMe"));
|
2007-07-19 20:30:17 +04:00
|
|
|
do_throw("should not get here");
|
|
|
|
} catch (e) {
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Cr.NS_ERROR_INVALID_ARG, e.result);
|
2007-07-19 20:30:17 +04:00
|
|
|
}
|
|
|
|
try {
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(1, stmt.getColumnIndex("Id"));
|
2007-07-19 20:30:17 +04:00
|
|
|
do_throw("should not get here");
|
|
|
|
} catch (e) {
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Cr.NS_ERROR_INVALID_ARG, e.result);
|
2007-07-19 20:30:17 +04:00
|
|
|
}
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.reset();
|
|
|
|
stmt.finalize();
|
2007-07-19 20:30:17 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_state_ready() {
|
2007-06-12 07:59:26 +04:00
|
|
|
var stmt = createStatement("SELECT name, id FROM test");
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.reset();
|
|
|
|
stmt.finalize();
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_state_executing() {
|
2007-06-12 07:59:26 +04:00
|
|
|
var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')");
|
|
|
|
stmt.execute();
|
|
|
|
stmt.execute();
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.finalize();
|
2007-06-12 07:59:26 +04:00
|
|
|
|
|
|
|
stmt = createStatement("SELECT name, id FROM test");
|
|
|
|
stmt.executeStep();
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_EXECUTING,
|
|
|
|
stmt.state);
|
2007-06-12 07:59:26 +04:00
|
|
|
stmt.executeStep();
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_EXECUTING,
|
|
|
|
stmt.state);
|
2007-06-12 07:59:26 +04:00
|
|
|
stmt.reset();
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
|
2007-09-19 07:26:51 +04:00
|
|
|
stmt.finalize();
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_state_after_finalize() {
|
2007-09-19 07:26:51 +04:00
|
|
|
var stmt = createStatement("SELECT name, id FROM test");
|
|
|
|
stmt.executeStep();
|
|
|
|
stmt.finalize();
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_INVALID, stmt.state);
|
2007-06-12 07:59:26 +04:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_failed_execute() {
|
2010-06-08 02:06:56 +04:00
|
|
|
var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')");
|
|
|
|
stmt.execute();
|
|
|
|
stmt.finalize();
|
|
|
|
var id = getOpenedDatabase().lastInsertRowID;
|
|
|
|
stmt = createStatement("INSERT INTO test(id, name) VALUES(:id, 'bar')");
|
|
|
|
stmt.params.id = id;
|
|
|
|
try {
|
|
|
|
// Should throw a constraint error
|
|
|
|
stmt.execute();
|
|
|
|
do_throw("Should have seen a constraint error");
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (e) {
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(getOpenedDatabase().lastError, Ci.mozIStorageError.CONSTRAINT);
|
2010-06-08 02:06:56 +04:00
|
|
|
}
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Ci.mozIStorageStatement.MOZ_STORAGE_STATEMENT_READY, stmt.state);
|
2010-06-08 02:06:56 +04:00
|
|
|
// Should succeed without needing to reset the statement manually
|
|
|
|
stmt.finalize();
|
|
|
|
}
|
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function test_bind_undefined() {
|
2016-01-20 18:37:00 +03:00
|
|
|
var stmt = createStatement("INSERT INTO test (name) VALUES ('foo')");
|
2016-02-04 01:22:33 +03:00
|
|
|
|
2016-01-20 18:37:00 +03:00
|
|
|
expectError(Cr.NS_ERROR_ILLEGAL_VALUE,
|
|
|
|
() => stmt.bindParameters(undefined));
|
|
|
|
|
|
|
|
stmt.finalize();
|
|
|
|
}
|
|
|
|
|
2007-06-12 07:59:26 +04:00
|
|
|
var tests = [test_parameterCount_none, test_parameterCount_one,
|
2007-07-15 22:03:20 +04:00
|
|
|
test_getParameterName, test_getParameterIndex_different,
|
|
|
|
test_getParameterIndex_same, test_columnCount,
|
2007-07-19 20:30:17 +04:00
|
|
|
test_getColumnName, test_getColumnIndex_same_case,
|
|
|
|
test_getColumnIndex_different_case, test_state_ready,
|
2007-11-13 11:26:45 +03:00
|
|
|
test_state_executing, test_state_after_finalize,
|
2010-06-08 02:06:56 +04:00
|
|
|
test_failed_execute,
|
2016-01-20 18:37:00 +03:00
|
|
|
test_bind_undefined,
|
2010-06-08 02:06:56 +04:00
|
|
|
];
|
2007-06-12 07:59:26 +04:00
|
|
|
|
2016-12-31 05:47:25 +03:00
|
|
|
function run_test() {
|
2007-06-12 07:59:26 +04:00
|
|
|
setup();
|
|
|
|
|
2015-12-04 19:00:03 +03:00
|
|
|
for (var i = 0; i < tests.length; i++) {
|
2007-06-12 07:59:26 +04:00
|
|
|
tests[i]();
|
2015-12-04 19:00:03 +03:00
|
|
|
}
|
|
|
|
|
2007-06-12 07:59:26 +04:00
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|