|
20 | 20 | import com.querydsl.core.types.Ops; |
21 | 21 | import com.querydsl.core.types.Path; |
22 | 22 | import com.querydsl.core.types.SubQueryExpression; |
| 23 | +import com.querydsl.core.types.Template; |
| 24 | +import com.querydsl.core.types.TemplateFactory; |
23 | 25 | import com.querydsl.core.types.dsl.BooleanExpression; |
24 | 26 | import com.querydsl.core.types.dsl.DateExpression; |
25 | 27 | import com.querydsl.core.types.dsl.DateTimeExpression; |
26 | 28 | import com.querydsl.core.types.dsl.Expressions; |
27 | 29 | import com.querydsl.core.types.dsl.NumberExpression; |
| 30 | +import com.querydsl.core.types.dsl.NumberTemplate; |
28 | 31 | import com.querydsl.core.types.dsl.SimpleExpression; |
| 32 | +import com.querydsl.core.types.dsl.SimpleTemplate; |
29 | 33 | import com.querydsl.core.types.dsl.StringExpression; |
| 34 | +import com.querydsl.core.types.dsl.StringTemplate; |
30 | 35 | import com.querydsl.core.types.dsl.Wildcard; |
| 36 | +import java.util.Arrays; |
31 | 37 | import java.util.EnumMap; |
32 | 38 | import java.util.List; |
33 | 39 | import java.util.Map; |
@@ -278,6 +284,92 @@ public static <T> RelationalFunctionCall<T> relationalFunctionCall( |
278 | 284 | return new RelationalFunctionCall<>(type, function, args); |
279 | 285 | } |
280 | 286 |
|
| 287 | + /** |
| 288 | + * Create a function call template string for the given function name and argument count. |
| 289 | + * |
| 290 | + * <p>The function name is inserted as-is, which allows fully qualified names such as {@code |
| 291 | + * schema.function}, {@code database.schema.function}, or {@code |
| 292 | + * linked_server.database.schema.function}. |
| 293 | + * |
| 294 | + * @param function function name (may contain dots for qualified names) |
| 295 | + * @param argCount number of arguments |
| 296 | + * @return template for the function call |
| 297 | + */ |
| 298 | + private static Template createFunctionCallTemplate(String function, int argCount) { |
| 299 | + var builder = new StringBuilder(); |
| 300 | + builder.append(function); |
| 301 | + builder.append("("); |
| 302 | + for (var i = 0; i < argCount; i++) { |
| 303 | + if (i > 0) { |
| 304 | + builder.append(", "); |
| 305 | + } |
| 306 | + builder.append("{").append(i).append("}"); |
| 307 | + } |
| 308 | + builder.append(")"); |
| 309 | + return TemplateFactory.DEFAULT.create(builder.toString()); |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * Create a scalar function call expression. |
| 314 | + * |
| 315 | + * <p>Unlike {@link #relationalFunctionCall(Class, String, Object...)}, this method creates a |
| 316 | + * scalar expression suitable for use in SELECT, WHERE, INSERT VALUES, and other non-FROM |
| 317 | + * contexts. |
| 318 | + * |
| 319 | + * <p>Supports fully qualified function names including cross-database calls: |
| 320 | + * |
| 321 | + * <ul> |
| 322 | + * <li>{@code function("my_function", arg)} → {@code my_function(?)} |
| 323 | + * <li>{@code function("dbo.my_function", arg1, arg2)} → {@code dbo.my_function(?, ?)} |
| 324 | + * <li>{@code function("other_db.dbo.my_function", arg)} → {@code |
| 325 | + * other_db.dbo.my_function(?)} |
| 326 | + * </ul> |
| 327 | + * |
| 328 | + * @param <T> return type |
| 329 | + * @param type return type class |
| 330 | + * @param function function name (may contain dots for schema-qualified names) |
| 331 | + * @param args function arguments |
| 332 | + * @return scalar function call expression |
| 333 | + */ |
| 334 | + public static <T> SimpleTemplate<T> function( |
| 335 | + Class<? extends T> type, String function, Object... args) { |
| 336 | + return Expressions.template( |
| 337 | + type, createFunctionCallTemplate(function, args.length), Arrays.asList(args)); |
| 338 | + } |
| 339 | + |
| 340 | + /** |
| 341 | + * Create a scalar function call expression that returns a String. |
| 342 | + * |
| 343 | + * <p>Supports fully qualified function names including cross-database calls. |
| 344 | + * |
| 345 | + * @param function function name (may contain dots for schema-qualified names) |
| 346 | + * @param args function arguments |
| 347 | + * @return string function call expression |
| 348 | + * @see #function(Class, String, Object...) |
| 349 | + */ |
| 350 | + public static StringTemplate stringFunction(String function, Object... args) { |
| 351 | + return Expressions.stringTemplate( |
| 352 | + createFunctionCallTemplate(function, args.length), Arrays.asList(args)); |
| 353 | + } |
| 354 | + |
| 355 | + /** |
| 356 | + * Create a scalar function call expression that returns a Number. |
| 357 | + * |
| 358 | + * <p>Supports fully qualified function names including cross-database calls. |
| 359 | + * |
| 360 | + * @param <T> number type |
| 361 | + * @param type return type class |
| 362 | + * @param function function name (may contain dots for schema-qualified names) |
| 363 | + * @param args function arguments |
| 364 | + * @return number function call expression |
| 365 | + * @see #function(Class, String, Object...) |
| 366 | + */ |
| 367 | + public static <T extends Number & Comparable<?>> NumberTemplate<T> numberFunction( |
| 368 | + Class<? extends T> type, String function, Object... args) { |
| 369 | + return Expressions.numberTemplate( |
| 370 | + type, createFunctionCallTemplate(function, args.length), Arrays.asList(args)); |
| 371 | + } |
| 372 | + |
281 | 373 | /** |
282 | 374 | * Create a nextval(sequence) expression |
283 | 375 | * |
|
0 commit comments