diff --git a/chrome/content/zotero/xpcom/db.js b/chrome/content/zotero/xpcom/db.js index b450d7c8d1..ba868ef877 100644 --- a/chrome/content/zotero/xpcom/db.js +++ b/chrome/content/zotero/xpcom/db.js @@ -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 * diff --git a/test/tests/dbTest.js b/test/tests/dbTest.js index 62d5afc7d1..74c1dc7e33 100644 --- a/test/tests/dbTest.js +++ b/test/tests/dbTest.js @@ -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')); + }); + }); });