Skip to content

Commit

Permalink
[Blazebit#1905] Add some ISO temporal format functions to use in Basi…
Browse files Browse the repository at this point in the history
…cUserTypes for multiset fetching
  • Loading branch information
dsarlo authored and beikov committed Jun 8, 2024
1 parent e5f6eb7 commit 3906100
Show file tree
Hide file tree
Showing 46 changed files with 1,530 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@
import com.blazebit.persistence.impl.function.datediff.year.MySQLYearDiffFunction;
import com.blazebit.persistence.impl.function.datediff.year.OracleYearDiffFunction;
import com.blazebit.persistence.impl.function.datediff.year.PostgreSQLYearDiffFunction;
import com.blazebit.persistence.impl.function.dateiso.DateIsoFunction;
import com.blazebit.persistence.impl.function.dateiso.MySQLDateIsoFunction;
import com.blazebit.persistence.impl.function.dateiso.PostgreSQLDateIsoFunction;
import com.blazebit.persistence.impl.function.dateiso.SQLServerDateIsoFunction;
import com.blazebit.persistence.impl.function.datetime.day.AccessDayFunction;
import com.blazebit.persistence.impl.function.datetime.day.DB2DayFunction;
import com.blazebit.persistence.impl.function.datetime.day.DayFunction;
Expand Down Expand Up @@ -409,6 +413,14 @@
import com.blazebit.persistence.impl.function.stringxmlagg.OracleGroupConcatBasedStringXmlAggFunction;
import com.blazebit.persistence.impl.function.stringxmlagg.PostgreSQLStringXmlAggFunction;
import com.blazebit.persistence.impl.function.subquery.SubqueryFunction;
import com.blazebit.persistence.impl.function.timeiso.MySQLTimeIsoFunction;
import com.blazebit.persistence.impl.function.timeiso.PostgreSQLTimeIsoFunction;
import com.blazebit.persistence.impl.function.timeiso.SQLServerTimeIsoFunction;
import com.blazebit.persistence.impl.function.timeiso.TimeIsoFunction;
import com.blazebit.persistence.impl.function.timestampiso.MySQLTimestampIsoFunction;
import com.blazebit.persistence.impl.function.timestampiso.PostgreSQLTimestampIsoFunction;
import com.blazebit.persistence.impl.function.timestampiso.SQLServerTimestampIsoFunction;
import com.blazebit.persistence.impl.function.timestampiso.TimestampIsoFunction;
import com.blazebit.persistence.impl.function.tomultiset.ToMultisetFunction;
import com.blazebit.persistence.impl.function.tostringjson.AbstractToStringJsonFunction;
import com.blazebit.persistence.impl.function.tostringjson.ForJsonPathToStringJsonFunction;
Expand Down Expand Up @@ -1978,6 +1990,33 @@ private void loadFunctions() {
jpqlFunctionGroup.add(dialectEntry.getKey(), new PercentileDiscFunction(dialectEntry.getValue()));
}
registerFunction(jpqlFunctionGroup);

jpqlFunctionGroup = new JpqlFunctionGroup( TimestampIsoFunction.FUNCTION_NAME, false );
jpqlFunctionGroup.add(null, new TimestampIsoFunction());
jpqlFunctionGroup.add("postgresql", new PostgreSQLTimestampIsoFunction());
jpqlFunctionGroup.add("cockroach", new PostgreSQLTimestampIsoFunction());
jpqlFunctionGroup.add("microsoft", new SQLServerTimestampIsoFunction());
jpqlFunctionGroup.add("mysql", new MySQLTimestampIsoFunction());
jpqlFunctionGroup.add("mysql8", new MySQLTimestampIsoFunction());
registerFunction(jpqlFunctionGroup);

jpqlFunctionGroup = new JpqlFunctionGroup( DateIsoFunction.FUNCTION_NAME, false );
jpqlFunctionGroup.add(null, new DateIsoFunction());
jpqlFunctionGroup.add("postgresql", new PostgreSQLDateIsoFunction());
jpqlFunctionGroup.add("cockroach", new PostgreSQLDateIsoFunction());
jpqlFunctionGroup.add("microsoft", new SQLServerDateIsoFunction());
jpqlFunctionGroup.add("mysql", new MySQLDateIsoFunction());
jpqlFunctionGroup.add("mysql8", new MySQLDateIsoFunction());
registerFunction(jpqlFunctionGroup);

jpqlFunctionGroup = new JpqlFunctionGroup( TimeIsoFunction.FUNCTION_NAME, false );
jpqlFunctionGroup.add(null, new TimeIsoFunction());
jpqlFunctionGroup.add("postgresql", new PostgreSQLTimeIsoFunction());
jpqlFunctionGroup.add("cockroach", new PostgreSQLTimeIsoFunction());
jpqlFunctionGroup.add("microsoft", new SQLServerTimeIsoFunction());
jpqlFunctionGroup.add("mysql", new MySQLTimeIsoFunction());
jpqlFunctionGroup.add("mysql8", new MySQLTimeIsoFunction());
registerFunction(jpqlFunctionGroup);
}

private void registerFunction(String name, JpqlFunction function) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2014 - 2024 Blazebit.
*
* 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.blazebit.persistence.impl.function.dateiso;

import com.blazebit.persistence.spi.FunctionRenderContext;
import com.blazebit.persistence.spi.JpqlFunction;

/**
* @author Christian Beikov
* @since 1.6.12
*/
public class DateIsoFunction implements JpqlFunction {
public static final String FUNCTION_NAME = "date_iso";

@Override
public boolean hasArguments() {
return true;
}

@Override
public boolean hasParenthesesIfNoArguments() {
return true;
}

@Override
public Class<?> getReturnType(Class<?> firstArgumentType) {
return String.class;
}

@Override
public void render(FunctionRenderContext context) {
if (context.getArgumentsSize() != 1) {
throw new RuntimeException("The date_iso function needs exactly one argument <date>! args=" + context);
}
context.addChunk( getExpression( context.getArgument( 0)));
}

protected String getExpression(String timestampArgument) {
return "to_char(" + timestampArgument + ", 'YYYY-MM-DD')";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,16 @@
* limitations under the License.
*/

package com.blazebit.persistence.view.impl.type;

import com.blazebit.persistence.view.spi.type.ImmutableBasicUserType;
package com.blazebit.persistence.impl.function.dateiso;

/**
*
* @author Christian Beikov
* @since 1.5.0
* @since 1.6.12
*/
public abstract class TimeZoneishBasicUserType<X> extends ImmutableBasicUserType<X> {

public class MySQLDateIsoFunction extends DateIsoFunction {
@Override
public abstract X fromString(CharSequence sequence);

@Override
public String toStringExpression(String expression) {
return "TO_CHAR(" + expression + ", 'TZ')";
protected String getExpression(String timestampArgument) {
return "date_format(" + timestampArgument + ", '%Y-%m-%d')";
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2014 - 2024 Blazebit.
*
* 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.blazebit.persistence.impl.function.dateiso;

/**
* @author Christian Beikov
* @since 1.6.12
*/
public class PostgreSQLDateIsoFunction extends DateIsoFunction {
@Override
protected String getExpression(String timestampArgument) {
return "to_char(" + timestampArgument + ", 'YYYY-MM-DD')";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2014 - 2024 Blazebit.
*
* 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.blazebit.persistence.impl.function.dateiso;

/**
* @author Christian Beikov
* @since 1.6.12
*/
public class SQLServerDateIsoFunction extends DateIsoFunction {
@Override
protected String getExpression(String timestampArgument) {
return "format(" + timestampArgument + ", 'yyyy-MM-dd')";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2014 - 2024 Blazebit.
*
* 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.blazebit.persistence.impl.function.timeiso;

/**
* @author Christian Beikov
* @since 1.6.12
*/
public class MySQLTimeIsoFunction extends TimeIsoFunction {
@Override
protected String getExpression(String timestampArgument) {
return "date_format(" + timestampArgument + ", '%H:%i:%S.%f')";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2014 - 2024 Blazebit.
*
* 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.blazebit.persistence.impl.function.timeiso;

/**
* @author Christian Beikov
* @since 1.6.12
*/
public class PostgreSQLTimeIsoFunction extends TimeIsoFunction {
@Override
protected String getExpression(String timestampArgument) {
return "to_char(" + timestampArgument + ", 'HH24:MI:SS.US')";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2014 - 2024 Blazebit.
*
* 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.blazebit.persistence.impl.function.timeiso;

/**
* @author Christian Beikov
* @since 1.6.12
*/
public class SQLServerTimeIsoFunction extends TimeIsoFunction {
@Override
protected String getExpression(String timestampArgument) {
return "format(" + timestampArgument + ", 'HH:mm:ss.nnnnnnn')";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2014 - 2024 Blazebit.
*
* 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.blazebit.persistence.impl.function.timeiso;

import com.blazebit.persistence.spi.FunctionRenderContext;
import com.blazebit.persistence.spi.JpqlFunction;

/**
* @author Christian Beikov
* @since 1.6.12
*/
public class TimeIsoFunction implements JpqlFunction {
public static final String FUNCTION_NAME = "time_iso";

@Override
public boolean hasArguments() {
return true;
}

@Override
public boolean hasParenthesesIfNoArguments() {
return true;
}

@Override
public Class<?> getReturnType(Class<?> firstArgumentType) {
return String.class;
}

@Override
public void render(FunctionRenderContext context) {
if (context.getArgumentsSize() != 1) {
throw new RuntimeException("The time_iso function needs exactly one argument <time>! args=" + context);
}
context.addChunk( getExpression( context.getArgument( 0)));
}

protected String getExpression(String timestampArgument) {
return "to_char(" + timestampArgument + ", 'HH24:MI:SS.FF9')";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2014 - 2024 Blazebit.
*
* 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.blazebit.persistence.impl.function.timestampiso;

/**
* @author Christian Beikov
* @since 1.6.12
*/
public class MySQLTimestampIsoFunction extends TimestampIsoFunction {
@Override
protected String getExpression(String timestampArgument) {
return "date_format(" + timestampArgument + ", '%Y-%m-%dT%H:%i:%S.%f')";
}

}
Loading

0 comments on commit 3906100

Please sign in to comment.