Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1170) Escape startkey/endkey in WebSQL allDocs() #1171

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/adapters/websql.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,22 +470,26 @@ function webSqlPouch(opts, callback) {
BY_SEQ_STORE + ' JOIN ' + DOC_STORE + ' ON ' + BY_SEQ_STORE + '.seq = ' +
DOC_STORE + '.winningseq';

var sqlArgs = [];
if ('keys' in opts) {
sql += ' WHERE ' + DOC_STORE + '.id IN (' + opts.keys.map(function (key) {
return quote(key);
sql += ' WHERE ' + DOC_STORE + '.id IN (' + opts.keys.map(function () {
return '?';
}).join(',') + ')';
sqlArgs = sqlArgs.concat(opts.keys);
} else {
if (start) {
sql += ' WHERE ' + DOC_STORE + '.id >= "' + start + '"';
sql += ' WHERE ' + DOC_STORE + '.id >= ?';
sqlArgs.push(start);
}
if (end) {
sql += (start ? ' AND ' : ' WHERE ') + DOC_STORE + '.id <= "' + end + '"';
sql += (start ? ' AND ' : ' WHERE ') + DOC_STORE + '.id <= ?';
sqlArgs.push(end);
}
sql += ' ORDER BY ' + DOC_STORE + '.id ' + (descending ? 'DESC' : 'ASC');
}

db.transaction(function (tx) {
tx.executeSql(sql, [], function (tx, result) {
tx.executeSql(sql, sqlArgs, function (tx, result) {
for (var i = 0, l = result.rows.length; i < l; i++) {
var doc = result.rows.item(i);
var metadata = JSON.parse(doc.metadata);
Expand Down
19 changes: 19 additions & 0 deletions tests/test.all_docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,23 @@ adapters.map(function(adapter) {
});
});

asyncTest('test escaped startkey/endkey', 1, function () {
testUtils.initTestDB(this.name, function (err, db) {
var id1 = "\"crazy id!\" a";
var id2 = "\"crazy id!\" z";
var docs = {
docs: [
{_id: id1, foo: "a"},
{_id: id2, foo: "z"}
]
};
db.bulkDocs(docs, function (err, res) {
db.allDocs({ startkey: id1, endkey: id2 }, function (err, res) {
equal(res.total_rows, 2, 'Accurately return total_rows count');
start();
});
});
});
});

});