Skip to content

Commit

Permalink
feat(common): add databaseExists method
Browse files Browse the repository at this point in the history
  • Loading branch information
mateonunez committed Mar 1, 2023
1 parent a59b9b8 commit a25936e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/drill/lib/queries/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 = []
Expand Down

0 comments on commit a25936e

Please sign in to comment.