Search in sources :

Example 1 with CountFunction

use of org.hibernate.dialect.function.CountFunction in project hibernate-orm by hibernate.

the class SQLServerDialect method initializeFunctionRegistry.

@Override
public void initializeFunctionRegistry(QueryEngine queryEngine) {
    super.initializeFunctionRegistry(queryEngine);
    final BasicTypeRegistry basicTypeRegistry = queryEngine.getTypeConfiguration().getBasicTypeRegistry();
    BasicType<Date> dateType = basicTypeRegistry.resolve(StandardBasicTypes.DATE);
    BasicType<Date> timeType = basicTypeRegistry.resolve(StandardBasicTypes.TIME);
    BasicType<Date> timestampType = basicTypeRegistry.resolve(StandardBasicTypes.TIMESTAMP);
    CommonFunctionFactory functionFactory = new CommonFunctionFactory(queryEngine);
    // For SQL-Server we need to cast certain arguments to varchar(max) to be able to concat them
    queryEngine.getSqmFunctionRegistry().register("count", new CountFunction(this, queryEngine.getTypeConfiguration(), SqlAstNodeRenderingMode.DEFAULT, "+", "varchar(max)", false));
    // AVG by default uses the input type, so we possibly need to cast the argument type, hence a special function
    functionFactory.avg_castingNonDoubleArguments(this, SqlAstNodeRenderingMode.DEFAULT);
    functionFactory.truncate_round();
    functionFactory.everyAny_minMaxIif();
    functionFactory.bitLength_pattern("datalength(?1) * 8");
    if (getVersion().isSameOrAfter(10)) {
        functionFactory.locate_charindex();
        functionFactory.stddevPopSamp_stdevp();
        functionFactory.varPopSamp_varp();
    }
    if (getVersion().isSameOrAfter(11)) {
        queryEngine.getSqmFunctionRegistry().register("format", new SQLServerFormatEmulation(this, queryEngine.getTypeConfiguration()));
        // actually translate() was added in 2017 but
        // it's not worth adding a new dialect for that!
        functionFactory.translate();
        functionFactory.median_percentileCont(true);
        queryEngine.getSqmFunctionRegistry().namedDescriptorBuilder("datefromparts").setInvariantType(dateType).setExactArgumentCount(3).setParameterTypes(INTEGER).register();
        queryEngine.getSqmFunctionRegistry().namedDescriptorBuilder("timefromparts").setInvariantType(timeType).setExactArgumentCount(5).setParameterTypes(INTEGER).register();
        queryEngine.getSqmFunctionRegistry().namedDescriptorBuilder("smalldatetimefromparts").setInvariantType(timestampType).setExactArgumentCount(5).setParameterTypes(INTEGER).register();
        queryEngine.getSqmFunctionRegistry().namedDescriptorBuilder("datetimefromparts").setInvariantType(timestampType).setExactArgumentCount(7).setParameterTypes(INTEGER).register();
        queryEngine.getSqmFunctionRegistry().namedDescriptorBuilder("datetime2fromparts").setInvariantType(timestampType).setExactArgumentCount(8).setParameterTypes(INTEGER).register();
        queryEngine.getSqmFunctionRegistry().namedDescriptorBuilder("datetimeoffsetfromparts").setInvariantType(timestampType).setExactArgumentCount(10).setParameterTypes(INTEGER).register();
    }
    functionFactory.windowFunctions();
    functionFactory.inverseDistributionOrderedSetAggregates_windowEmulation();
    functionFactory.hypotheticalOrderedSetAggregates_windowEmulation();
    if (getVersion().isSameOrAfter(14)) {
        functionFactory.listagg_stringAggWithinGroup("varchar(max)");
    }
}
Also used : CountFunction(org.hibernate.dialect.function.CountFunction) CommonFunctionFactory(org.hibernate.dialect.function.CommonFunctionFactory) SQLServerFormatEmulation(org.hibernate.dialect.function.SQLServerFormatEmulation) BasicTypeRegistry(org.hibernate.type.BasicTypeRegistry) Date(java.util.Date) DateTimeUtils.appendAsDate(org.hibernate.type.descriptor.DateTimeUtils.appendAsDate)

Example 2 with CountFunction

use of org.hibernate.dialect.function.CountFunction in project hibernate-orm by hibernate.

the class DerbyDialect method initializeFunctionRegistry.

@Override
public void initializeFunctionRegistry(QueryEngine queryEngine) {
    super.initializeFunctionRegistry(queryEngine);
    final BasicTypeRegistry basicTypeRegistry = queryEngine.getTypeConfiguration().getBasicTypeRegistry();
    final BasicType<String> stringType = basicTypeRegistry.resolve(StandardBasicTypes.STRING);
    CommonFunctionFactory functionFactory = new CommonFunctionFactory(queryEngine);
    // Derby needs an actual argument type for aggregates like SUM, AVG, MIN, MAX to determine the result type
    functionFactory.aggregates(this, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER);
    queryEngine.getSqmFunctionRegistry().register("count", new CountFunction(this, queryEngine.getTypeConfiguration(), SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER, "||", getCastTypeName(stringType, null, null, null), true));
    // AVG by default uses the input type, so we possibly need to cast the argument type, hence a special function
    functionFactory.avg_castingNonDoubleArguments(this, SqlAstNodeRenderingMode.DEFAULT);
    functionFactory.concat_pipeOperator();
    functionFactory.cot();
    functionFactory.chr_char();
    functionFactory.degrees();
    functionFactory.radians();
    functionFactory.log10();
    functionFactory.sinh();
    functionFactory.cosh();
    functionFactory.tanh();
    functionFactory.pi();
    functionFactory.rand();
    functionFactory.trim1();
    functionFactory.hourMinuteSecond();
    functionFactory.yearMonthDay();
    functionFactory.varPopSamp();
    functionFactory.stddevPopSamp();
    functionFactory.substring_substr();
    functionFactory.leftRight_substrLength();
    functionFactory.characterLength_length(SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER);
    functionFactory.power_expLn();
    functionFactory.round_floor();
    functionFactory.bitLength_pattern("length(?1)*8");
    queryEngine.getSqmFunctionRegistry().register("concat", new CastingConcatFunction(this, "||", true, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER, queryEngine.getTypeConfiguration()));
    // no way I can see to pad with anything other than spaces
    queryEngine.getSqmFunctionRegistry().register("lpad", new DerbyLpadEmulation(queryEngine.getTypeConfiguration()));
    queryEngine.getSqmFunctionRegistry().register("rpad", new DerbyRpadEmulation(queryEngine.getTypeConfiguration()));
    queryEngine.getSqmFunctionRegistry().register("least", new CaseLeastGreatestEmulation(true));
    queryEngine.getSqmFunctionRegistry().register("greatest", new CaseLeastGreatestEmulation(false));
    queryEngine.getSqmFunctionRegistry().register("overlay", new InsertSubstringOverlayEmulation(queryEngine.getTypeConfiguration(), true));
}
Also used : InsertSubstringOverlayEmulation(org.hibernate.dialect.function.InsertSubstringOverlayEmulation) DerbyLpadEmulation(org.hibernate.dialect.function.DerbyLpadEmulation) DerbyRpadEmulation(org.hibernate.dialect.function.DerbyRpadEmulation) CountFunction(org.hibernate.dialect.function.CountFunction) CommonFunctionFactory(org.hibernate.dialect.function.CommonFunctionFactory) CaseLeastGreatestEmulation(org.hibernate.dialect.function.CaseLeastGreatestEmulation) BasicTypeRegistry(org.hibernate.type.BasicTypeRegistry) CastingConcatFunction(org.hibernate.dialect.function.CastingConcatFunction)

Example 3 with CountFunction

use of org.hibernate.dialect.function.CountFunction in project hibernate-orm by hibernate.

the class DB2Dialect method initializeFunctionRegistry.

@Override
public void initializeFunctionRegistry(QueryEngine queryEngine) {
    super.initializeFunctionRegistry(queryEngine);
    CommonFunctionFactory functionFactory = new CommonFunctionFactory(queryEngine);
    // AVG by default uses the input type, so we possibly need to cast the argument type, hence a special function
    functionFactory.avg_castingNonDoubleArguments(this, SqlAstNodeRenderingMode.DEFAULT);
    functionFactory.cot();
    functionFactory.degrees();
    functionFactory.log();
    functionFactory.log10();
    functionFactory.radians();
    functionFactory.rand();
    functionFactory.soundex();
    functionFactory.trim2();
    functionFactory.space();
    functionFactory.repeat();
    queryEngine.getSqmFunctionRegistry().namedDescriptorBuilder("substr").setInvariantType(queryEngine.getTypeConfiguration().getBasicTypeRegistry().resolve(StandardBasicTypes.STRING)).setArgumentCountBetween(2, 4).setParameterTypes(FunctionParameterType.STRING, FunctionParameterType.INTEGER, FunctionParameterType.INTEGER, FunctionParameterType.ANY).setArgumentListSignature("(STRING string, INTEGER start[, INTEGER length[, units]])").register();
    queryEngine.getSqmFunctionRegistry().namedDescriptorBuilder("substring").setInvariantType(queryEngine.getTypeConfiguration().getBasicTypeRegistry().resolve(StandardBasicTypes.STRING)).setArgumentCountBetween(2, 4).setParameterTypes(FunctionParameterType.STRING, FunctionParameterType.INTEGER, FunctionParameterType.INTEGER, FunctionParameterType.ANY).setArgumentListSignature("(STRING string{ INTEGER from|,} start[{ INTEGER for|,} length[, units]])").register();
    functionFactory.translate();
    functionFactory.bitand();
    functionFactory.bitor();
    functionFactory.bitxor();
    functionFactory.bitnot();
    functionFactory.yearMonthDay();
    functionFactory.hourMinuteSecond();
    functionFactory.dayofweekmonthyear();
    functionFactory.weekQuarter();
    functionFactory.daynameMonthname();
    functionFactory.lastDay();
    functionFactory.toCharNumberDateTimestamp();
    functionFactory.dateTimeTimestamp();
    functionFactory.concat_pipeOperator();
    functionFactory.octetLength();
    functionFactory.ascii();
    functionFactory.char_chr();
    functionFactory.position();
    functionFactory.trunc();
    functionFactory.truncate();
    functionFactory.insert();
    functionFactory.overlayCharacterLength_overlay();
    functionFactory.median();
    functionFactory.stddev();
    functionFactory.stddevPopSamp();
    functionFactory.regrLinearRegressionAggregates();
    functionFactory.variance();
    functionFactory.stdevVarianceSamp();
    functionFactory.addYearsMonthsDaysHoursMinutesSeconds();
    functionFactory.yearsMonthsDaysHoursMinutesSecondsBetween();
    functionFactory.dateTrunc();
    functionFactory.bitLength_pattern("length(?1)*8");
    // DB2 wants parameter operands to be casted to allow lengths bigger than 255
    queryEngine.getSqmFunctionRegistry().register("concat", new CastingConcatFunction(this, "||", true, SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER, queryEngine.getTypeConfiguration()));
    // For the count distinct emulation distinct
    queryEngine.getSqmFunctionRegistry().register("count", new CountFunction(this, queryEngine.getTypeConfiguration(), SqlAstNodeRenderingMode.DEFAULT, "||", getCastTypeName(queryEngine.getTypeConfiguration().getBasicTypeRegistry().resolve(StandardBasicTypes.STRING), null, null, null), true));
    queryEngine.getSqmFunctionRegistry().register("format", new DB2FormatEmulation(queryEngine.getTypeConfiguration()));
    queryEngine.getSqmFunctionRegistry().namedDescriptorBuilder("posstr").setInvariantType(queryEngine.getTypeConfiguration().getBasicTypeRegistry().resolve(StandardBasicTypes.INTEGER)).setExactArgumentCount(2).setParameterTypes(FunctionParameterType.STRING, FunctionParameterType.STRING).setArgumentListSignature("(STRING string, STRING pattern)").register();
    functionFactory.windowFunctions();
    if (getDB2Version().isSameOrAfter(9, 5)) {
        functionFactory.listagg(null);
        if (getDB2Version().isSameOrAfter(11, 1)) {
            functionFactory.inverseDistributionOrderedSetAggregates();
            functionFactory.hypotheticalOrderedSetAggregates_windowEmulation();
        }
    }
}
Also used : CountFunction(org.hibernate.dialect.function.CountFunction) CommonFunctionFactory(org.hibernate.dialect.function.CommonFunctionFactory) DB2FormatEmulation(org.hibernate.dialect.function.DB2FormatEmulation) CastingConcatFunction(org.hibernate.dialect.function.CastingConcatFunction)

Example 4 with CountFunction

use of org.hibernate.dialect.function.CountFunction in project hibernate-orm by hibernate.

the class SybaseDialect method initializeFunctionRegistry.

@Override
public void initializeFunctionRegistry(QueryEngine queryEngine) {
    super.initializeFunctionRegistry(queryEngine);
    CommonFunctionFactory functionFactory = new CommonFunctionFactory(queryEngine);
    // For SQL-Server we need to cast certain arguments to varchar(16384) to be able to concat them
    queryEngine.getSqmFunctionRegistry().register("count", new CountFunction(this, queryEngine.getTypeConfiguration(), SqlAstNodeRenderingMode.DEFAULT, "+", "varchar(16384)", false));
    // AVG by default uses the input type, so we possibly need to cast the argument type, hence a special function
    functionFactory.avg_castingNonDoubleArguments(this, SqlAstNodeRenderingMode.DEFAULT);
    // this doesn't work 100% on earlier versions of Sybase
    // which were missing the third parameter in charindex()
    // TODO: we could emulate it with substring() like in Postgres
    functionFactory.locate_charindex();
    functionFactory.replace_strReplace();
    functionFactory.everyAny_minMaxCase();
    functionFactory.bitLength_pattern("datalength(?1) * 8");
    queryEngine.getSqmFunctionRegistry().register("timestampadd", new IntegralTimestampaddFunction(this, queryEngine.getTypeConfiguration()));
}
Also used : IntegralTimestampaddFunction(org.hibernate.dialect.function.IntegralTimestampaddFunction) CountFunction(org.hibernate.dialect.function.CountFunction) CommonFunctionFactory(org.hibernate.dialect.function.CommonFunctionFactory)

Aggregations

CommonFunctionFactory (org.hibernate.dialect.function.CommonFunctionFactory)4 CountFunction (org.hibernate.dialect.function.CountFunction)4 CastingConcatFunction (org.hibernate.dialect.function.CastingConcatFunction)2 BasicTypeRegistry (org.hibernate.type.BasicTypeRegistry)2 Date (java.util.Date)1 CaseLeastGreatestEmulation (org.hibernate.dialect.function.CaseLeastGreatestEmulation)1 DB2FormatEmulation (org.hibernate.dialect.function.DB2FormatEmulation)1 DerbyLpadEmulation (org.hibernate.dialect.function.DerbyLpadEmulation)1 DerbyRpadEmulation (org.hibernate.dialect.function.DerbyRpadEmulation)1 InsertSubstringOverlayEmulation (org.hibernate.dialect.function.InsertSubstringOverlayEmulation)1 IntegralTimestampaddFunction (org.hibernate.dialect.function.IntegralTimestampaddFunction)1 SQLServerFormatEmulation (org.hibernate.dialect.function.SQLServerFormatEmulation)1 DateTimeUtils.appendAsDate (org.hibernate.type.descriptor.DateTimeUtils.appendAsDate)1