Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* 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.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq.impl;
import static java.lang.Boolean.TRUE;
import static java.util.Arrays.asList;
import static org.jooq.Clause.SELECT;
import static org.jooq.Clause.SELECT_CONNECT_BY;
import static org.jooq.Clause.SELECT_EXCEPT;
import static org.jooq.Clause.SELECT_FROM;
import static org.jooq.Clause.SELECT_GROUP_BY;
import static org.jooq.Clause.SELECT_HAVING;
import static org.jooq.Clause.SELECT_INTERSECT;
import static org.jooq.Clause.SELECT_INTO;
import static org.jooq.Clause.SELECT_ORDER_BY;
import static org.jooq.Clause.SELECT_SELECT;
import static org.jooq.Clause.SELECT_START_WITH;
import static org.jooq.Clause.SELECT_UNION;
import static org.jooq.Clause.SELECT_UNION_ALL;
import static org.jooq.Clause.SELECT_WHERE;
import static org.jooq.Clause.SELECT_WINDOW;
import static org.jooq.Operator.OR;
// ...
// ...
// ...
import static org.jooq.SQLDialect.CUBRID;
// ...
// ...
import static org.jooq.SQLDialect.DERBY;
import static org.jooq.SQLDialect.FIREBIRD;
import static org.jooq.SQLDialect.H2;
import static org.jooq.SQLDialect.HSQLDB;
// ...
// ...
import static org.jooq.SQLDialect.MARIADB;
import static org.jooq.SQLDialect.MYSQL;
// ...
import static org.jooq.SQLDialect.POSTGRES;
import static org.jooq.SQLDialect.SQLITE;
// ...
// ...
// ...
// ...
import static org.jooq.SortOrder.ASC;
import static org.jooq.impl.CombineOperator.EXCEPT;
import static org.jooq.impl.CombineOperator.INTERSECT;
import static org.jooq.impl.CombineOperator.UNION;
import static org.jooq.impl.CombineOperator.UNION_ALL;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.one;
import static org.jooq.impl.DSL.orderBy;
import static org.jooq.impl.DSL.row;
// ...
// ...
import static org.jooq.impl.Utils.DATA_LOCALLY_SCOPED_DATA_MAP;
import static org.jooq.impl.Utils.DATA_OMIT_INTO_CLAUSE;
import static org.jooq.impl.Utils.DATA_OVERRIDE_ALIASES_IN_ORDER_BY;
// ...
import static org.jooq.impl.Utils.DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE;
import static org.jooq.impl.Utils.DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY;
import static org.jooq.impl.Utils.DATA_SELECT_INTO_TABLE;
import static org.jooq.impl.Utils.DATA_UNALIAS_ALIASES_IN_ORDER_BY;
import static org.jooq.impl.Utils.DATA_WINDOW_DEFINITIONS;
import static org.jooq.impl.Utils.DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES;
import static org.jooq.impl.Utils.fieldArray;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.jooq.Clause;
import org.jooq.Condition;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.GroupField;
import org.jooq.JoinType;
import org.jooq.Operator;
import org.jooq.Param;
import org.jooq.Record;
import org.jooq.Row;
import org.jooq.SQLDialect;
import org.jooq.Select;
import org.jooq.SelectQuery;
import org.jooq.SortField;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableLike;
import org.jooq.TableOnStep;
import org.jooq.TablePartitionByStep;
import org.jooq.WindowDefinition;
import org.jooq.exception.DataAccessException;
import org.jooq.tools.StringUtils;
/**
* A sub-select is a SELECT statement that can be combined with
* other SELECT statement in UNIONs and similar
* operations.
*
* @author Lukas Eder
*/
class SelectQueryImpl extends AbstractResultQuery implements SelectQuery {
/**
* Generated UID
*/
private static final long serialVersionUID = 1646393178384872967L;
private static final Clause[] CLAUSES = { SELECT };
private final WithImpl with;
private final SelectFieldList select;
private Table> into;
private String hint;
private String option;
private boolean distinct;
private final QueryPartList> distinctOn;
private boolean forUpdate;
private final QueryPartList> forUpdateOf;
private final TableList forUpdateOfTables;
private ForUpdateMode forUpdateMode;
private int forUpdateWait;
private boolean forShare;
/* [pro] xx
xxxxxxx xxxxxxx xxxxxxxxxxxxxxxx
xxxxxxx xxxxxxx xxxxxxxxxxxxx
xx [/pro] */
private final TableList from;
private final ConditionProviderImpl condition;
private final ConditionProviderImpl connectBy;
private boolean connectByNoCycle;
private final ConditionProviderImpl connectByStartWith;
private boolean grouping;
private final QueryPartList groupBy;
private final ConditionProviderImpl having;
private final WindowList window;
private final SortFieldList orderBy;
private boolean orderBySiblings;
private final QueryPartList> seek;
private boolean seekBefore;
private final Limit limit;
private final List unionOp;
private final List>> union;
private final SortFieldList unionOrderBy;
private boolean unionOrderBySiblings; // [#3579] TODO
private final QueryPartList> unionSeek;
private boolean unionSeekBefore; // [#3579] TODO
private final Limit unionLimit;
SelectQueryImpl(WithImpl with, Configuration configuration) {
this(with, configuration, null);
}
SelectQueryImpl(WithImpl with, Configuration configuration, boolean distinct) {
this(with, configuration, null, distinct);
}
SelectQueryImpl(WithImpl with, Configuration configuration, TableLike extends R> from) {
this(with, configuration, from, false);
}
SelectQueryImpl(WithImpl with, Configuration configuration, TableLike extends R> from, boolean distinct) {
super(configuration);
this.with = with;
this.distinct = distinct;
this.distinctOn = new QueryPartList>();
this.select = new SelectFieldList();
this.from = new TableList();
this.condition = new ConditionProviderImpl();
this.connectBy = new ConditionProviderImpl();
this.connectByStartWith = new ConditionProviderImpl();
this.groupBy = new QueryPartList();
this.having = new ConditionProviderImpl();
this.window = new WindowList();
this.orderBy = new SortFieldList();
this.seek = new QueryPartList>();
this.limit = new Limit();
this.unionOp = new ArrayList();
this.union = new ArrayList>>();
this.unionOrderBy = new SortFieldList();
this.unionSeek = new QueryPartList>();
this.unionLimit = new Limit();
if (from != null) {
this.from.add(from.asTable());
}
this.forUpdateOf = new QueryPartList>();
this.forUpdateOfTables = new TableList();
}
@Override
public final int fetchCount() throws DataAccessException {
return DSL.using(configuration()).fetchCount(this);
}
@SuppressWarnings("unchecked")
@Override
public final Field asField() {
if (getSelect().size() != 1) {
throw new IllegalStateException("Can only use single-column ResultProviderQuery as a field");
}
return new ScalarSubquery(this, (DataType) getSelect().get(0).getDataType());
}
@Override
public final Field asField(String alias) {
return this. asField().as(alias);
}
@Override
public final Row fieldsRow() {
return asTable().fieldsRow();
}
@Override
public final Field field(Field field) {
return asTable().field(field);
}
@Override
public final Field> field(String string) {
return asTable().field(string);
}
@Override
public final Field> field(int index) {
return asTable().field(index);
}
@Override
public final Field>[] fields() {
return asTable().fields();
}
@Override
public final Table asTable() {
// Its usually better to alias nested selects that are used in
// the FROM clause of a query
return new DerivedTable(this).as("alias_" + Utils.hash(this));
}
@Override
public final Table asTable(String alias) {
return new DerivedTable(this).as(alias);
}
@Override
public final Table asTable(String alias, String... fieldAliases) {
return new DerivedTable(this).as(alias, fieldAliases);
}
@Override
protected final Field>[] getFields(ResultSetMetaData meta) {
// [#1808] TODO: Restrict this field list, in case a restricting fetch()
// method was called to get here
List> fields = getSelect();
// If no projection was specified explicitly, create fields from result
// set meta data instead. This is typically the case for SELECT * ...
if (fields.isEmpty()) {
Configuration configuration = configuration();
return new MetaDataFieldProvider(configuration, meta).getFields();
}
return fieldArray(fields);
}
@Override
public final Clause[] clauses(Context> ctx) {
return CLAUSES;
}
@Override
public final void accept(Context> context) {
SQLDialect dialect = context.dialect();
SQLDialect family = context.family();
// [#2791] TODO: Instead of explicitly manipulating these data() objects, future versions
// of jOOQ should implement a push / pop semantics to clearly delimit such scope.
Object renderTrailingLimit = context.data(DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE);
try {
if (renderTrailingLimit != null)
context.data().remove(DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE);
if (into != null
&& context.data(DATA_OMIT_INTO_CLAUSE) == null
&& asList(CUBRID, DERBY, FIREBIRD, H2, MARIADB, MYSQL, POSTGRES, SQLITE).contains(family)) {
context.data(DATA_OMIT_INTO_CLAUSE, true);
context.visit(DSL.createTable(into).as(this));
context.data().remove(DATA_OMIT_INTO_CLAUSE);
return;
}
if (with != null)
context.visit(with).formatSeparator();
pushWindow(context);
Boolean wrapDerivedTables = (Boolean) context.data(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES);
if (TRUE.equals(wrapDerivedTables)) {
context.sql("(")
.formatIndentStart()
.formatNewLine()
.data(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES, null);
}
switch (dialect) {
/* [pro] xx
xx xxxxxx xxxxx xxx xxxxxx xxxxxxxxxxxxxx xxxx xxxxx xxxxxx xxxxxx
xxxx xxxxxxx
xxxx xxxxxxxxxx
xxxx xxxxxxxxxx
xxxx xxxxxxxxxx
xx xxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxx
xx xxxx xxxx xxxxx xxx xxx xxxxxxxxxxxxx
xxxx xxxx
xxxx xxxxxx
xxxx xxxxxxx x
xx xxx xxxxxxxx xxxxxxxx x xxxxxx xxxxx xxxxxxx xxxxxxx
xx xxxxxx xxx xxxxxxx xxxx xxxxxx
xx xxxxxxxxxxxxxxxxxxxxxxxx xx xxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xx xxxxxxxx xxx xx xx xxxxxxxxx
xxxx xx xxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxx
x
xx xxxxxx xxx xxx xxx xxxxxx xxxxxxx x xxx xxxxxx xxxxxxx xxxxxx
xx xxxxxx xxx xx xxxxxxxxx xx xxx xxxxxxx xxx xx xxx
xxxx xxxxxxx
xxxx xxxxxxxxxxx
xxxx xxxx
xxxx xxxxxxxxxxxxxx x
xx xxxxxx xxx xxxxxxxx xxxxxxx xxxxxx xxx xxxxxxx xxxx xxxxxx
xx xxxxxxxxxxxxxxxxxxxxxxxx xx xxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
xx xxxxxx xxxxxxxxxx
xxxx xx xxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxx
x
xx xxxxxxxx xxx xxxx xx xxxxx xxxxxxx
xxxx xxxxxxxxx
xx xxxxxx xxx xxx xx xxxxx xx xxxxxxx xxx xxxx xxxxxxx
xxxx xxxxxxx x
xx xxxxxx xxx xxxxxxxx xxxxxxx xxxxxx xxx xxxxxxx xxxx xxxxxx
xx xxxxxxxxxxxxxxxxxxxxxxxxxxxx xx xxxxxxx xx xxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
xx xxxxxx xxxxxxxxxx
xxxx xx xxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxx
x
xx [/pro] */
// By default, render the dialect's limit clause
default: {
toSQLReferenceLimitDefault(context);
break;
}
}
// [#1296] FOR UPDATE is simulated in some dialects using ResultSet.CONCUR_UPDATABLE
if (forUpdate && !asList(CUBRID).contains(family)) {
context.formatSeparator()
.keyword("for update");
if (!forUpdateOf.isEmpty()) {
context.sql(" ").keyword("of").sql(" ");
Utils.fieldNames(context, forUpdateOf);
}
else if (!forUpdateOfTables.isEmpty()) {
context.sql(" ").keyword("of").sql(" ");
switch (family) {
// Some dialects don't allow for an OF [table-names] clause
// It can be emulated by listing the table's fields, though
/* [pro] xx
xxxx xxxx
xxxx xxxxxxxxx
xxxx xxxxxxx
xxxx xxxxxxx
xx [/pro] */
case DERBY: {
forUpdateOfTables.toSQLFieldNames(context);
break;
}
// Render the OF [table-names] clause
default:
Utils.tableNames(context, forUpdateOfTables);
break;
}
}
// [#3186] Firebird's FOR UPDATE clause has a different semantics. To achieve "regular"
// FOR UPDATE semantics, we should use FOR UPDATE WITH LOCK
if (family == FIREBIRD) {
context.sql(" ").keyword("with lock");
}
if (forUpdateMode != null) {
context.sql(" ");
context.keyword(forUpdateMode.toSQL());
if (forUpdateMode == ForUpdateMode.WAIT) {
context.sql(" ");
context.sql(forUpdateWait);
}
}
}
else if (forShare) {
switch (dialect) {
// MySQL has a non-standard implementation for the "FOR SHARE" clause
case MARIADB:
case MYSQL:
context.formatSeparator()
.keyword("lock in share mode");
break;
// Postgres is known to implement the "FOR SHARE" clause like this
default:
context.formatSeparator()
.keyword("for share");
break;
}
}
/* [pro] xx
xx xxxxxxx xxx xxxxxx x xxx xxxxxx xxxx xxxxx xxxxxx x xxxx xxxx xxxx xxxxxxx
xxxx xx xxxxxxxxxxxxxxxxx x
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxx xxxxx xxxxxxxxx
x
xxxx xx xxxxxxxxxxxxxx x
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxx xxxx xxxxxxx
x
xx [/pro] */
// [#1952] SQL Server OPTION() clauses as well as many other optional
// end-of-query clauses are appended to the end of a query
if (!StringUtils.isBlank(option)) {
context.formatSeparator()
.sql(option);
}
if (TRUE.equals(wrapDerivedTables)) {
context.formatIndentEnd()
.formatNewLine()
.sql(")")
.data(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES, true);
}
/* [pro] xx
xx xxxx xxxxxxx
xx xxxxxxxxxxxx
xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x
xxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxx xxxxxxxxxx x xxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xx xxxxxxxxxxx xx xxxxx
xxxxxxxxxx x xxxxx
xx xxxxxxxxxxx xx xxxx
xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xx xxxxx x
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxx
xxxxxx xx
xxxxxxxxxxxxxxxxxxx
x
xxxxxxxxxxxxxxxxxxxxxxxxx
x
xx [/pro] */
}
finally {
if (renderTrailingLimit != null)
context.data(DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE, renderTrailingLimit);
}
}
@SuppressWarnings("unchecked")
private final void pushWindow(Context> context) {
// [#531] [#2790] Make the WINDOW clause available to the SELECT clause
// to be able to inline window definitions if the WINDOW clause is not
// supported.
if (!getWindow().isEmpty()) {
((Map