forked from Blazebit/blaze-persistence
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,291 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
core/impl/src/main/java/com/blazebit/persistence/impl/function/cast/DB2CastFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright 2014 - 2020 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.cast; | ||
|
||
import com.blazebit.persistence.impl.util.JpqlFunctionUtil; | ||
import com.blazebit.persistence.spi.DbmsDialect; | ||
import com.blazebit.persistence.spi.FunctionRenderContext; | ||
|
||
/** | ||
* @author Christian Beikov | ||
* @since 1.2.0 | ||
*/ | ||
public class DB2CastFunction extends CastFunction { | ||
|
||
private final String[] clobReturningFunctions = new String[] { | ||
"json_value", | ||
"json_query" | ||
}; | ||
private final String[] clobCompatibleCastTargetTypes = new String[] { | ||
"char", | ||
"varchar", | ||
"graphic", | ||
"vargraphic", | ||
"dbclob", | ||
"blob", | ||
"xml" | ||
}; | ||
|
||
public DB2CastFunction(Class<?> castType, DbmsDialect dbmsDialect) { | ||
super(castType, dbmsDialect); | ||
} | ||
|
||
@Override | ||
public void render(FunctionRenderContext context) { | ||
if (context.getArgumentsSize() != 1 && context.getArgumentsSize() != 2) { | ||
throw new RuntimeException("The " + functionName + " function needs one argument <expression> with an optional second argument <sql-type-name>! args=" + context); | ||
} | ||
String effectiveCastTargetType; | ||
if (context.getArgumentsSize() == 1) { | ||
effectiveCastTargetType = defaultSqlCastType; | ||
} else { | ||
effectiveCastTargetType = JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(1)); | ||
} | ||
boolean insertVarcharCast = isClobReturningFunction(context.getArgument(0)) && !isClobCompatibleCastTarget(effectiveCastTargetType); | ||
context.addChunk("cast("); | ||
if (insertVarcharCast) { | ||
context.addChunk("cast("); | ||
} | ||
context.addArgument(0); | ||
if (insertVarcharCast) { | ||
context.addChunk(" as varchar(32000))"); | ||
} | ||
context.addChunk(" as "); | ||
context.addChunk(effectiveCastTargetType); | ||
context.addChunk(")"); | ||
} | ||
|
||
@Override | ||
public String getCastExpression(String argument) { | ||
boolean insertVarcharCast = isClobReturningFunction(argument) && !isClobCompatibleCastTarget(defaultSqlCastType); | ||
if (insertVarcharCast) { | ||
return "cast(cast(" + argument + " as varchar(32000)) as " + defaultSqlCastType + ")"; | ||
} else { | ||
return "cast(" + argument + " as " + defaultSqlCastType + ")"; | ||
} | ||
} | ||
|
||
private boolean isClobReturningFunction(String castSource) { | ||
for (int i = 0; i < clobReturningFunctions.length; i++) { | ||
if (castSource.toLowerCase().startsWith(clobReturningFunctions[i] + "(")) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean isClobCompatibleCastTarget(String castTargetType) { | ||
for (int i = 0; i < clobCompatibleCastTargetTypes.length; i++) { | ||
if (castTargetType.equalsIgnoreCase(clobCompatibleCastTargetTypes[i]) || | ||
castTargetType.toLowerCase().startsWith(clobCompatibleCastTargetTypes[i] + "(")) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...src/main/java/com/blazebit/persistence/impl/function/jsonget/AbstractJsonGetFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2014 - 2020 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.jsonget; | ||
|
||
import com.blazebit.persistence.spi.FunctionRenderContext; | ||
import com.blazebit.persistence.spi.JpqlFunction; | ||
|
||
/** | ||
* @author Moritz Becker | ||
* @since 1.5.0 | ||
*/ | ||
public abstract class AbstractJsonGetFunction implements JpqlFunction { | ||
|
||
public static final String FUNCTION_NAME = "json_get"; | ||
|
||
@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() < 2) { | ||
throw new RuntimeException("The " + FUNCTION_NAME + " function requires at least two arguments <jsonField>, <key1|arrayIndex1>, ..., <keyN|arrayIndexN>! args=" + context); | ||
} | ||
render0(context); | ||
} | ||
|
||
protected abstract void render0(FunctionRenderContext context); | ||
|
||
public static String toJsonPath(Object[] pathElements, int to, boolean quotePathElements) { | ||
StringBuilder jsonPathBuilder = new StringBuilder("$"); | ||
for (int i = 0; i < to; i++) { | ||
if (pathElements[i] instanceof Integer) { | ||
jsonPathBuilder.append('['); | ||
jsonPathBuilder.append((int) pathElements[i]); | ||
jsonPathBuilder.append(']'); | ||
} else { | ||
jsonPathBuilder.append('.'); | ||
if (quotePathElements) { | ||
jsonPathBuilder.append("\""); | ||
} | ||
jsonPathBuilder.append((String) pathElements[i]); | ||
if (quotePathElements) { | ||
jsonPathBuilder.append("\""); | ||
} | ||
} | ||
} | ||
return jsonPathBuilder.toString(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...impl/src/main/java/com/blazebit/persistence/impl/function/jsonget/DB2JsonGetFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2014 - 2020 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.jsonget; | ||
|
||
import com.blazebit.persistence.impl.util.JpqlFunctionUtil; | ||
import com.blazebit.persistence.spi.FunctionRenderContext; | ||
|
||
/** | ||
* @author Moritz Becker | ||
* @since 1.5.0 | ||
*/ | ||
public class DB2JsonGetFunction extends AbstractJsonGetFunction { | ||
|
||
@Override | ||
protected void render0(FunctionRenderContext context) { | ||
Object[] jsonPathElements = new Object[context.getArgumentsSize()]; | ||
jsonPathElements[0] = "val"; | ||
for (int i = 1; i < context.getArgumentsSize(); i++) { | ||
try { | ||
jsonPathElements[i] = Integer.parseInt(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i))); | ||
} catch (NumberFormatException e) { | ||
jsonPathElements[i] = JpqlFunctionUtil.unquoteDoubleQuotes(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i))); | ||
} | ||
} | ||
String jsonPath = AbstractJsonGetFunction.toJsonPath(jsonPathElements, jsonPathElements.length, false); | ||
|
||
context.addChunk("json_query(concat('{\"val\":', concat("); | ||
context.addArgument(0); | ||
context.addChunk(", '}'))"); | ||
context.addChunk(",'"); | ||
context.addChunk(jsonPath); | ||
context.addChunk("' OMIT QUOTES)"); | ||
} | ||
} |
Oops, something went wrong.