This repository has been archived by the owner on Jul 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
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
3 changed files
with
80 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cn.javaer.snippets.box.jooq; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.jooq.Condition; | ||
import org.jooq.Field; | ||
import org.jooq.JSONB; | ||
import org.jooq.impl.DSL; | ||
|
||
import java.util.Collections; | ||
|
||
/** | ||
* @author cn-src | ||
*/ | ||
public class Sql { | ||
|
||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
private Sql() {} | ||
|
||
public static Condition arrayContained(final Field<String[]> arrayField, final String[] arrayValue) { | ||
return DSL.condition("{0} <@ {1}", arrayField, | ||
DSL.val(arrayValue, arrayField.getDataType())); | ||
} | ||
|
||
public static Condition jsonbContains(final Field<JSONB> jsonField, final String jsonKey, final Object jsonValue) { | ||
try { | ||
final String json = Sql.objectMapper.writeValueAsString(Collections.singletonMap(jsonKey, jsonValue)); | ||
return DSL.condition("{0}::jsonb @> {1}::jsonb", jsonField, | ||
DSL.val(json, jsonField.getDataType())); | ||
} | ||
catch (final JsonProcessingException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
} |
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