-
Notifications
You must be signed in to change notification settings - Fork 1
adding new property TableName in the ui and its functionality #5
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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,11 +36,14 @@ | |
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.lang.reflect.Field; | ||
| //import java.sql.*; | ||
| import java.sql.Connection; | ||
| import java.sql.DatabaseMetaData; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.ResultSetMetaData; | ||
| import java.sql.SQLException; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Properties; | ||
|
|
@@ -105,6 +108,25 @@ public List<SnowflakeFieldDescriptor> describeQuery(String query) throws IOExcep | |
| return fieldDescriptors; | ||
| } | ||
|
|
||
| public List<SnowflakeFieldDescriptor> describeTable(String schemaName, String tableName) throws SQLException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add Javadoc for public methods |
||
| List<SnowflakeFieldDescriptor> fieldDescriptors = new ArrayList<>(); | ||
|
|
||
| try (Connection connection = dataSource.getConnection()) { | ||
| DatabaseMetaData dbMetaData = connection.getMetaData(); | ||
|
|
||
| try (ResultSet columns = dbMetaData.getColumns(null, schemaName, tableName, null)) { | ||
| while (columns.next()) { | ||
| String columnName = columns.getString("COLUMN_NAME"); | ||
| int columnType = columns.getInt("DATA_TYPE"); | ||
| boolean nullable = columns.getInt("NULLABLE") == DatabaseMetaData.columnNullable; | ||
|
|
||
| fieldDescriptors.add(new SnowflakeFieldDescriptor(columnName, columnType, nullable)); | ||
| } | ||
| } | ||
| } | ||
| return fieldDescriptors; | ||
| } | ||
|
|
||
| private void initDataSource(SnowflakeBasicDataSource dataSource, BaseSnowflakeConfig config) { | ||
| dataSource.setDatabaseName(config.getDatabase()); | ||
| dataSource.setSchema(config.getSchemaName()); | ||
|
|
@@ -193,4 +215,8 @@ private static String writeTextToTmpFile(String text) { | |
| throw new RuntimeException("Cannot write key to temporary file", e); | ||
| } | ||
| } | ||
|
|
||
| public String getSchema() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add javadoc here as well |
||
| return config.getSchemaName(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,8 +104,8 @@ private void validateInputSchema(Schema schema, FailureCollector failureCollecto | |
| } | ||
|
|
||
| SnowflakeAccessor snowflakeAccessor = new SnowflakeAccessor(this); | ||
| Schema expectedSchema = SchemaHelper.getSchema(snowflakeAccessor, String.format(GET_FIELDS_QUERY, tableName)); | ||
|
|
||
| // Schema expectedSchema = SchemaHelper.getSchema(snowflakeAccessor, String.format(GET_FIELDS_QUERY, tableName)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comments |
||
| Schema expectedSchema = SchemaHelper.getSchema(snowflakeAccessor, getSchemaName(), tableName, null); | ||
| try { | ||
| SchemaHelper.checkCompatibility(expectedSchema, schema); | ||
| } catch (IllegalArgumentException ex) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| package io.cdap.plugin.snowflake.source.batch; | ||
|
|
||
| import au.com.bytecode.opencsv.CSVReader; | ||
| import com.google.common.base.Strings; | ||
| import io.cdap.plugin.snowflake.common.SnowflakeErrorType; | ||
| import io.cdap.plugin.snowflake.common.client.SnowflakeAccessor; | ||
| import io.cdap.plugin.snowflake.common.util.DocumentUrlUtil; | ||
|
|
@@ -77,7 +78,11 @@ public SnowflakeSourceAccessor(SnowflakeBatchSourceConfig config, String escapeC | |
| */ | ||
| public List<String> prepareStageSplits() { | ||
| LOG.info("Loading data into stage: '{}'", STAGE_PATH); | ||
| String copy = String.format(COMAND_COPY_INTO, QueryUtil.removeSemicolon(config.getImportQuery())); | ||
| String importQuery = config.getImportQuery(); | ||
| if (Strings.isNullOrEmpty(importQuery)) { | ||
| importQuery = "SELECT * FROM " + config.getTableName(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to use String.format method |
||
| } | ||
| String copy = String.format(COMAND_COPY_INTO, QueryUtil.removeSemicolon(importQuery)); | ||
| if (config.getMaxSplitSize() > 0) { | ||
| copy = copy + String.format(COMMAND_MAX_FILE_SIZE, config.getMaxSplitSize()); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,8 @@ | |
| public class SchemaHelperTest { | ||
|
|
||
| private static final String MOCK_STAGE = "mockStage"; | ||
| private static final String MOCK_SCHEMA = "mockSchema"; | ||
| private static final String MOCK_TABLE = "mockTable"; | ||
|
|
||
| @Test | ||
| public void testGetSchema() { | ||
|
|
@@ -48,7 +50,11 @@ public void testGetSchema() { | |
| ); | ||
|
|
||
| MockFailureCollector collector = new MockFailureCollector(MOCK_STAGE); | ||
| Schema actual = SchemaHelper.getSchema(null, expected.toString(), collector, null); | ||
| SnowflakeBatchSourceConfig mockConfig = Mockito.mock(SnowflakeBatchSourceConfig.class); | ||
| Mockito.when(mockConfig.canConnect()).thenReturn(false); | ||
| Mockito.when(mockConfig.getSchema()).thenReturn(expected.toString()); | ||
|
|
||
| Schema actual = SchemaHelper.getSchema(mockConfig, collector); | ||
|
|
||
| Assert.assertTrue(collector.getValidationFailures().isEmpty()); | ||
| Assert.assertEquals(expected, actual); | ||
|
|
@@ -57,8 +63,10 @@ public void testGetSchema() { | |
| @Test | ||
| public void testGetSchemaInvalidJson() { | ||
| MockFailureCollector collector = new MockFailureCollector(MOCK_STAGE); | ||
| SchemaHelper.getSchema(null, "{}", collector, null); | ||
|
|
||
| SnowflakeBatchSourceConfig mockConfig = Mockito.mock(SnowflakeBatchSourceConfig.class); | ||
| Mockito.when(mockConfig.getSchema()).thenReturn("{}"); | ||
| // SchemaHelper.getSchema(null, "{}", collector, null); | ||
|
||
| SchemaHelper.getSchema(mockConfig, collector); | ||
| ValidationAssertions.assertValidationFailed( | ||
| collector, Collections.singletonList(SnowflakeBatchSourceConfig.PROPERTY_SCHEMA)); | ||
| } | ||
|
|
@@ -74,8 +82,7 @@ public void testGetSchemaFromSnowflakeUnknownType() throws IOException { | |
|
|
||
| Mockito.when(snowflakeAccessor.describeQuery(importQuery)).thenReturn(sample); | ||
|
|
||
| SchemaHelper.getSchema(snowflakeAccessor, null, collector, importQuery); | ||
|
|
||
| SchemaHelper.getSchema(snowflakeAccessor, MOCK_SCHEMA, MOCK_TABLE, importQuery); | ||
| ValidationAssertions.assertValidationFailed( | ||
| collector, Collections.singletonList(SnowflakeBatchSourceConfig.PROPERTY_SCHEMA)); | ||
| } | ||
|
|
@@ -146,7 +153,7 @@ public void testGetSchemaFromSnowflake() throws IOException { | |
|
|
||
| Mockito.when(snowflakeAccessor.describeQuery(importQuery)).thenReturn(sample); | ||
|
|
||
| Schema actual = SchemaHelper.getSchema(snowflakeAccessor, null, collector, importQuery); | ||
| Schema actual = SchemaHelper.getSchema(snowflakeAccessor, MOCK_SCHEMA, MOCK_TABLE, importQuery); | ||
|
|
||
| Assert.assertTrue(collector.getValidationFailures().isEmpty()); | ||
| Assert.assertEquals(expected, actual); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove comments