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 @@ -1639,6 +1639,16 @@ default boolean supportsUsers() {
return false;
}

/**
* @return true if {@code grantRole} may name a user where it names a role: the database grants a
* user to another user or role like a role, and the grantee holds the user's privileges.
* PostgreSQL does, where a user is a role that may log in; MySQL from 8.0 does, where
* users and roles are accounts alike. Most databases grant roles only.
*/
default boolean supportsUserAsRole() {
return false;
}

/** One table-rename step of a multi-rename. */
record TableRename(TableReference table, String newName) {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.consistencytest;

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

import java.util.stream.Stream;

import org.eclipse.daanse.sql.dialect.api.Dialect;
import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.db.clickhouse.ClickHouseDialect;
import org.eclipse.daanse.sql.dialect.db.common.AnsiDialect;
import org.eclipse.daanse.sql.dialect.db.derby.DerbyDialect;
import org.eclipse.daanse.sql.dialect.db.duckdb.DuckDbDialect;
import org.eclipse.daanse.sql.dialect.db.h2.H2Dialect;
import org.eclipse.daanse.sql.dialect.db.hive.HiveDialect;
import org.eclipse.daanse.sql.dialect.db.impala.ImpalaDialect;
import org.eclipse.daanse.sql.dialect.db.mariadb.MariaDBDialect;
import org.eclipse.daanse.sql.dialect.db.mssqlserver.MicrosoftSqlServerDialect;
import org.eclipse.daanse.sql.dialect.db.mysql.MySqlDialect;
import org.eclipse.daanse.sql.dialect.db.oracle.OracleDialect;
import org.eclipse.daanse.sql.dialect.db.postgresql.PostgreSqlDialect;
import org.eclipse.daanse.sql.dialect.db.sqlite.SqliteDialect;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Which dialects grant a user like a role: PostgreSQL, where a user is a role that may log in, and MySQL from 8.0,
* where users and roles are accounts alike. The others grant roles only.
*/
class UserAsRoleTest {

static Stream<Arguments> dialects() {
return Stream.of(of("postgresql", new PostgreSqlDialect(), true),
of("mysql", new MySqlDialect(), true),
of("mysql 5.7", new MySqlDialect(version(5, 7)), false),
of("mariadb", new MariaDBDialect(), false),
of("oracle", new OracleDialect(), false),
of("mssql", new MicrosoftSqlServerDialect(), false),
of("h2", new H2Dialect(), false),
of("clickhouse", new ClickHouseDialect(), false),
of("derby", new DerbyDialect(), false),
of("hive", new HiveDialect(), false),
of("impala", new ImpalaDialect(), false),
of("ansi", new AnsiDialect(), false),
of("duckdb", new DuckDbDialect(), false),
of("sqlite", new SqliteDialect(), false));
}

@ParameterizedTest(name = "{0}")
@MethodSource("dialects")
void grantsAUserLikeARole(Dialect dialect, boolean expected) {
assertThat(dialect.ddlGenerator().supportsUserAsRole()).as("supportsUserAsRole").isEqualTo(expected);
}

private static Arguments of(String name, Dialect dialect, boolean expected) {
return Arguments.of(Named.of(name, dialect), expected);
}

private static DialectInitData version(int major, int minor) {
return DialectInitData.ansiDefaults().withVersion(major, minor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ public boolean supportsUsers() {
|| dialectVersion.atLeast(new org.eclipse.daanse.sql.dialect.api.DialectVersion(10, 4, 2));
}

/** MariaDB grants a role only, one made with {@code CREATE ROLE}; a user is no role there. */
@Override
public boolean supportsUserAsRole() {
return false;
}

/**
* MariaDB keeps a descending index column from 10.8; before, it took {@code DESC} and ignored it.
* A dialect made without a connection takes such a version.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.mariadb.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.SQLException;
import java.sql.Statement;

import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.db.mariadb.MariaDBDialect;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.testcontainers.containers.MariaDBContainer;

/**
* MariaDB grants a role only, one made with {@code CREATE ROLE}: a user named where a role stands is refused, unlike in
* MySQL. The dialect is built from a live connection.
*/
@EnabledIfSystemProperty(named = "integration.docker", matches = "true")
class MariaDBUserAsRoleTest {

@ParameterizedTest(name = "mariadb:{0}")
@CsvSource({ "10.6", "11.4" })
void aUserIsNoRole(String tag) throws Exception {
try (MariaDBContainer<?> db = new MariaDBContainer<>("mariadb:" + tag).withDatabaseName("rt")
.withUsername("rt").withPassword("rt")) {
db.start();
// root may create roles and users; the container's user may not
try (Connection c = DriverManager.getConnection(db.getJdbcUrl(), "root", db.getPassword());
Statement st = c.createStatement()) {
MariaDBDialect dialect = new MariaDBDialect(DialectInitData.fromConnection(c));

assertThat(dialect.supportsUserAsRole()).as("supportsUserAsRole").isFalse();

st.execute(dialect.createUser("rt_user", false).orElseThrow());
st.execute(dialect.createUser("rt_other", false).orElseThrow());
assertThatThrownBy(() -> st.execute(dialect.grantRole("rt_user", "rt_other", false)))
.as("a user granted like a role").isInstanceOf(SQLException.class);

st.execute(dialect.createRole("rt_role", false).orElseThrow());
st.execute(dialect.grantRole("rt_role", "rt_other", false));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1122,4 +1122,13 @@ protected boolean createOrReplaceRoutine() {
public boolean supportsRoles() {
return dialectVersion.isUnknownOrAtLeast(8, 0);
}

/**
* MySQL: from 8.0 users and roles are accounts alike, and {@code GRANT 'u' TO 'v'} grants a user
* like a role.
*/
@Override
public boolean supportsUserAsRole() {
return supportsRoles();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void roles(String tag, boolean expected) throws Exception {
MySqlDialect dialect = new MySqlDialect(DialectInitData.fromConnection(c));

assertThat(dialect.supportsRoles()).as("supportsRoles").isEqualTo(expected);
assertThat(dialect.supportsUserAsRole()).as("supportsUserAsRole").isEqualTo(expected);

if (!expected) {
assertThat(dialect.createRole("rt_role", false)).as("no role on 5.7").isEmpty();
Expand Down Expand Up @@ -90,6 +91,18 @@ void roles(String tag, boolean expected) throws Exception {
assertThat(String.join(" ", rows(st, "SHOW GRANTS FOR rt_user"))).as("with the admin option")
.contains("GRANT `rt_role`@`%` TO `rt_user`@`%` WITH ADMIN OPTION");

// a user granted like a role: the grantee holds the user's privileges
st.execute(dialect.grantTablePrivilege("INSERT", ITEMS, "rt_user", false));
st.execute(dialect.createUser("rt_other", false).orElseThrow());
st.execute(dialect.grantRole("rt_user", "rt_other", false));
assertThat(String.join(" ", rows(st, "SHOW GRANTS FOR rt_other"))).as("the user granted like a role")
.contains("GRANT `rt_user`@`%` TO `rt_other`@`%`");
// MySQL folds what the user holds itself and through its role into one line: SELECT, INSERT
assertThat(String.join(" ", rows(st, "SHOW GRANTS FOR rt_other USING rt_user")))
.as("the grantee holds the user's privilege")
.contains("INSERT ON `rt`.`items` TO `rt_other`@`%`");
st.execute(dialect.dropUser("rt_other", false).orElseThrow());

st.execute(dialect.dropRole("rt_role", false).orElseThrow());
assertThat(rows(st, "SELECT user FROM mysql.user WHERE user = 'rt_role'")).as("dropped").isEmpty();
assertThat(String.join(" ", rows(st, "SHOW GRANTS FOR rt_user"))).as("the grant gone with the role")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,12 @@ public boolean supportsUsers() {
return true;
}

/** PostgreSQL: a user is a role that may log in, and a role is granted to another as any role is. */
@Override
public boolean supportsUserAsRole() {
return true;
}

/**
* PostgreSQL has no {@code CREATE USER IF NOT EXISTS}: with {@code ifNotExists} the statement is a
* {@code DO} block that takes an existing role of the name as done.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.postgresql.container;

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

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Optional;

import org.eclipse.daanse.sql.dialect.api.DialectInitData;
import org.eclipse.daanse.sql.dialect.db.postgresql.PostgreSqlDialect;
import org.eclipse.daanse.sql.model.schema.SchemaReference;
import org.eclipse.daanse.sql.model.schema.TableReference;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.testcontainers.containers.PostgreSQLContainer;

/**
* PostgreSQL grants a user like a role: a user is a role that may log in, and {@code GRANT "a" TO "b"} makes b hold
* what a holds, its own privileges among it. The dialect is built from a live connection.
*/
@EnabledIfSystemProperty(named = "integration.docker", matches = "true")
class PostgreSqlUserAsRoleTest {

private static final TableReference ITEMS = new TableReference(
Optional.of(new SchemaReference(Optional.empty(), "public")), "items", TableReference.TYPE_TABLE);

@ParameterizedTest(name = "postgres:{0}")
@CsvSource({ "12-alpine", "16-alpine" })
void aUserGrantedLikeARole(String tag) throws Exception {
try (PostgreSQLContainer<?> pg = new PostgreSQLContainer<>("postgres:" + tag)) {
pg.start();
try (Connection c = pg.createConnection(""); Statement st = c.createStatement()) {
st.execute("CREATE TABLE items(id INT)");
PostgreSqlDialect dialect = new PostgreSqlDialect(DialectInitData.fromConnection(c));

assertThat(dialect.supportsUserAsRole()).as("supportsUserAsRole").isTrue();

st.execute(dialect.createUser("rt_user", false).orElseThrow());
st.execute(dialect.createUser("rt_other", false).orElseThrow());
st.execute(dialect.grantTablePrivilege("SELECT", ITEMS, "rt_user", false));
assertThat(holds(st, "rt_other")).as("before the grant").isFalse();

st.execute(dialect.grantRole("rt_user", "rt_other", false));
assertThat(holds(st, "rt_other")).as("the grantee holds the user's privilege").isTrue();
assertThat(holds(st, "rt_user")).as("the user keeps its own").isTrue();

st.execute(dialect.revokeRole("rt_user", "rt_other"));
assertThat(holds(st, "rt_other")).as("revoked").isFalse();
}
}
}

private static boolean holds(Statement st, String role) throws SQLException {
try (ResultSet rs = st.executeQuery("SELECT has_table_privilege('" + role + "', 'public.items', 'SELECT')")) {
rs.next();
return rs.getBoolean(1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1717,55 +1717,64 @@ private static boolean isNumericJdbc(java.sql.JDBCType t) {
@Override
public Optional<List<org.eclipse.daanse.sql.jdbc.api.schema.RoleMembership>> getAllRoleMemberships(Connection connection)
throws SQLException {
// DBA_ROLE_PRIVS; fallback: own memberships via USER_ROLE_PRIVS.
// With plain read rights: the user's own memberships (USER_ROLE_PRIVS, those of PUBLIC among
// them) and those of the roles it holds, directly or through other roles (ROLE_ROLE_PRIVS).
List<org.eclipse.daanse.sql.jdbc.api.schema.RoleMembership> result = new ArrayList<>();
try (PreparedStatement ps = connection.prepareStatement(
"SELECT GRANTEE, GRANTED_ROLE, ADMIN_OPTION FROM DBA_ROLE_PRIVS ORDER BY GRANTEE, GRANTED_ROLE");
try (PreparedStatement ps = connection.prepareStatement("""
SELECT USERNAME AS GRANTEE, GRANTED_ROLE, ADMIN_OPTION FROM USER_ROLE_PRIVS
UNION
SELECT ROLE AS GRANTEE, GRANTED_ROLE, ADMIN_OPTION FROM ROLE_ROLE_PRIVS
ORDER BY 1, 2""");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
result.add(new org.eclipse.daanse.sql.jdbc.record.schema.RoleMembershipRecord(
rs.getString("GRANTEE"), rs.getString("GRANTED_ROLE"),
Optional.empty(), Optional.ofNullable(rs.getString("ADMIN_OPTION"))));
}
return Optional.of(List.copyOf(result));
} catch (SQLException e) {
result.clear();
}
try (PreparedStatement ps = connection.prepareStatement(
"SELECT USERNAME, GRANTED_ROLE, ADMIN_OPTION FROM USER_ROLE_PRIVS ORDER BY GRANTED_ROLE");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
result.add(new org.eclipse.daanse.sql.jdbc.record.schema.RoleMembershipRecord(
rs.getString("USERNAME"), rs.getString("GRANTED_ROLE"),
Optional.empty(), Optional.ofNullable(rs.getString("ADMIN_OPTION"))));
}
}
return Optional.of(List.copyOf(result));
}

@Override
public Optional<List<org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal>> getAllPrincipals(Connection connection)
throws SQLException {
// DBA_USERS/DBA_ROLES need elevated rights; without them: empty.
// With plain read rights: ALL_USERS names every user to every user; the roles are those the
// user holds, directly or through other roles.
List<org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal> result = new ArrayList<>();
try (PreparedStatement ps = connection.prepareStatement(
"SELECT USERNAME FROM DBA_USERS ORDER BY USERNAME");
"SELECT USERNAME FROM ALL_USERS ORDER BY USERNAME");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
result.add(new org.eclipse.daanse.sql.jdbc.record.schema.DatabasePrincipalRecord(rs.getString("USERNAME"), org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal.KIND_USER));
}
} catch (SQLException e) {
return Optional.empty();
}
try (PreparedStatement ps = connection.prepareStatement(
"SELECT ROLE FROM DBA_ROLES ORDER BY ROLE");
for (String role : roleNames(connection)) {
result.add(new org.eclipse.daanse.sql.jdbc.record.schema.DatabasePrincipalRecord(role,
org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal.KIND_ROLE));
}
return Optional.of(List.copyOf(result));
}

/**
* The roles the current user holds: granted to it or to PUBLIC (USER_ROLE_PRIVS), and those
* granted to these roles in turn (ROLE_ROLE_PRIVS, which shows the roles the user has access to).
*/
private static List<String> roleNames(Connection connection) throws SQLException {
List<String> roles = new ArrayList<>();
try (PreparedStatement ps = connection.prepareStatement("""
SELECT GRANTED_ROLE AS ROLE FROM USER_ROLE_PRIVS
UNION
SELECT ROLE FROM ROLE_ROLE_PRIVS
UNION
SELECT GRANTED_ROLE AS ROLE FROM ROLE_ROLE_PRIVS
ORDER BY 1""");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
result.add(new org.eclipse.daanse.sql.jdbc.record.schema.DatabasePrincipalRecord(rs.getString("ROLE"), org.eclipse.daanse.sql.jdbc.api.schema.DatabasePrincipal.KIND_ROLE));
roles.add(rs.getString("ROLE"));
}
} catch (SQLException e) {
return Optional.empty();
}
return Optional.of(List.copyOf(result));
return roles;
}
}
Loading
Loading