This repository has been archived by the owner on Nov 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
OracleConnection.java
146 lines (124 loc) · 5.27 KB
/
OracleConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.debezium.connector.oracle;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.debezium.config.Configuration;
import io.debezium.jdbc.JdbcConnection;
import io.debezium.relational.Column;
import io.debezium.relational.TableEditor;
import io.debezium.relational.TableId;
import io.debezium.relational.Tables;
import io.debezium.relational.Tables.ColumnNameFilter;
import io.debezium.relational.Tables.TableFilter;
import oracle.jdbc.OracleTypes;
public class OracleConnection extends JdbcConnection {
private final static Logger LOGGER = LoggerFactory.getLogger(OracleConnection.class);
/**
* Returned by column metadata in Oracle if no scale is set;
*/
private static final int ORACLE_UNSET_SCALE = -127;
public OracleConnection(Configuration config, ConnectionFactory connectionFactory) {
super(config, connectionFactory);
}
public void setSessionToPdb(String pdbName) {
Statement statement = null;
try {
statement = connection().createStatement();
statement.execute("alter session set container=" + pdbName);
}
catch (SQLException e) {
throw new RuntimeException(e);
}
finally {
if (statement != null) {
try {
statement.close();
}
catch (SQLException e) {
LOGGER.error("Couldn't close statement", e);
}
}
}
}
public void resetSessionToCdb() {
Statement statement = null;
try {
statement = connection().createStatement();
statement.execute("alter session set container=cdb$root");
}
catch (SQLException e) {
throw new RuntimeException(e);
}
finally {
if (statement != null) {
try {
statement.close();
}
catch (SQLException e) {
LOGGER.error("Couldn't close statement", e);
}
}
}
}
@Override
public Set<TableId> readTableNames(String databaseCatalog, String schemaNamePattern, String tableNamePattern,
String[] tableTypes)
throws SQLException {
Set<TableId> tableIds = super.readTableNames(null, schemaNamePattern, tableNamePattern, tableTypes);
return tableIds.stream()
.map(t -> new TableId(databaseCatalog, t.schema(), t.table()))
.collect(Collectors.toSet());
}
@Override
public void readSchema(Tables tables, String databaseCatalog, String schemaNamePattern, TableFilter tableFilter,
ColumnNameFilter columnFilter, boolean removeTablesNotFoundInJdbc)
throws SQLException {
super.readSchema(tables, null, schemaNamePattern, null, columnFilter, removeTablesNotFoundInJdbc);
Set<TableId> tableIds = tables.tableIds().stream().filter(x -> schemaNamePattern.equals(x.schema())).collect(Collectors.toSet());
for (TableId tableId : tableIds) {
// super.readSchema() populates ids without the catalog; hence we apply the filtering only
// here and if a table is included, overwrite it with a new id including the catalog
TableId tableIdWithCatalog = new TableId(databaseCatalog, tableId.schema(), tableId.table());
if (tableFilter.isIncluded(tableIdWithCatalog)) {
TableEditor editor = tables.editTable(tableId);
editor.tableId(tableIdWithCatalog);
List<String> columnNames = new ArrayList<>(editor.columnNames());
for (String columnName : columnNames) {
Column column = editor.columnWithName(columnName);
if (column.jdbcType() == Types.TIMESTAMP) {
editor.addColumn(
column.edit()
.length(column.scale().orElse(Column.UNSET_INT_VALUE))
.scale(null)
.create());
}
// NUMBER columns without scale value have it set to -127 instead of null;
// let's rectify that
else if (column.jdbcType() == OracleTypes.NUMBER) {
column.scale()
.filter(s -> s == ORACLE_UNSET_SCALE)
.ifPresent(s -> {
editor.addColumn(
column.edit()
.scale(null)
.create());
});
}
}
tables.overwriteTable(editor.create());
}
tables.removeTable(tableId);
}
}
}