-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
858 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
66 changes: 66 additions & 0 deletions
66
oracle12/src/main/java/org/jdbi/v3/oracle12/OracleDatabaseRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
145
oracle12/src/main/java/org/jdbi/v3/oracle12/OracleReturning.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<Integer> 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
19
oracle12/src/main/java/org/jdbi/v3/oracle12/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.