Skip to content

Commit

Permalink
oralce12 module moved back
Browse files Browse the repository at this point in the history
Closes #2660
  • Loading branch information
stoyants authored and stevenschlansker committed May 20, 2024
1 parent c9b3b8f commit 260422e
Show file tree
Hide file tree
Showing 12 changed files with 858 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/versions-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,32 @@ jobs:
MAVEN_CONFIG: "-B -fae -pl :jdbi3-spring5 -Ddep.spring.version=${{ matrix.test-version }}"
run: |
make run-tests
oracle:
name: Test Oracle versions
runs-on: ubuntu-latest

strategy:
matrix:
test-version: [ '21-slim-faststart', '18-slim-faststart' ]

steps:
- uses: actions/checkout@v4

- uses: actions/setup-java@v4
with:
java-version: 21
distribution: temurin
cache: maven

- name: Build
env:
MAVEN_CONFIG: "-B -fae"
run: |
make install-fast
- name: Test
env:
MAVEN_CONFIG: "-B -fae -Doracle.container.version=${{ matrix.test-version }}"
run: |
make run-slow-tests
7 changes: 7 additions & 0 deletions internal/build/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
<dep.moshi.version>1.15.0</dep.moshi.version>
<dep.mssql.version>12.4.2.jre11</dep.mssql.version>
<dep.mysql.version>8.2.0</dep.mysql.version>
<dep.oracle-xe.version>23.3.0.23.09</dep.oracle-xe.version>
<dep.otj-pg-embedded.version>1.0.2</dep.otj-pg-embedded.version>
<dep.pg-embedded.version>5.1.0</dep.pg-embedded.version>
<dep.plugin.asciidoctor.version>2.2.4</dep.plugin.asciidoctor.version>
Expand Down Expand Up @@ -313,6 +314,12 @@
<version>${dep.postgresql.version}</version>
</dependency>

<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>${dep.oracle-xe.version}</version>
</dependency>

<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
Expand Down
110 changes: 110 additions & 0 deletions oracle12/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jdbi.internal</groupId>
<artifactId>jdbi3-parent</artifactId>
<version>3.45.2-SNAPSHOT</version>
</parent>

<groupId>org.jdbi</groupId>
<artifactId>jdbi3-oracle12</artifactId>

<name>jdbi3 oracle12</name>
<description>Support for Oracle 12+ specific features.</description>

<properties>
<moduleName>org.jdbi.v3.oracle12</moduleName>
</properties>

<dependencies>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-core</artifactId>
</dependency>

<!-- https://www.oracle.com/database/technologies/maven-central-guide.html -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-testing</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oracle-xe</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>jdbc</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-core</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-sqlobject</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-testcontainers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jdbi.v3.oracle12;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import oracle.jdbc.pool.OracleDataSource;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.testing.JdbiRule;
import org.testcontainers.containers.OracleContainer;

/**
* JUnit 4 Helper for a database managed by testcontainers.
*/
public class OracleDatabaseRule extends JdbiRule {

private final String uri;
private final String username;
private final String password;

public OracleDatabaseRule(OracleContainer oracleContainer) {
this.uri = oracleContainer.getJdbcUrl();
this.username = oracleContainer.getUsername();
this.password = oracleContainer.getPassword();
}

@Override
protected DataSource createDataSource() {
try {
OracleDataSource dataSource = new OracleDataSource();
dataSource.setURL(uri);
dataSource.setUser(username);
dataSource.setPassword(password);

return dataSource;
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}

public static void createTables(OracleContainer oracleContainer) throws SQLException {
try (Connection connection = oracleContainer.createConnection("")) {
Jdbi jdbi = Jdbi.create(connection);

jdbi.withHandle(handle -> {
handle.execute("create sequence something_id_sequence INCREMENT BY 1 START WITH 100");
handle.execute("create table something (name varchar(200), id int, constraint something_id primary key (id))");
return null;
});
}
}
}
145 changes: 145 additions & 0 deletions oracle12/src/main/java/org/jdbi/v3/oracle12/OracleReturning.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jdbi.v3.oracle12;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import oracle.jdbc.OraclePreparedStatement;
import org.jdbi.v3.core.argument.Argument;
import org.jdbi.v3.core.result.ResultBearing;
import org.jdbi.v3.core.result.ResultProducer;
import org.jdbi.v3.core.result.ResultProducers;
import org.jdbi.v3.core.statement.Binding;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.core.statement.StatementCustomizer;
import org.jdbi.v3.meta.Beta;

/**
* Returns a {@link ResultBearing} from Oracle's "DML Returning" features introduced in 10.2. To use,
* add a {@link #returnParameters()} customizer to the statement and register with one or more return parameters. Then
* execute the statement with {@link #returningDml()} result producer:
* <p>
* <pre>
* List&lt;Integer&gt; ids = handle.createUpdate("insert into something (id, name) values (17, 'Brian') returning id into ?")
* .addCustomizer(OracleReturning.returnParameters().register(0, OracleTypes.INTEGER))
* .execute(OracleReturning.returningDml())
* .mapTo(int.class)
* .list();
*
* assertThat(ids).containsExactly(17);
* </pre>
* <p>
* This class still is beta, and may be changed incompatibly or removed at any time.
*/
@Beta
public class OracleReturning {

private OracleReturning() {}

public static ReturnParameters returnParameters() {
return new ReturnParameters();
}

/**
* Result producer that returns a {@link ResultBearing} over the statement "DML returning" parameters. Used in
* conjunction with {@link #returnParameters()} to register return parameters.
*
* @return ResultBearing of returned columns.
* @see OraclePreparedStatement#getReturnResultSet()
*/
public static ResultProducer<ResultBearing> returningDml() {
return (supplier, ctx) -> ResultProducers.createResultBearing(supplier, statement -> unwrapOracleStatement(statement).getReturnResultSet(), ctx);
}

private static OraclePreparedStatement unwrapOracleStatement(Statement stmt) throws SQLException {
if (!stmt.isWrapperFor(OraclePreparedStatement.class)) {
throw new IllegalStateException("Statement is not an instance of, nor a wrapper of, OraclePreparedStatement");
}
return stmt.unwrap(OraclePreparedStatement.class);
}

static class ReturnParam implements Argument {

private final String name;
private final int index;
private final int oracleType;

ReturnParam(String name, int oracleType) {
this.name = name;
this.index = -1;
this.oracleType = oracleType;
}

ReturnParam(int index, int oracleType) {
this.name = null;
this.index = index;
this.oracleType = oracleType;
}

@Override
public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException {
unwrapOracleStatement(statement).registerReturnParameter(position, oracleType);
}

void bind(Binding binding) {
if (name == null) {
binding.addPositional(index, this);
} else {
binding.addNamed(name, this);
}
}
}

public static class ReturnParameters implements StatementCustomizer {

private final List<ReturnParam> returnParams = new ArrayList<>();

ReturnParameters() {}

@Override
public void beforeBinding(PreparedStatement stmt, StatementContext ctx) throws SQLException {
for (ReturnParam returnParam : returnParams) {
returnParam.bind(ctx.getBinding());
}
}

/**
* Registers a return parameter on the Oracle prepared statement.
*
* @param index 0-based index of the return parameter
* @param oracleType one of the values from {@link oracle.jdbc.OracleTypes}
* @return The same instance, for method chaining
*/
public ReturnParameters register(int index, int oracleType) {
returnParams.add(new ReturnParam(index, oracleType));
return this;
}

/**
* Registers a return parameter on the Oracle prepared statement.
*
* @param name name of the return parameter
* @param oracleType one of the values from {@link oracle.jdbc.OracleTypes}
* @return The same instance, for method chaining
*/
public ReturnParameters register(String name, int oracleType) {
returnParams.add(new ReturnParam(name, oracleType));
return this;
}
}
}
19 changes: 19 additions & 0 deletions oracle12/src/main/java/org/jdbi/v3/oracle12/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>
* The <code>oracle</code> package provides integration with the Oracle JDBC driver.
* </p>
*/
package org.jdbi.v3.oracle12;

0 comments on commit 260422e

Please sign in to comment.