Skip to content

Commit a77e211

Browse files
Merge pull request #657 from cloudsufi/oracle-supportforxml
Enable xml type support for oracle for dts-connectors only
2 parents c67cc21 + d506e38 commit a77e211

8 files changed

Lines changed: 108 additions & 11 deletions

File tree

oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConnector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ protected DBConnectorPath getDBConnectorPath(String path) {
115115
protected SchemaReader getSchemaReader(String sessionID) {
116116
return new OracleSourceSchemaReader(sessionID, config.getTreatAsOldTimestamp(),
117117
config.getTreatPrecisionlessNumAsDeci(),
118-
config.getTreatTimestampLTZAsTimestamp());
118+
config.getTreatTimestampLTZAsTimestamp(),
119+
config.getXmlTypeEnabled());
119120
}
120121

121122
@Override

oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConnectorConfig.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ public OracleConnectorConfig(String host, int port, String user, String password
4242
public OracleConnectorConfig(String host, int port, String user, String password, String jdbcPluginName,
4343
String connectionArguments, String connectionType, String database) {
4444
this(host, port, user, password, jdbcPluginName, connectionArguments, connectionType, database, null, null, null,
45-
null, null);
45+
null, null, null);
4646
}
4747

4848
public OracleConnectorConfig(String host, int port, String user, String password, String jdbcPluginName,
4949
String connectionArguments, String connectionType, String database,
5050
String role, Boolean useSSL, @Nullable Boolean treatAsOldTimestamp,
5151
@Nullable Boolean treatPrecisionlessNumAsDeci,
52-
@Nullable Boolean treatTimestampLTZAsTimestamp) {
52+
@Nullable Boolean treatTimestampLTZAsTimestamp,
53+
@Nullable Boolean enableXmlType) {
5354

5455
this.host = host;
5556
this.port = port;
@@ -64,6 +65,7 @@ public OracleConnectorConfig(String host, int port, String user, String password
6465
this.treatAsOldTimestamp = treatAsOldTimestamp;
6566
this.treatPrecisionlessNumAsDeci = treatPrecisionlessNumAsDeci;
6667
this.treatTimestampLTZAsTimestamp = treatTimestampLTZAsTimestamp;
68+
this.enableXmlType = enableXmlType;
6769
}
6870

6971
@Override
@@ -101,10 +103,16 @@ public String getConnectionString() {
101103
public Boolean treatPrecisionlessNumAsDeci;
102104

103105
@Name(OracleConstants.TREAT_TIMESTAMP_LTZ_AS_TIMESTAMP)
104-
@Description("A hidden field to handle mapping of Oracle Timestamp_LTZ data type to BQ Timestamp.")
106+
@Description("A hidden field to handle mapping of Oracle Timestamp_LTZ data type.")
105107
@Nullable
106108
public Boolean treatTimestampLTZAsTimestamp;
107109

110+
@Name(OracleConstants.ENABLE_XML_TYPE)
111+
@Description("A hidden field to handle mapping of Oracle XML type.")
112+
@Nullable
113+
public Boolean enableXmlType;
114+
115+
108116
@Override
109117
protected int getDefaultPort() {
110118
return 1521;
@@ -139,6 +147,10 @@ public Boolean getTreatTimestampLTZAsTimestamp() {
139147
return Boolean.TRUE.equals(treatTimestampLTZAsTimestamp);
140148
}
141149

150+
public Boolean getXmlTypeEnabled() {
151+
return Boolean.TRUE.equals(enableXmlType);
152+
}
153+
142154
@Override
143155
public Properties getConnectionArgumentsProperties() {
144156
Properties prop = super.getConnectionArgumentsProperties();

oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private OracleConstants() {
4747
public static final String TREAT_AS_OLD_TIMESTAMP = "treatAsOldTimestamp";
4848
public static final String TREAT_PRECISIONLESSNUM_AS_DECI = "treatPrecisionlessNumAsDeci";
4949
public static final String TREAT_TIMESTAMP_LTZ_AS_TIMESTAMP = "treatTimestampLTZAsTimestamp";
50+
public static final String ENABLE_XML_TYPE = "enableXmlType";
5051

5152
/**
5253
* Constructs the Oracle connection string based on the provided connection type, host, port, and database.

oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleSource.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ protected SchemaReader getSchemaReader() {
6969
boolean treatAsOldTimestamp = oracleSourceConfig.getConnection().getTreatAsOldTimestamp();
7070
boolean treatPrecisionlessNumAsDeci = oracleSourceConfig.getConnection().getTreatPrecisionlessNumAsDeci();
7171
boolean treatTimestampLTZAsTimestamp = oracleSourceConfig.getConnection().getTreatTimestampLTZAsTimestamp();
72+
boolean enableXmlType = oracleSourceConfig.getConnection().getXmlTypeEnabled();
7273

7374
return new OracleSourceSchemaReader(null, treatAsOldTimestamp, treatPrecisionlessNumAsDeci,
74-
treatTimestampLTZAsTimestamp);
75+
treatTimestampLTZAsTimestamp, enableXmlType);
7576
}
7677

7778
@Override
@@ -139,10 +140,12 @@ public OracleSourceConfig(String host, int port, String user, String password, S
139140
int defaultBatchValue, int defaultRowPrefetch,
140141
String importQuery, Integer numSplits, int fetchSize,
141142
String boundingQuery, String splitBy, Boolean useSSL, Boolean treatAsOldTimestamp,
142-
Boolean treatPrecisionlessNumAsDeci, Boolean treatTimestampLTZAsTimestamp) {
143+
Boolean treatPrecisionlessNumAsDeci, Boolean treatTimestampLTZAsTimestamp,
144+
Boolean enableXmlType) {
143145
this.connection = new OracleConnectorConfig(host, port, user, password, jdbcPluginName, connectionArguments,
144146
connectionType, database, role, useSSL, treatAsOldTimestamp,
145-
treatPrecisionlessNumAsDeci, treatTimestampLTZAsTimestamp);
147+
treatPrecisionlessNumAsDeci, treatTimestampLTZAsTimestamp,
148+
enableXmlType);
146149
this.defaultBatchValue = defaultBatchValue;
147150
this.defaultRowPrefetch = defaultRowPrefetch;
148151
this.fetchSize = fetchSize;

oracle-plugin/src/main/java/io/cdap/plugin/oracle/OracleSourceSchemaReader.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class OracleSourceSchemaReader extends CommonSchemaReader {
6060
BINARY_DOUBLE,
6161
BFILE,
6262
LONG,
63+
Types.SQLXML,
6364
LONG_RAW,
6465
Types.NUMERIC,
6566
Types.DECIMAL
@@ -69,16 +70,19 @@ public class OracleSourceSchemaReader extends CommonSchemaReader {
6970
private final Boolean isTimestampOldBehavior;
7071
private final Boolean isPrecisionlessNumAsDecimal;
7172
private final Boolean isTimestampLtzFieldTimestamp;
73+
private final Boolean isXmlTypeEnabled;
7274

7375
public OracleSourceSchemaReader() {
74-
this(null, false, false, false);
76+
this(null, false, false, false, false);
7577
}
7678
public OracleSourceSchemaReader(@Nullable String sessionID, boolean isTimestampOldBehavior,
77-
boolean isPrecisionlessNumAsDecimal, boolean isTimestampLtzFieldTimestamp) {
79+
boolean isPrecisionlessNumAsDecimal, boolean isTimestampLtzFieldTimestamp,
80+
boolean isXmlTypeEnabled) {
7881
this.sessionID = sessionID;
7982
this.isTimestampOldBehavior = isTimestampOldBehavior;
8083
this.isPrecisionlessNumAsDecimal = isPrecisionlessNumAsDecimal;
8184
this.isTimestampLtzFieldTimestamp = isTimestampLtzFieldTimestamp;
85+
this.isXmlTypeEnabled = isXmlTypeEnabled;
8286
}
8387

8488
@Override
@@ -103,6 +107,9 @@ public Schema getSchema(ResultSetMetaData metadata, int index) throws SQLExcepti
103107
case INTERVAL_YM:
104108
case LONG:
105109
return Schema.of(Schema.Type.STRING);
110+
case Types.SQLXML:
111+
// Enabling XML type support for DTS connectors only as it is not in working state in CDAP plugin.
112+
return isXmlTypeEnabled ? Schema.of(Schema.Type.STRING) : super.getSchema(metadata, index);
106113
case Types.NUMERIC:
107114
case Types.DECIMAL:
108115
// FLOAT and REAL are returned as java.sql.Types.NUMERIC but with value that is a java.lang.Double

oracle-plugin/src/test/java/io/cdap/plugin/oracle/OracleSchemaReaderTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.collect.Lists;
2020
import io.cdap.cdap.api.data.schema.Schema;
21+
import io.cdap.cdap.api.exception.ProgramFailureException;
2122
import org.junit.Assert;
2223
import org.junit.Test;
2324
import org.junit.runner.RunWith;
@@ -27,13 +28,14 @@
2728
import java.sql.ResultSet;
2829
import java.sql.ResultSetMetaData;
2930
import java.sql.SQLException;
31+
import java.sql.Types;
3032
import java.util.List;
3133

3234
public class OracleSchemaReaderTest {
3335

3436
@Test
3537
public void getSchema_timestampLTZFieldTrue_returnTimestamp() throws SQLException {
36-
OracleSourceSchemaReader schemaReader = new OracleSourceSchemaReader(null, false, false, true);
38+
OracleSourceSchemaReader schemaReader = new OracleSourceSchemaReader(null, false, false, true, false);
3739

3840
ResultSet resultSet = Mockito.mock(ResultSet.class);
3941
ResultSetMetaData metadata = Mockito.mock(ResultSetMetaData.class);
@@ -64,7 +66,7 @@ public void getSchema_timestampLTZFieldTrue_returnTimestamp() throws SQLExceptio
6466

6567
@Test
6668
public void getSchema_timestampLTZFieldFalse_returnDatetime() throws SQLException {
67-
OracleSourceSchemaReader schemaReader = new OracleSourceSchemaReader(null, false, false, false);
69+
OracleSourceSchemaReader schemaReader = new OracleSourceSchemaReader(null, false, false, false, false);
6870

6971
ResultSet resultSet = Mockito.mock(ResultSet.class);
7072
ResultSetMetaData metadata = Mockito.mock(ResultSetMetaData.class);
@@ -91,4 +93,37 @@ public void getSchema_timestampLTZFieldFalse_returnDatetime() throws SQLExceptio
9193
Assert.assertEquals(expectedSchemaFields.get(1).getName(), actualSchemaFields.get(1).getName());
9294
Assert.assertEquals(expectedSchemaFields.get(1).getSchema(), actualSchemaFields.get(1).getSchema());
9395
}
96+
97+
@Test
98+
public void getSchema_xmlField_returnString() throws SQLException {
99+
OracleSourceSchemaReader schemaReader = new OracleSourceSchemaReader(null, false, false, false, true);
100+
ResultSet resultSet = Mockito.mock(ResultSet.class);
101+
ResultSetMetaData metadata = Mockito.mock(ResultSetMetaData.class);
102+
Mockito.when(resultSet.getMetaData()).thenReturn(metadata);
103+
Mockito.when(metadata.getColumnCount()).thenReturn(1);
104+
Mockito.when(metadata.getColumnType(1)).thenReturn(Types.SQLXML);
105+
Mockito.when(metadata.getColumnName(1)).thenReturn("xmlData");
106+
107+
List<Schema.Field> actualSchemaFields = schemaReader.getSchemaFields(resultSet);
108+
109+
List<Schema.Field> expectedSchemaFields = Lists.newArrayList();
110+
expectedSchemaFields.add(Schema.Field.of("xmlData", Schema.of(Schema.Type.STRING)));
111+
Assert.assertEquals(expectedSchemaFields.get(0).getName(), actualSchemaFields.get(0).getName());
112+
Assert.assertEquals(expectedSchemaFields.get(0).getSchema(), actualSchemaFields.get(0).getSchema());
113+
}
114+
115+
@Test
116+
public void getSchema_xmlFieldDisabled_throwsProgramFailureException() throws SQLException {
117+
OracleSourceSchemaReader schemaReader = new OracleSourceSchemaReader(null,
118+
false, false, false, false);
119+
ResultSet resultSet = Mockito.mock(ResultSet.class);
120+
ResultSetMetaData metadata = Mockito.mock(ResultSetMetaData.class);
121+
Mockito.when(resultSet.getMetaData()).thenReturn(metadata);
122+
Mockito.when(metadata.getColumnCount()).thenReturn(1);
123+
Mockito.when(metadata.getColumnType(1)).thenReturn(Types.SQLXML);
124+
Mockito.when(metadata.getColumnName(1)).thenReturn("xmlData");
125+
126+
Assert.assertThrows(ProgramFailureException.class, () -> schemaReader.getSchemaFields(resultSet));
127+
128+
}
94129
}

oracle-plugin/widgets/Oracle-batchsource.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,25 @@
177177
]
178178
}
179179
},
180+
{
181+
"widget-type": "hidden",
182+
"label": "Enable Xml Type",
183+
"name": "enableXmlType",
184+
"widget-attributes": {
185+
"layout": "inline",
186+
"default": "false",
187+
"options": [
188+
{
189+
"id": "true",
190+
"label": "true"
191+
},
192+
{
193+
"id": "false",
194+
"label": "false"
195+
}
196+
]
197+
}
198+
},
180199
{
181200
"name": "connectionType",
182201
"label": "Connection Type",

oracle-plugin/widgets/Oracle-connector.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,25 @@
186186
}
187187
]
188188
}
189+
},
190+
{
191+
"widget-type": "hidden",
192+
"label": "Enable Xml Type",
193+
"name": "enableXmlType",
194+
"widget-attributes": {
195+
"layout": "inline",
196+
"default": "false",
197+
"options": [
198+
{
199+
"id": "true",
200+
"label": "true"
201+
},
202+
{
203+
"id": "false",
204+
"label": "false"
205+
}
206+
]
207+
}
189208
}
190209
]
191210
},

0 commit comments

Comments
 (0)