|
| 1 | +import snowflake from 'snowflake-sdk'; |
| 2 | +import { Query } from '../../query'; |
| 3 | +import { QueryResult } from '..'; |
| 4 | +import { |
| 5 | + createErrorResult, |
| 6 | + transformArrayBasedResult, |
| 7 | +} from '../../utils/transformer'; |
| 8 | +import { Database, TableColumn } from '../../models/database'; |
| 9 | +import { PostgreBaseConnection } from './../postgre/base'; |
| 10 | +import { |
| 11 | + buildMySQLDatabaseSchmea, |
| 12 | + MySQLConstraintColumnResult, |
| 13 | +} from '../mysql'; |
| 14 | + |
| 15 | +export class SnowflakeConnection extends PostgreBaseConnection { |
| 16 | + protected db: snowflake.Connection; |
| 17 | + |
| 18 | + constructor(db: any) { |
| 19 | + super(); |
| 20 | + this.db = db; |
| 21 | + } |
| 22 | + |
| 23 | + async connect(): Promise<any> { |
| 24 | + await new Promise((resolve, reject) => { |
| 25 | + this.db.connectAsync((err, conn) => { |
| 26 | + if (err) reject(err.message); |
| 27 | + else resolve(conn); |
| 28 | + }); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + async disconnect(): Promise<any> { |
| 33 | + await new Promise((resolve) => this.db.destroy(resolve)); |
| 34 | + } |
| 35 | + |
| 36 | + async testConnection(): Promise<{ error?: string }> { |
| 37 | + try { |
| 38 | + await this.connect(); |
| 39 | + const { data } = await this.query({ |
| 40 | + query: 'SELECT CURRENT_DATABASE() AS DBNAME;', |
| 41 | + }); |
| 42 | + |
| 43 | + await this.disconnect(); |
| 44 | + if (!data[0].DBNAME) return { error: 'Database does not exist' }; |
| 45 | + |
| 46 | + return {}; |
| 47 | + } catch (e) { |
| 48 | + if (e instanceof Error) return { error: e.message }; |
| 49 | + return { error: 'Unknown error' }; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + async fetchDatabaseSchema(): Promise<Database> { |
| 54 | + // Get the list of schema first |
| 55 | + const { data: schemaList } = await this.query<{ SCHEMA_NAME: string }>({ |
| 56 | + query: `SELECT SCHEMA_NAME FROM information_schema.schemata WHERE schema_name NOT IN ('INFORMATION_SCHEMA');`, |
| 57 | + }); |
| 58 | + |
| 59 | + // Get the list of all tables |
| 60 | + const { data: tableList } = await this.query<{ |
| 61 | + TABLE_NAME: string; |
| 62 | + TABLE_SCHEMA: string; |
| 63 | + }>({ |
| 64 | + query: `SELECT TABLE_NAME, TABLE_SCHEMA FROM information_schema.tables WHERE table_schema NOT IN ('INFORMATION_SCHEMA');`, |
| 65 | + }); |
| 66 | + |
| 67 | + // Get the list of all columns |
| 68 | + const { data: columnList } = await this.query<{ |
| 69 | + TABLE_SCHEMA: string; |
| 70 | + TABLE_NAME: string; |
| 71 | + COLUMN_NAME: string; |
| 72 | + DATA_TYPE: string; |
| 73 | + IS_NULLABLE: string; |
| 74 | + COLUMN_DEFAULT: string; |
| 75 | + ORDINAL_POSITION: number; |
| 76 | + }>({ |
| 77 | + query: `SELECT * FROM information_schema.columns WHERE table_schema NOT IN ('INFORMATION_SCHEMA');`, |
| 78 | + }); |
| 79 | + |
| 80 | + // Get the list of all constraints |
| 81 | + const { data: constraintsList } = await this.query<{ |
| 82 | + CONSTRAINT_SCHEMA: string; |
| 83 | + CONSTRAINT_NAME: string; |
| 84 | + TABLE_NAME: string; |
| 85 | + TABLE_SCHEMA: string; |
| 86 | + CONSTRAINT_TYPE: string; |
| 87 | + }>({ |
| 88 | + query: `SELECT * FROM information_schema.table_constraints WHERE CONSTRAINT_SCHEMA NOT IN ('INFORMATION_SCHEMA') AND CONSTRAINT_TYPE IN ('FOREIGN KEY', 'PRIMARY KEY', 'UNIQUE');`, |
| 89 | + }); |
| 90 | + |
| 91 | + // Mamic the key usages table using SHOW PRIMARY KEY and SHOW FOREIGN KEYS |
| 92 | + const { data: primaryKeyConstraint } = await this.query<{ |
| 93 | + schema_name: string; |
| 94 | + table_name: string; |
| 95 | + column_name: string; |
| 96 | + constraint_name: string; |
| 97 | + }>({ query: `SHOW PRIMARY KEYS;` }); |
| 98 | + |
| 99 | + const { data: foreignKeyConstraint } = await this.query<{ |
| 100 | + pk_schema_name: string; |
| 101 | + pk_table_name: string; |
| 102 | + pk_column_name: string; |
| 103 | + fk_schema_name: string; |
| 104 | + fk_table_name: string; |
| 105 | + fk_column_name: string; |
| 106 | + fk_name: string; |
| 107 | + }>({ query: `SHOW IMPORTED KEYS;` }); |
| 108 | + |
| 109 | + // Postgres structure is similar to MySQL, so we can reuse the MySQL schema builder |
| 110 | + // by just mapping the column names |
| 111 | + return buildMySQLDatabaseSchmea({ |
| 112 | + schemaList, |
| 113 | + tableList, |
| 114 | + columnList: columnList.map((column) => ({ |
| 115 | + COLUMN_TYPE: column.DATA_TYPE, |
| 116 | + ...column, |
| 117 | + COLUMN_KEY: '', |
| 118 | + EXTRA: '', |
| 119 | + })), |
| 120 | + constraintsList, |
| 121 | + constraintColumnsList: [ |
| 122 | + ...primaryKeyConstraint.map( |
| 123 | + (constraint): MySQLConstraintColumnResult => ({ |
| 124 | + TABLE_SCHEMA: constraint.schema_name, |
| 125 | + TABLE_NAME: constraint.table_name, |
| 126 | + COLUMN_NAME: constraint.column_name, |
| 127 | + CONSTRAINT_NAME: constraint.constraint_name, |
| 128 | + REFERENCED_TABLE_SCHEMA: '', |
| 129 | + REFERENCED_TABLE_NAME: '', |
| 130 | + REFERENCED_COLUMN_NAME: '', |
| 131 | + }) |
| 132 | + ), |
| 133 | + ...foreignKeyConstraint.map( |
| 134 | + (constraint): MySQLConstraintColumnResult => ({ |
| 135 | + TABLE_SCHEMA: constraint.fk_schema_name, |
| 136 | + TABLE_NAME: constraint.fk_table_name, |
| 137 | + COLUMN_NAME: constraint.fk_column_name, |
| 138 | + CONSTRAINT_NAME: constraint.fk_name, |
| 139 | + REFERENCED_TABLE_SCHEMA: constraint.pk_schema_name, |
| 140 | + REFERENCED_TABLE_NAME: constraint.pk_table_name, |
| 141 | + REFERENCED_COLUMN_NAME: constraint.pk_column_name, |
| 142 | + }) |
| 143 | + ), |
| 144 | + ], |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + createTable( |
| 149 | + schemaName: string | undefined, |
| 150 | + tableName: string, |
| 151 | + columns: TableColumn[] |
| 152 | + ): Promise<QueryResult> { |
| 153 | + const tempColumns = structuredClone(columns); |
| 154 | + for (const column of tempColumns) { |
| 155 | + if (column.definition.references) { |
| 156 | + column.definition.references.table = schemaName |
| 157 | + ? `${schemaName}.${column.definition.references.table}` |
| 158 | + : column.definition.references.table; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return super.createTable(schemaName, tableName, tempColumns); |
| 163 | + } |
| 164 | + |
| 165 | + async renameTable( |
| 166 | + schemaName: string | undefined, |
| 167 | + tableName: string, |
| 168 | + newTableName: string |
| 169 | + ): Promise<QueryResult> { |
| 170 | + // Schema is required for rename |
| 171 | + return super.renameTable( |
| 172 | + schemaName, |
| 173 | + tableName, |
| 174 | + schemaName ? `${schemaName}.${newTableName}` : newTableName |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + async query<T = Record<string, unknown>>( |
| 179 | + query: Query |
| 180 | + ): Promise<QueryResult<T>> { |
| 181 | + try { |
| 182 | + const [err, headers, rows] = await new Promise< |
| 183 | + [snowflake.SnowflakeError | undefined, string[], unknown[][]] |
| 184 | + >((resolve) => { |
| 185 | + this.db.execute({ |
| 186 | + sqlText: query.query, |
| 187 | + binds: query.parameters as snowflake.Binds, |
| 188 | + rowMode: 'array', |
| 189 | + complete: (err, stmt, rows) => { |
| 190 | + resolve([ |
| 191 | + err, |
| 192 | + err |
| 193 | + ? [] |
| 194 | + : stmt.getColumns().map((col) => col.getName()), |
| 195 | + rows as unknown[][], |
| 196 | + ]); |
| 197 | + }, |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + if (err) return createErrorResult(err.message) as QueryResult<T>; |
| 202 | + return transformArrayBasedResult( |
| 203 | + headers, |
| 204 | + (header) => ({ |
| 205 | + name: header, |
| 206 | + }), |
| 207 | + rows |
| 208 | + ) as QueryResult<T>; |
| 209 | + } catch (e) { |
| 210 | + return createErrorResult('Unknown error') as QueryResult<T>; |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments