-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#417 - ENH: Add JSON expressions ... path exists and value at path eq…
…uals value
- Loading branch information
Showing
18 changed files
with
584 additions
and
33 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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/avaje/ebeaninternal/server/core/JsonExpressionHandler.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,17 @@ | ||
package com.avaje.ebeaninternal.server.core; | ||
|
||
import com.avaje.ebeaninternal.api.SpiExpressionRequest; | ||
import com.avaje.ebeaninternal.server.expression.Op; | ||
|
||
/** | ||
* Adds the db platform specific json expression. | ||
*/ | ||
public interface JsonExpressionHandler { | ||
|
||
|
||
/** | ||
* Write the db platform specific json expression. | ||
*/ | ||
void addSql(SpiExpressionRequest request, String propName, String path, Op operator, Object value); | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/avaje/ebeaninternal/server/core/NotSupportedJsonExpression.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,15 @@ | ||
package com.avaje.ebeaninternal.server.core; | ||
|
||
import com.avaje.ebeaninternal.api.SpiExpressionRequest; | ||
import com.avaje.ebeaninternal.server.expression.Op; | ||
|
||
/** | ||
* Not supported JSON expression handler. | ||
*/ | ||
public class NotSupportedJsonExpression implements JsonExpressionHandler { | ||
|
||
@Override | ||
public void addSql(SpiExpressionRequest request, String propName, String path, Op operator, Object value) { | ||
throw new RuntimeException("JSON expressions only supported on Postgres and Oracle"); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/avaje/ebeaninternal/server/core/OracleJsonExpression.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,24 @@ | ||
package com.avaje.ebeaninternal.server.core; | ||
|
||
import com.avaje.ebeaninternal.api.SpiExpressionRequest; | ||
import com.avaje.ebeaninternal.server.expression.Op; | ||
|
||
/** | ||
* Postgres JSON expression handler | ||
*/ | ||
public class OracleJsonExpression implements JsonExpressionHandler { | ||
|
||
@Override | ||
public void addSql(SpiExpressionRequest request, String propName, String path, Op operator, Object value) { | ||
|
||
if (operator == Op.EXISTS) { | ||
request.append("json_exists(").append(propName).append(", '$.").append(path).append("')"); | ||
} else if (operator == Op.NOT_EXISTS) { | ||
request.append("not json_exists(").append(propName).append(", '$.").append(path).append("')"); | ||
} else { | ||
request.append("json_value(").append(propName).append(", '$.").append(path).append("')"); | ||
request.append(operator.bind()); | ||
} | ||
|
||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/avaje/ebeaninternal/server/core/PostgresJsonExpression.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,71 @@ | ||
package com.avaje.ebeaninternal.server.core; | ||
|
||
import com.avaje.ebeaninternal.api.SpiExpressionRequest; | ||
import com.avaje.ebeaninternal.server.expression.Op; | ||
|
||
/** | ||
* Postgres JSON expression handler | ||
*/ | ||
public class PostgresJsonExpression implements JsonExpressionHandler { | ||
|
||
@Override | ||
public void addSql(SpiExpressionRequest request, String propName, String path, Op operator, Object value) { | ||
|
||
StringBuilder sb = new StringBuilder(50); | ||
String[] paths = path.split("\\."); | ||
if (paths.length == 1) { | ||
// (t0.content ->> 'title') = 'Some value' | ||
sb.append("(").append(propName).append(" ->> '").append(path).append("')"); | ||
|
||
} else { | ||
// (t0.content #>> '{path,inner}') = 'Some value' | ||
sb.append("(").append(propName).append(" #>> '{"); | ||
for (int i = 0; i < paths.length; i++) { | ||
if (i > 0) { | ||
sb.append(","); | ||
} | ||
sb.append(paths[i]); | ||
} | ||
sb.append("}')"); | ||
} | ||
|
||
request.append(castType(sb.toString(), value)); | ||
request.append(operator.bind()); | ||
} | ||
|
||
/** | ||
* Postgres CAST the type if necessary as text values always returned from the json operators used. | ||
*/ | ||
private String castType(String expression, Object value) { | ||
|
||
if (value == null) { | ||
// for exists and not-exists expressions | ||
return expression; | ||
} | ||
|
||
// Postgres cast of returned text value | ||
if (isIntegerType(value)) { | ||
return expression+"::INTEGER"; | ||
} | ||
if (isNumberType(value)) { | ||
return expression+"::DECIMAL"; | ||
} | ||
if (isBooleanType(value)) { | ||
return expression+"::BOOLEAN"; | ||
} | ||
|
||
return expression; | ||
} | ||
|
||
private boolean isBooleanType(Object value) { | ||
return (value instanceof Boolean); | ||
} | ||
|
||
private boolean isIntegerType(Object value) { | ||
return (value instanceof Integer) || (value instanceof Long); | ||
} | ||
|
||
private boolean isNumberType(Object value) { | ||
return (value instanceof Number); | ||
} | ||
} |
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
Oops, something went wrong.