Skip to content

Commit 1f2e5be

Browse files
smorineaujeanbisutti
authored andcommitted
Added support for Oracle database
1 parent 2ef4559 commit 1f2e5be

13 files changed

Lines changed: 441 additions & 71 deletions

src/main/java/org/stdg/DatabaseMetadataFinder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import org.stdg.dbtype.DatabaseMetadataFinderFactory;
1717
import org.stdg.dbtype.DatabaseMetadataFinderWithCache;
1818

19+
import java.util.function.Function;
20+
1921
/**
2022
* Interface describing the methods needed by the library to retrieve some database metadata.
2123
*
@@ -27,4 +29,9 @@ public interface DatabaseMetadataFinder extends NotNullColumnsFinder
2729
, ReferencedTablesFinder
2830
, ColumnsMappingsFinder
2931
, PrimaryKeyColumnsFinder {
32+
33+
default Function<String, String> getFunctionToHaveMetadataTableName() {
34+
return tableName -> tableName;
35+
}
36+
3037
}

src/main/java/org/stdg/DatasetRow.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
package org.stdg;
1515

1616
import java.util.*;
17+
import java.util.function.Function;
1718
import java.util.stream.Stream;
1819

1920
import static java.util.stream.Collectors.toList;
2021

2122
public class DatasetRow {
2223

23-
private final String tableName;
24+
private String tableName;
2425

2526
private TreeMap<String, Object> columnValueByColumnName = new TreeMap<>();
2627

@@ -152,4 +153,9 @@ Object getValueOf(String columnName) {
152153
return columnValueByColumnName.get(columnName);
153154
}
154155

156+
void updateTableNameWith(Function<String, String> tableNameFunction) {
157+
String newTableName = tableNameFunction.apply(tableName);
158+
tableName = newTableName;
159+
}
160+
155161
}

src/main/java/org/stdg/DatasetRowSet.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import javax.sql.DataSource;
1717
import java.util.*;
18+
import java.util.function.Function;
1819

1920
class DatasetRowSet {
2021

@@ -37,6 +38,9 @@ void add(Collection<DatasetRow> datasetRows) {
3738

3839
private void add(DatasetRow datasetRow) {
3940

41+
Function<String, String> functionToHaveMetadataTableName = databaseMetadataFinder.getFunctionToHaveMetadataTableName();
42+
datasetRow.updateTableNameWith(functionToHaveMetadataTableName);
43+
4044
boolean rowIsMerged = datasetRow.mergeWithARowOf(datasetRows);
4145

4246
if (!rowIsMerged) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2021-2021 the original author or authors.
12+
*/
13+
package org.stdg.dbtype;
14+
15+
import org.stdg.ColumnOrdersFinder;
16+
import org.stdg.PreparedStatementBuilder;
17+
import org.stdg.SqlQuery;
18+
19+
import javax.sql.DataSource;
20+
import java.sql.Connection;
21+
import java.sql.PreparedStatement;
22+
import java.sql.ResultSet;
23+
import java.sql.SQLException;
24+
import java.util.ArrayList;
25+
import java.util.Collections;
26+
import java.util.List;
27+
28+
class BaseColumnOrdersFinder implements ColumnOrdersFinder {
29+
30+
private final DataSource dataSource;
31+
32+
private final SqlQuery notNullColumnsQuery;
33+
34+
BaseColumnOrdersFinder(DataSource dataSource, SqlQuery notNullColumnsQuery) {
35+
this.dataSource = dataSource;
36+
this.notNullColumnsQuery = notNullColumnsQuery;
37+
}
38+
39+
@Override
40+
public List<String> findDatabaseColumnOrdersOf(String tableName) {
41+
try (Connection connection = dataSource.getConnection();
42+
PreparedStatement columnOrderStatement = PreparedStatementBuilder.buildFrom(notNullColumnsQuery, connection)) {
43+
columnOrderStatement.setString(1, tableName);
44+
ResultSet queryResult = columnOrderStatement.executeQuery();
45+
return findColumnOrderFrom(queryResult);
46+
} catch (SQLException sqlException) {
47+
sqlException.printStackTrace();
48+
}
49+
return Collections.emptyList();
50+
}
51+
52+
private List<String> findColumnOrderFrom(ResultSet queryResult) throws SQLException {
53+
List<String> columnOrder = new ArrayList<>();
54+
while (queryResult.next()) {
55+
String columnName = queryResult.getString(3);
56+
columnOrder.add(columnName);
57+
}
58+
return columnOrder;
59+
}
60+
61+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2021-2021 the original author or authors.
12+
*/
13+
package org.stdg.dbtype;
14+
15+
import org.stdg.NotNullColumnsFinder;
16+
import org.stdg.PreparedStatementBuilder;
17+
import org.stdg.SqlQuery;
18+
19+
import javax.sql.DataSource;
20+
import java.sql.Connection;
21+
import java.sql.PreparedStatement;
22+
import java.sql.ResultSet;
23+
import java.sql.SQLException;
24+
import java.util.ArrayList;
25+
import java.util.Collection;
26+
import java.util.Collections;
27+
import java.util.List;
28+
29+
public class BaseNotNullColumnsFinder implements NotNullColumnsFinder {
30+
31+
private final DataSource dataSource;
32+
33+
private final SqlQuery notNullColumnsQuery;
34+
35+
BaseNotNullColumnsFinder(DataSource dataSource, SqlQuery notNullColumnsQuery) {
36+
this.dataSource = dataSource;
37+
this.notNullColumnsQuery = notNullColumnsQuery;
38+
}
39+
40+
@Override
41+
public Collection<String> findNotNullColumnsOf(String tableName) {
42+
try (Connection connection = dataSource.getConnection();
43+
PreparedStatement columnOrderStatement = PreparedStatementBuilder.buildFrom(notNullColumnsQuery, connection)) {
44+
columnOrderStatement.setString(1, tableName);
45+
ResultSet queryResult = columnOrderStatement.executeQuery();
46+
return findNotNullColumnsFrom(queryResult);
47+
} catch (SQLException sqlException) {
48+
sqlException.printStackTrace();
49+
}
50+
return Collections.emptyList();
51+
}
52+
53+
private List<String> findNotNullColumnsFrom(ResultSet resultSet) throws SQLException {
54+
List<String> notNullColumns = new ArrayList<>();
55+
while (resultSet.next()) {
56+
String columnName = resultSet.getString(3);
57+
notNullColumns.add(columnName);
58+
}
59+
return notNullColumns;
60+
}
61+
62+
}

src/main/java/org/stdg/dbtype/DatabaseMetadataFinderFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static DatabaseMetadataFinder createDatabaseMetadataFinderFrom(DataSource
5555
}
5656

5757
if(dbType.equals(ORACLE)) {
58-
return new OracleMetadataFinder();
58+
return new OracleMetadataFinder(dataSource);
5959
}
6060

6161
return new DefaultDatabaseMetadataFinder(dataSource);

src/main/java/org/stdg/dbtype/DatabaseMetadataFinderWithCache.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.List;
2222
import java.util.concurrent.ConcurrentHashMap;
23+
import java.util.function.Function;
2324

2425
/**
2526
* A DatabaseMetadataFinder caching method calls
@@ -71,4 +72,9 @@ public List<String> findPrimaryColumnsOf(String tableName) {
7172
return primaryColumnsByTableName.computeIfAbsent(tableName, t -> delegate.findPrimaryColumnsOf(tableName));
7273
}
7374

75+
@Override
76+
public Function<String, String> getFunctionToHaveMetadataTableName() {
77+
return delegate.getFunctionToHaveMetadataTableName();
78+
}
79+
7480
}

src/main/java/org/stdg/dbtype/DefaultColumnOrdersFinder.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,9 @@
1414
package org.stdg.dbtype;
1515

1616
import org.stdg.ColumnOrdersFinder;
17-
import org.stdg.PreparedStatementBuilder;
1817
import org.stdg.SqlQuery;
1918

2019
import javax.sql.DataSource;
21-
import java.sql.Connection;
22-
import java.sql.PreparedStatement;
23-
import java.sql.ResultSet;
24-
import java.sql.SQLException;
25-
import java.util.ArrayList;
26-
import java.util.Collections;
2720
import java.util.List;
2821

2922
class DefaultColumnOrdersFinder implements ColumnOrdersFinder {
@@ -37,32 +30,15 @@ class DefaultColumnOrdersFinder implements ColumnOrdersFinder {
3730
" where table_name=?" +
3831
" order by position");
3932

40-
private final DataSource dataSource;
33+
private BaseColumnOrdersFinder delegate;
4134

4235
DefaultColumnOrdersFinder(DataSource dataSource) {
43-
this.dataSource = dataSource;
36+
delegate = new BaseColumnOrdersFinder(dataSource, COLUMN_ORDER_QUERY);
4437
}
4538

4639
@Override
4740
public List<String> findDatabaseColumnOrdersOf(String tableName) {
48-
try (Connection connection = dataSource.getConnection();
49-
PreparedStatement columnOrderStatement = PreparedStatementBuilder.buildFrom(COLUMN_ORDER_QUERY, connection)) {
50-
columnOrderStatement.setString(1, tableName);
51-
ResultSet queryResult = columnOrderStatement.executeQuery();
52-
return findColumnOrderFrom(queryResult);
53-
} catch (SQLException sqlException) {
54-
sqlException.printStackTrace();
55-
}
56-
return Collections.emptyList();
57-
}
58-
59-
private List<String> findColumnOrderFrom(ResultSet queryResult) throws SQLException {
60-
List<String> columnOrder = new ArrayList<>();
61-
while (queryResult.next()) {
62-
String columnName = queryResult.getString(3);
63-
columnOrder.add(columnName);
64-
}
65-
return columnOrder;
41+
return delegate.findDatabaseColumnOrdersOf(tableName);
6642
}
6743

6844
}

src/main/java/org/stdg/dbtype/DefaultDatabaseMetadataFinder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.stdg.ReferencedTableSet;
1919

2020
import javax.sql.DataSource;
21+
import java.util.Collection;
2122
import java.util.Collections;
2223
import java.util.List;
2324

@@ -41,7 +42,7 @@ public ReferencedTableSet findReferencedTablesOf(String tableName) {
4142
}
4243

4344
@Override
44-
public List<String> findNotNullColumnsOf(String tableName) {
45+
public Collection<String> findNotNullColumnsOf(String tableName) {
4546
return defaultNotNullColumnsFinder.findNotNullColumnsOf(tableName);
4647
}
4748

src/main/java/org/stdg/dbtype/DefaultNotNullColumnsFinder.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.sql.ResultSet;
2424
import java.sql.SQLException;
2525
import java.util.ArrayList;
26+
import java.util.Collection;
2627
import java.util.Collections;
2728
import java.util.List;
2829

@@ -36,32 +37,15 @@ class DefaultNotNullColumnsFinder implements NotNullColumnsFinder {
3637
"where is_nullable = 'NO'\n" +
3738
" AND table_name=?");
3839

39-
private final DataSource dataSource;
40+
private BaseNotNullColumnsFinder delegate;
4041

4142
DefaultNotNullColumnsFinder(DataSource dataSource) {
42-
this.dataSource = dataSource;
43+
delegate = new BaseNotNullColumnsFinder(dataSource, NOT_NULL_COLUMNS_QUERY);
4344
}
4445

4546
@Override
46-
public List<String> findNotNullColumnsOf(String tableName) {
47-
try (Connection connection = dataSource.getConnection();
48-
PreparedStatement columnOrderStatement = PreparedStatementBuilder.buildFrom(NOT_NULL_COLUMNS_QUERY, connection)) {
49-
columnOrderStatement.setString(1, tableName);
50-
ResultSet queryResult = columnOrderStatement.executeQuery();
51-
return findNotNullColumnsFrom(queryResult);
52-
} catch (SQLException sqlException) {
53-
sqlException.printStackTrace();
54-
}
55-
return Collections.emptyList();
56-
}
57-
58-
private List<String> findNotNullColumnsFrom(ResultSet resultSet) throws SQLException {
59-
List<String> notNullColumns = new ArrayList<>();
60-
while (resultSet.next()) {
61-
String columnName = resultSet.getString(3);
62-
notNullColumns.add(columnName);
63-
}
64-
return notNullColumns;
47+
public Collection<String> findNotNullColumnsOf(String tableName) {
48+
return delegate.findNotNullColumnsOf(tableName);
6549
}
6650

6751
}

0 commit comments

Comments
 (0)