Navigation Menu

Skip to content

Commit

Permalink
feat(snowflake): Add support for QueryGenerator#tableExistsQuery (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-dgadomski committed Oct 4, 2022
1 parent 55051d0 commit a44772e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/dialects/snowflake/query-generator.js
Expand Up @@ -32,7 +32,7 @@ const FOREIGN_KEY_FIELDS = [
* @private
*/
const SNOWFLAKE_RESERVED_WORDS = 'account,all,alter,and,any,as,between,by,case,cast,check,column,connect,connections,constraint,create,cross,current,current_date,current_time,current_timestamp,current_user,database,delete,distinct,drop,else,exists,false,following,for,from,full,grant,group,gscluster,having,ilike,in,increment,inner,insert,intersect,into,is,issue,join,lateral,left,like,localtime,localtimestamp,minus,natural,not,null,of,on,or,order,organization,qualify,regexp,revoke,right,rlike,row,rows,sample,schema,select,set,some,start,table,tablesample,then,to,trigger,true,try_cast,union,unique,update,using,values,view,when,whenever,where,with'.split(',');

const typeWithoutDefault = new Set(['BLOB', 'TEXT', 'GEOMETRY', 'JSON']);

class SnowflakeQueryGenerator extends AbstractQueryGenerator {
Expand Down Expand Up @@ -170,6 +170,18 @@ class SnowflakeQueryGenerator extends AbstractQueryGenerator {
]);
}

tableExistsQuery(table) {
const tableName = table.tableName || table;
const schema = table.schema;

return Utils.joinSQLFragments([
'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = \'BASE TABLE\'',
`AND TABLE_SCHEMA = ${schema !== undefined ? this.escape(schema) : 'CURRENT_SCHEMA()'}`,
`AND TABLE_NAME = ${this.escape(tableName)}`,
';'
]);
}

addColumnQuery(table, key, dataType) {
return Utils.joinSQLFragments([
'ALTER TABLE',
Expand Down
11 changes: 11 additions & 0 deletions test/unit/dialects/snowflake/query-generator.test.js
Expand Up @@ -354,6 +354,17 @@ if (dialect === 'snowflake') {
}
],

tableExistsQuery: [
{
arguments: ['myTable'],
expectation: 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = \'BASE TABLE\' AND TABLE_SCHEMA = CURRENT_SCHEMA() AND TABLE_NAME = \'myTable\';',
},
{
arguments: [{ tableName: 'myTable', schema: 'mySchema' }],
expectation: 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = \'BASE TABLE\' AND TABLE_SCHEMA = \'mySchema\' AND TABLE_NAME = \'myTable\';',
},
],

selectQuery: [
{
arguments: ['myTable'],
Expand Down

0 comments on commit a44772e

Please sign in to comment.