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();
accept1(ctx);
}
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 = family == HSQLDB && renameConstraint != null;
if (!omitAlterTable) {
ctx.start(ALTER_TABLE_TABLE).keyword("alter table");
if (ifExists && supportsIfExists(ctx))
ctx.sql(' ').keyword("if exists");
ctx.sql(' ').visit(table).end(ALTER_TABLE_TABLE).formatIndentStart().formatSeparator();
}
if (renameTo != null) {
boolean qualify = ctx.qualify();
ctx.start(ALTER_TABLE_RENAME).qualify(false).keyword("rename to").sql(' ').visit(renameTo).qualify(qualify).end(ALTER_TABLE_RENAME);
} else if (renameColumn != null) {
boolean qualify = ctx.qualify();
ctx.start(ALTER_TABLE_RENAME_COLUMN).qualify(false);
switch(ctx.family()) {
case H2:
case HSQLDB:
ctx.keyword("alter column").sql(' ').visit(renameColumn).formatSeparator().keyword("rename to").sql(' ').visit(renameColumnTo);
break;
default:
ctx.keyword("rename column").sql(' ').visit(renameColumn).formatSeparator().keyword("to").sql(' ').visit(renameColumnTo);
break;
}
ctx.qualify(qualify).end(ALTER_TABLE_RENAME_COLUMN);
} else if (renameConstraint != null) {
boolean qualify = ctx.qualify();
ctx.start(ALTER_TABLE_RENAME_CONSTRAINT);
ctx.data(DATA_CONSTRAINT_REFERENCE, true);
if (family == HSQLDB) {
ctx.qualify(false).keyword("alter constraint").sql(' ').visit(renameConstraint).formatSeparator().keyword("rename to").sql(' ').visit(renameConstraintTo).qualify(qualify);
} else {
ctx.qualify(false).keyword("rename constraint").sql(' ').visit(renameConstraint).formatSeparator().keyword("to").sql(' ').visit(renameConstraintTo).qualify(qualify);
}
ctx.data().remove(DATA_CONSTRAINT_REFERENCE);
ctx.end(ALTER_TABLE_RENAME_CONSTRAINT);
} else if (addColumn != null) {
boolean qualify = ctx.qualify();
ctx.start(ALTER_TABLE_ADD).keyword("add").sql(' ');
ctx.qualify(false).visit(addColumn).sql(' ').qualify(qualify);
toSQLDDLTypeDeclaration(ctx, addColumnType);
if (!addColumnType.nullable()) {
ctx.sql(' ').keyword("not null");
} else // [#3400] [#4321] ... but not in Derby, Firebird
if (!asList(DERBY, FIREBIRD).contains(family)) {
ctx.sql(' ').keyword("null");
}
ctx.end(ALTER_TABLE_ADD);
} else if (addConstraint != null) {
boolean qualify = ctx.qualify();
ctx.start(ALTER_TABLE_ADD);
ctx.keyword("add").sql(' ').qualify(false).visit(addConstraint).qualify(qualify);
ctx.end(ALTER_TABLE_ADD);
} else if (alterColumn != null) {
ctx.start(ALTER_TABLE_ALTER);
switch(family) {
case CUBRID:
case MARIADB:
case MYSQL:
{
if (alterColumnDefault == null) {
// MySQL's CHANGE COLUMN clause has a mandatory RENAMING syntax...
ctx.keyword("change column").sql(' ').qualify(false).visit(alterColumn).qualify(true);
} else {
ctx.keyword("alter column");
}
break;
}
default:
ctx.keyword("alter");
break;
}
ctx.sql(' ').qualify(false).visit(alterColumn).qualify(true);
if (alterColumnType != null) {
switch(family) {
case DERBY:
ctx.sql(' ').keyword("set data type");
break;
case POSTGRES:
ctx.sql(' ').keyword("type");
break;
}
ctx.sql(' ');
toSQLDDLTypeDeclaration(ctx, alterColumnType);
if (!alterColumnType.nullable()) {
ctx.sql(' ').keyword("not null");
}
} else if (alterColumnDefault != null) {
ctx.start(ALTER_TABLE_ALTER_DEFAULT);
switch(family) {
default:
ctx.keyword("set default");
break;
}
ctx.sql(' ').visit(alterColumnDefault).end(ALTER_TABLE_ALTER_DEFAULT);
}
ctx.end(ALTER_TABLE_ALTER);
} else if (dropColumn != null) {
ctx.start(ALTER_TABLE_DROP);
switch(family) {
default:
ctx.keyword("drop");
break;
}
ctx.sql(' ').qualify(false).visit(dropColumn).qualify(true);
switch(family) {
default:
break;
}
if (dropColumnCascade) {
ctx.sql(' ').keyword("cascade");
}
ctx.end(ALTER_TABLE_DROP);
} else if (dropConstraint != null) {
ctx.start(ALTER_TABLE_DROP);
ctx.data(DATA_CONSTRAINT_REFERENCE, true);
ctx.keyword("drop constraint").sql(' ').visit(dropConstraint);
ctx.data().remove(DATA_CONSTRAINT_REFERENCE);
ctx.end(ALTER_TABLE_DROP);
}
if (!omitAlterTable)
ctx.formatIndentEnd();
}
use of org.jooq.SQLDialect in project jOOQ by jOOQ.
the class AbstractRoutine method execute.
@Override
public final int execute() {
SQLDialect family = configuration.family();
results.clear();
outValues.clear();
// DEFAULT parameters
if (family == POSTGRES) {
return executeSelectFromPOSTGRES();
} else // Procedures (no return value) are always executed as CallableStatement
if (type == null) {
return executeCallableStatement();
} else {
switch(family) {
// syntax for functions. Select functions from DUAL instead
case HSQLDB:
// returns a cursor. Instead, SELECT * FROM table(f()) works
if (SQLDataType.RESULT.equals(type.getSQLDataType())) {
return executeSelectFromHSQLDB();
} else // Fall through
{
}
case H2:
return executeSelect();
// DML statements
default:
return executeCallableStatement();
}
}
}
use of org.jooq.SQLDialect in project jOOQ by jOOQ.
the class RowCondition method delegate.
private final QueryPartInternal delegate(Configuration configuration) {
SQLDialect dialect = configuration.dialect();
// Regular comparison predicate emulation
if (asList(EQUALS, NOT_EQUALS).contains(comparator) && asList(DERBY, FIREBIRD, SQLITE).contains(dialect.family())) {
List<Condition> conditions = new ArrayList<Condition>();
Field<?>[] leftFields = left.fields();
Field<?>[] rightFields = right.fields();
for (int i = 0; i < leftFields.length; i++) {
conditions.add(leftFields[i].equal((Field) rightFields[i]));
}
Condition result = DSL.and(conditions);
if (comparator == NOT_EQUALS) {
result = result.not();
}
return (QueryPartInternal) result;
} else // Ordering comparison predicate emulation
if (asList(GREATER, GREATER_OR_EQUAL, LESS, LESS_OR_EQUAL).contains(comparator) && asList(DERBY, CUBRID, FIREBIRD, SQLITE).contains(dialect.family())) {
// The order component of the comparator (stripping the equal component)
Comparator order = (comparator == GREATER) ? GREATER : (comparator == GREATER_OR_EQUAL) ? GREATER : (comparator == LESS) ? LESS : (comparator == LESS_OR_EQUAL) ? LESS : null;
// [#2658] The factored order component of the comparator (enforcing the equal component)
Comparator factoredOrder = (comparator == GREATER) ? GREATER_OR_EQUAL : (comparator == GREATER_OR_EQUAL) ? GREATER_OR_EQUAL : (comparator == LESS) ? LESS_OR_EQUAL : (comparator == LESS_OR_EQUAL) ? LESS_OR_EQUAL : null;
// Whether the comparator has an equal component
boolean equal = (comparator == GREATER_OR_EQUAL) || (comparator == LESS_OR_EQUAL);
// The following algorithm emulates the equivalency of these expressions:
// (A, B, C) > (X, Y, Z)
// (A > X) OR (A = X AND B > Y) OR (A = X AND B = Y AND C > Z)
List<Condition> outer = new ArrayList<Condition>();
Field<?>[] leftFields = left.fields();
Field<?>[] rightFields = right.fields();
for (int i = 0; i < leftFields.length; i++) {
List<Condition> inner = new ArrayList<Condition>();
for (int j = 0; j < i; j++) {
inner.add(leftFields[j].equal((Field) rightFields[j]));
}
inner.add(leftFields[i].compare(order, (Field) rightFields[i]));
outer.add(DSL.and(inner));
}
if (equal) {
outer.add(new RowCondition(left, right, Comparator.EQUALS));
}
Condition result = DSL.or(outer);
// (A >= X) AND ((A > X) OR (A = X AND B > Y) OR (A = X AND B = Y AND C > Z))
if (leftFields.length > 1) {
result = leftFields[0].compare(factoredOrder, (Field) rightFields[0]).and(result);
}
return (QueryPartInternal) result;
} else {
return new Native();
}
}
use of org.jooq.SQLDialect in project jOOQ by jOOQ.
the class SelectQueryImpl method toSQLReference0.
/**
* This method renders the main part of a query without the LIMIT clause.
* This part is common to any type of limited query
*/
@SuppressWarnings("unchecked")
private final void toSQLReference0(Context<?> context, Field<?>[] originalFields, Field<?>[] alternativeFields) {
SQLDialect dialect = context.dialect();
SQLDialect family = dialect.family();
int unionOpSize = unionOp.size();
// The SQL standard specifies:
//
// <query expression> ::=
// [ <with clause> ] <query expression body>
// [ <order by clause> ] [ <result offset clause> ] [ <fetch first clause> ]
//
// Depending on the dialect and on various syntax elements, parts of the above must be wrapped in
// synthetic parentheses
boolean wrapQueryExpressionInDerivedTable;
boolean wrapQueryExpressionBodyInDerivedTable = false;
wrapQueryExpressionInDerivedTable = false || // interpreted as the (missing) INSERT column list's parens.
(context.data(DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST) != null && unionOpSize > 0);
if (wrapQueryExpressionInDerivedTable)
context.keyword("select").sql(" *").formatSeparator().keyword("from").sql(" (").formatIndentStart().formatNewLine();
// all databases, we need to wrap relevant subqueries in parentheses.
if (unionOpSize > 0) {
for (int i = unionOpSize - 1; i >= 0; i--) {
switch(unionOp.get(i)) {
case EXCEPT:
context.start(SELECT_EXCEPT);
break;
case EXCEPT_ALL:
context.start(SELECT_EXCEPT_ALL);
break;
case INTERSECT:
context.start(SELECT_INTERSECT);
break;
case INTERSECT_ALL:
context.start(SELECT_INTERSECT_ALL);
break;
case UNION:
context.start(SELECT_UNION);
break;
case UNION_ALL:
context.start(SELECT_UNION_ALL);
break;
}
unionParenthesis(context, "(");
}
}
// SELECT clause
// -------------
context.start(SELECT_SELECT).keyword("select").sql(' ');
// [#1493] Oracle hints come directly after the SELECT keyword
if (!StringUtils.isBlank(hint)) {
context.sql(hint).sql(' ');
}
if (!distinctOn.isEmpty()) {
context.keyword("distinct on").sql(" (").visit(distinctOn).sql(") ");
} else if (distinct) {
context.keyword("distinct").sql(' ');
}
context.declareFields(true);
// non-ambiguous column names as ambiguous column names are not allowed in subqueries
if (alternativeFields != null) {
if (wrapQueryExpressionBodyInDerivedTable && originalFields.length < alternativeFields.length)
context.visit(new SelectFieldList(Arrays.copyOf(alternativeFields, alternativeFields.length - 1)));
else
context.visit(new SelectFieldList(alternativeFields));
} else // arrays explicitly, as the subquery doesn't form an implicit RVE
if (context.subquery() && dialect == H2 && context.data(DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY) != null) {
Object data = context.data(DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY);
try {
context.data(DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY, null);
context.sql('(').visit(getSelect1()).sql(')');
} finally {
context.data(DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY, data);
}
} else // The default behaviour
{
context.visit(getSelect1());
}
context.declareFields(false).end(SELECT_SELECT);
// only in top level SELECTs
if (!context.subquery() && !asList().contains(family)) {
context.start(SELECT_INTO);
Table<?> actualInto = (Table<?>) context.data(DATA_SELECT_INTO_TABLE);
if (actualInto == null)
actualInto = into;
if (actualInto != null && context.data(DATA_OMIT_INTO_CLAUSE) == null && asList(HSQLDB, POSTGRES).contains(family)) {
context.formatSeparator().keyword("into").sql(' ').visit(actualInto);
}
context.end(SELECT_INTO);
}
// FROM and JOIN clauses
// ---------------------
context.start(SELECT_FROM).declareTables(true);
// [#....] Some SQL dialects do not require a FROM clause. Others do and
// jOOQ generates a "DUAL" table or something equivalent.
// See also org.jooq.impl.Dual for details.
boolean hasFrom = !getFrom().isEmpty() || asList(CUBRID, DERBY, FIREBIRD, HSQLDB, MARIADB, MYSQL).contains(family);
List<Condition> semiAntiJoinPredicates = null;
if (hasFrom) {
Object previousCollect = context.data(DATA_COLLECT_SEMI_ANTI_JOIN, true);
Object previousCollected = context.data(DATA_COLLECTED_SEMI_ANTI_JOIN, null);
context.formatSeparator().keyword("from").sql(' ').visit(getFrom());
semiAntiJoinPredicates = (List<Condition>) context.data(DATA_COLLECTED_SEMI_ANTI_JOIN, previousCollected);
context.data(DATA_COLLECT_SEMI_ANTI_JOIN, previousCollect);
}
context.declareTables(false).end(SELECT_FROM);
// WHERE clause
// ------------
context.start(SELECT_WHERE);
if (getWhere().getWhere() instanceof TrueCondition && semiAntiJoinPredicates == null)
;
else {
ConditionProviderImpl where = new ConditionProviderImpl();
if (semiAntiJoinPredicates != null)
where.addConditions(semiAntiJoinPredicates);
if (!(getWhere().getWhere() instanceof TrueCondition))
where.addConditions(getWhere());
context.formatSeparator().keyword("where").sql(' ').visit(where);
}
context.end(SELECT_WHERE);
// CONNECT BY clause
// -----------------
// CUBRID supports this clause only as [ START WITH .. ] CONNECT BY
// Oracle also knows the CONNECT BY .. [ START WITH ] alternative
// syntax
context.start(SELECT_START_WITH);
if (!(getConnectByStartWith().getWhere() instanceof TrueCondition)) {
context.formatSeparator().keyword("start with").sql(' ').visit(getConnectByStartWith());
}
context.end(SELECT_START_WITH);
context.start(SELECT_CONNECT_BY);
if (!(getConnectBy().getWhere() instanceof TrueCondition)) {
context.formatSeparator().keyword("connect by");
if (connectByNoCycle) {
context.sql(' ').keyword("nocycle");
}
context.sql(' ').visit(getConnectBy());
}
context.end(SELECT_CONNECT_BY);
// GROUP BY and HAVING clause
// --------------------------
context.start(SELECT_GROUP_BY);
if (grouping) {
context.formatSeparator().keyword("group by").sql(' ');
// [#1665] Empty GROUP BY () clauses need parentheses
if (getGroupBy().isEmpty()) {
// [#1681] Use the constant field from the dummy table Sybase ASE, Ingres
if (asList().contains(family)) {
context.sql("empty_grouping_dummy_table.dual");
} else // references, as in the ORDER BY clause!
if (asList(DERBY).contains(family)) {
context.sql('0');
} else // [#4447] CUBRID can't handle subqueries in GROUP BY
if (family == CUBRID) {
context.sql("1 + 0");
} else // [#4292] Some dialects don't support empty GROUP BY () clauses
if (asList(FIREBIRD, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE).contains(family)) {
context.sql('(').visit(DSL.select(one())).sql(')');
} else // Few dialects support the SQL standard "grand total" (i.e. empty grouping set)
{
context.sql("()");
}
} else {
context.visit(getGroupBy());
}
}
context.end(SELECT_GROUP_BY);
// HAVING clause
// -------------
context.start(SELECT_HAVING);
if (!(getHaving().getWhere() instanceof TrueCondition)) {
context.formatSeparator().keyword("having").sql(' ').visit(getHaving());
}
context.end(SELECT_HAVING);
// WINDOW clause
// -------------
context.start(SELECT_WINDOW);
if (!getWindow().isEmpty() && asList(POSTGRES).contains(family)) {
context.formatSeparator().keyword("window").sql(' ').declareWindows(true).visit(getWindow()).declareWindows(false);
}
context.end(SELECT_WINDOW);
// ORDER BY clause for local subselect
// -----------------------------------
toSQLOrderBy(context, originalFields, alternativeFields, false, wrapQueryExpressionBodyInDerivedTable, orderBy, limit);
// --------------------------------------------
if (unionOpSize > 0) {
unionParenthesis(context, ")");
for (int i = 0; i < unionOpSize; i++) {
CombineOperator op = unionOp.get(i);
for (Select<?> other : union.get(i)) {
context.formatSeparator().keyword(op.toSQL(dialect)).sql(' ');
unionParenthesis(context, "(");
context.visit(other);
unionParenthesis(context, ")");
}
// [#1658] Close parentheses opened previously
if (i < unionOpSize - 1)
unionParenthesis(context, ")");
switch(unionOp.get(i)) {
case EXCEPT:
context.end(SELECT_EXCEPT);
break;
case EXCEPT_ALL:
context.end(SELECT_EXCEPT_ALL);
break;
case INTERSECT:
context.end(SELECT_INTERSECT);
break;
case INTERSECT_ALL:
context.end(SELECT_INTERSECT_ALL);
break;
case UNION:
context.end(SELECT_UNION);
break;
case UNION_ALL:
context.end(SELECT_UNION_ALL);
break;
}
}
}
// ORDER BY clause for UNION
// -------------------------
boolean qualify = context.qualify();
try {
context.qualify(false);
toSQLOrderBy(context, originalFields, alternativeFields, wrapQueryExpressionInDerivedTable, wrapQueryExpressionBodyInDerivedTable, unionOrderBy, unionLimit);
} finally {
context.qualify(qualify);
}
}
Aggregations