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.util.Arrays.asList;
import static org.jooq.SQLDialect.CUBRID;
// ...
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.impl.DSL.name;
import static org.jooq.impl.Term.LIST_AGG;
import static org.jooq.impl.Term.ROW_NUMBER;
import static org.jooq.impl.Utils.DATA_LOCALLY_SCOPED_DATA_MAP;
import static org.jooq.impl.Utils.DATA_WINDOW_DEFINITIONS;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import org.jooq.AggregateFunction;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.Name;
import org.jooq.OrderedAggregateFunction;
import org.jooq.QueryPart;
import org.jooq.SQLDialect;
import org.jooq.SortField;
import org.jooq.WindowBeforeOverStep;
import org.jooq.WindowDefinition;
import org.jooq.WindowFinalStep;
import org.jooq.WindowIgnoreNullsStep;
import org.jooq.WindowOrderByStep;
import org.jooq.WindowOverStep;
import org.jooq.WindowPartitionByStep;
import org.jooq.WindowRowsAndStep;
import org.jooq.WindowRowsStep;
import org.jooq.WindowSpecification;
// ...
/**
* A field that handles built-in functions, aggregate functions, and window
* functions.
*
* @author Lukas Eder
*/
class Function extends AbstractField implements
// Cascading interface implementations for aggregate function behaviour
OrderedAggregateFunction,
AggregateFunction,
WindowBeforeOverStep,
// and for window function behaviour
WindowIgnoreNullsStep,
WindowPartitionByStep,
WindowRowsStep,
WindowRowsAndStep
{
private static final long serialVersionUID = 347252741712134044L;
// Mutually exclusive attributes: super.getName(), this.name, this.term
private final Name name;
private final Term term;
// Other attributes
private final QueryPartList arguments;
private final boolean distinct;
private final SortFieldList withinGroupOrderBy;
private final SortFieldList keepDenseRankOrderBy;
private WindowSpecificationImpl windowSpecification;
private WindowDefinitionImpl windowDefinition;
private Name windowName;
private boolean first;
private boolean ignoreNulls;
private boolean respectNulls;
// -------------------------------------------------------------------------
// XXX Constructors
// -------------------------------------------------------------------------
Function(String name, DataType type, QueryPart... arguments) {
this(name, false, type, arguments);
}
Function(Term term, DataType type, QueryPart... arguments) {
this(term, false, type, arguments);
}
Function(Name name, DataType type, QueryPart... arguments) {
this(name, false, type, arguments);
}
Function(String name, boolean distinct, DataType type, QueryPart... arguments) {
super(name, type);
this.term = null;
this.name = null;
this.distinct = distinct;
this.arguments = new QueryPartList(arguments);
this.keepDenseRankOrderBy = new SortFieldList();
this.withinGroupOrderBy = new SortFieldList();
}
Function(Term term, boolean distinct, DataType type, QueryPart... arguments) {
super(term.name().toLowerCase(), type);
this.term = term;
this.name = null;
this.distinct = distinct;
this.arguments = new QueryPartList(arguments);
this.keepDenseRankOrderBy = new SortFieldList();
this.withinGroupOrderBy = new SortFieldList();
}
Function(Name name, boolean distinct, DataType type, QueryPart... arguments) {
super(last(name.getName()), type);
this.term = null;
this.name = name;
this.distinct = distinct;
this.arguments = new QueryPartList(arguments);
this.keepDenseRankOrderBy = new SortFieldList();
this.withinGroupOrderBy = new SortFieldList();
}
private static String last(String... strings) {
if (strings != null && strings.length > 0) {
return strings[strings.length - 1];
}
return null;
}
// -------------------------------------------------------------------------
// XXX QueryPart API
// -------------------------------------------------------------------------
//
// @Override
// public final void bind(BindContext ctx) {
// if (term == LIST_AGG && asList(CUBRID, H2, HSQLDB, MARIADB, MYSQL).contains(ctx.configuration().dialect())) {
// ctx.visit(arguments.get(0));
// ctx.visit(withinGroupOrderBy);
//
// if (arguments.size() > 1) {
// ctx.visit(arguments.get(1));
// }
// }
// else {
// ctx.visit(arguments)
// .visit(keepDenseRankOrderBy)
// .visit(withinGroupOrderBy);
//
// QueryPart window = window(ctx);
// if (window != null)
// ctx.visit(window);
// }
// }
@Override
public final void accept(Context> ctx) {
if (term == LIST_AGG && asList(CUBRID, H2, HSQLDB, MARIADB, MYSQL).contains(ctx.configuration().dialect())) {
toSQLGroupConcat(ctx);
}
else if (term == LIST_AGG && asList(POSTGRES).contains(ctx.configuration().dialect())) {
toSQLStringAgg(ctx);
toSQLOverClause(ctx);
}
/* [pro] xx
xxxx xx xxxxx xx xxxxxxxx xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x
xxxxxxxxxxxxxxxxx
x
xx [/pro] */
else {
toSQLArguments(ctx);
toSQLKeepDenseRankOrderByClause(ctx);
toSQLWithinGroupClause(ctx);
toSQLOverClause(ctx);
}
}
/* [pro] xx
xxx
x xxxxxxx xxxxxxxxxxxxxxxxxxxxx xxxxxxxxxx xxx xxx
xx
xxxxxxx xxxx xxxxxxxxxxxxxxxxxxxxxx xxxx x
xx xxxx xx x xxxxxxxx xxxx xx xxxx xxx xxxxx xxx xxxx xxxxxx
xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xx xxxxxxx xxxxx xx xxx xx xxxxxxxxxxxxxxx xx
xx xxxxxxxxxxxxxxxxx x xx x
xxxxxxxxxxxxxxxxxxxxxxx
x
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xx xxxxxxxxxxxxxxxxx x xx x
xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxx xxx
x
xxxxxxxxxxxxxxxxxxxxxxxxxxxx
xx xxxxxxxxxxxxxxxxx x xx x
xxxxxxxxxxxxx xx xxxxxx
x
xxxxxxxxxxxxx xx xxxxxxx
xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x
xxxxxxxxx xxxxxxxxxxxxxxxxx xxxxxxxxxx xx
xxxxxxxxxxxxxxxxxxxxxxxxxxx
x
xxxxxxxxxxxxx xx xxxxxx
xxxxxxxxx xxxxxxxxxxxxxxxxxxxxxx xxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxx xx xxxxxxxxxxxx
xx xxxxxxxxxxxxxxxxx x xx x
xxxxxxxxxx xxx
xx xxx xxxxxxxxx xx xx xxxx xxxxx xxx xxx
xx xxx xxxxxxx xxx xxxxxx x
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x xxx
xxxxxxxxxxxxx xx xxxxxx
x
x
xx [/pro] */
/**
* [#1275] LIST_AGG simulation for Postgres, Sybase
*/
private void toSQLStringAgg(Context> ctx) {
toSQLFunctionName(ctx);
ctx.sql("(");
if (distinct) {
ctx.keyword("distinct").sql(" ");
}
// The explicit cast is needed in Postgres
ctx.visit(((Field>) arguments.get(0)).cast(String.class));
if (arguments.size() > 1) {
ctx.sql(", ");
ctx.visit(arguments.get(1));
}
else {
ctx.sql(", ''");
}
if (!withinGroupOrderBy.isEmpty()) {
ctx.sql(" ").keyword("order by").sql(" ")
.visit(withinGroupOrderBy);
}
ctx.sql(")");
}
/**
* [#1273] LIST_AGG simulation for MySQL and CUBRID
*/
private final void toSQLGroupConcat(Context> ctx) {
toSQLFunctionName(ctx);
ctx.sql("(");
if (distinct) {
ctx.keyword("distinct").sql(" ");
}
ctx.visit(arguments.get(0));
if (!withinGroupOrderBy.isEmpty()) {
ctx.sql(" ").keyword("order by").sql(" ")
.visit(withinGroupOrderBy);
}
if (arguments.size() > 1) {
ctx.sql(" ").keyword("separator").sql(" ")
.visit(arguments.get(1));
}
ctx.sql(")");
}
private final void toSQLOverClause(Context> ctx) {
QueryPart window = window(ctx);
// Render this clause only if needed
if (window == null)
return;
// [#1524] Don't render this clause where it is not supported
if (term == ROW_NUMBER && ctx.configuration().dialect() == HSQLDB)
return;
ctx.sql(" ")
.keyword("over")
.sql(" (")
.visit(window)
.sql(")");
}
@SuppressWarnings("unchecked")
private final QueryPart window(Context> ctx) {
if (windowSpecification != null)
return windowSpecification;
if (windowDefinition != null)
return windowDefinition;
// [#531] Inline window specifications if the WINDOW clause is not supported
if (windowName != null) {
if (asList(POSTGRES).contains(ctx.configuration().dialect().family())) {
return windowName;
}
Map