TableLike<?> values(DSLContext ctx, RowN[] rows, String tableName, String... fieldNames) { switch (sqlDialect.family()) { case H2: List<SelectField<?>> fields = Lists.newArrayListWithCapacity(fieldNames.length); for (int i = 1; i <= fieldNames.length; i++) { fields.add(DSL.field("C" + i).as(fieldNames[i-1])); } RenderContext context = ctx.renderContext(); context.start(TABLE_VALUES) .keyword("values") .formatIndentLockStart(); boolean firstRow = true; for (Row row : rows) { if (!firstRow) { context.sql(',').formatSeparator(); } context.sql(row.toString()); firstRow = false; } context.formatIndentLockEnd() .end(TABLE_VALUES); String valuesClause = context.render(); return ctx.select(fields).from(valuesClause).asTable(tableName); default: return DSL.values(rows).as(tableName, fieldNames); } }
@Override public final void toSQL(RenderContext context) { for (int i = 0; i < queries.size(); i++) { if (i != 0) { context.formatSeparator() .keyword(operator.toSQL(context.configuration().dialect())) .formatSeparator(); } wrappingParenthesis(context, "("); context.visit(queries.get(i)); wrappingParenthesis(context, ")"); } }
context.formatSeparator() .keyword("or") .sql(" "); context.formatSeparator() .keyword("and") .sql(" ");
final void toSQLReturning(RenderContext context) { if (!returning.isEmpty()) { switch (context.configuration().dialect()) { case FIREBIRD: case POSTGRES: context.formatSeparator() .keyword("returning") .sql(" ") .visit(returning); break; default: // Other dialects don't render a RETURNING clause, but // use JDBC's Statement.RETURN_GENERATED_KEYS mode instead break; } } }
context.formatSeparator() .start(INSERT_VALUES) .keyword("values") context.formatSeparator() .start(INSERT_SELECT); context.visit(insertSelect(context)); context.formatSeparator() .start(INSERT_VALUES) .keyword("values")
context.formatSeparator() .keyword(restartIdentity ? "restart identity" : "continue identity"); context.formatSeparator() .keyword(cascade ? "cascade" : "restrict");
.formatSeparator() .start(translatedClause) .keyword(keyword) context.formatSeparator() .start(TABLE_JOIN_PARTITION_BY) .keyword("partition by")
.declareTables(false) .end(MERGE_MERGE_INTO) .formatSeparator() .start(MERGE_USING) .declareTables(true) .formatSeparator() .start(MERGE_ON) context.formatSeparator() .keyword("when matched then update set").sql(" ") .visit(matchedUpdate); context.formatSeparator() .keyword("where").sql(" ") .visit(matchedWhere); context.formatSeparator() .keyword("delete where").sql(" ") .visit(matchedDeleteWhere); context.formatSeparator() .keyword("when not matched then insert").sql(" "); notMatchedInsert.toSQLReferenceKeys(context); context.formatSeparator() .start(MERGE_VALUES) .keyword("values").sql(" ") context.formatSeparator()
boolean first = true; for (Field<?> field : using) { context.formatSeparator(); context.formatSeparator() .start(TABLE_JOIN_USING) .keyword("using") context.formatSeparator(); context.formatSeparator() .start(TABLE_JOIN_ON) .keyword("on")
context.formatSeparator() .keyword("for update"); context.formatSeparator() .keyword("lock in share mode"); break; context.formatSeparator() .keyword("for share"); break; context.formatSeparator() .sql(option);
@Override public final void toSQL(RenderContext context) { boolean declare = context.declareTables(); context.start(DELETE_DELETE) .keyword("delete").sql(" "); // [#2464] MySQL supports a peculiar multi-table DELETE syntax for aliased tables: // DELETE t1 FROM my_table AS t1 if (asList(MARIADB, MYSQL).contains(context.configuration().dialect())) { // [#2579] TODO: Improve Table API to discover aliased tables more // reliably instead of resorting to instanceof: if (getFrom() instanceof TableAlias || (getFrom() instanceof TableImpl && ((TableImpl<R>)getFrom()).getAliasedTable() != null)) { context.visit(getFrom()) .sql(" "); } } context.keyword("from").sql(" ") .declareTables(true) .visit(getFrom()) .declareTables(declare) .end(DELETE_DELETE) .start(DELETE_WHERE); if (!(getWhere() instanceof TrueCondition)) { context.formatSeparator() .keyword("where").sql(" ") .visit(getWhere()); } context.end(DELETE_WHERE); }
.sql(",") .formatIndentStart() .formatSeparator() .visit(limitOffsetRownumber) .formatIndentEnd() context.formatSeparator() .keyword("from") .sql(" ") context.formatSeparator() .keyword("where") .sql(" ") context.formatSeparator() .keyword("start with") .sql(" ") context.formatSeparator() .keyword("connect by"); context.formatSeparator() .keyword("group by") .sql(" "); context.formatSeparator() .keyword("having") .sql(" ")
case SQLITE: { context.castMode(NEVER) .formatSeparator() .keyword("limit") .sql(" ").visit(numberOfRows) .formatSeparator() .keyword("limit") .sql(" ").visit(offsetOrZero) .formatSeparator() .keyword("rows") .sql(" ").visit(getLowerRownum().add(inline(1))) .formatSeparator() .keyword("offset") .sql(" ").visit(offsetOrZero) .formatSeparator() .keyword("limit") .sql(" ").visit(numberOfRows)
@Override public final void toSQL(RenderContext context) { context.start(INSERT_INSERT_INTO) .keyword("insert into") .sql(" ") .visit(into) .sql(" ("); // [#989] Avoid qualifying fields in INSERT field declaration boolean qualify = context.qualify(); context.qualify(false); String separator = ""; for (Field<?> field : fields) { context.sql(separator) .visit(field); separator = ", "; } context.qualify(qualify); context.sql(")") .end(INSERT_INSERT_INTO) .formatSeparator() .start(INSERT_SELECT) .visit(select) .end(INSERT_SELECT) .start(INSERT_ON_DUPLICATE_KEY_UPDATE) .end(INSERT_ON_DUPLICATE_KEY_UPDATE) .start(INSERT_RETURNING) .end(INSERT_RETURNING); }
context.formatSeparator() .start(UPDATE_SET) .keyword("set") context.formatSeparator() .keyword("from").sql(" ") .visit(from); context.formatSeparator() .keyword("where").sql(" ") .visit(getWhere());
private final void toSQLH2(RenderContext context) { context.keyword("merge into") .sql(" ") .declareTables(true) .visit(table) .formatSeparator(); context.sql("("); Utils.fieldNames(context, getH2Fields()); context.sql(")"); if (!getH2Keys().isEmpty()) { context.sql(" ").keyword("key").sql(" ("); Utils.fieldNames(context, getH2Keys()); context.sql(")"); } if (h2Select != null) { context.sql(" ") .visit(h2Select); } else { context.sql(" ").keyword("values").sql(" (") .visit(getH2Values()) .sql(")"); } }
for (Row row : rows) { if (!firstRow) { context.sql(",").formatSeparator();
case MYSQL: { toSQLInsert(context); context.formatSeparator() .start(INSERT_ON_DUPLICATE_KEY_UPDATE) .keyword("on duplicate key update") context.formatSeparator() .start(INSERT_ON_DUPLICATE_KEY_UPDATE) .keyword("on duplicate key update")