Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,48 @@
import java.util.List;
import java.util.Optional;

/**
* The clause that brings back what an {@code INSERT}, an {@code UPDATE} or a {@code DELETE} wrote, in one of two forms:
* <ul>
* <li>{@link #returning(List)}: the statement becomes a query, its rows read with {@code executeQuery}
* ({@code RETURNING} of PostgreSQL, MariaDB and SQLite, {@code OUTPUT} of SQL Server);</li>
* <li>{@link #returningInto(List)}: the values go into out parameters, one per column, bound after the statement's own
* parameters ({@code RETURNING … INTO} of Oracle).</li>
* </ul>
* A dialect has either, both or none; each is asked for on its own.
*/
public interface ReturningGenerator {

/**
* The clause that makes the statement return the given columns as rows, read with {@code executeQuery}.
*
* @return empty where the dialect has no such clause ({@link #supportsReturning()} is false), and for no columns
*/
default Optional<String> returning(List<String> columns) {
return Optional.empty();
}

/** Whether this dialect supports any form of RETURNING/OUTPUT clause. */
/** Whether {@link #returning(List)} writes a clause: the statement returns its rows. */
default boolean supportsReturning() {
return false;
}

/**
* {@code RETURNING c1, c2 INTO ?, ?}: the clause that puts the given columns of the rows the statement wrote into
* out parameters, one {@code ?} per column, in the order of the columns, after the statement's own parameters. The
* statement is executed with {@code executeUpdate}, and the values are read as the driver serves out parameters of
* a DML statement (Oracle: {@code OraclePreparedStatement.registerReturnParameter} and
* {@code getReturnResultSet}, a row per row written).
*
* @return empty where the dialect has no such clause ({@link #supportsReturningInto()} is false), for no columns,
* and for {@code *}, which names no column a parameter can be bound to
*/
default Optional<String> returningInto(List<String> columns) {
return Optional.empty();
}

/** Whether {@link #returningInto(List)} writes a clause: the statement returns into out parameters. */
default boolean supportsReturningInto() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.eclipse.daanse.sql.model.schema.Trigger.TriggerTiming;
import org.eclipse.daanse.sql.model.sql.BitOperation;
import org.eclipse.daanse.sql.dialect.api.generator.KnownFunction;
import org.eclipse.daanse.sql.dialect.api.generator.ReturningGenerator;
import org.eclipse.daanse.sql.dialect.api.generator.StatementHint;
import org.eclipse.daanse.sql.model.sql.OrderedColumn;
import org.eclipse.daanse.sql.model.type.BestFitColumnType;
Expand Down Expand Up @@ -126,7 +127,7 @@ public String binaryTypeName(java.util.OptionalInt size) {
}

private volatile org.eclipse.daanse.sql.dialect.api.generator.MergeGenerator cachedMergeGenerator;
private volatile org.eclipse.daanse.sql.dialect.api.generator.ReturningGenerator cachedReturningGenerator;
private volatile ReturningGenerator cachedReturningGenerator;

/** JDBC-free constructor for SQL generation. */
public OracleDialect() {
Expand Down Expand Up @@ -210,39 +211,31 @@ public java.util.Optional<String> upsert(UpsertSpec spec, java.util.List<String>
return local;
}

/**
* Oracle writes {@code RETURNING} only with {@code INTO}, its values bound to out parameters, in every version, 23
* too: {@link ReturningGenerator#returningInto(List) RETURNING c1, c2 INTO ?, ?}. A {@code RETURNING} clause alone,
* which a query reads back as rows, fails with ORA-00925, so {@link ReturningGenerator#returning(List)} writes
* none. {@code *} names no column a parameter can be bound to, and is not written either.
*/
@Override
public org.eclipse.daanse.sql.dialect.api.generator.ReturningGenerator returningGenerator() {
public ReturningGenerator returningGenerator() {
var local = cachedReturningGenerator;
if (local != null)
return local;
if (!dialectVersion.isUnknownOrAtLeast(23, 0)) {
local = super.returningGenerator();
cachedReturningGenerator = local;
return local;
}
local = new org.eclipse.daanse.sql.dialect.api.generator.ReturningGenerator() {
local = new ReturningGenerator() {
@Override
public boolean supportsReturning() {
public boolean supportsReturningInto() {
return true;
}

@Override
public java.util.Optional<String> returning(java.util.List<String> columns) {
if (columns == null || columns.isEmpty())
return java.util.Optional.empty();
if (columns.size() == 1 && "*".equals(columns.get(0))) {
// Oracle 23c does NOT support RETURNING * — only explicit columns.
return java.util.Optional.empty();
public Optional<String> returningInto(List<String> columns) {
if (columns == null || columns.isEmpty() || columns.contains("*")) {
return Optional.empty();
}
StringBuilder sb = new StringBuilder(" RETURNING ");
boolean first = true;
for (String c : columns) {
if (!first)
sb.append(", ");
sb.append(quoteIdentifier(c));
first = false;
}
return java.util.Optional.of(sb.toString());
return Optional.of(" RETURNING " + columns.stream().map(c -> quoteIdentifier(c))
.collect(Collectors.joining(", ")) + " INTO "
+ columns.stream().map(c -> "?").collect(Collectors.joining(", ")));
}
};
cachedReturningGenerator = local;
Expand All @@ -254,27 +247,34 @@ public boolean supportsDropTableCascade() {
return false;
}

/** Oracle: no {@code IF NOT EXISTS} on any DDL. */
/**
* {@code CREATE TABLE IF NOT EXISTS} from Oracle 23 (23ai); before, the server refuses it with ORA-00922. A dialect
* made without a connection knows no version and says no.
*/
@Override
public boolean supportsCreateTableIfNotExists() {
return false;
return dialectVersion.atLeast(23, 0);
}

/** {@code CREATE INDEX IF NOT EXISTS} from Oracle 23, as {@link #supportsCreateTableIfNotExists()}. */
@Override
public boolean supportsCreateIndexIfNotExists() {
return false;
return dialectVersion.atLeast(23, 0);
}

/** {@code DROP INDEX IF EXISTS} from Oracle 23, as {@link #supportsCreateTableIfNotExists()}. */
@Override
public boolean supportsDropIndexIfExists() {
return false;
return dialectVersion.atLeast(23, 0);
}

/** {@code DROP VIEW IF EXISTS} from Oracle 23, as {@link #supportsCreateTableIfNotExists()}. */
@Override
public boolean supportsDropViewIfExists() {
return false;
return dialectVersion.atLeast(23, 0);
}

/** No version of Oracle, 23 neither, drops a constraint if it exists: ORA-01735. */
@Override
public boolean supportsDropConstraintIfExists() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,8 @@ void merge_into_using_dual_inserts_then_updates() throws SQLException {
}

@Test
void returning_unsupported_on_pre_23c_oracle() {
// Oracle XE 21 reports major < 23 → generator falls through to empty default.
// (PG-style RETURNING was introduced in 23c, Aug 2023.)
void returning_unsupported() {
// Oracle writes RETURNING only with INTO, in every version: the generator writes no clause.
assertThat(dialect.returningGenerator().returning(List.of("id"))).isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.daanse.sql.dialect.db.oracle.container;

import java.time.Duration;

import org.testcontainers.containers.JdbcDatabaseContainer;

/**
* A container of an Oracle image: Oracle XE (11, 18, 21) and Oracle Free (23) are served by two Testcontainers modules.
* The password of the container's user is that of SYS and SYSTEM too. Oracle 11g has no pluggable database: it is
* reached by its SID, XE.
*/
final class OracleContainers {

private OracleContainers() {
}

@SuppressWarnings("resource")
static JdbcDatabaseContainer<?> of(String image) {
if (image.contains("oracle-free")) {
return new org.testcontainers.oracle.OracleContainer(image).withStartupTimeout(Duration.ofMinutes(5));
}
org.testcontainers.containers.OracleContainer xe = new org.testcontainers.containers.OracleContainer(image)
.withUsername("rt").withPassword("rt");
if (image.contains("oracle-xe:11")) {
xe = xe.usingSid();
}
return xe.withStartupTimeout(Duration.ofMinutes(5));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.daanse.sql.dialect.db.oracle.container;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.api.generator.DdlGenerator.RoutineDefinition;
import org.eclipse.daanse.sql.dialect.api.generator.DdlGenerator.RoutineParameter;
import org.eclipse.daanse.sql.dialect.api.generator.DdlGenerator.RoutineSignature;
import org.eclipse.daanse.sql.dialect.db.oracle.OracleDialect;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.testcontainers.containers.JdbcDatabaseContainer;

/**
* Oracle drops a function only if it exists, {@code DROP FUNCTION IF EXISTS}, from 23 (23ai) on. The dialect is built
* from a live connection: on every version the function it creates is called and named by its name alone, for a schema
* holds one routine of a name; from 23 on it writes IF EXISTS, the function it drops is gone and the drop of a missing
* one passes; before, on 18 and 21, it writes a plain DROP FUNCTION, for the server refuses IF EXISTS, and the drop of
* a missing function fails with ORA-04043.
*/
@EnabledIfSystemProperty(named = "integration.docker", matches = "true")
class OracleDropFunctionIfExistsTest {

@ParameterizedTest(name = "{0} -> DROP FUNCTION IF EXISTS {1}")
@CsvSource({ "gvenzl/oracle-xe:18-slim-faststart, false", "gvenzl/oracle-xe:21-slim-faststart, false",
"gvenzl/oracle-free:23.9-slim-faststart, true",
"gvenzl/oracle-free:23.26.4-slim-faststart, true" })
void dropFunctionIfExists(String image, boolean expected) throws Exception {
try (JdbcDatabaseContainer<?> ora = OracleContainers.of(image)) {
ora.start();
try (Connection c = DriverManager.getConnection(ora.getJdbcUrl(), ora.getUsername(), ora.getPassword());
Statement st = c.createStatement()) {
String schema = ora.getUsername().toUpperCase();
OracleDialect dialect = new OracleDialect(DialectInitData.fromConnection(c));

st.execute(dialect.createFunction(new RoutineDefinition(schema, "TWICE",
List.of(RoutineParameter.in("X", "NUMBER")), "NUMBER", "plsql",
"BEGIN RETURN \"X\" * 2; END;"), false).orElseThrow());
assertThat(exists(st)).as("created").isTrue();
assertThat(call(st)).as("called").isEqualTo(42);

// the types of its parameters are not written: a schema holds one function of a name
RoutineSignature twice = new RoutineSignature(schema, "TWICE", List.of("NUMBER"));
String drop = dialect.dropFunction(twice, true).orElseThrow();

if (!expected) {
assertThat(drop).as("a plain drop before 23")
.isEqualTo("DROP FUNCTION \"" + schema + "\".\"TWICE\"");
assertThatThrownBy(() -> st.execute("DROP FUNCTION IF EXISTS TWICE"))
.as("IF EXISTS is refused before 23").isInstanceOf(SQLException.class);
assertThat(exists(st)).as("the refused drop leaves the function").isTrue();
st.execute(drop);
assertThat(exists(st)).as("dropped").isFalse();
assertThatThrownBy(() -> st.execute(drop)).as("a missing one")
.isInstanceOf(SQLException.class).hasMessageContaining("ORA-04043");
return;
}

assertThat(drop).as("IF EXISTS from 23")
.isEqualTo("DROP FUNCTION IF EXISTS \"" + schema + "\".\"TWICE\"");
st.execute(drop);
assertThat(exists(st)).as("dropped").isFalse();
assertThatThrownBy(() -> call(st)).as("no longer called").isInstanceOf(SQLException.class);
st.execute(drop);
assertThatThrownBy(() -> st.execute(dialect.dropFunction(twice, false).orElseThrow()))
.as("a missing one without IF EXISTS").isInstanceOf(SQLException.class)
.hasMessageContaining("ORA-04043");
}
}
}

private static int call(Statement st) throws SQLException {
try (ResultSet rs = st.executeQuery("SELECT TWICE(21) FROM DUAL")) {
rs.next();
return rs.getInt(1);
}
}

private static boolean exists(Statement st) throws SQLException {
try (ResultSet rs = st.executeQuery(
"SELECT COUNT(*) FROM USER_OBJECTS WHERE OBJECT_TYPE = 'FUNCTION' AND OBJECT_NAME = 'TWICE'")) {
rs.next();
return rs.getInt(1) > 0;
}
}
}
Loading
Loading