зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1247602 - Allow to bind blobs in Sqlite.jsm. r=mak
MozReview-Commit-ID: 2yPO6kidqfn --HG-- extra : rebase_source : b17fcc66e2e0d27674aa16453e592cfd4f93a8a7
This commit is contained in:
Родитель
f5f8565677
Коммит
e1c6fc3795
|
@ -679,6 +679,14 @@ ConnectionData.prototype = Object.freeze({
|
|||
return;
|
||||
}
|
||||
|
||||
function bindParam(obj, key, val) {
|
||||
let isBlob = Array.isArray(val);
|
||||
let args = [key, val].concat(isBlob ? [val.length] : []);
|
||||
let methodName =
|
||||
`bind${isBlob ? "Blob" : ""}By${typeof key == "number" ? "Index" : "Name"}`;
|
||||
obj[methodName](...args);
|
||||
}
|
||||
|
||||
if (Array.isArray(params)) {
|
||||
// It's an array of separate params.
|
||||
if (params.length && (typeof(params[0]) == "object")) {
|
||||
|
@ -686,7 +694,7 @@ ConnectionData.prototype = Object.freeze({
|
|||
for (let p of params) {
|
||||
let bindings = paramsArray.newBindingParams();
|
||||
for (let [key, value] of Object.entries(p)) {
|
||||
bindings.bindByName(key, value);
|
||||
bindParam(bindings, key, value);
|
||||
}
|
||||
paramsArray.addParams(bindings);
|
||||
}
|
||||
|
@ -697,7 +705,7 @@ ConnectionData.prototype = Object.freeze({
|
|||
|
||||
// Indexed params.
|
||||
for (let i = 0; i < params.length; i++) {
|
||||
statement.bindByIndex(i, params[i]);
|
||||
bindParam(statement, i, params[i]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -705,7 +713,7 @@ ConnectionData.prototype = Object.freeze({
|
|||
// Named params.
|
||||
if (params && typeof(params) == "object") {
|
||||
for (let k in params) {
|
||||
statement.bindByName(k, params[k]);
|
||||
bindParam(statement, k, params[k]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
"use strict";
|
||||
|
||||
var {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
||||
|
@ -84,12 +81,10 @@ function* getDummyTempDatabase(name, extraOptions = {}) {
|
|||
return c;
|
||||
}
|
||||
|
||||
function run_test() {
|
||||
add_task(function* test_setup() {
|
||||
Cu.import("resource://testing-common/services/common/logging.js");
|
||||
initTestLogging("Trace");
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
});
|
||||
|
||||
add_task(function* test_open_normal() {
|
||||
let c = yield Sqlite.openConnection({path: "test_open_normal.sqlite"});
|
||||
|
@ -1091,3 +1086,59 @@ add_task(function* test_close_database_on_gc() {
|
|||
yield finalPromise;
|
||||
failTestsOnAutoClose(true);
|
||||
});
|
||||
|
||||
// Test all supported datatypes
|
||||
add_task(function* test_datatypes() {
|
||||
let c = yield getConnection("datatypes");
|
||||
yield c.execute("DROP TABLE IF EXISTS datatypes");
|
||||
yield c.execute(`CREATE TABLE datatypes (
|
||||
null_col NULL,
|
||||
integer_col INTEGER NOT NULL,
|
||||
text_col TEXT NOT NULL,
|
||||
blob_col BLOB NOT NULL,
|
||||
real_col REAL NOT NULL,
|
||||
numeric_col NUMERIC NOT NULL
|
||||
)`);
|
||||
const bindings = [
|
||||
{
|
||||
null_col: null,
|
||||
integer_col: 12345,
|
||||
text_col: "qwerty",
|
||||
blob_col: new Array(256).fill(undefined).map( (value, index) => index % 256 ),
|
||||
real_col: 3.14159265359,
|
||||
numeric_col: true
|
||||
},
|
||||
{
|
||||
null_col: null,
|
||||
integer_col: -12345,
|
||||
text_col: "",
|
||||
blob_col: new Array(256 * 2).fill(undefined).map( (value, index) => index % 256 ),
|
||||
real_col: Number.NEGATIVE_INFINITY,
|
||||
numeric_col: false
|
||||
}
|
||||
];
|
||||
|
||||
yield c.execute(`INSERT INTO datatypes VALUES (
|
||||
:null_col,
|
||||
:integer_col,
|
||||
:text_col,
|
||||
:blob_col,
|
||||
:real_col,
|
||||
:numeric_col
|
||||
)`, bindings);
|
||||
|
||||
let rows = yield c.execute("SELECT * FROM datatypes");
|
||||
Assert.ok(Array.isArray(rows));
|
||||
Assert.equal(rows.length, bindings.length);
|
||||
for (let i = 0 ; i < bindings.length; ++i) {
|
||||
let binding = bindings[i];
|
||||
let row = rows[i];
|
||||
for (let colName in binding) {
|
||||
// In Sqlite bool is stored and then retrieved as numeric.
|
||||
let val = typeof binding[colName] == "boolean" ? +binding[colName]
|
||||
: binding[colName];
|
||||
Assert.deepEqual(val, row.getResultByName(colName));
|
||||
}
|
||||
}
|
||||
yield c.close();
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче