org.jooq.impl.AbstractDMLQuery Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hello-world-plugin Show documentation
Show all versions of hello-world-plugin Show documentation
Kill Bill Hello World plugin
The newest version!
/*
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static java.util.Arrays.asList;
// ...
// ...
import static org.jooq.SQLDialect.HSQLDB;
// ...
// ...
// ...
import static org.jooq.conf.RenderNameStyle.LOWER;
import static org.jooq.conf.RenderNameStyle.UPPER;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.Tools.EMPTY_STRING;
import static org.jooq.impl.Tools.fieldArray;
import static org.jooq.impl.Tools.unqualify;
import static org.jooq.util.sqlite.SQLiteDSL.rowid;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.DSLContext;
import org.jooq.DeleteQuery;
import org.jooq.ExecuteContext;
import org.jooq.ExecuteListener;
import org.jooq.Field;
import org.jooq.Identity;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.Table;
import org.jooq.UpdateQuery;
import org.jooq.conf.RenderNameStyle;
import org.jooq.impl.Tools.DataKey;
import org.jooq.tools.jdbc.JDBCUtils;
/**
* @author Lukas Eder
*/
abstract class AbstractDMLQuery extends AbstractQuery {
/**
* Generated UID
*/
private static final long serialVersionUID = -7438014075226919192L;
final WithImpl with;
final Table table;
final QueryPartList> returning;
Result returned;
AbstractDMLQuery(Configuration configuration, WithImpl with, Table table) {
super(configuration);
this.with = with;
this.table = table;
this.returning = new QueryPartList>();
}
// @Override
public final void setReturning() {
setReturning(table.fields());
}
// @Override
public final void setReturning(Identity identity) {
if (identity != null) {
setReturning(identity.getField());
}
}
// @Override
public final void setReturning(Field>... fields) {
setReturning(Arrays.asList(fields));
}
// @Override
public final void setReturning(Collection extends Field>> fields) {
returning.clear();
returning.addAll(fields);
}
// @Override
public final R getReturnedRecord() {
if (getReturnedRecords().size() == 0) {
return null;
}
return getReturnedRecords().get(0);
}
// @Override
public final Result getReturnedRecords() {
if (returned == null) {
returned = new ResultImpl(configuration(), returning);
}
return returned;
}
@Override
public final void accept(Context> ctx) {
if (with != null)
ctx.visit(with).formatSeparator();
{
accept0(ctx);
}
}
abstract void accept0(Context> ctx);
final void toSQLReturning(Context> ctx) {
if (!returning.isEmpty()) {
switch (ctx.family()) {
case FIREBIRD:
case POSTGRES:
ctx.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;
}
}
}
@Override
protected final void prepare(ExecuteContext ctx) throws SQLException {
Connection connection = ctx.connection();
if (returning.isEmpty()) {
super.prepare(ctx);
return;
}
// Values should be returned from the INSERT
else {
switch (ctx.family()) {
// Postgres uses the RETURNING clause in SQL
case FIREBIRD:
case POSTGRES:
// SQLite will select last_insert_rowid() after the INSER
case SQLITE:
case CUBRID:
super.prepare(ctx);
return;
// Some dialects can only return AUTO_INCREMENT values
// Other values have to be fetched in a second step
// [#1260] TODO CUBRID supports this, but there's a JDBC bug
case DERBY:
case H2:
case MARIADB:
case MYSQL:
ctx.statement(connection.prepareStatement(ctx.sql(), Statement.RETURN_GENERATED_KEYS));
return;
// The default is to return all requested fields directly
case HSQLDB:
default: {
List names = new ArrayList();
RenderNameStyle style = configuration().settings().getRenderNameStyle();
for (Field> field : returning) {
// [#2845] Field names should be passed to JDBC in the case
// imposed by the user. For instance, if the user uses
// PostgreSQL generated case-insensitive Fields (default to lower case)
// and wants to query HSQLDB (default to upper case), they may choose
// to overwrite casing using RenderKeywordStyle.
if (style == UPPER)
names.add(field.getName().toUpperCase());
else if (style == LOWER)
names.add(field.getName().toLowerCase());
else
names.add(field.getName());
}
ctx.statement(connection.prepareStatement(ctx.sql(), names.toArray(EMPTY_STRING)));
return;
}
}
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected final int execute(ExecuteContext ctx, ExecuteListener listener) throws SQLException {
if (returning.isEmpty()) {
return super.execute(ctx, listener);
}
else {
int result = 1;
ResultSet rs;
switch (ctx.family()) {
// SQLite can select _rowid_ after the insert
case SQLITE: {
listener.executeStart(ctx);
result = ctx.statement().executeUpdate();
ctx.rows(result);
listener.executeEnd(ctx);
DSLContext create = DSL.using(ctx.configuration());
returned =
create.select(returning)
.from(table)
.where(rowid().equal(rowid().getDataType().convert(create.lastID())))
.fetchInto(table);
return result;
}
case CUBRID: {
listener.executeStart(ctx);
result = ctx.statement().executeUpdate();
ctx.rows(result);
listener.executeEnd(ctx);
selectReturning(ctx.configuration(), create(ctx.configuration()).lastID());
return result;
}
// Some dialects can only retrieve "identity" (AUTO_INCREMENT) values
// Additional values have to be fetched explicitly
// [#1260] TODO CUBRID supports this, but there's a JDBC bug
case DERBY:
case H2:
case MARIADB:
case MYSQL: {
listener.executeStart(ctx);
result = ctx.statement().executeUpdate();
ctx.rows(result);
listener.executeEnd(ctx);
rs = ctx.statement().getGeneratedKeys();
try {
List