Skip to content

Commit 7d6f685

Browse files
zhljenajoymajumdar
authored andcommitted
Fixing other apis in snowflake connector (Netflix#256)
1 parent ca770f7 commit 7d6f685

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

metacat-connector-jdbc/src/main/java/com/netflix/metacat/connector/jdbc/services/JdbcConnectorTableService.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public class JdbcConnectorTableService implements ConnectorTableService {
6565
private static final char LEFT_PAREN = '(';
6666
private static final char RIGHT_PAREN = ')';
6767
private static final char SPACE = ' ';
68-
private final DataSource dataSource;
69-
private final JdbcTypeConverter typeConverter;
68+
protected final DataSource dataSource;
7069
private final JdbcExceptionMapper exceptionMapper;
70+
private final JdbcTypeConverter typeConverter;
7171

7272
/**
7373
* Constructor.
@@ -95,8 +95,7 @@ public void delete(@Nonnull final ConnectorRequestContext context, @Nonnull fina
9595
final String databaseName = name.getDatabaseName();
9696
final String tableName = name.getTableName();
9797
log.debug("Attempting to delete table {} from database {} for request {}", tableName, databaseName, context);
98-
try (Connection connection = this.dataSource.getConnection()) {
99-
connection.setSchema(databaseName);
98+
try (Connection connection = this.getConnection(name.getDatabaseName())) {
10099
JdbcConnectorUtils.executeUpdate(connection, this.getDropTableSql(name, tableName));
101100
log.debug("Deleted table {} from database {} for request {}", tableName, databaseName, context);
102101
} catch (final SQLException se) {
@@ -111,9 +110,7 @@ public void delete(@Nonnull final ConnectorRequestContext context, @Nonnull fina
111110
public TableInfo get(@Nonnull final ConnectorRequestContext context, @Nonnull final QualifiedName name) {
112111
log.debug("Beginning to get table metadata for qualified name {} for request {}", name, context);
113112

114-
try (Connection connection = this.dataSource.getConnection()) {
115-
final String database = name.getDatabaseName();
116-
connection.setSchema(database);
113+
try (Connection connection = this.getConnection(name.getDatabaseName())) {
117114
final ImmutableList.Builder<FieldInfo> fields = ImmutableList.builder();
118115
try (ResultSet columns = this.getColumns(connection, name)) {
119116
while (columns.next()) {
@@ -196,8 +193,7 @@ public List<QualifiedName> listNames(
196193
final String catalog = name.getCatalogName();
197194
final String database = name.getDatabaseName();
198195

199-
try (Connection connection = this.dataSource.getConnection()) {
200-
connection.setSchema(database);
196+
try (Connection connection = this.getConnection(database)) {
201197
final List<QualifiedName> names = Lists.newArrayList();
202198
try (ResultSet tables = this.getTables(connection, name, prefix, null)) {
203199
while (tables.next()) {
@@ -248,7 +244,7 @@ public void rename(
248244
"Database names must match and they are " + oldDatabaseName + " and " + newDatabaseName
249245
);
250246
}
251-
try (Connection connection = this.dataSource.getConnection()) {
247+
try (Connection connection = this.getConnection(oldDatabaseName)) {
252248
connection.setSchema(oldDatabaseName);
253249
JdbcConnectorUtils.executeUpdate(
254250
connection,
@@ -267,6 +263,12 @@ public void rename(
267263
}
268264
}
269265

266+
protected Connection getConnection(@Nonnull @NonNull final String schema) throws SQLException {
267+
final Connection connection = this.dataSource.getConnection();
268+
connection.setSchema(schema);
269+
return connection;
270+
}
271+
270272
@Override
271273
public boolean exists(@Nonnull final ConnectorRequestContext context, @Nonnull final QualifiedName name) {
272274
boolean result = false;
@@ -398,7 +400,6 @@ protected String buildSourceType(
398400

399401
/**
400402
* Build the SQL for renaming a table out of the components provided. SQL will be executed.
401-
*
402403
* @param oldName The fully qualified name for the current table
403404
* @param finalOldTableName The string for what the current table should be called in the sql
404405
* @param finalNewTableName The string for what the new name fo the table should be in the sql

metacat-connector-snowflake/src/main/java/com/netflix/metacat/connector/snowflake/SnowflakeConnectorTableService.java

+21
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class SnowflakeConnectorTableService extends JdbcConnectorTableService {
5555
private static final String SQL_GET_AUDIT_INFO
5656
= "select created, last_altered from information_schema.tables"
5757
+ " where table_catalog=? and table_schema=? and table_name=?";
58+
5859
/**
5960
* Constructor.
6061
*
@@ -130,6 +131,13 @@ public void rename(@Nonnull final ConnectorRequestContext context,
130131
@Nonnull final QualifiedName newName) {
131132
super.rename(context, getSnowflakeName(oldName), getSnowflakeName(newName));
132133
}
134+
135+
@Override
136+
protected Connection getConnection(@Nonnull @NonNull final String schema) throws SQLException {
137+
final Connection connection = this.dataSource.getConnection();
138+
connection.setSchema(connection.getCatalog());
139+
return connection;
140+
}
133141

134142
/**
135143
* {@inheritDoc}
@@ -139,6 +147,19 @@ public boolean exists(@Nonnull final ConnectorRequestContext context, @Nonnull f
139147
return super.exists(context, getSnowflakeName(name));
140148
}
141149

150+
@Override
151+
protected ResultSet getColumns(
152+
@Nonnull @NonNull final Connection connection,
153+
@Nonnull @NonNull final QualifiedName name
154+
) throws SQLException {
155+
return connection.getMetaData().getColumns(
156+
connection.getCatalog(),
157+
name.getDatabaseName(),
158+
name.getTableName(),
159+
JdbcConnectorUtils.MULTI_CHARACTER_SEARCH
160+
);
161+
}
162+
142163
/**
143164
* {@inheritDoc}
144165
*/

metacat-connector-snowflake/src/test/groovy/com/netflix/metacat/connector/snowflake/SnowflakeConnectorTableServiceSpec.groovy

+8-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class SnowflakeConnectorTableServiceSpec extends Specification {
6363
then:
6464
1 * this.dataSource.getConnection() >> connection
6565
1 * connection.createStatement() >> statement
66-
1 * connection.setSchema(database)
66+
1 * connection.getCatalog() >> "dse"
67+
1 * connection.setSchema(_)
6768
1 * statement.executeUpdate(
6869
"DROP TABLE " + table
6970
)
@@ -83,7 +84,8 @@ class SnowflakeConnectorTableServiceSpec extends Specification {
8384
then:
8485
1 * this.dataSource.getConnection() >> connection
8586
1 * connection.createStatement() >> statement
86-
1 * connection.setSchema(database)
87+
1 * connection.getCatalog() >> "dse"
88+
1 * connection.setSchema(_)
8789
1 * statement.executeUpdate(
8890
"DROP TABLE " + table
8991
)
@@ -112,6 +114,8 @@ class SnowflakeConnectorTableServiceSpec extends Specification {
112114
then:
113115
1 * this.dataSource.getConnection() >> connection
114116
1 * connection.createStatement() >> statement
117+
1 * connection.getCatalog() >> "dse"
118+
2 * connection.setSchema(_)
115119
1 * statement.executeUpdate(
116120
"ALTER TABLE C RENAME TO D"
117121
)
@@ -129,6 +133,8 @@ class SnowflakeConnectorTableServiceSpec extends Specification {
129133
then:
130134
1 * this.dataSource.getConnection() >> connection
131135
1 * connection.createStatement() >> statement
136+
1 * connection.getCatalog() >> "dse"
137+
2 * connection.setSchema(_)
132138
1 * statement.executeUpdate(
133139
"ALTER TABLE C RENAME TO D"
134140
)

0 commit comments

Comments
 (0)