Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@
import com.querydsl.core.types.Ops;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.SubQueryExpression;
import com.querydsl.core.types.Template;
import com.querydsl.core.types.TemplateFactory;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.DateExpression;
import com.querydsl.core.types.dsl.DateTimeExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberExpression;
import com.querydsl.core.types.dsl.NumberTemplate;
import com.querydsl.core.types.dsl.SimpleExpression;
import com.querydsl.core.types.dsl.SimpleTemplate;
import com.querydsl.core.types.dsl.StringExpression;
import com.querydsl.core.types.dsl.StringTemplate;
import com.querydsl.core.types.dsl.Wildcard;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -278,6 +284,92 @@ public static <T> RelationalFunctionCall<T> relationalFunctionCall(
return new RelationalFunctionCall<>(type, function, args);
}

/**
* Create a function call template string for the given function name and argument count.
*
* <p>The function name is inserted as-is, which allows fully qualified names such as {@code
* schema.function}, {@code database.schema.function}, or {@code
* linked_server.database.schema.function}.
*
* @param function function name (may contain dots for qualified names)
* @param argCount number of arguments
* @return template for the function call
*/
private static Template createFunctionCallTemplate(String function, int argCount) {
var builder = new StringBuilder();
builder.append(function);
builder.append("(");
for (var i = 0; i < argCount; i++) {
if (i > 0) {
builder.append(", ");
}
builder.append("{").append(i).append("}");
}
builder.append(")");
return TemplateFactory.DEFAULT.create(builder.toString());
}

/**
* Create a scalar function call expression.
*
* <p>Unlike {@link #relationalFunctionCall(Class, String, Object...)}, this method creates a
* scalar expression suitable for use in SELECT, WHERE, INSERT VALUES, and other non-FROM
* contexts.
*
* <p>Supports fully qualified function names including cross-database calls:
*
* <ul>
* <li>{@code function("my_function", arg)} &rarr; {@code my_function(?)}
* <li>{@code function("dbo.my_function", arg1, arg2)} &rarr; {@code dbo.my_function(?, ?)}
* <li>{@code function("other_db.dbo.my_function", arg)} &rarr; {@code
* other_db.dbo.my_function(?)}
* </ul>
*
* @param <T> return type
* @param type return type class
* @param function function name (may contain dots for schema-qualified names)
* @param args function arguments
* @return scalar function call expression
*/
public static <T> SimpleTemplate<T> function(
Class<? extends T> type, String function, Object... args) {
return Expressions.template(
type, createFunctionCallTemplate(function, args.length), Arrays.asList(args));
}

/**
* Create a scalar function call expression that returns a String.
*
* <p>Supports fully qualified function names including cross-database calls.
*
* @param function function name (may contain dots for schema-qualified names)
* @param args function arguments
* @return string function call expression
* @see #function(Class, String, Object...)
*/
public static StringTemplate stringFunction(String function, Object... args) {
return Expressions.stringTemplate(
createFunctionCallTemplate(function, args.length), Arrays.asList(args));
}

/**
* Create a scalar function call expression that returns a Number.
*
* <p>Supports fully qualified function names including cross-database calls.
*
* @param <T> number type
* @param type return type class
* @param function function name (may contain dots for schema-qualified names)
* @param args function arguments
* @return number function call expression
* @see #function(Class, String, Object...)
*/
public static <T extends Number & Comparable<?>> NumberTemplate<T> numberFunction(
Class<? extends T> type, String function, Object... args) {
return Expressions.numberTemplate(
type, createFunctionCallTemplate(function, args.length), Arrays.asList(args));
}

/**
* Create a nextval(sequence) expression
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* 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.
*/
package com.querydsl.sql;

import static org.assertj.core.api.Assertions.assertThat;

import com.querydsl.core.types.ConstantImpl;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberTemplate;
import com.querydsl.core.types.dsl.SimpleTemplate;
import com.querydsl.core.types.dsl.StringTemplate;
import com.querydsl.sql.dml.SQLInsertClause;
import com.querydsl.sql.domain.QSurvey;
import org.junit.Test;

public class SQLExpressionsFunctionTest {

private static final QSurvey survey = QSurvey.survey;

@Test
public void simpleFunction() {
SimpleTemplate<String> expr = SQLExpressions.function(String.class, "my_function", survey.name);
assertThat(expr.toString()).containsIgnoringCase("my_function(SURVEY.NAME)");
}

@Test
public void twoPartName() {
StringTemplate expr =
SQLExpressions.stringFunction("dbo.my_function", survey.name, survey.name2);
assertThat(expr.toString()).containsIgnoringCase("dbo.my_function(SURVEY.NAME, SURVEY.NAME2)");
}

@Test
public void threePartName() {
StringTemplate expr =
SQLExpressions.stringFunction(
"external_db.dbo.my_function",
ConstantImpl.create("PARAM"),
survey.name,
ConstantImpl.create(""));
assertThat(expr.toString())
.containsIgnoringCase("external_db.dbo.my_function(PARAM, SURVEY.NAME, )");
}

@Test
public void numberFunction() {
NumberTemplate<Integer> expr =
SQLExpressions.numberFunction(Integer.class, "dbo.calculate", survey.id);
assertThat(expr.toString()).containsIgnoringCase("dbo.calculate(SURVEY.ID)");
}

@Test
public void stringFunction() {
StringTemplate expr = SQLExpressions.stringFunction("my_encrypt", survey.name);
assertThat(expr.toString()).containsIgnoringCase("my_encrypt(SURVEY.NAME)");
}

@Test
public void functionInSelect() {
SQLQuery<?> query = new SQLQuery<Void>(SQLServerTemplates.DEFAULT);
query.select(SQLExpressions.stringFunction("dbo.my_function", survey.name)).from(survey);
assertThat(query.toString())
.isEqualTo(
"""
select dbo.my_function(SURVEY.NAME)
from SURVEY SURVEY\
""");
}

@Test
public void functionInWhere() {
SQLQuery<?> query = new SQLQuery<Void>(SQLServerTemplates.DEFAULT);
query
.select(survey.name)
.from(survey)
.where(SQLExpressions.stringFunction("dbo.decrypt", survey.name).eq("expected"));
assertThat(query.toString())
.isEqualTo(
"""
select SURVEY.NAME
from SURVEY SURVEY
where dbo.decrypt(SURVEY.NAME) = ?\
""");
}

@Test
public void functionInInsertValues() {
var config = new Configuration(SQLServerTemplates.DEFAULT);
SQLInsertClause insert = new SQLInsertClause((java.sql.Connection) null, config, survey);
insert.set(
survey.name, SQLExpressions.stringFunction("dbo.encrypt", Expressions.constant("value")));

assertThat(insert.toString()).contains("dbo.encrypt(?)");
}

@Test
public void fourPartName() {
StringTemplate expr =
SQLExpressions.stringFunction("linked_server.external_db.dbo.my_function", survey.name);
assertThat(expr.toString())
.containsIgnoringCase("linked_server.external_db.dbo.my_function(SURVEY.NAME)");
}

@Test
public void multipleArguments() {
StringTemplate expr =
SQLExpressions.stringFunction(
"schema.func",
ConstantImpl.create("A"),
survey.name,
survey.id,
ConstantImpl.create("B"));
assertThat(expr.toString()).containsIgnoringCase("schema.func(A, SURVEY.NAME, SURVEY.ID, B)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,40 @@ on not (SURVEY.NAME like tokFunc.prop escape '\\')\
""");
}

@Test
public void scalarFunctionCall() {
var expr =
SQLExpressions.select(
SQLExpressions.stringFunction("external_db.dbo.my_function", survey.name))
.from(survey);

var serializer = new SQLSerializer(new Configuration(new SQLServerTemplates()));
serializer.serialize(expr.getMetadata(), false);
assertThat(serializer.toString())
.isEqualTo(
"""
select external_db.dbo.my_function(SURVEY.NAME)
from SURVEY SURVEY\
""");
}

@Test
public void scalarFunctionCallInWhere() {
var query = new SQLQuery<Void>(new Configuration(new SQLServerTemplates()));
query
.select(survey.name)
.from(survey)
.where(SQLExpressions.stringFunction("dbo.decrypt", survey.name).eq("test"));

assertThat(query.toString())
.isEqualTo(
"""
select SURVEY.NAME
from SURVEY SURVEY
where dbo.decrypt(SURVEY.NAME) = ?\
""");
}

@Test
public void functionCall3() {
RelationalFunctionCall<String> func =
Expand Down
Loading