Skip to content

Commit

Permalink
[Blazebit#841] Apply requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mobe91 committed Jul 20, 2020
1 parent 85249ea commit e98aece
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
*/
public class DB2CastFunction extends CastFunction {

private final String[] clobReturningFunctions = new String[] {
"json_value",
"json_query"
private static final String[] CLOB_RETURNING_FUNCTIONS = new String[] {
"json_value(",
"json_query("
};
private final String[] clobCompatibleCastTargetTypes = new String[] {
private static final String[] CLOB_COMPATIBLE_CAST_TARGET_TYPES = new String[] {
"char",
"varchar",
"graphic",
Expand Down Expand Up @@ -79,19 +79,21 @@ public String getCastExpression(String argument) {
}
}

private boolean isClobReturningFunction(String castSource) {
for (int i = 0; i < clobReturningFunctions.length; i++) {
if (castSource.toLowerCase().startsWith(clobReturningFunctions[i] + "(")) {
private static boolean isClobReturningFunction(String castSource) {
for (int i = 0; i < CLOB_RETURNING_FUNCTIONS.length; i++) {
if (castSource.toLowerCase().startsWith(CLOB_RETURNING_FUNCTIONS[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] + "(")) {
private static boolean isClobCompatibleCastTarget(String castTargetType) {
for (int i = 0; i < CLOB_COMPATIBLE_CAST_TARGET_TYPES.length; i++) {
if (castTargetType.toLowerCase().indexOf(CLOB_COMPATIBLE_CAST_TARGET_TYPES[i]) == 0 &&
(castTargetType.length() == CLOB_COMPATIBLE_CAST_TARGET_TYPES[i].length() ||
castTargetType.charAt(CLOB_COMPATIBLE_CAST_TARGET_TYPES[i].length()) == '(')
) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/
package com.blazebit.persistence.impl.function.jsonget;

import com.blazebit.persistence.impl.util.JpqlFunctionUtil;
import com.blazebit.persistence.spi.FunctionRenderContext;
import com.blazebit.persistence.spi.JpqlFunction;

import java.util.ArrayList;
import java.util.List;

/**
* @author Moritz Becker
* @since 1.5.0
Expand Down Expand Up @@ -51,24 +55,37 @@ public void render(FunctionRenderContext context) {

protected abstract void render0(FunctionRenderContext context);

public static String toJsonPath(Object[] pathElements, int to, boolean quotePathElements) {
public static String toJsonPath(List<Object> pathElements, int to, boolean quotePathElements) {
StringBuilder jsonPathBuilder = new StringBuilder("$");
for (int i = 0; i < to; i++) {
if (pathElements[i] instanceof Integer) {
Object currentPathElement = pathElements.get(i);
if (currentPathElement instanceof Integer) {
jsonPathBuilder.append('[');
jsonPathBuilder.append((int) pathElements[i]);
jsonPathBuilder.append((int) currentPathElement);
jsonPathBuilder.append(']');
} else {
jsonPathBuilder.append('.');
if (quotePathElements) {
jsonPathBuilder.append("\"");
}
jsonPathBuilder.append((String) pathElements[i]);
jsonPathBuilder.append((String) currentPathElement);
if (quotePathElements) {
jsonPathBuilder.append("\"");
}
}
}
return jsonPathBuilder.toString();
}

public static List<Object> retrieveJsonPathElements(FunctionRenderContext context, int pathStartOffset) {
List<Object> jsonPathElements = new ArrayList<>(context.getArgumentsSize() - pathStartOffset);
for (int i = pathStartOffset; i < context.getArgumentsSize(); i++) {
try {
jsonPathElements.add(Integer.parseInt(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i))));
} catch (NumberFormatException e) {
jsonPathElements.add(JpqlFunctionUtil.unquoteDoubleQuotes(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i))));
}
}
return jsonPathElements;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.blazebit.persistence.impl.function.jsonget;

import com.blazebit.persistence.impl.util.JpqlFunctionUtil;
import com.blazebit.persistence.spi.FunctionRenderContext;

import java.util.List;

/**
* @author Moritz Becker
* @since 1.5.0
Expand All @@ -26,16 +27,9 @@ 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);
List<Object> jsonPathElements = AbstractJsonGetFunction.retrieveJsonPathElements(context, 1);
jsonPathElements.add(0, "val");
String jsonPath = AbstractJsonGetFunction.toJsonPath(jsonPathElements, jsonPathElements.size(), false);

context.addChunk("json_query(concat('{\"val\":', concat(");
context.addArgument(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.blazebit.persistence.impl.function.jsonget;

import com.blazebit.persistence.impl.util.JpqlFunctionUtil;
import com.blazebit.persistence.spi.FunctionRenderContext;

import java.util.List;

/**
* @author Moritz Becker
* @since 1.5.0
Expand All @@ -26,15 +27,8 @@ public class MSSQLJsonGetFunction extends AbstractJsonGetFunction {

@Override
protected void render0(FunctionRenderContext context) {
Object[] jsonPathElements = new Object[context.getArgumentsSize() - 1];
for (int i = 1; i < context.getArgumentsSize(); i++) {
try {
jsonPathElements[i - 1] = Integer.parseInt(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i)));
} catch (NumberFormatException e) {
jsonPathElements[i - 1] = JpqlFunctionUtil.unquoteDoubleQuotes(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i)));
}
}
String jsonPath = AbstractJsonGetFunction.toJsonPath(jsonPathElements, jsonPathElements.length, true);
List<Object> jsonPathElements = AbstractJsonGetFunction.retrieveJsonPathElements(context, 1);
String jsonPath = AbstractJsonGetFunction.toJsonPath(jsonPathElements, jsonPathElements.size(), false);

context.addChunk("coalesce(json_value(");
context.addArgument(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.blazebit.persistence.impl.function.jsonget;

import com.blazebit.persistence.impl.util.JpqlFunctionUtil;
import com.blazebit.persistence.spi.FunctionRenderContext;

import java.util.List;

/**
* @author Moritz Becker
* @since 1.5.0
Expand All @@ -26,15 +27,8 @@ public class MySQL8JsonGetFunction extends AbstractJsonGetFunction {

@Override
protected void render0(FunctionRenderContext context) {
Object[] jsonPathElements = new Object[context.getArgumentsSize() - 1];
for (int i = 1; i < context.getArgumentsSize(); i++) {
try {
jsonPathElements[i - 1] = Integer.parseInt(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i)));
} catch (NumberFormatException e) {
jsonPathElements[i - 1] = JpqlFunctionUtil.unquoteDoubleQuotes(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i)));
}
}
String jsonPath = AbstractJsonGetFunction.toJsonPath(jsonPathElements, jsonPathElements.length, true);
List<Object> jsonPathElements = AbstractJsonGetFunction.retrieveJsonPathElements(context, 1);
String jsonPath = AbstractJsonGetFunction.toJsonPath(jsonPathElements, jsonPathElements.size(), true);

context.addChunk("json_unquote(");
context.addChunk("nullif(json_extract(");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.blazebit.persistence.impl.function.jsonget;

import com.blazebit.persistence.impl.util.JpqlFunctionUtil;
import com.blazebit.persistence.spi.FunctionRenderContext;

import java.util.List;

/**
* @author Moritz Becker
* @since 1.5.0
Expand All @@ -26,15 +27,8 @@ public class OracleJsonGetFunction extends AbstractJsonGetFunction {

@Override
protected void render0(FunctionRenderContext context) {
Object[] jsonPathElements = new Object[context.getArgumentsSize() - 1];
for (int i = 1; i < context.getArgumentsSize(); i++) {
try {
jsonPathElements[i - 1] = Integer.parseInt(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i)));
} catch (NumberFormatException e) {
jsonPathElements[i - 1] = JpqlFunctionUtil.unquoteDoubleQuotes(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i)));
}
}
String jsonPath = toJsonPath(jsonPathElements, jsonPathElements.length, true);
List<Object> jsonPathElements = AbstractJsonGetFunction.retrieveJsonPathElements(context, 1);
String jsonPath = toJsonPath(jsonPathElements, jsonPathElements.size(), true);

context.addChunk("coalesce(json_value(");
context.addArgument(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
package com.blazebit.persistence.impl.function.jsonset;

import com.blazebit.persistence.impl.function.jsonget.AbstractJsonGetFunction;
import com.blazebit.persistence.impl.util.JpqlFunctionUtil;
import com.blazebit.persistence.spi.FunctionRenderContext;

import java.util.List;

/**
* @author Moritz Becker
* @since 1.5.0
Expand All @@ -27,15 +28,8 @@ public class MSSQLJsonSetFunction extends AbstractJsonGetFunction {

@Override
protected void render0(FunctionRenderContext context) {
Object[] jsonPathElements = new Object[context.getArgumentsSize() - 2];
for (int i = 2; i < context.getArgumentsSize(); i++) {
try {
jsonPathElements[i - 2] = Integer.parseInt(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i)));
} catch (NumberFormatException e) {
jsonPathElements[i - 2] = JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i));
}
}
String jsonPath = AbstractJsonGetFunction.toJsonPath(jsonPathElements, jsonPathElements.length, true);
List<Object> jsonPathElements = AbstractJsonGetFunction.retrieveJsonPathElements(context, 2);
String jsonPath = AbstractJsonGetFunction.toJsonPath(jsonPathElements, jsonPathElements.size(), true);

context.addChunk("(select case when isjson(temp.val) = 0 then (case ");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
package com.blazebit.persistence.impl.function.jsonset;

import com.blazebit.persistence.impl.function.jsonget.AbstractJsonGetFunction;
import com.blazebit.persistence.impl.util.JpqlFunctionUtil;
import com.blazebit.persistence.spi.FunctionRenderContext;

import java.util.List;

/**
* @author Moritz Becker
* @since 1.5.0
Expand All @@ -27,27 +28,20 @@ public class MySQL8JsonSetFunction extends AbstractJsonSetFunction {

@Override
protected void render0(FunctionRenderContext context) {
Object[] jsonPathElements = new Object[context.getArgumentsSize() - 2];
for (int i = 2; i < context.getArgumentsSize(); i++) {
try {
jsonPathElements[i - 2] = Integer.parseInt(JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i)));
} catch (NumberFormatException e) {
jsonPathElements[i - 2] = JpqlFunctionUtil.unquoteSingleQuotes(context.getArgument(i));
}
}
List<Object> jsonPathElements = AbstractJsonGetFunction.retrieveJsonPathElements(context, 2);

context.addChunk("(select ");
context.addChunk("json_merge_patch(");
context.addArgument(0);
context.addChunk(", concat('");

for (int i = 0; i < jsonPathElements.length; i++) {
for (int i = 0; i < jsonPathElements.size(); i++) {
startJsonPathElement(context, jsonPathElements, i);
}
context.addChunk("', ");
context.addChunk("temp.val");
context.addChunk(", '");
for (int i = jsonPathElements.length - 1; i >= 0; i--) {
for (int i = jsonPathElements.size() - 1; i >= 0; i--) {
endJsonPathElement(context, jsonPathElements, i);
}
context.addChunk("'))");
Expand All @@ -57,8 +51,8 @@ protected void render0(FunctionRenderContext context) {
context.addChunk(")) temp(val))");
}

private void startJsonPathElement(FunctionRenderContext context, Object[] pathElems, int curIndex) {
Object pathElem = pathElems[curIndex];
private void startJsonPathElement(FunctionRenderContext context, List<Object> pathElems, int curIndex) {
Object pathElem = pathElems.get(curIndex);
if (pathElem instanceof Integer) {
context.addChunk("[', ");

Expand All @@ -80,7 +74,7 @@ private void startJsonPathElement(FunctionRenderContext context, Object[] pathEl
context.addChunk(")");
context.addChunk(", ',', ");
}
if (curIndex < pathElems.length - 1) {
if (curIndex < pathElems.size()) {
context.addChunk("coalesce(json_merge_patch(");
renderJsonGet(context, AbstractJsonGetFunction.toJsonPath(pathElems, curIndex + 1, true));
context.addChunk(", concat('");
Expand All @@ -94,17 +88,17 @@ private void startJsonPathElement(FunctionRenderContext context, Object[] pathEl
}
}

private void endJsonPathElement(FunctionRenderContext context, Object[] pathElems, int curIndex) {
Object pathElem = pathElems[curIndex];
private void endJsonPathElement(FunctionRenderContext context, List<Object> pathElems, int curIndex) {
Object pathElem = pathElems.get(curIndex);
if (pathElem instanceof Integer) {
context.addChunk("'");
if (curIndex < pathElems.length - 1) {
if (curIndex < pathElems.size() - 1) {
context.addChunk(")), concat('");
for (int i = curIndex + 1; i < pathElems.length; i++) {
for (int i = curIndex + 1; i < pathElems.size(); i++) {
startJsonPathElement(context, pathElems, i);
}
context.addChunk("', temp.val, '");
for (int i = pathElems.length - 1; i >= curIndex + 1; i--) {
for (int i = pathElems.size() - 1; i >= curIndex + 1; i--) {
endJsonPathElement(context, pathElems, i);
}
context.addChunk("'))");
Expand Down
Loading

0 comments on commit e98aece

Please sign in to comment.