Skip to content

Commit

Permalink
[Blazebit#1905] Add timestampiso function to abstract timestamp forma…
Browse files Browse the repository at this point in the history
…tting
  • Loading branch information
beikov committed Jun 6, 2024
1 parent 7d99f03 commit b13e55e
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@
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.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 +1982,15 @@ 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);
}

private void registerFunction(String name, JpqlFunction function) {
Expand Down
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-%d %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.timestampiso;

/**
* @author Christian Beikov
* @since 1.6.12
*/
public class PostgreSQLTimestampIsoFunction extends TimestampIsoFunction {
@Override
protected String getExpression(String timestampArgument) {
return "to_char(" + timestampArgument + ", 'YYYY-MM-DD 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.timestampiso;

/**
* @author Christian Beikov
* @since 1.6.12
*/
public class SQLServerTimestampIsoFunction extends TimestampIsoFunction {
@Override
protected String getExpression(String timestampArgument) {
return "format(" + timestampArgument + ", 'yyyy-MM-dd 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.timestampiso;

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

/**
* @author Christian Beikov
* @since 1.6.12
*/
public class TimestampIsoFunction implements JpqlFunction {
public static final String FUNCTION_NAME = "timestamp_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 timestamp_iso function needs exactly one argument <timestamp>! args=" + context);
}
context.addChunk( getExpression( context.getArgument( 0)));
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public abstract class TimestampishImmutableBasicUserType<X> extends ImmutableBas

@Override
public String toStringExpression(String expression) {
return "TO_CHAR(" + expression + ", 'YYYY-MM-DD HH24:MI:SS.US')";
return "TIMESTAMP_ISO(" + expression + ")";
}
}

0 comments on commit b13e55e

Please sign in to comment.