Add Zotero.DB.columnExists(table, column)

This commit is contained in:
Dan Stillman 2020-11-15 03:26:34 -05:00
parent b0e065a4ae
commit 6bcc8af86b
2 changed files with 23 additions and 0 deletions

View file

@ -799,6 +799,14 @@ Zotero.DBConnection.prototype.tableExists = Zotero.Promise.coroutine(function* (
});
Zotero.DBConnection.prototype.columnExists = async function (table, column) {
await this._getConnectionAsync();
var sql = `SELECT COUNT(*) FROM pragma_table_info(?) WHERE name=?`;
var count = await this.valueQueryAsync(sql, [table, column]);
return !!count;
};
/**
* Parse SQL string and execute transaction with all statements
*

View file

@ -341,4 +341,19 @@ describe("Zotero.DB", function() {
assert.ok(callback2Ran);
});
})
describe("#columnExists()", function () {
it("should return true if a column exists", async function () {
assert.isTrue(await Zotero.DB.columnExists('items', 'itemID'));
});
it("should return false if a column doesn't exists", async function () {
assert.isFalse(await Zotero.DB.columnExists('items', 'foo'));
});
it("should return false if a table doesn't exists", async function () {
assert.isFalse(await Zotero.DB.columnExists('foo', 'itemID'));
});
});
});