|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.community.dialect.function; |
| 6 | + |
| 7 | +import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor; |
| 8 | +import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators; |
| 9 | +import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers; |
| 10 | +import org.hibernate.sql.ast.SqlAstTranslator; |
| 11 | +import org.hibernate.sql.ast.spi.SqlAppender; |
| 12 | +import org.hibernate.sql.ast.tree.SqlAstNode; |
| 13 | +import org.hibernate.type.StandardBasicTypes; |
| 14 | +import org.hibernate.type.spi.TypeConfiguration; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +public class InterSystemsIRISLogFunction extends AbstractSqmSelfRenderingFunctionDescriptor { |
| 19 | + |
| 20 | + public InterSystemsIRISLogFunction(TypeConfiguration typeConfiguration) { |
| 21 | + super( |
| 22 | + "log", |
| 23 | + StandardArgumentsValidators.between(1, 2), |
| 24 | + StandardFunctionReturnTypeResolvers.invariant( |
| 25 | + typeConfiguration.getBasicTypeRegistry() |
| 26 | + .resolve( StandardBasicTypes.DOUBLE) |
| 27 | + ), |
| 28 | + null |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void render( |
| 34 | + SqlAppender sqlAppender, |
| 35 | + List<? extends SqlAstNode> arguments, |
| 36 | + SqlAstTranslator<?> walker) { |
| 37 | + |
| 38 | + if (arguments.size() == 1) { |
| 39 | + // LOG(x) → log(x) |
| 40 | + sqlAppender.appendSql("log("); |
| 41 | + arguments.get(0).accept(walker); |
| 42 | + sqlAppender.appendSql(")"); |
| 43 | + } |
| 44 | + else if (arguments.size() == 2) { |
| 45 | + // LOG(base, value) → (log(value) / log(base)) |
| 46 | + sqlAppender.appendSql("(log("); |
| 47 | + arguments.get(1).accept(walker); |
| 48 | + sqlAppender.appendSql(")/log("); |
| 49 | + arguments.get(0).accept(walker); |
| 50 | + sqlAppender.appendSql("))"); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments