Skip to content

Commit 87418f5

Browse files
committed
Add basic implementation for Oracle database
1 parent bd188aa commit 87418f5

8 files changed

Lines changed: 169 additions & 5 deletions

File tree

THIRD_PARTY.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ SQL test data generator uses only for testing and doesn't ship with:
1616
* Microsoft JDBC Driver for SQL Server licensed under MIT License (https://github.com/microsoft/mssql-jdbc/blob/dev/LICENSE)
1717
* PostgreSQL Docker image licensed under MIT License (https://github.com/docker-library/postgres/blob/master/LICENSE)
1818
* PostgreSQL JDBC Driver licensed under BSD 2-Clause "Simplified" License (https://github.com/pgjdbc/pgjdbc/blob/master/LICENSE)
19+
* Oracle Database Express Edition licensed under Oracle Free Use Terms and Conditions (https://docs.oracle.com/en/database/oracle/oracle-database/18/xelic/licensing-information.html#GUID-5D29C372-9B49-4A4D-A9AF-70ACA12CCF46, https://www.oracle.com/downloads/licenses/oracle-free-license.html)
20+
* Oracle JDBC driver licensed under Oracle Free Use Terms and Conditions (https://www.oracle.com/downloads/licenses/oracle-free-license.html)

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@
107107
<version>42.2.13</version>
108108
<scope>test</scope>
109109
</dependency>
110+
<dependency>
111+
<groupId>org.testcontainers</groupId>
112+
<artifactId>oracle-xe</artifactId>
113+
<version>${testcontainers.version}</version>
114+
<scope>test</scope>
115+
</dependency>
116+
<dependency>
117+
<groupId>com.oracle.database.jdbc</groupId>
118+
<artifactId>ojdbc11</artifactId>
119+
<version>21.1.0.0</version>
120+
<scope>test</scope>
121+
</dependency>
110122
<dependency>
111123
<groupId>org.junit.jupiter</groupId>
112124
<artifactId>junit-jupiter</artifactId>

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ boolean hasNotNullValueForColumn(String columnName) {
5353
}
5454

5555
void sortColumnsFollowing(List<String> databaseColumnOrders) {
56-
ColumnNamesComparator columnNamesComparator = ColumnNamesComparator.from(databaseColumnOrders);
57-
TreeMap<String, Object> columnValueByColumnName = new TreeMap<>(columnNamesComparator);
58-
columnValueByColumnName.putAll(this.columnValueByColumnName);
59-
this.columnValueByColumnName = columnValueByColumnName;
56+
if (!databaseColumnOrders.isEmpty()) {
57+
ColumnNamesComparator columnNamesComparator = ColumnNamesComparator.from(databaseColumnOrders);
58+
TreeMap<String, Object> columnValueByColumnName = new TreeMap<>(columnNamesComparator);
59+
columnValueByColumnName.putAll(this.columnValueByColumnName);
60+
this.columnValueByColumnName = columnValueByColumnName;
61+
}
6062
}
6163

6264
boolean mergeWithARowOf(Collection<DatasetRow> datasetRows) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public static DatabaseMetadataFinder createDatabaseMetadataFinderFrom(DataSource
5454
return new MSSQLServerMetadataFinder(dataSource);
5555
}
5656

57+
if(dbType.equals(ORACLE)) {
58+
return new OracleMetadataFinder();
59+
}
60+
5761
return new DefaultDatabaseMetadataFinder(dataSource);
5862

5963
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public enum DatabaseType {
3030
MICROSOFT_SQL_SERVER("jdbc:sqlserver")
3131
,/**MySQL*/
3232
MY_SQL("jdbc:mysql")
33+
,/**Oracle*/
34+
ORACLE("jdbc:oracle")
3335
,/**PostgreSQL*/
3436
POSTGRE_SQL("jdbc:postgresql")
3537
,/**Other database type*/
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
14+
package org.stdg.dbtype;
15+
16+
import org.stdg.ColumnsMappingGroup;
17+
import org.stdg.DatabaseMetadataFinder;
18+
import org.stdg.ReferencedTableSet;
19+
20+
import java.util.Collection;
21+
import java.util.List;
22+
23+
import static java.util.Collections.emptyList;
24+
25+
public class OracleMetadataFinder implements DatabaseMetadataFinder {
26+
27+
@Override
28+
public List<String> findDatabaseColumnOrdersOf(String tableName) {
29+
return emptyList();
30+
}
31+
32+
@Override
33+
public ColumnsMappingGroup findColumnsMappingsOf(String tableName) {
34+
return ColumnsMappingGroup.NO_MAPPING;
35+
}
36+
37+
@Override
38+
public Collection<String> findNotNullColumnsOf(String tableName) {
39+
return emptyList();
40+
}
41+
42+
@Override
43+
public List<String> findPrimaryColumnsOf(String tableName) {
44+
return emptyList();
45+
}
46+
47+
@Override
48+
public ReferencedTableSet findReferencedTablesOf(String tableName) {
49+
return ReferencedTableSet.NONE;
50+
}
51+
52+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
14+
package org.stdg.test;
15+
16+
import org.junit.jupiter.api.AfterAll;
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Test;
19+
import org.stdg.DatasetRow;
20+
import org.stdg.SqlTestDataGenerator;
21+
import org.testcontainers.containers.OracleContainer;
22+
23+
import javax.sql.DataSource;
24+
import java.util.List;
25+
26+
import static org.stdg.test.TestTable.*;
27+
import static org.stdg.test.TestTable.TestTableAssert.assertThat;
28+
29+
public class OracleTest {
30+
31+
private static DataSource DATA_SOURCE;
32+
33+
private static final OracleContainer ORACLE_CONTAINER
34+
= new OracleContainer("gvenzl/oracle-xe")
35+
.withEnv("ORACLE_PASSWORD", "oracle");
36+
37+
private static SqlExecutor SQL_EXECUTOR;
38+
39+
@BeforeAll
40+
public static void beforeAll() {
41+
ORACLE_CONTAINER.start();
42+
String jdbcUrl = ORACLE_CONTAINER.getJdbcUrl();
43+
DATA_SOURCE = DataSourceBuilder.INSTANCE.build(jdbcUrl
44+
, ORACLE_CONTAINER.getUsername()
45+
, ORACLE_CONTAINER.getPassword());
46+
SQL_EXECUTOR = new SqlExecutor(DATA_SOURCE);
47+
}
48+
49+
@AfterAll
50+
public static void stopContainer() {
51+
ORACLE_CONTAINER.stop();
52+
}
53+
54+
@Test
55+
public void should_generate_working_insert() {
56+
57+
// GIVEN
58+
TestTable table =
59+
buildUniqueTable(DATA_SOURCE
60+
, "TABLE_NAME"
61+
, "col1 NUMBER"
62+
+ ", col2 varchar(25)"
63+
+ ", col3 varchar(25)")
64+
.create()
65+
.insertValues("1, 'col2_val', 'col3_val'");
66+
67+
String tableName = table.getTableName();
68+
DatasetRow datasetRow =
69+
DatasetRow.ofTable(tableName)
70+
.addColumnValue("col1", 1)
71+
.addColumnValue("col2", "col2_val")
72+
.addColumnValue("col3", "col3_val");
73+
74+
//WHEN
75+
SqlTestDataGenerator sqlTestDataGenerator = SqlTestDataGenerator.buildFrom(DATA_SOURCE);
76+
77+
List<String> insertStatements = sqlTestDataGenerator.generateInsertListFor(datasetRow);
78+
79+
// THEN
80+
table.recreate();
81+
82+
SQL_EXECUTOR.execute(insertStatements);
83+
assertThat(table)
84+
.withGeneratedInserts(insertStatements)
85+
.hasNumberOfRows(1)
86+
.row(0).hasValues(1, "col2_val", "col3_val");
87+
88+
}
89+
90+
}

src/test/java/org/stdg/test/TestTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ TestTable recreate() {
6565
}
6666

6767
TestTable drop() {
68-
sqlExecutor.execute("drop table if exists " + tableName);
68+
sqlExecutor.execute("drop table " + tableName);
6969
return this;
7070
}
7171

0 commit comments

Comments
 (0)