From a25936e07867f77fda196da2b05cb610ea0edd6f Mon Sep 17 00:00:00 2001 From: mateonunez Date: Thu, 2 Mar 2023 00:17:19 +0100 Subject: [PATCH] feat(common): add databaseExists method --- packages/drill/lib/queries/common.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/drill/lib/queries/common.js b/packages/drill/lib/queries/common.js index c2db5e62..a30ef33d 100644 --- a/packages/drill/lib/queries/common.js +++ b/packages/drill/lib/queries/common.js @@ -6,6 +6,7 @@ export default async function (logger, db, sql) { return { selectData: (tableName, columns, where) => selectData(logger, db, sql, tableName, columns, where), [symbols.privateMethods]: { + databaseExists: (database) => databaseExists(logger, db, sql, database), createDatabase: (database, options) => createDatabase(logger, db, sql, database, options), dropDatabase: (database, options) => dropDatabase(logger, db, sql, database), createTable: (tableName, columns, options) => createTable(logger, db, sql, tableName, columns, options), @@ -17,6 +18,16 @@ export default async function (logger, db, sql) { } } +async function databaseExists (logger, db, sql, database) { + if (logger) logger.info(`Checking if database "${database}" exists.`) + const response = await db.query(sql` + SELECT SCHEMA_NAME + FROM INFORMATION_SCHEMA.SCHEMATA + WHERE SCHEMA_NAME = ${database} + `) + return response.length > 0 +} + async function selectData (logger, db, sql, tableName, columns = '*', where = {}) { if (logger) logger.info(`Selecting data from "${tableName}".`) const columnDefinitions = []