Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(snowflake): Add support for QueryGenerator#tableExistsQuery #15087

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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