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-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.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_EXCEPT_ALL;
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_INTERSECT_ALL;
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.EXCEPT_ALL;
import static org.jooq.impl.CombineOperator.INTERSECT;
import static org.jooq.impl.CombineOperator.INTERSECT_ALL;
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.Tools.fieldArray;
import static org.jooq.impl.Tools.hasAmbiguousNames;
import static org.jooq.impl.Tools.DataKey.DATA_COLLECTED_SEMI_ANTI_JOIN;
import static org.jooq.impl.Tools.DataKey.DATA_COLLECT_SEMI_ANTI_JOIN;
import static org.jooq.impl.Tools.DataKey.DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST;
import static org.jooq.impl.Tools.DataKey.DATA_LOCALLY_SCOPED_DATA_MAP;
import static org.jooq.impl.Tools.DataKey.DATA_OMIT_INTO_CLAUSE;
import static org.jooq.impl.Tools.DataKey.DATA_OVERRIDE_ALIASES_IN_ORDER_BY;
import static org.jooq.impl.Tools.DataKey.DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE;
import static org.jooq.impl.Tools.DataKey.DATA_ROW_VALUE_EXPRESSION_PREDICATE_SUBQUERY;
import static org.jooq.impl.Tools.DataKey.DATA_SELECT_INTO_TABLE;
import static org.jooq.impl.Tools.DataKey.DATA_UNALIAS_ALIASES_IN_ORDER_BY;
import static org.jooq.impl.Tools.DataKey.DATA_WINDOW_DEFINITIONS;
import static org.jooq.impl.Tools.DataKey.DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;
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.Name;
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.SelectField;
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.impl.Tools.DataKey;
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
*/
final 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;
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(Configuration configuration, WithImpl with) {
this(configuration, with, null);
}
SelectQueryImpl(Configuration configuration, WithImpl with, boolean distinct) {
this(configuration, with, null, distinct);
}
SelectQueryImpl(Configuration configuration, WithImpl with, TableLike extends R> from) {
this(configuration, with, from, false);
}
SelectQueryImpl(Configuration configuration, WithImpl with, 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() {
List> s = getSelect();
if (s.size() != 1) {
throw new IllegalStateException("Can only use single-column ResultProviderQuery as a field");
}
return new ScalarSubquery(this, (DataType) s.get(0).getDataType());
}
@Override
public final Field asField(String alias) {
return this. asField().as(alias);
}
@Override
public Field asField(Function super Field, ? extends String> aliasFunction) {
return this. asField().as(aliasFunction);
}
@Override
public final Row fieldsRow() {
return asTable().fieldsRow();
}
@Override
public final Stream> fieldStream() {
return Stream.of(fields());
}
@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(String name, Class type) {
return asTable().field(name, type);
}
@Override
public final Field field(String name, DataType dataType) {
return asTable().field(name, dataType);
}
@Override
public final Field> field(Name string) {
return asTable().field(string);
}
@Override
public final Field field(Name name, Class type) {
return asTable().field(name, type);
}
@Override
public final Field field(Name name, DataType dataType) {
return asTable().field(name, dataType);
}
@Override
public final Field> field(int index) {
return asTable().field(index);
}
@Override
public final Field field(int index, Class type) {
return asTable().field(index, type);
}
@Override
public final Field field(int index, DataType dataType) {
return asTable().field(index, dataType);
}
@Override
public final Field>[] fields() {
return asTable().fields();
}
@Override
public final Field>[] fields(Field>... fields) {
return asTable().fields(fields);
}
@Override
public final Field>[] fields(String... fieldNames) {
return asTable().fields(fieldNames);
}
@Override
public final Field>[] fields(Name... fieldNames) {
return asTable().fields(fieldNames);
}
@Override
public final Field>[] fields(int... fieldIndexes) {
return asTable().fields(fieldIndexes);
}
@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_" + Tools.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
public final Table asTable(String alias, Function super Field>, ? extends String> aliasFunction) {
return new DerivedTable(this).as(alias, aliasFunction);
}
@Override
public final Table asTable(String alias, BiFunction super Field>, ? super Integer, ? extends String> aliasFunction) {
return new DerivedTable(this).as(alias, aliasFunction);
}
@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);
Object localDataMap = context.data(DATA_LOCALLY_SCOPED_DATA_MAP);
try {
if (renderTrailingLimit != null)
context.data().remove(DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE);
context.data(DATA_LOCALLY_SCOPED_DATA_MAP, new HashMap