Search in sources :

Example 31 with SQLDialect

use of org.jooq.SQLDialect in project jOOQ by jOOQ.

the class Alias method acceptDeclareAliasStandard.

private final void acceptDeclareAliasStandard(Context<?> context) {
    if (wrapped instanceof TableImpl)
        context.scopeMarkStart(wrapping);
    SQLDialect dialect = context.dialect();
    SQLDialect family = context.family();
    boolean emulatedDerivedColumnList = false;
    // Hence, wrap the table reference in a subselect
    if (fieldAliases != null && (SUPPORT_DERIVED_COLUMN_NAMES_SPECIAL1.contains(dialect)) && (wrapped instanceof TableImpl || wrapped instanceof CommonTableExpressionImpl)) {
        visitSubquery(context, select(asterisk()).from(((Table<?>) wrapped).as(alias)), true, false, false);
    } else // results using UNION ALL
    if (fieldAliases != null && (emulatedDerivedColumnList || SUPPORT_DERIVED_COLUMN_NAMES_SPECIAL2.contains(dialect))) {
        emulatedDerivedColumnList = true;
        if (wrapped instanceof Values && NO_SUPPORT_VALUES.contains(dialect)) {
            context.data(DATA_SELECT_ALIASES, fieldAliases, t -> toSQLWrapped(t));
        } else {
            // [#3156] Do not SELECT * from derived tables to prevent ambiguously defined columns
            // in those derived tables
            Select<?> wrappedAsSelect = wrapped instanceof Select ? (Select<?>) wrapped : wrapped instanceof DerivedTable ? ((DerivedTable<?>) wrapped).query() : select(asterisk()).from(((Table<?>) wrapped).as(alias));
            List<Field<?>> select = wrappedAsSelect.getSelect();
            if (emulatedDerivedColumnList) {
                SelectFieldList<Field<?>> fields = new SelectFieldList<>();
                for (int i = 0; i < fieldAliases.length; i++) {
                    switch(family) {
                        default:
                            fields.add(field("null").as(fieldAliases[i]));
                            break;
                    }
                }
                visitSubquery(context, select(fields).where(falseCondition()).unionAll(wrappedAsSelect), true, false, false);
            }
        }
    } else
        // The default behaviour
        toSQLWrapped(context);
    // [#291] some aliases cause trouble, if they are not explicitly marked using "as"
    toSQLAs(context);
    context.sql(' ').qualify(false, c -> c.visit(alias));
    // [#1801] Add field aliases to the table alias, if applicable
    if (fieldAliases != null && !emulatedDerivedColumnList) {
        toSQLDerivedColumnList(context);
    } else {
        // TODO: Is this still needed?
        switch(family) {
            case HSQLDB:
            case POSTGRES:
            case YUGABYTEDB:
                {
                    // The javac compiler doesn't like casting of generics
                    Object o = wrapped;
                    if (context.declareTables() && o instanceof ArrayTable)
                        context.sql('(').visit(wrap(((ArrayTable) o).fields()).qualify(false)).sql(')');
                    break;
                }
        }
    }
    if (wrapped instanceof TableImpl)
        context.scopeMarkEnd(wrapping);
}
Also used : QueryPartListView.wrap(org.jooq.impl.QueryPartListView.wrap) Tools.map(org.jooq.impl.Tools.map) UEmpty(org.jooq.impl.QOM.UEmpty) FIELD(org.jooq.Clause.FIELD) DATA_AS_REQUIRED(org.jooq.impl.Tools.BooleanDataKey.DATA_AS_REQUIRED) DSL.field(org.jooq.impl.DSL.field) Table(org.jooq.Table) POSTGRES(org.jooq.SQLDialect.POSTGRES) DATA_UNALIAS_ALIASED_EXPRESSIONS(org.jooq.impl.Tools.BooleanDataKey.DATA_UNALIAS_ALIASED_EXPRESSIONS) DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES(org.jooq.impl.Tools.BooleanDataKey.DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES) DATA_SELECT_ALIASES(org.jooq.impl.Tools.DataKey.DATA_SELECT_ALIASES) Clause(org.jooq.Clause) Tools.visitSubquery(org.jooq.impl.Tools.visitSubquery) FIELD_REFERENCE(org.jooq.Clause.FIELD_REFERENCE) SQLDialect(org.jooq.SQLDialect) Quoted(org.jooq.Name.Quoted) Select(org.jooq.Select) Predicate(java.util.function.Predicate) Name(org.jooq.Name) DSL.falseCondition(org.jooq.impl.DSL.falseCondition) Set(java.util.Set) Field(org.jooq.Field) DSL.select(org.jooq.impl.DSL.select) CUBRID(org.jooq.SQLDialect.CUBRID) QueryPart(org.jooq.QueryPart) HSQLDB(org.jooq.SQLDialect.HSQLDB) List(java.util.List) TABLE(org.jooq.Clause.TABLE) K_AS(org.jooq.impl.Keywords.K_AS) Context(org.jooq.Context) Tools.fieldNames(org.jooq.impl.Tools.fieldNames) TRUE(java.lang.Boolean.TRUE) DERBY(org.jooq.SQLDialect.DERBY) EMPTY_NAME(org.jooq.impl.Tools.EMPTY_NAME) HashSet(java.util.HashSet) SQLITE(org.jooq.SQLDialect.SQLITE) TABLE_REFERENCE(org.jooq.Clause.TABLE_REFERENCE) MYSQL(org.jooq.SQLDialect.MYSQL) TABLE_ALIAS(org.jooq.Clause.TABLE_ALIAS) Record(org.jooq.Record) DSL.asterisk(org.jooq.impl.DSL.asterisk) FIELD_ALIAS(org.jooq.Clause.FIELD_ALIAS) FIREBIRD(org.jooq.SQLDialect.FIREBIRD) RenderOptionalKeyword(org.jooq.conf.RenderOptionalKeyword) IGNITE(org.jooq.SQLDialect.IGNITE) YUGABYTEDB(org.jooq.SQLDialect.YUGABYTEDB) Tools.combine(org.jooq.impl.Tools.combine) NO_SUPPORT_VALUES(org.jooq.impl.Values.NO_SUPPORT_VALUES) H2(org.jooq.SQLDialect.H2) MARIADB(org.jooq.SQLDialect.MARIADB) Table(org.jooq.Table) SQLDialect(org.jooq.SQLDialect) Select(org.jooq.Select) List(java.util.List)

Example 32 with SQLDialect

use of org.jooq.SQLDialect in project jOOQ by jOOQ.

the class AlterTableImpl method accept1.

private final void accept1(Context<?> ctx) {
    SQLDialect family = ctx.family();
    boolean omitAlterTable = (renameConstraint != null && family == HSQLDB) || (renameColumn != null && SUPPORT_RENAME_COLUMN.contains(ctx.dialect()));
    boolean renameTable = renameTo != null && SUPPORT_RENAME_TABLE.contains(ctx.dialect());
    boolean renameObject = renameTo != null && (false);
    if (!omitAlterTable) {
        ctx.start(ALTER_TABLE_TABLE).visit(renameObject ? K_RENAME_OBJECT : renameTable ? K_RENAME_TABLE : K_ALTER_TABLE);
        if (ifExists && supportsIfExists(ctx))
            ctx.sql(' ').visit(K_IF_EXISTS);
        ctx.sql(' ').visit(table).end(ALTER_TABLE_TABLE).formatIndentStart().formatSeparator();
    }
    if (comment != null) {
        ctx.visit(K_COMMENT).sql(' ').visit(comment);
    } else if (renameTo != null) {
        boolean qualify = ctx.qualify();
        ctx.start(ALTER_TABLE_RENAME);
        if (NO_SUPPORT_RENAME_QUALIFIED_TABLE.contains(ctx.dialect()))
            ctx.qualify(false);
        ctx.visit(renameObject || renameTable ? K_TO : K_RENAME_TO).sql(' ').visit(renameTo);
        if (NO_SUPPORT_RENAME_QUALIFIED_TABLE.contains(ctx.dialect()))
            ctx.qualify(qualify);
        ctx.end(ALTER_TABLE_RENAME);
    } else if (renameColumn != null) {
        ctx.start(ALTER_TABLE_RENAME_COLUMN);
        switch(ctx.family()) {
            case DERBY:
                ctx.visit(K_RENAME_COLUMN).sql(' ').visit(renameColumn).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
                break;
            case H2:
            case HSQLDB:
                ctx.visit(K_ALTER_COLUMN).sql(' ').qualify(false, c -> c.visit(renameColumn)).formatSeparator().visit(K_RENAME_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
                break;
            case FIREBIRD:
                ctx.visit(K_ALTER_COLUMN).sql(' ').qualify(false, c -> c.visit(renameColumn)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
                break;
            default:
                ctx.visit(K_RENAME_COLUMN).sql(' ').qualify(false, c -> c.visit(renameColumn)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
                break;
        }
        ctx.end(ALTER_TABLE_RENAME_COLUMN);
    } else if (renameIndex != null) {
        ctx.start(ALTER_TABLE_RENAME_INDEX).visit(K_RENAME_INDEX).sql(' ').qualify(false, c -> c.visit(renameIndex)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameIndexTo)).end(ALTER_TABLE_RENAME_INDEX);
    } else if (renameConstraint != null) {
        ctx.start(ALTER_TABLE_RENAME_CONSTRAINT);
        ctx.data(DATA_CONSTRAINT_REFERENCE, true, c1 -> {
            if (family == HSQLDB)
                c1.visit(K_ALTER_CONSTRAINT).sql(' ').qualify(false, c2 -> c2.visit(renameConstraint)).formatSeparator().visit(K_RENAME_TO).sql(' ').qualify(false, c2 -> c2.visit(renameConstraintTo));
            else
                c1.visit(K_RENAME_CONSTRAINT).sql(' ').qualify(false, c2 -> c2.visit(renameConstraint)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c2 -> c2.visit(renameConstraintTo));
        });
        ctx.end(ALTER_TABLE_RENAME_CONSTRAINT);
    } else if (add != null) {
        boolean multiAdd = REQUIRE_REPEAT_ADD_ON_MULTI_ALTER.contains(ctx.dialect());
        boolean parens = !multiAdd;
        boolean comma = true;
        ctx.start(ALTER_TABLE_ADD).visit(addColumnKeyword(ctx)).sql(' ');
        if (parens)
            ctx.sql('(');
        boolean indent = !multiAdd && add.size() > 1;
        if (indent)
            ctx.formatIndentStart().formatNewLine();
        for (int i = 0; i < add.size(); i++) {
            if (i > 0) {
                ctx.sql(comma ? "," : "").formatSeparator();
                if (multiAdd)
                    ctx.visit(addColumnKeyword(ctx)).sql(' ');
            }
            TableElement part = add.get(i);
            ctx.qualify(false, c -> c.visit(part));
            if (part instanceof Field) {
                Field<?> f = (Field<?>) part;
                ctx.sql(' ');
                toSQLDDLTypeDeclarationForAddition(ctx, f.getDataType());
            }
        }
        if (indent)
            ctx.formatIndentEnd().formatNewLine();
        if (parens)
            ctx.sql(')');
        acceptFirstBeforeAfter(ctx);
        ctx.end(ALTER_TABLE_ADD);
    } else if (addColumn != null) {
        ctx.start(ALTER_TABLE_ADD).visit(addColumnKeyword(ctx)).sql(' ');
        if (ifNotExistsColumn && supportsIfNotExistsColumn(ctx))
            ctx.visit(K_IF_NOT_EXISTS).sql(' ');
        ctx.qualify(false, c -> c.visit(addColumn)).sql(' ');
        toSQLDDLTypeDeclarationForAddition(ctx, addColumnType);
        acceptFirstBeforeAfter(ctx);
        ctx.end(ALTER_TABLE_ADD);
    } else if (addConstraint != null) {
        ctx.start(ALTER_TABLE_ADD);
        ctx.visit(K_ADD).sql(' ');
        ctx.visit(addConstraint);
        ctx.end(ALTER_TABLE_ADD);
    } else if (alterConstraint != null) {
        ctx.start(ALTER_TABLE_ALTER);
        ctx.data(DATA_CONSTRAINT_REFERENCE, true, c -> {
            switch(family) {
                default:
                    ctx.visit(K_ALTER);
                    break;
            }
            ctx.sql(' ').visit(K_CONSTRAINT).sql(' ').visit(alterConstraint);
            ConstraintImpl.acceptEnforced(ctx, alterConstraintEnforced);
        });
        ctx.end(ALTER_TABLE_ALTER);
    } else if (alterColumn != null) {
        ctx.start(ALTER_TABLE_ALTER);
        switch(family) {
            case CUBRID:
            case MARIADB:
            case MYSQL:
                {
                    // MySQL's CHANGE COLUMN clause has a mandatory RENAMING syntax...
                    if (alterColumnDefault == null && !alterColumnDropDefault)
                        ctx.visit(K_CHANGE_COLUMN).sql(' ').qualify(false, c -> c.visit(alterColumn));
                    else
                        ctx.visit(K_ALTER_COLUMN);
                    break;
                }
            default:
                ctx.visit(K_ALTER);
                break;
        }
        ctx.sql(' ');
        ctx.qualify(false, c -> c.visit(alterColumn));
        if (alterColumnType != null) {
            switch(family) {
                case DERBY:
                    ctx.sql(' ').visit(K_SET_DATA_TYPE);
                    break;
                case FIREBIRD:
                case POSTGRES:
                case YUGABYTEDB:
                    ctx.sql(' ').visit(K_TYPE);
                    break;
            }
            ctx.sql(' ');
            toSQLDDLTypeDeclaration(ctx, alterColumnType);
            toSQLDDLTypeDeclarationIdentityBeforeNull(ctx, alterColumnType);
            // [#3805] Some databases cannot change the type and the NOT NULL constraint in a single statement
            if (!NO_SUPPORT_ALTER_TYPE_AND_NULL.contains(ctx.dialect())) {
                switch(alterColumnType.nullability()) {
                    case NULL:
                        ctx.sql(' ').visit(K_NULL);
                        break;
                    case NOT_NULL:
                        ctx.sql(' ').visit(K_NOT_NULL);
                        break;
                    case DEFAULT:
                        break;
                }
            }
            toSQLDDLTypeDeclarationIdentityAfterNull(ctx, alterColumnType);
        } else if (alterColumnDefault != null) {
            ctx.start(ALTER_TABLE_ALTER_DEFAULT);
            switch(family) {
                default:
                    ctx.sql(' ').visit(K_SET_DEFAULT);
                    break;
            }
            ctx.sql(' ').visit(alterColumnDefault).end(ALTER_TABLE_ALTER_DEFAULT);
        } else if (alterColumnDropDefault) {
            ctx.start(ALTER_TABLE_ALTER_DEFAULT);
            switch(family) {
                case MARIADB:
                case MYSQL:
                    ctx.sql(' ').visit(K_SET_DEFAULT).sql(' ').visit(K_NULL);
                    break;
                default:
                    ctx.sql(' ').visit(K_DROP_DEFAULT);
                    break;
            }
            ctx.end(ALTER_TABLE_ALTER_DEFAULT);
        } else if (alterColumnNullability != null) {
            ctx.start(ALTER_TABLE_ALTER_NULL);
            switch(ctx.family()) {
                default:
                    ctx.sql(' ').visit(alterColumnNullability.nullable() ? K_DROP_NOT_NULL : K_SET_NOT_NULL);
                    break;
            }
            ctx.end(ALTER_TABLE_ALTER_NULL);
        }
        ctx.end(ALTER_TABLE_ALTER);
    } else if (dropColumns != null) {
        ctx.start(ALTER_TABLE_DROP);
        if (REQUIRE_REPEAT_DROP_ON_MULTI_ALTER.contains(ctx.dialect())) {
            String separator = "";
            for (Field<?> dropColumn : dropColumns) {
                ctx.sql(separator);
                acceptDropColumn(ctx);
                if (ifExistsColumn && supportsIfExistsColumn(ctx))
                    ctx.sql(' ').visit(K_IF_EXISTS);
                ctx.sql(' ').qualify(false, c -> c.visit(dropColumn));
                separator = ", ";
            }
        } else {
            acceptDropColumn(ctx);
            if (ifExistsColumn && supportsIfExistsColumn(ctx))
                ctx.sql(' ').visit(K_IF_EXISTS);
            ctx.sql(' ');
            ctx.qualify(false, c -> c.visit(dropColumns));
        }
        acceptCascade(ctx);
        ctx.end(ALTER_TABLE_DROP);
    } else if (dropConstraint != null) {
        ctx.start(ALTER_TABLE_DROP);
        ctx.data(DATA_CONSTRAINT_REFERENCE, true, c -> {
            if (dropConstraintType == FOREIGN_KEY && NO_SUPPORT_DROP_CONSTRAINT.contains(c.dialect())) {
                c.visit(K_DROP).sql(' ').visit(K_FOREIGN_KEY).sql(' ').visit(dropConstraint);
            } else if (dropConstraintType == PRIMARY_KEY && NO_SUPPORT_DROP_CONSTRAINT.contains(c.dialect())) {
                c.visit(K_DROP).sql(' ').visit(K_PRIMARY_KEY);
            } else {
                // [#9382] In some dialects, unnamed UNIQUE constraints can be
                // dropped by dropping their declarations.
                c.visit(dropConstraint.getUnqualifiedName().empty() ? K_DROP : K_DROP_CONSTRAINT).sql(' ');
                if (ifExistsConstraint && !NO_SUPPORT_IF_EXISTS_CONSTRAINT.contains(c.dialect()))
                    c.visit(K_IF_EXISTS).sql(' ');
                c.visit(dropConstraint);
            }
            acceptCascade(c);
        });
        ctx.end(ALTER_TABLE_DROP);
    } else if (dropConstraintType == PRIMARY_KEY) {
        ctx.start(ALTER_TABLE_DROP);
        ctx.visit(K_DROP).sql(' ').visit(K_PRIMARY_KEY);
        ctx.end(ALTER_TABLE_DROP);
    }
    if (!omitAlterTable)
        ctx.formatIndentEnd();
}
Also used : Arrays(java.util.Arrays) K_ADD_COLUMN(org.jooq.impl.Keywords.K_ADD_COLUMN) K_RENAME_OBJECT(org.jooq.impl.Keywords.K_RENAME_OBJECT) UNIQUE(org.jooq.impl.ConstraintType.UNIQUE) DSL.field(org.jooq.impl.DSL.field) ALTER_TABLE_ADD(org.jooq.Clause.ALTER_TABLE_ADD) DSL.condition(org.jooq.impl.DSL.condition) POSTGRES(org.jooq.SQLDialect.POSTGRES) Tools.toSQLDDLTypeDeclarationIdentityAfterNull(org.jooq.impl.Tools.toSQLDDLTypeDeclarationIdentityAfterNull) K_PRIMARY_KEY(org.jooq.impl.Keywords.K_PRIMARY_KEY) ALTER_TABLE_RENAME_CONSTRAINT(org.jooq.Clause.ALTER_TABLE_RENAME_CONSTRAINT) DSL.begin(org.jooq.impl.DSL.begin) AlterTableFinalStep(org.jooq.AlterTableFinalStep) RESTRICT(org.jooq.impl.QOM.Cascade.RESTRICT) FOREIGN_KEY(org.jooq.impl.ConstraintType.FOREIGN_KEY) Record1(org.jooq.Record1) Index(org.jooq.Index) ALTER_TABLE_ALTER_DEFAULT(org.jooq.Clause.ALTER_TABLE_ALTER_DEFAULT) SQLDialect(org.jooq.SQLDialect) Select(org.jooq.Select) DSL.constraint(org.jooq.impl.DSL.constraint) K_DROP(org.jooq.impl.Keywords.K_DROP) Keyword(org.jooq.Keyword) K_DROP_DEFAULT(org.jooq.impl.Keywords.K_DROP_DEFAULT) K_IF(org.jooq.impl.Keywords.K_IF) Set(java.util.Set) K_SET_DATA_TYPE(org.jooq.impl.Keywords.K_SET_DATA_TYPE) QueryPart(org.jooq.QueryPart) K_END_IF(org.jooq.impl.Keywords.K_END_IF) K_FIRST(org.jooq.impl.Keywords.K_FIRST) DSL.noCondition(org.jooq.impl.DSL.noCondition) K_ALTER_COLUMN(org.jooq.impl.Keywords.K_ALTER_COLUMN) K_DROP_COLUMN(org.jooq.impl.Keywords.K_DROP_COLUMN) K_CHANGE_COLUMN(org.jooq.impl.Keywords.K_CHANGE_COLUMN) K_DROP_NOT_NULL(org.jooq.impl.Keywords.K_DROP_NOT_NULL) K_CONSTRAINTS(org.jooq.impl.Keywords.K_CONSTRAINTS) TRUE(java.lang.Boolean.TRUE) ALTER_TABLE_ALTER(org.jooq.Clause.ALTER_TABLE_ALTER) ALTER_TABLE_ALTER_NULL(org.jooq.Clause.ALTER_TABLE_ALTER_NULL) DERBY(org.jooq.SQLDialect.DERBY) Tools.tryCatch(org.jooq.impl.Tools.tryCatch) NOT_NULL(org.jooq.Nullability.NOT_NULL) K_RENAME(org.jooq.impl.Keywords.K_RENAME) DATA_CONSTRAINT_REFERENCE(org.jooq.impl.Tools.BooleanDataKey.DATA_CONSTRAINT_REFERENCE) AlterTableAlterStep(org.jooq.AlterTableAlterStep) K_DEFAULT(org.jooq.impl.Keywords.K_DEFAULT) K_RENAME_CONSTRAINT(org.jooq.impl.Keywords.K_RENAME_CONSTRAINT) FieldOrConstraint(org.jooq.FieldOrConstraint) ALTER_TABLE_RENAME_INDEX(org.jooq.Clause.ALTER_TABLE_RENAME_INDEX) DSL.sql(org.jooq.impl.DSL.sql) VARCHAR(org.jooq.impl.SQLDataType.VARCHAR) K_COLUMN(org.jooq.impl.Keywords.K_COLUMN) K_ALTER_CONSTRAINT(org.jooq.impl.Keywords.K_ALTER_CONSTRAINT) Tools.toSQLDDLTypeDeclaration(org.jooq.impl.Tools.toSQLDDLTypeDeclaration) MYSQL(org.jooq.SQLDialect.MYSQL) ALTER_TABLE(org.jooq.Clause.ALTER_TABLE) K_SET_NOT_NULL(org.jooq.impl.Keywords.K_SET_NOT_NULL) FIREBIRD(org.jooq.SQLDialect.FIREBIRD) K_CHANGE(org.jooq.impl.Keywords.K_CHANGE) Constraint(org.jooq.Constraint) AlterTableAddStep(org.jooq.AlterTableAddStep) ALTER_TABLE_RENAME_COLUMN(org.jooq.Clause.ALTER_TABLE_RENAME_COLUMN) YUGABYTEDB(org.jooq.SQLDialect.YUGABYTEDB) K_ADD(org.jooq.impl.Keywords.K_ADD) MARIADB(org.jooq.SQLDialect.MARIADB) ALTER_TABLE_TABLE(org.jooq.Clause.ALTER_TABLE_TABLE) Tools.endExecuteImmediate(org.jooq.impl.Tools.endExecuteImmediate) K_BEFORE(org.jooq.impl.Keywords.K_BEFORE) K_EXCEPTION(org.jooq.impl.Keywords.K_EXCEPTION) UNotYetImplemented(org.jooq.impl.QOM.UNotYetImplemented) K_LIKE(org.jooq.impl.Keywords.K_LIKE) PRIMARY_KEY(org.jooq.impl.ConstraintType.PRIMARY_KEY) Table(org.jooq.Table) ALTER_TABLE_RENAME(org.jooq.Clause.ALTER_TABLE_RENAME) K_EXEC(org.jooq.impl.Keywords.K_EXEC) Clause(org.jooq.Clause) K_ALTER(org.jooq.impl.Keywords.K_ALTER) K_FOREIGN_KEY(org.jooq.impl.Keywords.K_FOREIGN_KEY) K_NULL(org.jooq.impl.Keywords.K_NULL) K_NOT_NULL(org.jooq.impl.Keywords.K_NOT_NULL) K_IF_EXISTS(org.jooq.impl.Keywords.K_IF_EXISTS) DSLContext(org.jooq.DSLContext) AlterTableUsingIndexStep(org.jooq.AlterTableUsingIndexStep) K_CONSTRAINT(org.jooq.impl.Keywords.K_CONSTRAINT) Nullability(org.jooq.Nullability) DataType(org.jooq.DataType) NULL(org.jooq.Nullability.NULL) DSL.name(org.jooq.impl.DSL.name) Tools.toSQLDDLTypeDeclarationForAddition(org.jooq.impl.Tools.toSQLDDLTypeDeclarationForAddition) AlterTableStep(org.jooq.AlterTableStep) K_RENAME_INDEX(org.jooq.impl.Keywords.K_RENAME_INDEX) Name(org.jooq.Name) K_RAISE(org.jooq.impl.Keywords.K_RAISE) Collection(java.util.Collection) Cascade(org.jooq.impl.QOM.Cascade) Field(org.jooq.Field) DSL.select(org.jooq.impl.DSL.select) CUBRID(org.jooq.SQLDialect.CUBRID) K_POSITION(org.jooq.impl.Keywords.K_POSITION) HSQLDB(org.jooq.SQLDialect.HSQLDB) K_USING_INDEX(org.jooq.impl.Keywords.K_USING_INDEX) List(java.util.List) Context(org.jooq.Context) DSL.commentOnTable(org.jooq.impl.DSL.commentOnTable) AlterTableAlterConstraintStep(org.jooq.AlterTableAlterConstraintStep) K_RESTRICT(org.jooq.impl.Keywords.K_RESTRICT) TableElement(org.jooq.TableElement) Tools.toSQLDDLTypeDeclarationIdentityBeforeNull(org.jooq.impl.Tools.toSQLDDLTypeDeclarationIdentityBeforeNull) Query(org.jooq.Query) K_AFTER(org.jooq.impl.Keywords.K_AFTER) AlterTableDropStep(org.jooq.AlterTableDropStep) AlterTableRenameConstraintToStep(org.jooq.AlterTableRenameConstraintToStep) K_CASCADE(org.jooq.impl.Keywords.K_CASCADE) K_RENAME_TABLE(org.jooq.impl.Keywords.K_RENAME_TABLE) AlterTableRenameIndexToStep(org.jooq.AlterTableRenameIndexToStep) DSL.index(org.jooq.impl.DSL.index) K_MODIFY(org.jooq.impl.Keywords.K_MODIFY) K_TO(org.jooq.impl.Keywords.K_TO) Tools.executeImmediate(org.jooq.impl.Tools.executeImmediate) K_ELSE(org.jooq.impl.Keywords.K_ELSE) Comment(org.jooq.Comment) K_ALTER_TABLE(org.jooq.impl.Keywords.K_ALTER_TABLE) K_WHEN(org.jooq.impl.Keywords.K_WHEN) K_TYPE(org.jooq.impl.Keywords.K_TYPE) DSL.inline(org.jooq.impl.DSL.inline) K_WITH_NO_DATACOPY(org.jooq.impl.Keywords.K_WITH_NO_DATACOPY) K_DROP_CONSTRAINT(org.jooq.impl.Keywords.K_DROP_CONSTRAINT) FALSE(java.lang.Boolean.FALSE) Tools.beginExecuteImmediate(org.jooq.impl.Tools.beginExecuteImmediate) K_SET_DEFAULT(org.jooq.impl.Keywords.K_SET_DEFAULT) K_COMMENT(org.jooq.impl.Keywords.K_COMMENT) ALTER_TABLE_DROP(org.jooq.Clause.ALTER_TABLE_DROP) AlterTableRenameColumnToStep(org.jooq.AlterTableRenameColumnToStep) K_RENAME_COLUMN(org.jooq.impl.Keywords.K_RENAME_COLUMN) K_RENAME_TO(org.jooq.impl.Keywords.K_RENAME_TO) CASCADE(org.jooq.impl.QOM.Cascade.CASCADE) Tools.begin(org.jooq.impl.Tools.begin) Configuration(org.jooq.Configuration) H2(org.jooq.SQLDialect.H2) Tools.fieldsByName(org.jooq.impl.Tools.fieldsByName) K_REPLACE(org.jooq.impl.Keywords.K_REPLACE) K_IF_NOT_EXISTS(org.jooq.impl.Keywords.K_IF_NOT_EXISTS) Collections(java.util.Collections) K_THEN(org.jooq.impl.Keywords.K_THEN) Field(org.jooq.Field) SQLDialect(org.jooq.SQLDialect) TableElement(org.jooq.TableElement)

Example 33 with SQLDialect

use of org.jooq.SQLDialect in project jOOQ by jOOQ.

the class AlterTableImpl method accept0.

private final void accept0(Context<?> ctx) {
    SQLDialect family = ctx.family();
    if (comment != null) {
        switch(family) {
            case MARIADB:
            case MYSQL:
                break;
            default:
                ctx.visit(commentOnTable(table).is(comment));
                return;
        }
    }
    if (family == FIREBIRD) {
        if (addFirst) {
            begin(ctx, c1 -> {
                executeImmediate(c1, c2 -> accept1(c2));
                c1.formatSeparator();
                executeImmediate(c1, c2 -> {
                    c2.visit(K_ALTER_TABLE).sql(' ').visit(table).sql(' ').visit(K_ALTER).sql(' ').visit(addColumn).sql(' ').visit(K_POSITION).sql(" 1");
                });
            });
            return;
        }
    }
    if (renameIndexTo != null) {
        switch(family) {
            case MARIADB:
            case MYSQL:
                break;
            // [#5724] Most databases use schema-scoped index names: Ignore the table.
            default:
                ctx.visit(DSL.alterIndex(renameIndex).renameTo(renameIndexTo));
                return;
        }
    }
    // [#3805] Compound statements to alter data type and change nullability in a single statement if needed.
    if (alterColumnType != null && alterColumnType.nullability() != Nullability.DEFAULT) {
        switch(family) {
            case POSTGRES:
            case YUGABYTEDB:
                alterColumnTypeAndNullabilityInBlock(ctx);
                return;
        }
    }
    accept1(ctx);
}
Also used : SQLDialect(org.jooq.SQLDialect)

Example 34 with SQLDialect

use of org.jooq.SQLDialect in project jOOQ by jOOQ.

the class DefaultRenderContext method literal.

@Override
public final RenderContext literal(String literal) {
    // be null for CustomTable et al.
    if (literal == null)
        return this;
    SQLDialect family = family();
    // Quoting is needed when explicitly requested...
    boolean needsQuote = // http://www.sqlite.org/lang_keywords.html for details ...
    (family != SQLITE && quote()) || // [#2367] ... yet, do quote when an identifier is a SQLite keyword
    (family == SQLITE && SQLITE_KEYWORDS.contains(literal.toUpperCase(renderLocale(configuration().settings())))) || // [#1982] [#3360] ... yet, do quote when an identifier contains special characters
    (family == SQLITE && !IDENTIFIER_PATTERN.matcher(literal).matches());
    literal = applyNameCase(literal);
    if (needsQuote) {
        char[][][] quotes = QUOTES.get(family);
        char start = quotes[QUOTE_START_DELIMITER][0][0];
        char end = quotes[QUOTE_END_DELIMITER][0][0];
        sql(start);
        // situations
        if (literal.indexOf(end) > -1)
            sql(StringUtils.replace(literal, new String(quotes[QUOTE_END_DELIMITER][0]), new String(quotes[QUOTE_END_DELIMITER_ESCAPED][0])), true);
        else
            sql(literal, true);
        sql(end);
    } else
        sql(literal, true);
    return this;
}
Also used : SQLDialect(org.jooq.SQLDialect)

Example 35 with SQLDialect

use of org.jooq.SQLDialect in project jOOQ by jOOQ.

the class DefaultDataType method getDataType.

public static final DataType<?> getDataType(SQLDialect dialect, String typeName) {
    SQLDialect family = dialect.family();
    int ordinal = family.ordinal();
    String upper = typeName.toUpperCase();
    String normalised = typeName;
    DataType<?> result = TYPES_BY_NAME[ordinal].get(upper);
    // [#3225] Normalise only if necessary
    if (result == null) {
        result = TYPES_BY_NAME[ordinal].get(normalised = DefaultDataType.normalise(typeName));
        // UDT data types and others are registered using DEFAULT
        if (result == null) {
            result = TYPES_BY_NAME[SQLDialect.DEFAULT.ordinal()].get(normalised);
            // [#5713] TODO: A more generic type aliasing system would be useful, in general!
            if (result == null && "INT".equals(normalised))
                result = TYPES_BY_NAME[SQLDialect.DEFAULT.ordinal()].get("INTEGER");
            else // [#4065] PostgreSQL reports array types as _typename, e.g. _varchar
            if (result == null && SUPPORT_POSTGRES_ARRAY_NOTATION.contains(dialect) && normalised.charAt(0) == '_')
                result = getDataType(dialect, normalised.substring(1)).getArrayDataType();
            else // [#6466] HSQLDB reports array types as XYZARRAY. H2 should, too
            if (result == null && SUPPORT_HSQLDB_ARRAY_NOTATION.contains(dialect) && upper.endsWith(" ARRAY"))
                result = getDataType(dialect, typeName.substring(0, typeName.length() - 6)).getArrayDataType();
            else // [#9609] H2 might still report an untyped array, too
            if (result == null && SUPPORT_HSQLDB_ARRAY_NOTATION.contains(dialect) && upper.equals("ARRAY"))
                result = SQLDataType.OTHER.getArrayDataType();
            // catching the exception in jOOQ-codegen
            if (result == null)
                throw new SQLDialectNotSupportedException("Type " + typeName + " is not supported in dialect " + dialect, false);
        }
    }
    return result;
}
Also used : SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) SQLDialect(org.jooq.SQLDialect)

Aggregations

SQLDialect (org.jooq.SQLDialect)36 List (java.util.List)7 Select (org.jooq.Select)7 Condition (org.jooq.Condition)6 Table (org.jooq.Table)6 Set (java.util.Set)5 Configuration (org.jooq.Configuration)5 QueryPart (org.jooq.QueryPart)5 Record (org.jooq.Record)5 Connection (java.sql.Connection)4 Collection (java.util.Collection)4 Field (org.jooq.Field)4 TRUE (java.lang.Boolean.TRUE)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Arrays.asList (java.util.Arrays.asList)3 Clause (org.jooq.Clause)3 Comparator (org.jooq.Comparator)3 Context (org.jooq.Context)3 Name (org.jooq.Name)3