Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,64 @@ default String foreignKeyClause(String constraintName, List<String> fkColumns, T
return sb.toString();
}

/**
* When a constraint is checked: at once, after each statement ({@link #NOT_DEFERRABLE}); or at
* the end of the transaction, where a transaction may defer it
* ({@code SET CONSTRAINTS … DEFERRED}) and by default does not ({@link #INITIALLY_IMMEDIATE})
* or does ({@link #INITIALLY_DEFERRED}).
*/
enum Deferrability {
NOT_DEFERRABLE, INITIALLY_IMMEDIATE, INITIALLY_DEFERRED
}

/**
* One foreign key as a table constraint, as
* {@link #foreignKeyClause(String, List, TableReference, List, String, String)} writes it, with
* {@code DEFERRABLE INITIALLY IMMEDIATE} or {@code DEFERRABLE INITIALLY DEFERRED} after it for a
* key that may be checked at the end of the transaction.
*
* @return empty for a deferrable key where the database checks every key at once
* ({@link #supportsDeferrableForeignKeys()} is false)
*/
default Optional<String> foreignKeyClause(String constraintName, List<String> fkColumns,
TableReference referencedTable, List<String> referencedColumns, String onDelete, String onUpdate,
Deferrability deferrability) {
String clause = foreignKeyClause(constraintName, fkColumns, referencedTable, referencedColumns, onDelete,
onUpdate);
if (deferrability == null || deferrability == Deferrability.NOT_DEFERRABLE) {
return Optional.of(clause);
}
if (!supportsDeferrableForeignKeys()) {
return Optional.empty();
}
return Optional.of(clause + (deferrability == Deferrability.INITIALLY_DEFERRED
? " DEFERRABLE INITIALLY DEFERRED"
: " DEFERRABLE INITIALLY IMMEDIATE"));
}

/**
* {@code ALTER TABLE schema.table ADD} the foreign key
* {@link #foreignKeyClause(String, List, TableReference, List, String, String, Deferrability)}
* writes.
*
* @return empty where that clause is, and where a foreign key cannot be added to an existing table
* ({@link #supportsAddForeignKeyConstraint()} is false)
*/
default Optional<String> addForeignKeyConstraint(TableReference table, String constraintName,
List<String> fkColumns, TableReference referencedTable, List<String> referencedColumns, String onDelete,
String onUpdate, Deferrability deferrability) {
if (!supportsAddForeignKeyConstraint()) {
return Optional.empty();
}
return foreignKeyClause(constraintName, fkColumns, referencedTable, referencedColumns, onDelete, onUpdate,
deferrability).map(clause -> "ALTER TABLE " + qualified(table) + " ADD " + clause);
}

/** @return true if a foreign key may be checked at the end of the transaction ({@code DEFERRABLE}) */
default boolean supportsDeferrableForeignKeys() {
return false;
}

/** {@code ALTER TABLE schema.table ADD CONSTRAINT name CHECK (expression)}. */
default String addCheckConstraint(TableReference table, String constraintName, String expression) {
return "ALTER TABLE " + qualified(table) + " ADD " + checkConstraintClause(constraintName, expression);
Expand Down Expand Up @@ -933,16 +991,136 @@ default Optional<String> dropProcedure(String procedureName, String schemaName,
return Optional.empty();
}

// -------------------- DDL — functions and procedures --------------------

/** How a parameter of a routine passes its value: into the routine, out of it, or both. */
enum ParameterMode {
IN, OUT, INOUT
}

/**
* A parameter of a routine: its name, how it passes its value ({@code IN} where none is given)
* and its type, written as given.
*/
record RoutineParameter(String name, ParameterMode mode, String type) {

public RoutineParameter {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("parameter name must not be blank");
}
if (type == null || type.isBlank()) {
throw new IllegalArgumentException("parameter type must not be blank");
}
mode = mode == null ? ParameterMode.IN : mode;
}

/** An {@code IN} parameter. */
public static RoutineParameter in(String name, String type) {
return new RoutineParameter(name, ParameterMode.IN, type);
}
}

/**
* A function or procedure to create: its schema ({@code null} for none), its name, its parameters
* in order, the type a function returns ({@code null} for a procedure, and for a function whose
* {@code OUT} parameters make its result where the database derives the result from them), the
* language of its body, and the body. The body is always the database's own and is written
* unchanged.
*/
record RoutineDefinition(String schemaName, String name, List<RoutineParameter> parameters, String returnType,
String language, String body) {

public RoutineDefinition {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("routine name must not be blank");
}
if (body == null || body.isBlank()) {
throw new IllegalArgumentException("routine body must not be blank");
}
parameters = parameters == null ? List.of() : List.copyOf(parameters);
}
}

/**
* A routine to drop: its schema ({@code null} for none), its name and, to name one of several
* routines of that name (overloads), the types of its parameters in order; {@code null} names it
* by its name alone.
*/
record RoutineSignature(String schemaName, String name, List<String> parameterTypes) {

public RoutineSignature {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("routine name must not be blank");
}
parameterTypes = parameterTypes == null ? null : List.copyOf(parameterTypes);
}
}

/**
* The languages this dialect writes the body of a routine in, in lower case ({@code sql},
* {@code plpgsql}, {@code plsql}, {@code tsql}); empty where it writes no routines. A routine whose
* body is in another language is not written.
*/
default java.util.Set<String> routineLanguages() {
return java.util.Set.of();
}

/**
* {@code CREATE [OR REPLACE] FUNCTION schema.name (parameters) RETURNS type … body}: one
* statement, without a client's delimiter. With {@code orReplace} an existing function of that
* signature is replaced where the dialect can say so; elsewhere the statement fails on it.
*
* @return empty where the dialect writes no function in the definition's language
* ({@link #routineLanguages()}), or cannot write one of its parameters' modes
*/
default Optional<String> createFunction(RoutineDefinition function, boolean orReplace) {
return Optional.empty();
}

/**
* {@code CREATE [OR REPLACE] PROCEDURE schema.name (parameters) … body}, as
* {@link #createFunction} writes a function; the definition's return type is not used.
*
* @return empty where the dialect writes no procedure in the definition's language
* ({@link #routineLanguages()}), or cannot write one of its parameters' modes
*/
default Optional<String> createProcedure(RoutineDefinition procedure, boolean orReplace) {
return Optional.empty();
}

/**
* {@code DROP FUNCTION [IF EXISTS] schema.name [(types)]}: the types name one overload where the
* database has overloads. With {@code ifExists} a missing function is no error where the dialect
* can say so.
*
* @return empty where the dialect writes no routines
*/
default Optional<String> dropFunction(RoutineSignature function, boolean ifExists) {
return Optional.empty();
}

/**
* {@code DROP PROCEDURE [IF EXISTS] schema.name [(types)]}, as {@link #dropFunction} drops a
* function.
*
* @return empty where the dialect writes no routines
*/
default Optional<String> dropProcedure(RoutineSignature procedure, boolean ifExists) {
return Optional.empty();
}

// -------------------- comments --------------------

/**
* {@code COMMENT ON TABLE schema.table IS '…'}; a {@code null} comment
* removes it ({@code IS NULL}).
* {@code COMMENT ON TABLE schema.table IS '…'}, and {@code COMMENT ON VIEW} for a view
* ({@link TableReference#TYPE_VIEW}); a {@code null} comment removes it ({@code IS NULL}).
*
* @return empty when the dialect cannot store table comments
* @return empty when the dialect cannot store table comments, and for a view where it keeps none
* on a view ({@link #supportsCommentOnView()})
*/
default Optional<String> commentOnTable(TableReference table, String comment) {
return Optional.of("COMMENT ON TABLE " + qualified(table) + " IS " + commentLiteral(comment));
String kind = TableReference.TYPE_VIEW.equals(table.type()) ? "VIEW " : "TABLE ";
return Optional.of("COMMENT ON " + kind + qualified(table) + " IS " + commentLiteral(comment));
}

/**
Expand All @@ -960,6 +1138,15 @@ default Optional<String> commentOnColumn(TableReference table, String columnName
+ commentLiteral(comment));
}

/**
* @return true if {@code commentOnTable} and {@code commentOnColumn} write a comment on a view
* ({@link TableReference#TYPE_VIEW}) and on a view's column; false where the database
* keeps none on a view, and both are empty for one
*/
default boolean supportsCommentOnView() {
return true;
}

/** ANSI string literal for a comment ({@code '} doubled), or {@code NULL}. */
private static String commentLiteral(String comment) {
return comment == null ? "NULL" : "'" + comment.replace("'", "''") + "'";
Expand Down Expand Up @@ -1207,16 +1394,52 @@ private static String schemaKey(TableReference tbl) {
// The grantor of a privilege is never rendered — it is implied by the
// session executing the statement.

/** {@code CREATE ROLE "x"}; a user is created by {@link #createUser(String, boolean)}. */
/**
* {@code CREATE ROLE "x"}, written whether or not the database has roles; a user is created by
* {@link #createUser(String, boolean)}. {@link #createRole(String, boolean)} says where the
* database has no roles.
*/
default String createRole(String roleName) {
return "CREATE ROLE " + quoteIdentifier(roleName);
}

/** {@code DROP ROLE "x"}. */
/** {@code DROP ROLE "x"}; {@link #dropRole(String, boolean)} says where the database has no roles. */
default String dropRole(String roleName) {
return "DROP ROLE " + quoteIdentifier(roleName);
}

/**
* {@code CREATE ROLE [IF NOT EXISTS] "x"}: a principal that holds privileges and is granted to
* users and other roles. With {@code ifNotExists} an existing role is no error where the dialect
* can say so; elsewhere the statement fails on it.
*
* @return empty where the database has no roles ({@link #supportsRoles()} is false)
*/
default Optional<String> createRole(String roleName, boolean ifNotExists) {
if (!supportsRoles()) {
return Optional.empty();
}
return Optional.of("CREATE ROLE " + (ifNotExists ? "IF NOT EXISTS " : "") + quoteIdentifier(roleName));
}

/**
* {@code DROP ROLE [IF EXISTS] "x"}. With {@code ifExists} a missing role is no error where the
* dialect can say so; elsewhere the statement fails on it.
*
* @return empty where the database has no roles ({@link #supportsRoles()} is false)
*/
default Optional<String> dropRole(String roleName, boolean ifExists) {
if (!supportsRoles()) {
return Optional.empty();
}
return Optional.of("DROP ROLE " + (ifExists ? "IF EXISTS " : "") + quoteIdentifier(roleName));
}

/** @return true if {@code createRole} / {@code dropRole} with a flag render statements */
default boolean supportsRoles() {
return true;
}

/**
* {@code CREATE USER [IF NOT EXISTS] "x"}: a principal that may log in and receives privileges
* and roles as a role does. The user is created without credentials, as the API takes none: it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,19 @@ public boolean supportsAtomicMultiRenameTable() {
/** {@code ALTER TABLE t MODIFY COMMENT '…'}; {@code null} clears it. */
@Override
public java.util.Optional<String> commentOnTable(TableReference table, String comment) {
if (TableReference.TYPE_VIEW.equals(table.type())) {
return java.util.Optional.empty();
}
return java.util.Optional.of("ALTER TABLE " + qualified(table) + " MODIFY COMMENT " + chLiteral(comment));
}

/** {@code ALTER TABLE t COMMENT COLUMN c '…'}; {@code null} clears it. */
@Override
public java.util.Optional<String> commentOnColumn(TableReference table, String columnName, String comment,
org.eclipse.daanse.sql.model.schema.ColumnMetaData currentMeta) {
if (TableReference.TYPE_VIEW.equals(table.type())) {
return java.util.Optional.empty();
}
return java.util.Optional.of("ALTER TABLE " + qualified(table) + " COMMENT COLUMN "
+ quoteIdentifier(columnName) + " " + chLiteral(comment));
}
Expand Down Expand Up @@ -340,4 +346,10 @@ public Optional<String> createUser(String userName, boolean ifNotExists) {
return Optional.of("CREATE USER " + (ifNotExists ? "IF NOT EXISTS " : "") + quoteIdentifier(userName)
+ " HOST NONE");
}

/** ClickHouse sets a comment with {@code ALTER TABLE … MODIFY COMMENT}, which takes no view. */
@Override
public boolean supportsCommentOnView() {
return false;
}
}
Loading
Loading