├── .gitignore ├── LICENSE ├── changelog.md ├── integration-tests ├── pom.xml └── src │ └── test │ ├── java │ └── com │ │ └── github │ │ └── t9t │ │ └── jooq │ │ └── json │ │ └── testing │ │ ├── AbstractJsonDSLTest.java │ │ ├── JsonBindingIT.java │ │ ├── JsonDSLArrayElementIT.java │ │ ├── JsonDSLArrayElementTextIT.java │ │ ├── JsonDSLArrayLengthIT.java │ │ ├── JsonDSLExtractPathIT.java │ │ ├── JsonDSLExtractPathTextIT.java │ │ ├── JsonDSLFieldByKeyIT.java │ │ ├── JsonDSLFieldByKeyTextIT.java │ │ ├── JsonDSLObjectAtPathIT.java │ │ ├── JsonDSLObjectAtPathTextIT.java │ │ ├── JsonDSLStripNullsIT.java │ │ ├── JsonDSLTypeOfIT.java │ │ ├── JsonbDSLConcatIT.java │ │ ├── JsonbDSLContainedInIT.java │ │ ├── JsonbDSLContainsIT.java │ │ ├── JsonbDSLDeleteElementIT.java │ │ ├── JsonbDSLDeleteIT.java │ │ ├── JsonbDSLDeletePathIT.java │ │ ├── JsonbDSLHasAllKeysIT.java │ │ ├── JsonbDSLHasAnyKeyIT.java │ │ ├── JsonbDSLHasKeyIT.java │ │ ├── JsonbDSLPrettyIT.java │ │ └── TestDb.java │ ├── kotlin │ └── com │ │ └── github │ │ └── t9t │ │ └── jooq │ │ └── json │ │ └── testing │ │ ├── JsonKotlinIT.kt │ │ ├── json │ │ └── JsonDSLTest.kt │ │ └── jsonb │ │ └── JsonbDSLTest.kt │ └── resources │ └── migration │ └── V1__test_database.sql ├── jooq-postgresql-json ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── t9t │ │ └── jooq │ │ └── json │ │ ├── JsonDSL.java │ │ ├── JsonbDSL.java │ │ └── StringConverter.java │ └── kotlin │ └── com │ └── github │ └── t9t │ └── jooq │ └── json │ ├── json │ └── JsonDSL.kt │ └── jsonb │ └── JsonbDSL.kt ├── pom.xml ├── readme.md ├── release.sh └── startdb.sh /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | /idea 3 | /.idea 4 | *.iml 5 | .DS_Store 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2019 t9t 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # 4.0.0 2 | Removed the transitive dependency on `jooq` as it became too time consuming to update it and release this library each 3 | time. Additionally the jOOQ 3.17 release (Open Source Edition) only JDK 17 is supported, while `jooq-postgresql-json` 4 | should still be compatible with JDK 11 and jOOQ 3.16. 5 | 6 | From now on, every new jOOQ release will be tested with `jooq-postgresql-json` and a new release will only be created 7 | when incompatibilities are found and fixed. The readme will be periodically updated to reflect the latest jOOQ versions 8 | that `jooq-postgresql-json` is tested with. 9 | 10 | # 3.2.3 11 | - Updated jOOQ version to 3.16.7 12 | 13 | # 3.2.2 14 | - Updated jOOQ version to 3.16.6 15 | 16 | # 3.2.1 17 | - Updated jOOQ version to 3.16.5 18 | 19 | # 3.2.0 20 | - Updated jOOQ version to 3.16.3 21 | 22 | # 3.1.2 23 | - Updated jOOQ version to 3.15.5 24 | 25 | # 3.1.1 26 | - Updated jOOQ version to 3.15.4 27 | 28 | # 3.1.0 29 | - Updated jOOQ version to 3.15.1 30 | 31 | # 3.0.0 (**breaking**) 32 | - Upgraded JDK to build the library from 8 to 11 33 | - Updated the target Java version from 8 to 11 34 | - Library can no longer be used with Java 8 projects 35 | - Upgrade prepares for updating to jOOQ version 3.15, which dropped Java 8 support in the community version 36 | 37 | # 2.0.3 38 | - Updated jOOQ version to 3.14.12 39 | 40 | # 2.0.2 41 | - Updated jOOQ version to 3.14.9 42 | 43 | # 2.0.1 44 | - Updated jOOQ version to 3.14.6 45 | 46 | # 2.0.0 (**breaking**) 47 | 48 | - Changed the type of the Kotlin extension functions from `Field` to `Field` (ie. nullable), to 49 | better interact with Kotlin code generated by the jOOQ code generator 50 | ([#13](https://github.com/t9t/jooq-postgresql-json/issues/13)). 51 | 52 | # 1.3.0 53 | 54 | - Introduced Kotlin extension functions ([#11](https://github.com/t9t/jooq-postgresql-json/issues/11) & 55 | [#12](https://github.com/t9t/jooq-postgresql-json/issues/12)) 56 | 57 | # 1.2.2 58 | 59 | - Updated jOOQ version to 3.14.4 60 | 61 | # 1.2.1 62 | 63 | - Updated jOOQ version to 3.14.3 64 | 65 | # 1.2.0 66 | - Updated jOOQ version to 3.14.0 67 | 68 | # 1.1.2 69 | - Updated jOOQ version to 3.13.4 70 | 71 | # 1.1.1 72 | - Updated jOOQ version to 3.13.2 73 | 74 | # 1.1.0 75 | - Updated jOOQ version to 3.13.1 76 | 77 | # 1.0.0 (**breaking**) 78 | - Upgraded to jOOQ 3.12.3, which has its own `JSON` and `JSONB` types, which means: 79 | - Removed `Json` and `Jsonb` classes, replacing them with `JSON` and `JSONB` respectively in `JsonDSL` and `JsonbDSL` 80 | - Removed `JsonBinding`, `JsonbBinding`, and `JsonStringBinding` as they are now obsolete 81 | 82 | # 0.4.0 83 | - Added a handful of processing functions: 84 | - `json(b)_array_length` 85 | - `json(b)_extract_path` 86 | - `json(b)_extract_path_text` 87 | - `json(b)_typeof` 88 | - `json(b)_strip_nulls` 89 | - `jsonb_pretty` 90 | 91 | # 0.3.1 92 | - Fix release to Maven central repository 93 | 94 | # 0.3.0 95 | - Added `Json` and `Jsonb` to encapsulate `json` and `jsonb` fields and values 96 | - Added `JsonBinding` and `JsonbBinding` to map `json` and `jsonb` fields to the above 97 | - Added `JsonDSL` and `JsonbDSL` containing PostgreSQL `json` and `jsonb` operators 98 | 99 | # 0.2.0 100 | - Split `JsonbStringBinding` for `jsonb` to leverage potential performance benefit. 101 | 102 | # 0.1.0 103 | - Initial release containing only `JsonStringBinding`. 104 | -------------------------------------------------------------------------------- /integration-tests/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | jooq-postgresql-json-parent 6 | com.github.t9t.jooq 7 | 4.0.1-SNAPSHOT 8 | 9 | 10 | integration-tests 11 | 12 | 13 | 23719 14 | jooq 15 | jooq 16 | jooq 17 | jooq 18 | jdbc:postgresql://127.0.0.1:${pg.port}/${pg.dbname} 19 | 20 | ${project.build.directory}/generated-sources/jooq 21 | 22 | 23 | 24 | 25 | com.github.t9t.jooq 26 | jooq-postgresql-json 27 | 28 | 29 | 30 | org.jooq 31 | jooq 32 | 33 | 34 | 35 | org.postgresql 36 | postgresql 37 | test 38 | 39 | 40 | 41 | org.jetbrains.kotlin 42 | kotlin-stdlib 43 | true 44 | 45 | 46 | 47 | org.junit.jupiter 48 | junit-jupiter-engine 49 | test 50 | 51 | 52 | org.junit.jupiter 53 | junit-jupiter-params 54 | test 55 | 56 | 57 | 58 | com.fasterxml.jackson.core 59 | jackson-databind 60 | test 61 | 62 | 63 | 64 | 65 | 66 | run-integration-tests 67 | 68 | 69 | !maven.test.skip 70 | 71 | 72 | 73 | 74 | 75 | io.fabric8 76 | docker-maven-plugin 77 | 78 | 79 | stop-existing-postgres 80 | clean 81 | 82 | stop 83 | 84 | 85 | 86 | start-postgres 87 | generate-test-sources 88 | 89 | start 90 | 91 | 92 | 93 | stop-postgres 94 | post-integration-test 95 | 96 | stop 97 | 98 | 99 | 100 | 101 | 102 | 103 | postgres 104 | postgres:11 105 | 106 | 107 | ${pg.dbname} 108 | ${pg.user} 109 | ${pg.password} 110 | 111 | 112 | ${pg.port}:5432 113 | 114 | 115 | \[1\]\s+LOG:\s+database system is ready to accept connections 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | org.flywaydb 126 | flyway-maven-plugin 127 | 128 | 129 | generate-test-sources 130 | 131 | migrate 132 | 133 | 134 | 135 | 136 | ${db.url} 137 | ${pg.user} 138 | ${pg.password} 139 | schema_version
140 | 141 | ${db.schema} 142 | 143 | 144 | filesystem:src/test/resources/migration 145 | 146 |
147 | 148 | 149 | org.postgresql 150 | postgresql 151 | ${postgresql.driver.version} 152 | 153 | 154 |
155 | 156 | 157 | org.jooq 158 | jooq-codegen-maven 159 | 160 | 161 | codegen-java 162 | generate-test-sources 163 | 164 | generate 165 | 166 | 167 | 168 | 169 | org.jooq.meta.postgres.PostgresDatabase 170 | .* 171 | 172 | 173 | jooq 174 | 175 | 176 | 177 | 178 | com.github.t9t.jooq.generated.java 179 | ${test.jooq-codegen.directory} 180 | 181 | 182 | true 183 | false 184 | 185 | 186 | 187 | 188 | 189 | codegen-kotlin 190 | generate-test-sources 191 | 192 | generate 193 | 194 | 195 | 196 | org.jooq.codegen.KotlinGenerator 197 | 198 | org.jooq.meta.postgres.PostgresDatabase 199 | .* 200 | 201 | 202 | jooq 203 | 204 | 205 | 206 | 207 | com.github.t9t.jooq.generated.kotlin 208 | ${test.jooq-codegen.directory} 209 | 210 | 211 | 212 | false 213 | false 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | ${db.url} 222 | ${pg.user} 223 | ${pg.password} 224 | 225 | 226 | 227 | 228 | org.postgresql 229 | postgresql 230 | ${postgresql.driver.version} 231 | 232 | 233 | 234 | 235 | 236 | org.jetbrains.kotlin 237 | kotlin-maven-plugin 238 | 239 | 240 | 241 | org.codehaus.mojo 242 | build-helper-maven-plugin 243 | 244 | 245 | add-test-sources 246 | generate-test-sources 247 | 248 | add-test-source 249 | 250 | 251 | 252 | ${test.jooq-codegen.directory} 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | org.apache.maven.plugins 261 | maven-failsafe-plugin 262 | 263 | 264 | 265 | integration-test 266 | verify 267 | 268 | 269 | 270 | 271 | 272 | 273 | org.apache.maven.plugins 274 | maven-deploy-plugin 275 | 276 | true 277 | 278 | 279 |
280 |
281 |
282 |
283 |
284 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/AbstractJsonDSLTest.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.fasterxml.jackson.databind.JsonNode; 4 | import com.fasterxml.jackson.databind.ObjectMapper; 5 | import org.jooq.DSLContext; 6 | import org.jooq.Field; 7 | import org.jooq.JSON; 8 | import org.jooq.JSONB; 9 | import org.jooq.SQLDialect; 10 | import org.jooq.impl.DSL; 11 | import org.junit.jupiter.params.ParameterizedTest; 12 | import org.junit.jupiter.params.provider.Arguments; 13 | import org.junit.jupiter.params.provider.MethodSource; 14 | 15 | import java.io.IOException; 16 | import java.util.List; 17 | import java.util.stream.Collectors; 18 | 19 | import static com.github.t9t.jooq.generated.java.Tables.JSON_TEST; 20 | import static java.util.Objects.requireNonNull; 21 | import static org.junit.jupiter.api.Assertions.assertEquals; 22 | import static org.junit.jupiter.api.Assertions.assertNull; 23 | 24 | public abstract class AbstractJsonDSLTest { 25 | private static final DSLContext dsl = DSL.using(TestDb.createDataSource(), SQLDialect.POSTGRES); 26 | private static final ObjectMapper om = new ObjectMapper(); 27 | 28 | private static final String template = "{\"obj\": {\"i\": 5521, \"b\": true}, \"arr\": [{\"d\": 4408}, 10, true, \"s\"], \"num\": 1337, \"str\": \"Hello, %s world!\", \"n\": null}"; 29 | private static final String arrayTemplate = "[{\"d\": 4408}, 10, true, \"%s array\"]"; 30 | 31 | static final Field json = JSON_TEST.DATA; 32 | static final Field jsonb = JSON_TEST.DATAB; 33 | 34 | static List generateParams(String baseName, List paramList) { 35 | return paramList.stream().map(p -> { 36 | String name = String.format("%s_%s", baseName, p.name); 37 | return Arguments.of(name, requireNonNull(p.jsonData, "jsonData"), p.expected, requireNonNull(p.fieldToSelect, "fieldToSelect")); 38 | }).collect(Collectors.toList()); 39 | } 40 | 41 | private void setUp(String jsonData) { 42 | dsl.deleteFrom(JSON_TEST).execute(); 43 | 44 | assertEquals(1, dsl.insertInto(JSON_TEST) 45 | .columns(JSON_TEST.NAME, JSON_TEST.DATA, JSON_TEST.DATAB) 46 | .values("json-dsl-test", JSON.valueOf(jsonData), JSONB.valueOf(jsonData)) 47 | .execute()); 48 | assertEquals(1, dsl.fetchCount(JSON_TEST)); 49 | } 50 | 51 | @ParameterizedTest(name = "{index} {0}") 52 | @MethodSource("params") 53 | public void test(String ignoredTestName, String jsonData, Object expected, Field fieldToSelect) { 54 | setUp(jsonData); 55 | 56 | Object data = dsl.select(fieldToSelect).from(JSON_TEST).fetchOne().value1(); 57 | 58 | if (expected == null) { 59 | assertNull(data); 60 | return; 61 | } 62 | 63 | if (expected instanceof JsonNode) { 64 | if (data instanceof JSON) { 65 | assertEquals(expected, toNode(data.toString())); 66 | } else if (data instanceof JSONB) { 67 | assertEquals(expected, toNode(data.toString())); 68 | } else { 69 | assertEquals(expected, toNode((String) data)); 70 | } 71 | return; 72 | } 73 | 74 | assertEquals(expected, data); 75 | } 76 | 77 | static JsonNode toNode(String s) { 78 | try { 79 | return om.readTree(s); 80 | } catch (IOException e) { 81 | throw new AssertionError("Unable to parse JSON: " + s, e); 82 | } 83 | } 84 | 85 | static Params test(String name) { 86 | return new Params(name + "_json", String.format(template, "json")); 87 | } 88 | 89 | static Params btest(String name) { 90 | return new Params(name + "_jsonb", String.format(template, "jsonb")); 91 | } 92 | 93 | static class Params { 94 | private final String name; 95 | private String jsonData; 96 | private Object expected; 97 | private Field fieldToSelect; 98 | 99 | private Params(String name, String jsonData) { 100 | this.name = name; 101 | this.jsonData = jsonData; 102 | } 103 | 104 | Params forArray() { 105 | this.jsonData = String.format(arrayTemplate, "json" + (name.endsWith("b") ? "b" : "")); 106 | return this; 107 | } 108 | 109 | Params usingJson(String json) { 110 | this.jsonData = json; 111 | return this; 112 | } 113 | 114 | Params selecting(Field fieldToSelect) { 115 | this.fieldToSelect = fieldToSelect; 116 | return this; 117 | } 118 | 119 | Params expectNull() { 120 | this.expected = null; 121 | return this; 122 | } 123 | 124 | Params expect(Object o) { 125 | this.expected = o; 126 | return this; 127 | } 128 | 129 | Params expectString(String s) { 130 | return expect(s); 131 | } 132 | 133 | Params expectJson(String s) { 134 | this.expected = JSON.valueOf(s); 135 | return this; 136 | } 137 | 138 | Params expectJsonb(String s) { 139 | this.expected = JSONB.valueOf(s); 140 | return this; 141 | } 142 | 143 | Params expectJson(JsonNode node) { 144 | this.expected = node; 145 | return this; 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonBindingIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | 4 | import org.jooq.DSLContext; 5 | import org.jooq.JSON; 6 | import org.jooq.JSONB; 7 | import org.jooq.Record1; 8 | import org.jooq.Record2; 9 | import org.jooq.SQLDialect; 10 | import org.jooq.impl.DSL; 11 | import org.junit.jupiter.api.BeforeEach; 12 | import org.junit.jupiter.api.Test; 13 | 14 | import javax.sql.DataSource; 15 | 16 | import static com.github.t9t.jooq.generated.java.Tables.JSON_TEST; 17 | import static org.junit.jupiter.api.Assertions.assertEquals; 18 | import static org.junit.jupiter.api.Assertions.assertNull; 19 | 20 | /** 21 | * Tests the basic assumptions about jOOQ binding json and jsonb fields to the {@link JSON} and {@link JSONB} classes 22 | * and its null handling. 23 | */ 24 | public class JsonBindingIT { 25 | private static final DataSource ds = TestDb.createDataSource(); 26 | private static final DSLContext dsl = DSL.using(ds, SQLDialect.POSTGRES); 27 | 28 | @BeforeEach 29 | public void setUp() { 30 | dsl.deleteFrom(JSON_TEST).execute(); 31 | assertEquals(4, dsl.execute("insert into jooq.json_test (name, data, datab)" + 32 | " values " + 33 | "('both', '{\"json\": {\"int\": 100, \"str\": \"Hello, JSON world!\", \"object\": {\"v\": 200}, \"n\": null}}', '{\"jsonb\": {\"int\": 100, \"str\": \"Hello, JSONB world!\", \"object\": {\"v\": 200}, \"n\": null}}')," + 34 | "('empty', '{}', '{}')," + 35 | "('null-sql', null, null)," + 36 | "('null-json', 'null'::json, 'null'::jsonb)")); 37 | } 38 | 39 | @Test 40 | public void selectJson() { 41 | Record1 r = dsl.select(JSON_TEST.DATA) 42 | .from(JSON_TEST) 43 | .where(JSON_TEST.NAME.eq("both")) 44 | .fetchOne(); 45 | 46 | assertEquals(JSON.valueOf("{\"json\": {\"int\": 100, \"str\": \"Hello, JSON world!\", \"object\": {\"v\": 200}, \"n\": null}}"), r.value1()); 47 | } 48 | 49 | @Test 50 | public void selectJsonEmpty() { 51 | Record1 r = dsl.select(JSON_TEST.DATA) 52 | .from(JSON_TEST) 53 | .where(JSON_TEST.NAME.eq("empty")) 54 | .fetchOne(); 55 | 56 | assertEquals(JSON.valueOf("{}"), r.value1()); 57 | } 58 | 59 | @Test 60 | public void selectJsonNullSql() { 61 | Record1 r = dsl.select(JSON_TEST.DATA) 62 | .from(JSON_TEST) 63 | .where(JSON_TEST.NAME.eq("null-sql")) 64 | .fetchOne(); 65 | 66 | assertNull(r.value1()); 67 | } 68 | 69 | 70 | @Test 71 | public void selectJsonNullJsonValue() { 72 | Record1 r = dsl.select(JSON_TEST.DATA) 73 | .from(JSON_TEST) 74 | .where(JSON_TEST.NAME.eq("null-json")) 75 | .fetchOne(); 76 | 77 | assertEquals("null", r.value1().toString()); 78 | } 79 | 80 | @Test 81 | public void selectJsonb() { 82 | Record1 r = dsl.select(JSON_TEST.DATAB) 83 | .from(JSON_TEST) 84 | .where(JSON_TEST.NAME.eq("both")) 85 | .fetchOne(); 86 | 87 | assertEquals(JSONB.valueOf("{\"jsonb\": {\"n\": null, \"int\": 100, \"str\": \"Hello, JSONB world!\", \"object\": {\"v\": 200}}}"), r.value1()); 88 | } 89 | 90 | @Test 91 | public void selectJsonbEmpty() { 92 | Record1 r = dsl.select(JSON_TEST.DATAB) 93 | .from(JSON_TEST) 94 | .where(JSON_TEST.NAME.eq("empty")) 95 | .fetchOne(); 96 | 97 | assertEquals(JSONB.valueOf("{}"), r.value1()); 98 | } 99 | 100 | @Test 101 | public void selectJsonbNullSql() { 102 | Record1 r = dsl.select(JSON_TEST.DATAB) 103 | .from(JSON_TEST) 104 | .where(JSON_TEST.NAME.eq("null-sql")) 105 | .fetchOne(); 106 | 107 | assertNull(r.value1()); 108 | } 109 | 110 | @Test 111 | public void selectJsonbNullJsonValue() { 112 | Record1 r = dsl.select(JSON_TEST.DATAB) 113 | .from(JSON_TEST) 114 | .where(JSON_TEST.NAME.eq("null-json")) 115 | .fetchOne(); 116 | 117 | assertEquals("null", r.value1().toString()); 118 | } 119 | 120 | @Test 121 | public void selectBothJsonAndJsonB() { 122 | Record2 r = dsl.select(JSON_TEST.DATA, JSON_TEST.DATAB) 123 | .from(JSON_TEST) 124 | .where(JSON_TEST.NAME.eq("null-sql")) 125 | .fetchOne(); 126 | 127 | assertNull(r.value1()); 128 | assertNull(r.value2()); 129 | } 130 | 131 | @Test 132 | public void testInsertJson() { 133 | dsl.deleteFrom(JSON_TEST).where(JSON_TEST.NAME.eq("json-insert")).execute(); 134 | 135 | JSON value = JSON.valueOf("{\"x\": 1}"); 136 | 137 | assertEquals(1, dsl.insertInto(JSON_TEST) 138 | .set(JSON_TEST.NAME, "json-insert") 139 | .set(JSON_TEST.DATA, value) 140 | .execute()); 141 | 142 | assertEquals(value, dsl.select(JSON_TEST.DATA).from(JSON_TEST).where(JSON_TEST.NAME.eq("json-insert")).fetchOne().value1()); 143 | } 144 | 145 | @Test 146 | public void testInsertJsonb() { 147 | dsl.deleteFrom(JSON_TEST).where(JSON_TEST.NAME.eq("jsonb-insert")).execute(); 148 | 149 | 150 | JSONB value = JSONB.valueOf("{\"x\": 1}"); 151 | 152 | assertEquals(1, dsl.insertInto(JSON_TEST) 153 | .set(JSON_TEST.NAME, "jsonb-insert") 154 | .set(JSON_TEST.DATAB, value) 155 | .execute()); 156 | 157 | assertEquals(value, dsl.select(JSON_TEST.DATAB).from(JSON_TEST).where(JSON_TEST.NAME.eq("jsonb-insert")).fetchOne().value1()); 158 | } 159 | 160 | @Test 161 | public void testUpdateJson() { 162 | dsl.deleteFrom(JSON_TEST).where(JSON_TEST.NAME.eq("json-update")).execute(); 163 | 164 | JSON initial = JSON.valueOf("{\"x\": 1}"); 165 | JSON updated = JSON.valueOf("{\"y\": 2}"); 166 | 167 | assertEquals(1, dsl.insertInto(JSON_TEST).set(JSON_TEST.NAME, "json-update").set(JSON_TEST.DATA, initial).execute()); 168 | assertEquals(1, dsl.update(JSON_TEST).set(JSON_TEST.DATA, updated).where(JSON_TEST.NAME.eq("json-update")).execute()); 169 | 170 | assertEquals(updated, dsl.select(JSON_TEST.DATA).from(JSON_TEST).where(JSON_TEST.NAME.eq("json-update")).fetchOne().value1()); 171 | } 172 | 173 | @Test 174 | public void testUpdateJsonb() { 175 | dsl.deleteFrom(JSON_TEST).where(JSON_TEST.NAME.eq("jsonb-update")).execute(); 176 | 177 | JSONB initial = JSONB.valueOf("{\"x\": 1}"); 178 | JSONB updated = JSONB.valueOf("{\"y\": 2}"); 179 | 180 | assertEquals(1, dsl.insertInto(JSON_TEST).set(JSON_TEST.NAME, "jsonb-update").set(JSON_TEST.DATAB, initial).execute()); 181 | assertEquals(1, dsl.update(JSON_TEST).set(JSON_TEST.DATAB, updated).where(JSON_TEST.NAME.eq("jsonb-update")).execute()); 182 | 183 | assertEquals(updated, dsl.select(JSON_TEST.DATAB).from(JSON_TEST).where(JSON_TEST.NAME.eq("jsonb-update")).fetchOne().value1()); 184 | } 185 | 186 | } 187 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonDSLArrayElementIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.github.t9t.jooq.json.JsonbDSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonDSL.arrayElement; 10 | 11 | public class JsonDSLArrayElementIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("arrayElement", Arrays.asList( 14 | test("getFirstObject").forArray().selecting(arrayElement(json, 0)).expectJson("{\"d\": 4408}"), 15 | test("getString").forArray().selecting(arrayElement(json, 3)).expectJson("\"json array\""), 16 | test("outOfBounds").forArray().selecting(arrayElement(json, 100)).expectNull(), 17 | test("negativeIndex").forArray().selecting(arrayElement(json, -2)).expectJson("true"), 18 | test("onObject").forArray().selecting(arrayElement(json, 100)).expectNull(), 19 | 20 | btest("getFirstObject").forArray().selecting(JsonbDSL.arrayElement(jsonb, 0)).expectJsonb("{\"d\": 4408}"), 21 | btest("getString").forArray().selecting(JsonbDSL.arrayElement(jsonb, 3)).expectJsonb("\"jsonb array\""), 22 | btest("outOfBounds").forArray().selecting(JsonbDSL.arrayElement(jsonb, 100)).expectNull(), 23 | btest("negativeIndex").forArray().selecting(JsonbDSL.arrayElement(jsonb, -2)).expectJsonb("true"), 24 | btest("onObject").forArray().selecting(JsonbDSL.arrayElement(jsonb, 100)).expectNull() 25 | )); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonDSLArrayElementTextIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.github.t9t.jooq.json.JsonbDSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonDSL.arrayElementText; 10 | 11 | public class JsonDSLArrayElementTextIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("arrayElementText", Arrays.asList( 14 | test("getFirstObject").forArray().selecting(arrayElementText(json, 0)).expectString("{\"d\": 4408}"), 15 | test("getString").forArray().selecting(arrayElementText(json, 3)).expectString("json array"), 16 | test("outOfBounds").forArray().selecting(arrayElementText(json, 100)).expectString(null), 17 | test("negativeIndex").forArray().selecting(arrayElementText(json, -2)).expectString("true"), 18 | test("onObject").selecting(arrayElementText(json, 100)).expectNull(), 19 | 20 | btest("getFirstObject").forArray().selecting(JsonbDSL.arrayElementText(jsonb, 0)).expectString("{\"d\": 4408}"), 21 | btest("getString").forArray().selecting(JsonbDSL.arrayElementText(jsonb, 3)).expectString("jsonb array"), 22 | btest("outOfBounds").forArray().selecting(JsonbDSL.arrayElementText(jsonb, 100)).expectString(null), 23 | btest("negativeIndex").forArray().selecting(JsonbDSL.arrayElementText(jsonb, -2)).expectString("true"), 24 | btest("onObject").selecting(JsonbDSL.arrayElementText(jsonb, 100)).expectNull() 25 | )); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonDSLArrayLengthIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.github.t9t.jooq.json.JsonbDSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonDSL.arrayLength; 10 | 11 | public class JsonDSLArrayLengthIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("arrayLength", Arrays.asList( 14 | test("zeroLength").usingJson("[]").selecting(arrayLength(json)).expect(0), 15 | test("nonZeroLength").usingJson("[1, \"b\", false]").selecting(arrayLength(json)).expect(3), 16 | 17 | btest("zeroLength").usingJson("[]").selecting(JsonbDSL.arrayLength(jsonb)).expect(0), 18 | btest("nonZeroLength").usingJson("[1, \"b\", false]").selecting(JsonbDSL.arrayLength(jsonb)).expect(3) 19 | )); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonDSLExtractPathIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.github.t9t.jooq.json.JsonbDSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonDSL.extractPath; 10 | 11 | public class JsonDSLExtractPathIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("extractPath", Arrays.asList( 14 | test("oneLevel").selecting(extractPath(json, "str")).expectJson("\"Hello, json world!\""), 15 | test("obj").selecting(extractPath(json, "obj")).expectJson(toNode("{\"i\": 5521, \"b\": true}")), 16 | test("deepVarargs").selecting(extractPath(json, "arr", "0", "d")).expectJson("4408"), 17 | test("deepCollection").selecting(extractPath(json, Arrays.asList("arr", "0", "d"))).expectJson("4408"), 18 | test("notExistingPath").selecting(extractPath(json, "not", "existing", "path")).expectNull(), 19 | 20 | btest("oneLevel").selecting(JsonbDSL.extractPath(jsonb, "str")).expectJsonb("\"Hello, jsonb world!\""), 21 | btest("obj").selecting(JsonbDSL.extractPath(jsonb, "obj")).expectJson(toNode("{\"i\": 5521, \"b\": true}")), 22 | btest("deepVarargs").selecting(JsonbDSL.extractPath(jsonb, "arr", "0", "d")).expectJsonb("4408"), 23 | btest("deepCollection").selecting(JsonbDSL.extractPath(jsonb, Arrays.asList("arr", "0", "d"))).expectJsonb("4408"), 24 | btest("notExistingPath").selecting(JsonbDSL.extractPath(jsonb, "not", "existing", "path")).expectNull() 25 | )); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonDSLExtractPathTextIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.github.t9t.jooq.json.JsonbDSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonDSL.extractPathText; 10 | 11 | public class JsonDSLExtractPathTextIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("extractPathText", Arrays.asList( 14 | test("oneLevel").selecting(extractPathText(json, "str")).expectString("Hello, json world!"), 15 | test("obj").selecting(extractPathText(json, "obj")).expectJson(toNode("{\"i\": 5521, \"b\": true}")), 16 | test("deepVarargs").selecting(extractPathText(json, "arr", "0", "d")).expectString("4408"), 17 | test("deepCollection").selecting(extractPathText(json, Arrays.asList("arr", "0", "d"))).expectString("4408"), 18 | test("notExistingPath").selecting(extractPathText(json, "not", "existing", "path")).expectNull(), 19 | 20 | btest("oneLevel").selecting(JsonbDSL.extractPathText(jsonb, "str")).expectString("Hello, jsonb world!"), 21 | btest("obj").selecting(JsonbDSL.extractPathText(jsonb, "obj")).expectJson(toNode("{\"i\": 5521, \"b\": true}")), 22 | btest("deepVarargs").selecting(JsonbDSL.extractPathText(jsonb, "arr", "0", "d")).expectString("4408"), 23 | btest("deepCollection").selecting(JsonbDSL.extractPathText(jsonb, Arrays.asList("arr", "0", "d"))).expectString("4408"), 24 | btest("notExistingPath").selecting(JsonbDSL.extractPathText(jsonb, "not", "existing", "path")).expectNull() 25 | )); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonDSLFieldByKeyIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.github.t9t.jooq.json.JsonbDSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonDSL.fieldByKey; 10 | 11 | public class JsonDSLFieldByKeyIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("fieldByKey", Arrays.asList( 14 | test("string").selecting(fieldByKey(json, "str")).expectJson("\"Hello, json world!\""), 15 | test("twoLevels").selecting(fieldByKey(fieldByKey(json, "obj"), "i")).expectJson("5521"), 16 | test("nullField").selecting(fieldByKey(json, "n")).expectJson("null"), 17 | test("notExistingField").selecting(fieldByKey(json, "notExisting")).expectNull(), 18 | 19 | btest("string").selecting(JsonbDSL.fieldByKey(jsonb, "str")).expectJsonb("\"Hello, jsonb world!\""), 20 | btest("twoLevels").selecting(JsonbDSL.fieldByKey(JsonbDSL.fieldByKey(jsonb, "obj"), "i")).expectJsonb("5521"), 21 | btest("nullField").selecting(JsonbDSL.fieldByKey(jsonb, "n")).expectJsonb("null"), 22 | btest("notExistingField").selecting(JsonbDSL.fieldByKey(jsonb, "notExisting")).expectNull() 23 | )); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonDSLFieldByKeyTextIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.github.t9t.jooq.json.JsonDSL; 4 | import com.github.t9t.jooq.json.JsonbDSL; 5 | import org.junit.jupiter.params.provider.Arguments; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | import static com.github.t9t.jooq.json.JsonDSL.fieldByKeyText; 11 | 12 | public class JsonDSLFieldByKeyTextIT extends AbstractJsonDSLTest { 13 | public static List params() { 14 | return generateParams("fieldByKeyText", Arrays.asList( 15 | test("string").selecting(fieldByKeyText(json, "str")).expectString("Hello, json world!"), 16 | test("twoLevels").selecting(fieldByKeyText(JsonDSL.fieldByKey(json, "obj"), "i")).expectString("5521"), 17 | test("nullField").selecting(fieldByKeyText(json, "n")).expectNull(), 18 | test("notExistingField").selecting(fieldByKeyText(json, "notExisting")).expectNull(), 19 | 20 | btest("string").selecting(JsonbDSL.fieldByKeyText(jsonb, "str")).expectString("Hello, jsonb world!"), 21 | btest("twoLevels").selecting(JsonbDSL.fieldByKeyText(JsonbDSL.fieldByKey(jsonb, "obj"), "i")).expectString("5521"), 22 | btest("nullField").selecting(JsonbDSL.fieldByKeyText(jsonb, "n")).expectNull(), 23 | btest("notExistingField").selecting(JsonbDSL.fieldByKeyText(jsonb, "notExisting")).expectNull() 24 | )); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonDSLObjectAtPathIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.github.t9t.jooq.json.JsonbDSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonDSL.objectAtPath; 10 | 11 | public class JsonDSLObjectAtPathIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("objectAtPath", Arrays.asList( 14 | test("oneLevel").selecting(objectAtPath(json, "str")).expectJson("\"Hello, json world!\""), 15 | test("obj").selecting(objectAtPath(json, "obj")).expectJson(toNode("{\"i\": 5521, \"b\": true}")), 16 | test("deepVarargs").selecting(objectAtPath(json, "arr", "0", "d")).expectJson("4408"), 17 | test("deepCollection").selecting(objectAtPath(json, Arrays.asList("arr", "0", "d"))).expectJson("4408"), 18 | test("notExistingPath").selecting(objectAtPath(json, "not", "existing", "path")).expectNull(), 19 | 20 | btest("oneLevel").selecting(JsonbDSL.objectAtPath(jsonb, "str")).expectJsonb("\"Hello, jsonb world!\""), 21 | btest("obj").selecting(JsonbDSL.objectAtPath(jsonb, "obj")).expectJson(toNode("{\"i\": 5521, \"b\": true}")), 22 | btest("deepVarargs").selecting(JsonbDSL.objectAtPath(jsonb, "arr", "0", "d")).expectJsonb("4408"), 23 | btest("deepCollection").selecting(JsonbDSL.objectAtPath(jsonb, Arrays.asList("arr", "0", "d"))).expectJsonb("4408"), 24 | btest("notExistingPath").selecting(JsonbDSL.objectAtPath(jsonb, "not", "existing", "path")).expectNull() 25 | )); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonDSLObjectAtPathTextIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.github.t9t.jooq.json.JsonbDSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonDSL.objectAtPathText; 10 | 11 | public class JsonDSLObjectAtPathTextIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("objectAtPathText", Arrays.asList( 14 | test("oneLevel").selecting(objectAtPathText(json, "str")).expectString("Hello, json world!"), 15 | test("obj").selecting(objectAtPathText(json, "obj")).expectJson(toNode("{\"i\": 5521, \"b\": true}")), 16 | test("deepVarargs").selecting(objectAtPathText(json, "arr", "0", "d")).expectString("4408"), 17 | test("deepCollection").selecting(objectAtPathText(json, Arrays.asList("arr", "0", "d"))).expectString("4408"), 18 | test("notExistingPath").selecting(objectAtPathText(json, "not", "existing", "path")).expectNull(), 19 | 20 | btest("oneLevel").selecting(JsonbDSL.objectAtPathText(jsonb, "str")).expectString("Hello, jsonb world!"), 21 | btest("obj").selecting(JsonbDSL.objectAtPathText(jsonb, "obj")).expectJson(toNode("{\"i\": 5521, \"b\": true}")), 22 | btest("deepVarargs").selecting(JsonbDSL.objectAtPathText(jsonb, "arr", "0", "d")).expectString("4408"), 23 | btest("deepCollection").selecting(JsonbDSL.objectAtPathText(jsonb, Arrays.asList("arr", "0", "d"))).expectString("4408"), 24 | btest("notExistingPath").selecting(JsonbDSL.objectAtPathText(jsonb, "not", "existing", "path")).expectNull() 25 | )); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonDSLStripNullsIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.github.t9t.jooq.json.JsonbDSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonDSL.stripNulls; 10 | 11 | public class JsonDSLStripNullsIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("stripNulls", Arrays.asList( 14 | test("object").selecting(stripNulls(json)).expectJson(toNode("{\"obj\": {\"i\": 5521, \"b\": true}, \"arr\": [{\"d\": 4408}, 10, true, \"s\"], \"num\": 1337, \"str\": \"Hello, json world!\"}")), 15 | test("array").usingJson("[\"a\", null, \"b\", 2, null, 3, null]").selecting(stripNulls(json)).expectJson(toNode("[\"a\", null, \"b\", 2, null, 3, null]")), 16 | test("mixAndNested").usingJson("{\"ar\": [1, null, {\"d\": null}], \"o\": {\"n\": null, \"x\": 2, \"z\": [null]}, \"t\": null}") 17 | .selecting(stripNulls(json)).expectJson(toNode("{\"ar\": [1, null, {}], \"o\": {\"x\": 2, \"z\": [null]}}")), 18 | 19 | btest("object").selecting(JsonbDSL.stripNulls(jsonb)).expectJson(toNode("{\"obj\": {\"i\": 5521, \"b\": true}, \"arr\": [{\"d\": 4408}, 10, true, \"s\"], \"num\": 1337, \"str\": \"Hello, jsonb world!\"}")), 20 | btest("array").usingJson("[\"a\", null, \"b\", 2, null, 3, null]").selecting(JsonbDSL.stripNulls(jsonb)).expectJson(toNode("[\"a\", null, \"b\", 2, null, 3, null]")), 21 | btest("mixAndNested").usingJson("{\"ar\": [1, null, {\"d\": null}], \"o\": {\"n\": null, \"x\": 2, \"z\": [null]}, \"t\": null}") 22 | .selecting(JsonbDSL.stripNulls(jsonb)).expectJson(toNode("{\"ar\": [1, null, {}], \"o\": {\"x\": 2, \"z\": [null]}}")) 23 | )); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonDSLTypeOfIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import com.github.t9t.jooq.json.JsonbDSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonDSL.typeOf; 10 | 11 | public class JsonDSLTypeOfIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("typeOf", Arrays.asList( 14 | test("object").selecting(typeOf(json)).expectString("object"), 15 | test("array").forArray().selecting(typeOf(json)).expectString("array"), 16 | test("string").usingJson("\"Hello, world\"").selecting(typeOf(json)).expectString("string"), 17 | test("number").usingJson("5521").selecting(typeOf(json)).expectString("number"), 18 | test("numberFraction").usingJson("55.21").selecting(typeOf(json)).expectString("number"), 19 | test("numberNegative").usingJson("-5521").selecting(typeOf(json)).expectString("number"), 20 | test("booleanTrue").usingJson("true").selecting(typeOf(json)).expectString("boolean"), 21 | test("booleanFalse").usingJson("false").selecting(typeOf(json)).expectString("boolean"), 22 | test("null").usingJson("null").selecting(typeOf(json)).expectString("null"), 23 | test("nullString").usingJson("\"null\"").selecting(typeOf(json)).expectString("string"), 24 | 25 | btest("object").selecting(JsonbDSL.typeOf(jsonb)).expectString("object"), 26 | btest("array").forArray().selecting(JsonbDSL.typeOf(jsonb)).expectString("array"), 27 | btest("string").usingJson("\"Hello, world\"").selecting(JsonbDSL.typeOf(jsonb)).expectString("string"), 28 | btest("number").usingJson("5521").selecting(JsonbDSL.typeOf(jsonb)).expectString("number"), 29 | btest("numberFraction").usingJson("55.21").selecting(JsonbDSL.typeOf(jsonb)).expectString("number"), 30 | btest("numberNegative").usingJson("-5521").selecting(JsonbDSL.typeOf(jsonb)).expectString("number"), 31 | btest("booleanTrue").usingJson("true").selecting(JsonbDSL.typeOf(jsonb)).expectString("boolean"), 32 | btest("booleanFalse").usingJson("false").selecting(JsonbDSL.typeOf(jsonb)).expectString("boolean"), 33 | btest("null").usingJson("null").selecting(JsonbDSL.typeOf(jsonb)).expectString("null"), 34 | btest("nullString").usingJson("\"null\"").selecting(JsonbDSL.typeOf(jsonb)).expectString("string") 35 | )); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonbDSLConcatIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import org.junit.jupiter.params.provider.Arguments; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static com.github.t9t.jooq.json.JsonbDSL.concat; 9 | import static com.github.t9t.jooq.json.JsonbDSL.field; 10 | 11 | public class JsonbDSLConcatIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("concat", Arrays.asList( 14 | btest("objects").selecting(concat(field("{\"a\": 10}"), field("{\"b\": 20}"))).expectJsonb("{\"a\": 10, \"b\": 20}"), 15 | btest("arrays").selecting(concat(field("[\"a\", 10, true]"), field("[50, {\"b\": 20}]"))).expectJsonb("[\"a\", 10, true, 50, {\"b\": 20}]"), 16 | btest("arrayAndObject").selecting(concat(field("[\"a\", 10, true]"), field("{\"b\": 20}"))).expectJsonb("[\"a\", 10, true, {\"b\": 20}]"), 17 | btest("nested").selecting(concat(concat(field("{\"a\": 10}"), field("{\"b\": 20}")), field("{\"c\": 30}"))).expectJsonb("{\"a\": 10, \"b\": 20, \"c\": 30}") 18 | )); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonbDSLContainedInIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import org.jooq.impl.DSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonbDSL.containedIn; 10 | import static com.github.t9t.jooq.json.JsonbDSL.field; 11 | 12 | public class JsonbDSLContainedInIT extends AbstractJsonDSLTest { 13 | public static List params() { 14 | return generateParams("contains", Arrays.asList( 15 | btest("simple").selecting(DSL.field(containedIn(field("{\"num\": 1337}"), jsonb))).expect(true), 16 | btest("complex").selecting(DSL.field(containedIn(field("{\"obj\": {\"i\": 5521, \"b\": true}}"), jsonb))).expect(true), 17 | btest("array").forArray().selecting(DSL.field(containedIn(field("[\"jsonb array\", 10]"), jsonb))).expect(true), 18 | btest("doesn't").selecting(DSL.field(containedIn(field("{\"num\": 1338}"), jsonb))).expect(false) 19 | )); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonbDSLContainsIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import org.jooq.impl.DSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonbDSL.contains; 10 | import static com.github.t9t.jooq.json.JsonbDSL.field; 11 | 12 | public class JsonbDSLContainsIT extends AbstractJsonDSLTest { 13 | public static List params() { 14 | return generateParams("contains", Arrays.asList( 15 | btest("simple").selecting(DSL.field(contains(jsonb, field("{\"num\": 1337}")))).expect(true), 16 | btest("complex").selecting(DSL.field(contains(jsonb, field("{\"obj\": {\"i\": 5521, \"b\": true}}")))).expect(true), 17 | btest("arrayInt").forArray().selecting(DSL.field(contains(jsonb, field("10")))).expect(true), 18 | btest("arrayStr").forArray().selecting(DSL.field(contains(jsonb, field("\"jsonb array\"")))).expect(true), 19 | btest("arrayArray").forArray().selecting(DSL.field(contains(jsonb, field("[\"jsonb array\", 10]")))).expect(true), 20 | btest("doesn't").selecting(DSL.field(contains(jsonb, field("{\"num\": 1338}")))).expect(false) 21 | )); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonbDSLDeleteElementIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import org.junit.jupiter.params.provider.Arguments; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static com.github.t9t.jooq.json.JsonbDSL.deleteElement; 9 | import static com.github.t9t.jooq.json.JsonbDSL.field; 10 | 11 | public class JsonbDSLDeleteElementIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("deleteElement", Arrays.asList( 14 | btest("array").selecting(deleteElement(field("[\"a\", 1, \"b\", false, \"c\"]"), 2)).expectJsonb("[\"a\", 1, false, \"c\"]"), 15 | btest("nested").selecting(deleteElement(deleteElement(field("[\"a\", 1, \"b\", false, \"c\"]"), 2), 2)).expectJsonb("[\"a\", 1, \"c\"]"), 16 | btest("negativeIndex").selecting(deleteElement(field("[\"a\", 1, \"b\", false, \"c\"]"), -2)).expectJsonb("[\"a\", 1, \"b\", \"c\"]") 17 | )); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonbDSLDeleteIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import org.junit.jupiter.params.provider.Arguments; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static com.github.t9t.jooq.json.JsonbDSL.delete; 9 | import static com.github.t9t.jooq.json.JsonbDSL.field; 10 | 11 | public class JsonbDSLDeleteIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("delete", Arrays.asList( 14 | btest("obj").selecting(delete(field("{\"a\": 10, \"b\": 20}"), "a")).expectJsonb("{\"b\": 20}"), 15 | btest("array").selecting(delete(field("[\"a\", 1, \"b\", false, \"c\"]"), "b")).expectJsonb("[\"a\", 1, false, \"c\"]"), 16 | btest("nested").selecting(delete(delete(field("{\"a\": 10, \"b\": 20, \"c\": 30}"), "a"), "c")).expectJsonb("{\"b\": 20}"), 17 | 18 | btest("multi_obj").selecting(delete(field("{\"a\": 10, \"b\": 20, \"c\": 30}"), "b", "a")).expectJsonb("{\"c\": 30}"), 19 | btest("multi_array").selecting(delete(field("[\"a\", 1, \"b\", false, \"c\"]"), "b", "a")).expectJsonb("[1, false, \"c\"]") 20 | )); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonbDSLDeletePathIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import org.junit.jupiter.params.provider.Arguments; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static com.github.t9t.jooq.json.JsonbDSL.deletePath; 9 | import static com.github.t9t.jooq.json.JsonbDSL.field; 10 | 11 | public class JsonbDSLDeletePathIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("deletePath", Arrays.asList( 14 | btest("obj").selecting(deletePath(field("{\"a\": 10, \"b\": 20}"), "a")).expectJsonb("{\"b\": 20}"), 15 | btest("array").selecting(deletePath(field("[\"a\", 1, \"b\", false, \"c\"]"), "2")).expectJsonb("[\"a\", 1, false, \"c\"]"), 16 | btest("complex").selecting(deletePath(field("{\"a\": 10, \"b\": [20, {\"x\": {\"y\": [1, 2, 3]}}], \"c\": 30}"), "b", "1", "x", "y", "0")) 17 | .expectJsonb("{\"a\": 10, \"b\": [20, {\"x\": {\"y\": [2, 3]}}], \"c\": 30}") 18 | )); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonbDSLHasAllKeysIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import org.jooq.impl.DSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonbDSL.hasAllKeys; 10 | 11 | public class JsonbDSLHasAllKeysIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("hasAllKeys", Arrays.asList( 14 | btest("one").selecting(DSL.field(hasAllKeys(jsonb, "str"))).expect(true), 15 | btest("multiple").selecting(DSL.field(hasAllKeys(jsonb, "str", "obj", "num"))).expect(true), 16 | btest("collection").selecting(DSL.field(hasAllKeys(jsonb, Arrays.asList("str", "obj", "num")))).expect(true), 17 | btest("doesn't").selecting(DSL.field(hasAllKeys(jsonb, "nope"))).expect(false), 18 | btest("someDon't").selecting(DSL.field(hasAllKeys(jsonb, "str", "nope"))).expect(false), 19 | btest("array").forArray().selecting(DSL.field(hasAllKeys(jsonb, "jsonb array"))).expect(true) 20 | )); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonbDSLHasAnyKeyIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import org.jooq.impl.DSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonbDSL.hasAnyKey; 10 | 11 | public class JsonbDSLHasAnyKeyIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("hasAnyKey", Arrays.asList( 14 | btest("one").selecting(DSL.field(hasAnyKey(jsonb, "str"))).expect(true), 15 | btest("multiple").selecting(DSL.field(hasAnyKey(jsonb, "str", "obj", "num"))).expect(true), 16 | btest("collection").selecting(DSL.field(hasAnyKey(jsonb, Arrays.asList("str", "obj", "num")))).expect(true), 17 | btest("doesn't").selecting(DSL.field(hasAnyKey(jsonb, "nope"))).expect(false), 18 | btest("someDon't").selecting(DSL.field(hasAnyKey(jsonb, "str", "nope"))).expect(true), 19 | btest("array").forArray().selecting(DSL.field(hasAnyKey(jsonb, "jsonb array", "nope"))).expect(true) 20 | )); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonbDSLHasKeyIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import org.jooq.impl.DSL; 4 | import org.junit.jupiter.params.provider.Arguments; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import static com.github.t9t.jooq.json.JsonbDSL.hasKey; 10 | 11 | public class JsonbDSLHasKeyIT extends AbstractJsonDSLTest { 12 | public static List params() { 13 | return generateParams("hasKey", Arrays.asList( 14 | btest("does").selecting(DSL.field(hasKey(jsonb, "str"))).expect(true), 15 | btest("array").forArray().selecting(DSL.field(hasKey(jsonb, "jsonb array"))).expect(true), 16 | btest("doesn't").selecting(DSL.field(hasKey(jsonb, "nope"))).expect(false) 17 | )); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/JsonbDSLPrettyIT.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import org.junit.jupiter.params.provider.Arguments; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | import static com.github.t9t.jooq.json.JsonbDSL.pretty; 9 | 10 | public class JsonbDSLPrettyIT extends AbstractJsonDSLTest { 11 | public static List params() { 12 | return generateParams("pretty", Arrays.asList( 13 | btest("emptyObject").usingJson("{}").selecting(pretty(jsonb)).expectString("{\n}"), 14 | btest("emptyArray").usingJson("[]").selecting(pretty(jsonb)).expectString("[\n]"), 15 | btest("object").usingJson("{\"a\": \"b\"}").selecting(pretty(jsonb)).expectString("{\n \"a\": \"b\"\n}"), 16 | btest("array").usingJson("[\"a\", \"b\"]").selecting(pretty(jsonb)).expectString("[\n \"a\",\n \"b\"\n]") 17 | )); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /integration-tests/src/test/java/com/github/t9t/jooq/json/testing/TestDb.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing; 2 | 3 | import org.postgresql.ds.PGSimpleDataSource; 4 | 5 | import javax.sql.DataSource; 6 | 7 | final class TestDb { 8 | private TestDb() { 9 | } 10 | 11 | static DataSource createDataSource() { 12 | PGSimpleDataSource ds = new PGSimpleDataSource(); 13 | ds.setURL("jdbc:postgresql://localhost:23719/jooq"); 14 | ds.setUser("jooq"); 15 | ds.setPassword("jooq"); 16 | return ds; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /integration-tests/src/test/kotlin/com/github/t9t/jooq/json/testing/JsonKotlinIT.kt: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing 2 | 3 | import com.github.t9t.jooq.generated.kotlin.tables.references.JSON_TEST 4 | import org.jooq.JSON 5 | import org.jooq.JSONB 6 | import org.jooq.Record1 7 | import org.jooq.SQLDialect 8 | import org.jooq.impl.DSL 9 | import org.junit.jupiter.api.Assertions.assertEquals 10 | import org.junit.jupiter.api.Assertions.assertNull 11 | import org.junit.jupiter.api.BeforeEach 12 | import org.junit.jupiter.api.Test 13 | 14 | /** 15 | * Validates Kotlin, jOOQ and JSON(B) interoperability 16 | */ 17 | class JsonKotlinIT { 18 | private val ds = TestDb.createDataSource() 19 | private val dsl = DSL.using(ds, SQLDialect.POSTGRES) 20 | 21 | @BeforeEach 22 | fun setUp() { 23 | dsl.deleteFrom(JSON_TEST).execute() 24 | assertEquals(4, dsl.execute("insert into jooq.json_test (name, data, datab)" + 25 | " values " + 26 | "('both', '{\"json\": {\"int\": 100, \"str\": \"Hello, JSON world!\", \"object\": {\"v\": 200}, \"n\": null}}', '{\"jsonb\": {\"int\": 100, \"str\": \"Hello, JSONB world!\", \"object\": {\"v\": 200}, \"n\": null}}')," + 27 | "('empty', '{}', '{}')," + 28 | "('null-sql', null, null)," + 29 | "('null-json', 'null'::json, 'null'::jsonb)").toLong()) 30 | } 31 | 32 | @Test 33 | fun `select JSON NULL SQL, returning non-null JSON type`() { 34 | val r: Record1? = dsl.select(JSON_TEST.DATA) 35 | .from(JSON_TEST) 36 | .where(JSON_TEST.NAME.eq("null-sql")) 37 | .fetchOne() 38 | assertNull(r?.value1()) 39 | } 40 | 41 | @Test 42 | fun `select JSON NULL SQL, returning nullable JSON type`() { 43 | val r: Record1? = dsl.select(JSON_TEST.DATA) 44 | .from(JSON_TEST) 45 | .where(JSON_TEST.NAME.eq("null-sql")) 46 | .fetchOne() 47 | assertNull(r?.value1()) 48 | } 49 | 50 | @Test 51 | fun `select null JSON value`() { 52 | val r: Record1? = dsl.select(JSON_TEST.DATA) 53 | .from(JSON_TEST) 54 | .where(JSON_TEST.NAME.eq("null-json")) 55 | .fetchOne() 56 | assertEquals("null", r?.value1()?.toString()) 57 | } 58 | 59 | @Test 60 | fun `select JSON NULL SQL, returning non-null JSONB type`() { 61 | val r: Record1? = dsl.select(JSON_TEST.DATAB) 62 | .from(JSON_TEST) 63 | .where(JSON_TEST.NAME.eq("null-sql")) 64 | .fetchOne() 65 | assertNull(r?.value1()) 66 | } 67 | 68 | @Test 69 | fun `select JSON NULL SQL, returning nullable JSONB type`() { 70 | val r: Record1? = dsl.select(JSON_TEST.DATAB) 71 | .from(JSON_TEST) 72 | .where(JSON_TEST.NAME.eq("null-sql")) 73 | .fetchOne() 74 | assertNull(r?.value1()) 75 | } 76 | 77 | @Test 78 | fun `select null JSONB value`() { 79 | val r = dsl.select(JSON_TEST.DATAB) 80 | .from(JSON_TEST) 81 | .where(JSON_TEST.NAME.eq("null-json")) 82 | .fetchOne() 83 | assertEquals("null", r?.value1()?.toString()) 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /integration-tests/src/test/kotlin/com/github/t9t/jooq/json/testing/json/JsonDSLTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing.json 2 | 3 | import com.github.t9t.jooq.json.JsonDSL 4 | import com.github.t9t.jooq.json.json.* 5 | import org.jooq.Field 6 | import org.jooq.JSON 7 | import org.jooq.impl.DSL 8 | import org.junit.jupiter.api.Assertions.assertEquals 9 | import org.junit.jupiter.api.Test 10 | 11 | class JsonDSLTest { 12 | 13 | private val jsonField: Field = DSL.field("foo.bar", JSON::class.java) 14 | 15 | @Test 16 | fun `should provide extension function to create field from JSON`() { 17 | /* Given */ 18 | val json = JSON.valueOf("{}") 19 | /* When */ 20 | val jsonField = JsonDSL.field(json) 21 | val jsonFieldExt = json.toField() 22 | /* Then */ 23 | assertEquals(jsonField, jsonFieldExt) 24 | } 25 | 26 | @Test 27 | fun `should provide extension function for arrayElement`() { 28 | /* Given */ 29 | val index = 1 30 | /* When */ 31 | val arrayElementField = JsonDSL.arrayElement(jsonField, index) 32 | val arrayElementFieldExt = jsonField.arrayElement(index) 33 | /* Then */ 34 | assertEquals(arrayElementField, arrayElementFieldExt) 35 | } 36 | 37 | @Test 38 | fun `should provide extension function for arrayElementText`() { 39 | /* Given */ 40 | val index = 1 41 | /* When */ 42 | val arrayElementTextField = JsonDSL.arrayElementText(jsonField, index) 43 | val arrayElementTextFieldExt = jsonField.arrayElementText(index) 44 | /* Then */ 45 | assertEquals(arrayElementTextField, arrayElementTextFieldExt) 46 | } 47 | 48 | @Test 49 | fun `should provide extension function for fieldByKey`() { 50 | /* Given */ 51 | val key = "key" 52 | /* When */ 53 | val fieldByKeyField = JsonDSL.fieldByKey(jsonField, key) 54 | val fieldByKeyFieldExt = jsonField.fieldByKey(key) 55 | /* Then */ 56 | assertEquals(fieldByKeyField, fieldByKeyFieldExt) 57 | } 58 | 59 | @Test 60 | fun `should provide extension function for fieldByKeyText`() { 61 | /* Given */ 62 | val key = "key" 63 | /* When */ 64 | val fieldByKeyTextField = JsonDSL.fieldByKeyText(jsonField, key) 65 | val fieldByKeyTextFieldExt = jsonField.fieldByKeyText(key) 66 | /* Then */ 67 | assertEquals(fieldByKeyTextField, fieldByKeyTextFieldExt) 68 | } 69 | 70 | @Test 71 | fun `should provide extension function for objectAtPath with varargs`() { 72 | /* Given */ 73 | val path = arrayOf("path", "to", "key") 74 | /* When */ 75 | val objectAtPathField = JsonDSL.objectAtPath(jsonField, *path) 76 | val objectAtPathFieldExt = jsonField.objectAtPath(*path) 77 | /* Then */ 78 | assertEquals(objectAtPathField, objectAtPathFieldExt) 79 | } 80 | 81 | @Test 82 | fun `should provide extension function for objectAtPath with collection`() { 83 | /* Given */ 84 | val path = arrayOf("path", "to", "key") 85 | /* When */ 86 | val objectAtPathField = JsonDSL.objectAtPath(jsonField, path.toList()) 87 | val objectAtPathFieldExt = jsonField.objectAtPath(path.toList()) 88 | /* Then */ 89 | assertEquals(objectAtPathField, objectAtPathFieldExt) 90 | } 91 | 92 | @Test 93 | fun `should provide extension function for objectAtPathText with varargs`() { 94 | /* Given */ 95 | val path = arrayOf("path", "to", "key") 96 | /* When */ 97 | val objectAtPathTextField = JsonDSL.objectAtPathText(jsonField, *path) 98 | val objectAtPathTextFieldExt = jsonField.objectAtPathText(*path) 99 | /* Then */ 100 | assertEquals(objectAtPathTextField, objectAtPathTextFieldExt) 101 | } 102 | 103 | @Test 104 | fun `should provide extension function for objectAtPathText with collection`() { 105 | /* Given */ 106 | val path = arrayOf("path", "to", "key") 107 | /* When */ 108 | val objectAtPathTextField = JsonDSL.objectAtPathText(jsonField, path.toList()) 109 | val objectAtPathTextFieldExt = jsonField.objectAtPathText(path.toList()) 110 | /* Then */ 111 | assertEquals(objectAtPathTextField, objectAtPathTextFieldExt) 112 | } 113 | 114 | @Test 115 | fun `should provide function for arrayLength`() { 116 | /* Given */ 117 | /* When */ 118 | val arrayLengthField = JsonDSL.arrayLength(jsonField) 119 | val arrayLengthFieldExt = arrayLength(jsonField) 120 | /* Then */ 121 | assertEquals(arrayLengthField, arrayLengthFieldExt) 122 | } 123 | 124 | @Test 125 | fun `should provide function for extractPath with varargs`() { 126 | /* Given */ 127 | val path = arrayOf("path", "to", "key") 128 | /* When */ 129 | val extractPathField = JsonDSL.extractPath(jsonField, *path) 130 | val extractPathFieldExt = extractPath(jsonField, *path) 131 | /* Then */ 132 | assertEquals(extractPathField, extractPathFieldExt) 133 | } 134 | 135 | @Test 136 | fun `should provide function for extractPath with collection`() { 137 | /* Given */ 138 | val path = arrayOf("path", "to", "key") 139 | /* When */ 140 | val extractPathField = JsonDSL.extractPath(jsonField, path.toList()) 141 | val extractPathFieldExt = extractPath(jsonField, path.toList()) 142 | /* Then */ 143 | assertEquals(extractPathField, extractPathFieldExt) 144 | } 145 | 146 | @Test 147 | fun `should provide function for extractPathText with varargs`() { 148 | /* Given */ 149 | val path = arrayOf("path", "to", "key") 150 | /* When */ 151 | val extractPathTextField = JsonDSL.extractPathText(jsonField, *path) 152 | val extractPathTextFieldExt = extractPathText(jsonField, *path) 153 | /* Then */ 154 | assertEquals(extractPathTextField, extractPathTextFieldExt) 155 | } 156 | 157 | @Test 158 | fun `should provide function for extractPathText with collection`() { 159 | /* Given */ 160 | val path = arrayOf("path", "to", "key") 161 | /* When */ 162 | val extractPathTextField = JsonDSL.extractPathText(jsonField, path.toList()) 163 | val extractPathTextFieldExt = extractPathText(jsonField, path.toList()) 164 | /* Then */ 165 | assertEquals(extractPathTextField, extractPathTextFieldExt) 166 | } 167 | 168 | @Test 169 | fun `should provide function for typeOf`() { 170 | /* Given */ 171 | /* When */ 172 | val typeOfField = JsonDSL.typeOf(jsonField) 173 | val typeOfFieldExt = typeOf(jsonField) 174 | /* Then */ 175 | assertEquals(typeOfField, typeOfFieldExt) 176 | } 177 | 178 | @Test 179 | fun `should provide function for stripNulls`() { 180 | /* Given */ 181 | /* When */ 182 | val stripNullsField = JsonDSL.stripNulls(jsonField) 183 | val stripNullsFieldExt = stripNulls(jsonField) 184 | /* Then */ 185 | assertEquals(stripNullsField, stripNullsFieldExt) 186 | } 187 | 188 | } 189 | -------------------------------------------------------------------------------- /integration-tests/src/test/kotlin/com/github/t9t/jooq/json/testing/jsonb/JsonbDSLTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json.testing.jsonb 2 | 3 | import com.github.t9t.jooq.json.JsonbDSL 4 | import com.github.t9t.jooq.json.jsonb.* 5 | import org.jooq.Field 6 | import org.jooq.JSONB 7 | import org.jooq.impl.DSL 8 | import org.junit.jupiter.api.Assertions.assertEquals 9 | import org.junit.jupiter.api.Test 10 | 11 | class JsonbDSLTest { 12 | 13 | private val jsonbField: Field = DSL.field("foo.bar", JSONB::class.java) 14 | 15 | @Test 16 | fun `should provide extension function to create field from JSON`() { 17 | /* Given */ 18 | val jsonb = JSONB.valueOf("{}") 19 | /* When */ 20 | val jsonbField = JsonbDSL.field(jsonb) 21 | val jsonbFieldExt = jsonb.toField() 22 | /* Then */ 23 | assertEquals(jsonbField, jsonbFieldExt) 24 | } 25 | 26 | @Test 27 | fun `should provide extension function for arrayElement`() { 28 | /* Given */ 29 | val index = 1 30 | /* When */ 31 | val arrayElementField = JsonbDSL.arrayElement(jsonbField, index) 32 | val arrayElementFieldExt = jsonbField.arrayElement(index) 33 | /* Then */ 34 | assertEquals(arrayElementField, arrayElementFieldExt) 35 | } 36 | 37 | @Test 38 | fun `should provide extension function for arrayElementText`() { 39 | /* Given */ 40 | val index = 1 41 | /* When */ 42 | val arrayElementTextField = JsonbDSL.arrayElementText(jsonbField, index) 43 | val arrayElementTextFieldExt = jsonbField.arrayElementText(index) 44 | /* Then */ 45 | assertEquals(arrayElementTextField, arrayElementTextFieldExt) 46 | } 47 | 48 | @Test 49 | fun `should provide extension function for fieldByKey`() { 50 | /* Given */ 51 | val key = "key" 52 | /* When */ 53 | val fieldByKeyField = JsonbDSL.fieldByKey(jsonbField, key) 54 | val fieldByKeyFieldExt = jsonbField.fieldByKey(key) 55 | /* Then */ 56 | assertEquals(fieldByKeyField, fieldByKeyFieldExt) 57 | } 58 | 59 | @Test 60 | fun `should provide extension function for fieldByKeyText`() { 61 | /* Given */ 62 | val key = "key" 63 | /* When */ 64 | val fieldByKeyTextField = JsonbDSL.fieldByKeyText(jsonbField, key) 65 | val fieldByKeyTextFieldExt = jsonbField.fieldByKeyText(key) 66 | /* Then */ 67 | assertEquals(fieldByKeyTextField, fieldByKeyTextFieldExt) 68 | } 69 | 70 | @Test 71 | fun `should provide extension function for objectAtPath with varargs`() { 72 | /* Given */ 73 | val path = arrayOf("path", "to", "key") 74 | /* When */ 75 | val objectAtPathField = JsonbDSL.objectAtPath(jsonbField, *path) 76 | val objectAtPathFieldExt = jsonbField.objectAtPath(*path) 77 | /* Then */ 78 | assertEquals(objectAtPathField, objectAtPathFieldExt) 79 | } 80 | 81 | @Test 82 | fun `should provide extension function for objectAtPath with collection`() { 83 | /* Given */ 84 | val path = arrayOf("path", "to", "key") 85 | /* When */ 86 | val objectAtPathField = JsonbDSL.objectAtPath(jsonbField, path.toList()) 87 | val objectAtPathFieldExt = jsonbField.objectAtPath(path.toList()) 88 | /* Then */ 89 | assertEquals(objectAtPathField, objectAtPathFieldExt) 90 | } 91 | 92 | @Test 93 | fun `should provide extension function for objectAtPathText with varargs`() { 94 | /* Given */ 95 | val path = arrayOf("path", "to", "key") 96 | /* When */ 97 | val objectAtPathTextField = JsonbDSL.objectAtPathText(jsonbField, *path) 98 | val objectAtPathTextFieldExt = jsonbField.objectAtPathText(*path) 99 | /* Then */ 100 | assertEquals(objectAtPathTextField, objectAtPathTextFieldExt) 101 | } 102 | 103 | @Test 104 | fun `should provide extension function for objectAtPathText with collection`() { 105 | /* Given */ 106 | val path = arrayOf("path", "to", "key") 107 | /* When */ 108 | val objectAtPathTextField = JsonbDSL.objectAtPathText(jsonbField, path.toList()) 109 | val objectAtPathTextFieldExt = jsonbField.objectAtPathText(path.toList()) 110 | /* Then */ 111 | assertEquals(objectAtPathTextField, objectAtPathTextFieldExt) 112 | } 113 | 114 | @Test 115 | fun `should provide extension function for contains`() { 116 | /* Given */ 117 | val other: Field = DSL.field("other.bar", JSONB::class.java) 118 | /* When */ 119 | val containsField = JsonbDSL.contains(jsonbField, other) 120 | val containsFieldExt = jsonbField.containsJson(other) 121 | /* Then */ 122 | assertEquals(containsField, containsFieldExt) 123 | } 124 | 125 | @Test 126 | fun `should provide extension function for containedIn`() { 127 | /* Given */ 128 | val other: Field = DSL.field("other.bar", JSONB::class.java) 129 | /* When */ 130 | val containedInField = JsonbDSL.containedIn(jsonbField, other) 131 | val containedInFieldExt = jsonbField.containedIn(other) 132 | /* Then */ 133 | assertEquals(containedInField, containedInFieldExt) 134 | } 135 | 136 | @Test 137 | fun `should provide extension function for hasKey`() { 138 | /* Given */ 139 | val key = "bar" 140 | /* When */ 141 | val hasKeyField = JsonbDSL.hasKey(jsonbField, key) 142 | val hasKeyFieldExt = jsonbField.hasKey(key) 143 | /* Then */ 144 | assertEquals(hasKeyField, hasKeyFieldExt) 145 | } 146 | 147 | @Test 148 | fun `should provide extension function for hasAnyKey with varargs`() { 149 | /* Given */ 150 | val keys = arrayOf("key1", "key2") 151 | /* When */ 152 | val hasAnyKeyField = JsonbDSL.hasAnyKey(jsonbField, *keys) 153 | val hasAnyKeyFieldExt = jsonbField.hasAnyKey(*keys) 154 | /* Then */ 155 | assertEquals(hasAnyKeyField, hasAnyKeyFieldExt) 156 | } 157 | 158 | @Test 159 | fun `should provide extension function for hasAnyKey with Collection`() { 160 | /* Given */ 161 | val keys = arrayOf("key1", "key2") 162 | /* When */ 163 | val hasAnyKeyField = JsonbDSL.hasAnyKey(jsonbField, keys.toList()) 164 | val hasAnyKeyFieldExt = jsonbField.hasAnyKey(keys.toList()) 165 | /* Then */ 166 | assertEquals(hasAnyKeyField, hasAnyKeyFieldExt) 167 | } 168 | 169 | @Test 170 | fun `should provide extension function for hasAllKeys with varargs`() { 171 | /* Given */ 172 | val keys = arrayOf("key1", "key2") 173 | /* When */ 174 | val hasAllKeysField = JsonbDSL.hasAllKeys(jsonbField, *keys) 175 | val hasAllKeysFieldExt = jsonbField.hasAllKeys(*keys) 176 | /* Then */ 177 | assertEquals(hasAllKeysField, hasAllKeysFieldExt) 178 | } 179 | 180 | @Test 181 | fun `should provide extension function for hasAllKeys with Collection`() { 182 | /* Given */ 183 | val keys = arrayOf("key1", "key2") 184 | /* When */ 185 | val hasAllKeysField = JsonbDSL.hasAllKeys(jsonbField, keys.toList()) 186 | val hasAllKeysFieldExt = jsonbField.hasAllKeys(keys.toList()) 187 | /* Then */ 188 | assertEquals(hasAllKeysField, hasAllKeysFieldExt) 189 | } 190 | 191 | @Test 192 | fun `should provide extension function for concat`() { 193 | /* Given */ 194 | val other: Field = DSL.field("other.bar", JSONB::class.java) 195 | /* When */ 196 | val concatField = JsonbDSL.concat(jsonbField, other) 197 | val concatFieldExt = jsonbField.concatJson(other) 198 | /* Then */ 199 | assertEquals(concatField, concatFieldExt) 200 | } 201 | 202 | @Test 203 | fun `should provide extension function for delete with key or element`() { 204 | /* Given */ 205 | val key = "foo" 206 | /* When */ 207 | val deleteField = JsonbDSL.delete(jsonbField, key) 208 | val deleteFieldExt = jsonbField.delete(key) 209 | /* Then */ 210 | assertEquals(deleteField, deleteFieldExt) 211 | } 212 | 213 | @Test 214 | fun `should provide extension function for delete with varargs`() { 215 | /* Given */ 216 | val keys = arrayOf("key1", "key2") 217 | /* When */ 218 | val deleteField = JsonbDSL.delete(jsonbField, *keys) 219 | val deleteFieldExt = jsonbField.delete(*keys) 220 | /* Then */ 221 | assertEquals(deleteField, deleteFieldExt) 222 | } 223 | 224 | @Test 225 | fun `should provide extension function for deleteElement`() { 226 | /* Given */ 227 | val index = 1 228 | /* When */ 229 | val deleteElementField = JsonbDSL.deleteElement(jsonbField, index) 230 | val deleteElementFieldExt = jsonbField.deleteElement(index) 231 | /* Then */ 232 | assertEquals(deleteElementField, deleteElementFieldExt) 233 | } 234 | 235 | @Test 236 | fun `should provide extension function for deletePath`() { 237 | /* Given */ 238 | val keys = arrayOf("key1", "key2") 239 | /* When */ 240 | val deletePathField = JsonbDSL.deletePath(jsonbField, *keys) 241 | val deletePathFieldExt = jsonbField.deletePath(*keys) 242 | /* Then */ 243 | assertEquals(deletePathField, deletePathFieldExt) 244 | } 245 | 246 | 247 | 248 | 249 | 250 | @Test 251 | fun `should provide function for arrayLength`() { 252 | /* Given */ 253 | /* When */ 254 | val arrayLengthField = JsonbDSL.arrayLength(jsonbField) 255 | val arrayLengthFieldExt = arrayLength(jsonbField) 256 | /* Then */ 257 | assertEquals(arrayLengthField, arrayLengthFieldExt) 258 | } 259 | 260 | @Test 261 | fun `should provide function for extractPath with varargs`() { 262 | /* Given */ 263 | val path = arrayOf("path", "to", "key") 264 | /* When */ 265 | val extractPathField = JsonbDSL.extractPath(jsonbField, *path) 266 | val extractPathFieldExt = extractPath(jsonbField, *path) 267 | /* Then */ 268 | assertEquals(extractPathField, extractPathFieldExt) 269 | } 270 | 271 | @Test 272 | fun `should provide function for extractPath with collection`() { 273 | /* Given */ 274 | val path = arrayOf("path", "to", "key") 275 | /* When */ 276 | val extractPathField = JsonbDSL.extractPath(jsonbField, path.toList()) 277 | val extractPathFieldExt = extractPath(jsonbField, path.toList()) 278 | /* Then */ 279 | assertEquals(extractPathField, extractPathFieldExt) 280 | } 281 | 282 | @Test 283 | fun `should provide function for extractPathText with varargs`() { 284 | /* Given */ 285 | val path = arrayOf("path", "to", "key") 286 | /* When */ 287 | val extractPathTextField = JsonbDSL.extractPathText(jsonbField, *path) 288 | val extractPathTextFieldExt = extractPathText(jsonbField, *path) 289 | /* Then */ 290 | assertEquals(extractPathTextField, extractPathTextFieldExt) 291 | } 292 | 293 | @Test 294 | fun `should provide function for extractPathText with collection`() { 295 | /* Given */ 296 | val path = arrayOf("path", "to", "key") 297 | /* When */ 298 | val extractPathTextField = JsonbDSL.extractPathText(jsonbField, path.toList()) 299 | val extractPathTextFieldExt = extractPathText(jsonbField, path.toList()) 300 | /* Then */ 301 | assertEquals(extractPathTextField, extractPathTextFieldExt) 302 | } 303 | 304 | @Test 305 | fun `should provide function for typeOf`() { 306 | /* Given */ 307 | /* When */ 308 | val typeOfField = JsonbDSL.typeOf(jsonbField) 309 | val typeOfFieldExt = typeOf(jsonbField) 310 | /* Then */ 311 | assertEquals(typeOfField, typeOfFieldExt) 312 | } 313 | 314 | @Test 315 | fun `should provide function for stripNulls`() { 316 | /* Given */ 317 | /* When */ 318 | val stripNullsField = JsonbDSL.stripNulls(jsonbField) 319 | val stripNullsFieldExt = stripNulls(jsonbField) 320 | /* Then */ 321 | assertEquals(stripNullsField, stripNullsFieldExt) 322 | } 323 | 324 | @Test 325 | fun `should provide function for pretty`() { 326 | /* Given */ 327 | /* When */ 328 | val prettyField = JsonbDSL.pretty(jsonbField) 329 | val prettyFieldExt = pretty(jsonbField) 330 | /* Then */ 331 | assertEquals(prettyField, prettyFieldExt) 332 | } 333 | 334 | } 335 | -------------------------------------------------------------------------------- /integration-tests/src/test/resources/migration/V1__test_database.sql: -------------------------------------------------------------------------------- 1 | create table jooq.json_test 2 | ( 3 | id bigserial primary key, 4 | name text unique not null, 5 | data json null, 6 | datab jsonb null 7 | ); 8 | 9 | create table jooq.json_str_test 10 | ( 11 | id bigserial primary key, 12 | name text unique not null, 13 | data json null, 14 | datab jsonb null 15 | ); 16 | 17 | -- To see what the jOOQ Kotlin code generator does with not-nullable JSON fields 18 | create table jooq.json_not_null 19 | ( 20 | id bigserial primary key, 21 | name text unique not null, 22 | data json not null, 23 | datab jsonb not null 24 | ); 25 | -------------------------------------------------------------------------------- /jooq-postgresql-json/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | jooq-postgresql-json-parent 6 | com.github.t9t.jooq 7 | 4.0.1-SNAPSHOT 8 | 9 | 10 | jooq-postgresql-json 11 | 12 | jooq-postgresql-json 13 | jOOQ support for PostgreSQL json & jsonb 14 | 15 | 16 | 17 | org.jooq 18 | jooq 19 | provided 20 | 21 | 22 | 23 | org.jetbrains.kotlin 24 | kotlin-stdlib 25 | true 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.apache.maven.plugins 34 | maven-jar-plugin 35 | 36 | 37 | 38 | org.jetbrains.kotlin 39 | kotlin-maven-plugin 40 | 41 | 42 | 43 | org.apache.maven.plugins 44 | maven-source-plugin 45 | 46 | 47 | attach-sources 48 | 49 | jar 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.apache.maven.plugins 57 | maven-javadoc-plugin 58 | 59 | 60 | attach-javadocs 61 | 62 | jar 63 | 64 | 65 | 66 | 67 | 68 | 69 | org.apache.maven.plugins 70 | maven-deploy-plugin 71 | 72 | false 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /jooq-postgresql-json/src/main/java/com/github/t9t/jooq/json/JsonDSL.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json; 2 | 3 | import org.jooq.Field; 4 | import org.jooq.JSON; 5 | import org.jooq.impl.DSL; 6 | 7 | import java.util.Collection; 8 | 9 | /** 10 | *

Functions for {@code json} PostgreSQL operator support in jOOQ

11 | * 12 | *

Reference: https://www.postgresql.org/docs/11/functions-json.html

13 | */ 14 | public final class JsonDSL { 15 | /** 16 | * Create a jOOQ {@link Field} wrapping a {@link JSON} object representing a {@code json} value for the JSON 17 | * string. Note that the JSON is not validated (any formatting errors will only occur when 18 | * interacting with the database). 19 | * 20 | * @param json JSON string 21 | * @return {@code json} {@code Field} for the JSON string 22 | */ 23 | public static Field field(String json) { 24 | return field(JSON.valueOf(json)); 25 | } 26 | 27 | /** 28 | * Create a jOOQ {@link Field} wrapping the {@link JSON} object. 29 | * 30 | * @param json {@code JSON} object to wrap 31 | * @return {@code json} {@code Field} for the {@code JSON} object 32 | */ 33 | public static Field field(JSON json) { 34 | return DSL.field("{0}", JSON.class, json); 35 | } 36 | 37 | /** 38 | *

Get JSON array element (indexed from zero, negative integers count from the end), using the 39 | * -> operator

40 | * 41 | *

Example: '[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json->2

42 | *

Example result: {"c":"baz"}

43 | * 44 | * @param jsonField A JSON {@code Field} containing an array to get the array element from 45 | * @param index Array index; negative values count from the end 46 | * @return A {@code Field} representing the extracted array element 47 | */ 48 | public static Field arrayElement(Field jsonField, int index) { 49 | return DSL.field("{0}->{1}", JSON.class, jsonField, index); 50 | } 51 | 52 | /** 53 | *

Get JSON array element as {@code text} rather than {@code json(b)} (indexed from zero, negative integers 54 | * count from the end), using the ->> operator

55 | * 56 | *

Example: '[1,2,3]'::json->>2

57 | *

Example result: 3

58 | * 59 | * @param jsonField A JSON {@code Field} containing an array to get the array element from 60 | * @param index Array index; negative values count from the end 61 | * @return A {@code Field} representing the extracted array element, as text 62 | */ 63 | public static Field arrayElementText(Field jsonField, int index) { 64 | return DSL.field("{0}->>{1}", String.class, jsonField, index); 65 | } 66 | 67 | /** 68 | *

Get JSON object field by key using the -> operator

69 | * 70 | *

Example: '{"a": {"b":"foo"}}'::json->'a'

71 | *

Example result: {"b":"foo"}

72 | * 73 | * @param jsonField The JSON {@code Field} to extract the field from 74 | * @param key JSON field key name 75 | * @return A {@code Field} representing the extracted value 76 | */ 77 | public static Field fieldByKey(Field jsonField, String key) { 78 | return DSL.field("{0}->{1}", JSON.class, jsonField, key); 79 | } 80 | 81 | /** 82 | *

Get JSON object field as {@code text} rather than {@code json(b)}, using the ->> 83 | * operator

84 | * 85 | *

Example: '{"a":1,"b":2}'::json->>'b'

86 | *

Example result: 2

87 | * 88 | * @param jsonField The JSON {@code Field} to extract the field from 89 | * @param key JSON field key name 90 | * @return A {@code Field} representing the extracted array element, as text 91 | */ 92 | public static Field fieldByKeyText(Field jsonField, String key) { 93 | return DSL.field("{0}->>{1}", String.class, jsonField, key); 94 | } 95 | 96 | /** 97 | *

Get JSON object at specified path using the #> operator

98 | * 99 | *

Example: '{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}'

100 | *

Example result: {"c": "foo"}

101 | * 102 | * @param jsonField The JSON {@code Field} to extract the path from 103 | * @param path Path to the the object to return 104 | * @return A {@code Field} representing the object at the specified path 105 | * @see #objectAtPath(Field, Collection) 106 | */ 107 | public static Field objectAtPath(Field jsonField, String... path) { 108 | return DSL.field("{0}#>{1}", JSON.class, jsonField, DSL.array(path)); 109 | } 110 | 111 | /** 112 | *

Get JSON object at specified path using the #> operator

113 | * 114 | *

Example: '{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}'

115 | *

Example result: {"c": "foo"}

116 | * 117 | * @param jsonField The JSON {@code Field} to extract the path from 118 | * @param path Path to the the object to return 119 | * @return A {@code Field} representing the object at the specified path 120 | * @see #objectAtPath(Field, String...) 121 | */ 122 | public static Field objectAtPath(Field jsonField, Collection path) { 123 | return objectAtPath(jsonField, path.toArray(new String[0])); 124 | } 125 | 126 | /** 127 | *

Get JSON object at specified path as {@code text} rather than {@code json(b)}, using the #>> 128 | * operator

129 | * 130 | *

Example: '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'

131 | *

Example result: 3

132 | * 133 | * @param jsonField The JSON {@code Field} to extract the path from 134 | * @param path Path to the the object to return 135 | * @return A {@code Field} representing the object at the specified path, as text 136 | * @see #objectAtPathText(Field, Collection) 137 | */ 138 | public static Field objectAtPathText(Field jsonField, String... path) { 139 | return DSL.field("{0}#>>{1}", String.class, jsonField, DSL.array(path)); 140 | } 141 | 142 | /** 143 | *

Get JSON object at specified path as {@code text} rather than {@code json(b)}, using the #>> 144 | * operator

145 | * 146 | *

Example: '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'

147 | *

Example result: 3

148 | * 149 | * @param jsonField The JSON {@code Field} to extract the path from 150 | * @param path Path to the the object to return 151 | * @return A {@code Field} representing the object at the specified path, as text 152 | * @see #objectAtPath(Field, String...) 153 | */ 154 | public static Field objectAtPathText(Field jsonField, Collection path) { 155 | return objectAtPathText(jsonField, path.toArray(new String[0])); 156 | } 157 | 158 | /** 159 | *

Returns the number of elements in the outermost JSON array.

160 | * 161 | *

Example: json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]')

162 | *

Example result: 5

163 | * 164 | * @param jsonField The JSON {@code Field} containing an array to measure the length of 165 | * @return Length of the array 166 | */ 167 | public static Field arrayLength(Field jsonField) { 168 | return DSL.field("json_array_length({0})", Integer.class, jsonField); 169 | } 170 | 171 | /** 172 | *

Returns JSON value pointed to by {@code path} (equivalent to #> operator, ie. 173 | * {@link #objectAtPath(Field, String...)}).

174 | * 175 | *

Example: json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4')

176 | *

Example result: {"f5":99,"f6":"foo"}

177 | * 178 | * @param jsonField The JSON {@code Field} to extract the path from 179 | * @param path Path to the the object to return 180 | * @return A {@code Field} representing the object at the specified path 181 | * @see #objectAtPath(Field, String...) 182 | * @see #objectAtPath(Field, Collection) 183 | * @see #extractPath(Field, Collection) 184 | */ 185 | public static Field extractPath(Field jsonField, String... path) { 186 | return DSL.field("json_extract_path({0}, VARIADIC {1})", JSON.class, jsonField, DSL.array(path)); 187 | } 188 | 189 | /** 190 | *

Returns JSON value pointed to by {@code path} (equivalent to #> operator, ie. 191 | * {@link #objectAtPath(Field, Collection)}).

192 | * 193 | *

Example: json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4')

194 | *

Example result: {"f5":99,"f6":"foo"}

195 | * 196 | * @param jsonField The JSON {@code Field} to extract the path from 197 | * @param path Path to the the object to return 198 | * @return A {@code Field} representing the object at the specified path 199 | * @see #objectAtPath(Field, String...) 200 | * @see #objectAtPath(Field, Collection) 201 | * @see #extractPath(Field, String...) 202 | */ 203 | public static Field extractPath(Field jsonField, Collection path) { 204 | return extractPath(jsonField, path.toArray(new String[0])); 205 | } 206 | 207 | /** 208 | *

Returns JSON value pointed to by {@code path} as text (equivalent to #>> operator, ie. 209 | * {@link #objectAtPathText(Field, String...)}).

210 | * 211 | *

Example: json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6')

212 | *

Example result: foo

213 | * 214 | * @param jsonField The JSON {@code Field} to extract the path from 215 | * @param path Path to the the object to return 216 | * @return A {@code Field} representing the object at the specified path, as text 217 | * @see #objectAtPathText(Field, String...) 218 | * @see #objectAtPathText(Field, Collection) 219 | * @see #extractPathText(Field, Collection) 220 | */ 221 | public static Field extractPathText(Field jsonField, String... path) { 222 | return DSL.field("json_extract_path_text({0}, VARIADIC {1})", String.class, jsonField, DSL.array(path)); 223 | } 224 | 225 | /** 226 | *

Returns JSON value pointed to by {@code path} as text (equivalent to #>> operator, ie. 227 | * {@link #objectAtPathText(Field, Collection)}).

228 | * 229 | *

Example: json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6')

230 | *

Example result: foo

231 | * 232 | * @param jsonField The JSON {@code Field} to extract the path from 233 | * @param path Path to the the object to return 234 | * @return A {@code Field} representing the object at the specified path, as text 235 | * @see #objectAtPathText(Field, String...) 236 | * @see #objectAtPathText(Field, Collection) 237 | * @see #extractPathText(Field, String...) 238 | */ 239 | public static Field extractPathText(Field jsonField, Collection path) { 240 | return extractPathText(jsonField, path.toArray(new String[0])); 241 | } 242 | 243 | /** 244 | *

Returns the type of the outermost JSON value as a text string. Possible types are {@code object}, {@code array}, 245 | * {@code string}, {@code number}, {@code boolean}, and {@code null}.

246 | * 247 | *

Example: json_typeof('-123.4')

248 | *

Example result: number

249 | * 250 | * @param jsonField The JSON {@code Field} to determine the type of 251 | * @return The JSON type 252 | */ 253 | public static Field typeOf(Field jsonField) { 254 | return DSL.field("json_typeof({0})", String.class, jsonField); 255 | } 256 | 257 | /** 258 | *

Returns a JSON {@code Field} with all object fields that have {@code null} values omitted. Other {@code null} 259 | * values (eg. in arrays) are untouched.

260 | * 261 | *

Example: json_strip_nulls('[{"f1":1,"f2":null},2,null,3]')

262 | *

Example result: [{"f1":1},2,null,3]

263 | * 264 | * @param jsonField The JSON {@code Field} to remove {@code null} values from 265 | * @return A JSON {@code Field} with {@code null} object fields removed 266 | */ 267 | public static Field stripNulls(Field jsonField) { 268 | return DSL.field("json_strip_nulls({0})", JSON.class, jsonField); 269 | } 270 | } 271 | -------------------------------------------------------------------------------- /jooq-postgresql-json/src/main/java/com/github/t9t/jooq/json/JsonbDSL.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json; 2 | 3 | import org.jooq.Condition; 4 | import org.jooq.Field; 5 | import org.jooq.JSONB; 6 | import org.jooq.impl.DSL; 7 | 8 | import java.util.Collection; 9 | 10 | /** 11 | *

Functions for {@code jsonb} PostgreSQL operator support in jOOQ

12 | * 13 | *

Reference: https://www.postgresql.org/docs/11/functions-json.html

14 | */ 15 | public final class JsonbDSL { 16 | /** 17 | * Create a jOOQ {@link Field} wrapping a {@link JSONB} object representing a {@code jsonb} value for the JSON 18 | * string. Note that the JSON is not validated (any formatting errors will only occur when 19 | * interacting with the database). 20 | * 21 | * @param json JSON string 22 | * @return {@code jsonb} {@code Field} for the JSON string 23 | */ 24 | public static Field field(String json) { 25 | return field(JSONB.valueOf(json)); 26 | } 27 | 28 | /** 29 | * Create a jOOQ {@link Field} wrapping the {@link JSONB} object. 30 | * 31 | * @param jsonb {@code JSONB} object to wrap 32 | * @return {@code jsonb} {@code Field} for the {@code JSONB} object 33 | */ 34 | public static Field field(JSONB jsonb) { 35 | return DSL.field("{0}", JSONB.class, jsonb); 36 | } 37 | 38 | /** 39 | *

Get JSON array element (indexed from zero, negative integers count from the end), using the 40 | * -> operator

41 | * 42 | *

Example: '[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json->2

43 | *

Example result: {"c":"baz"}

44 | * 45 | * @param jsonField A JSON {@code Field} containing an array to get the array element from 46 | * @param index Array index; negative values count from the end 47 | * @return A {@code Field} representing the extracted array element 48 | */ 49 | public static Field arrayElement(Field jsonField, int index) { 50 | return DSL.field("{0}->{1}", JSONB.class, jsonField, index); 51 | } 52 | 53 | /** 54 | *

Get JSON array element as {@code text} rather than {@code json(b)} (indexed from zero, negative integers 55 | * count from the end), using the ->> operator

56 | * 57 | *

Example: '[1,2,3]'::json->>2

58 | *

Example result: 3

59 | * 60 | * @param jsonField A JSON {@code Field} containing an array to get the array element from 61 | * @param index Array index; negative values count from the end 62 | * @return A {@code Field} representing the extracted array element, as text 63 | */ 64 | public static Field arrayElementText(Field jsonField, int index) { 65 | return DSL.field("{0}->>{1}", String.class, jsonField, index); 66 | } 67 | 68 | /** 69 | *

Get JSON object field by key using the -> operator

70 | * 71 | *

Example: '{"a": {"b":"foo"}}'::json->'a'

72 | *

Example result: {"b":"foo"}

73 | * 74 | * @param jsonField The JSON {@code Field} to extract the field from 75 | * @param key JSON field key name 76 | * @return A {@code Field} representing the extracted value 77 | */ 78 | public static Field fieldByKey(Field jsonField, String key) { 79 | return DSL.field("{0}->{1}", JSONB.class, jsonField, key); 80 | } 81 | 82 | /** 83 | *

Get JSON object field as {@code text} rather than {@code json(b)}, using the ->> 84 | * operator

85 | * 86 | *

Example: '{"a":1,"b":2}'::json->>'b'

87 | *

Example result: 2

88 | * 89 | * @param jsonField The JSON {@code Field} to extract the field from 90 | * @param key JSON field key name 91 | * @return A {@code Field} representing the extracted array element, as text 92 | */ 93 | public static Field fieldByKeyText(Field jsonField, String key) { 94 | return DSL.field("{0}->>{1}", String.class, jsonField, key); 95 | } 96 | 97 | /** 98 | *

Get JSON object at specified path using the #> operator

99 | * 100 | *

Example: '{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}'

101 | *

Example result: {"c": "foo"}

102 | * 103 | * @param jsonField The JSON {@code Field} to extract the path from 104 | * @param path Path to the the object to return 105 | * @return A {@code Field} representing the object at the specified path 106 | * @see #objectAtPath(Field, Collection) 107 | */ 108 | public static Field objectAtPath(Field jsonField, String... path) { 109 | return DSL.field("{0}#>{1}", JSONB.class, jsonField, DSL.array(path)); 110 | } 111 | 112 | /** 113 | *

Get JSON object at specified path using the #> operator

114 | * 115 | *

Example: '{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}'

116 | *

Example result: {"c": "foo"}

117 | * 118 | * @param jsonField The JSON {@code Field} to extract the path from 119 | * @param path Path to the the object to return 120 | * @return A {@code Field} representing the object at the specified path 121 | * @see #objectAtPath(Field, String...) 122 | */ 123 | public static Field objectAtPath(Field jsonField, Collection path) { 124 | return objectAtPath(jsonField, path.toArray(new String[0])); 125 | } 126 | 127 | /** 128 | *

Get JSON object at specified path as {@code text} rather than {@code json(b)}, using the #>> 129 | * operator

130 | * 131 | *

Example: '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'

132 | *

Example result: 3

133 | * 134 | * @param jsonField The JSON {@code Field} to extract the path from 135 | * @param path Path to the the object to return 136 | * @return A {@code Field} representing the object at the specified path, as text 137 | * @see #objectAtPathText(Field, Collection) 138 | */ 139 | public static Field objectAtPathText(Field jsonField, String... path) { 140 | return DSL.field("{0}#>>{1}", String.class, jsonField, DSL.array(path)); 141 | } 142 | 143 | /** 144 | *

Get JSON object at specified path as {@code text} rather than {@code json(b)}, using the #>> 145 | * operator

146 | * 147 | *

Example: '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'

148 | *

Example result: 3

149 | * 150 | * @param jsonField The JSON {@code Field} to extract the path from 151 | * @param path Path to the the object to return 152 | * @return A {@code Field} representing the object at the specified path, as text 153 | * @see #objectAtPath(Field, String...) 154 | */ 155 | public static Field objectAtPathText(Field jsonField, Collection path) { 156 | return objectAtPathText(jsonField, path.toArray(new String[0])); 157 | } 158 | 159 | /** 160 | *

Does the {@code left} JSON value contain the {@code right} JSON path/value entries at the top level? Uses the 161 | * {@code @>} operator.

162 | * 163 | *

Example: '{"a":1, "b":2}'::jsonb @> '{"b":2}'::jsonb

164 | * 165 | * @param left The JSON {@code Field} that should contain {@code right} 166 | * @param right The JSON {@code Field} that should be contained in {@code left} 167 | * @return A {@code Condition} representing whether {@code left} is contained in {@code right} 168 | */ 169 | public static Condition contains(Field left, Field right) { 170 | return DSL.condition("{0} @> {1}", left, right); 171 | } 172 | 173 | /** 174 | *

Are the {@code left} JSON path/value entries contained at the top level within the {@code right} JSON value? 175 | * Uses the {@code <@} operator.

176 | * 177 | *

Example: '{"b":2}'::jsonb <@ '{"a":1, "b":2}'::jsonb

178 | * 179 | * @param left The JSON {@code Field} that should be contained in {@code right} 180 | * @param right The JSON {@code Field} that should contain {@code left} 181 | * @return A {@code Condition} representing whether {@code right} is contained in {@code left} 182 | */ 183 | public static Condition containedIn(Field left, Field right) { 184 | return DSL.condition("{0} <@ {1}", left, right); 185 | } 186 | 187 | /** 188 | *

Does the string exist as a top-level key within the JSON value? Uses the {@code ?} operator.

189 | * 190 | *

Example: '{"a":1, "b":2}'::jsonb ? 'b'

191 | * 192 | * @param f The JSON {@code Field} that should contain the {@code key} 193 | * @param key The key that should exist at the top level in the JSON 194 | * @return A {@code Condition} representing whether the key is contained in the JSON value 195 | */ 196 | public static Condition hasKey(Field f, String key) { 197 | return DSL.condition("{0} ?? {1}", f, key); 198 | } 199 | 200 | /** 201 | *

Do any of these array strings exist as top-level keys? Uses the {@code ?|} operator.

202 | * 203 | *

Example: '{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'c']

204 | * 205 | * @param f The JSON {@code Field} that should contain any of the {@code keys} 206 | * @param keys List of keys that may exist in the JSON value 207 | * @return A {@code Condition} representing whether any of the {@code keys} exist 208 | * @see #hasAnyKey(Field, Collection) 209 | */ 210 | public static Condition hasAnyKey(Field f, String... keys) { 211 | return DSL.condition("{0} ??| {1}", f, DSL.array(keys)); 212 | } 213 | 214 | /** 215 | *

Do any of these array strings exist as top-level keys? Uses the {@code ?|} operator.

216 | * 217 | *

Example: '{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'c']

218 | * 219 | * @param f The JSON {@code Field} that should contain any of the {@code keys} 220 | * @param keys List of keys that may exist in the JSON value 221 | * @return A {@code Condition} representing whether any of the {@code keys} exist 222 | * @see #hasAnyKey(Field, String...) 223 | */ 224 | public static Condition hasAnyKey(Field f, Collection keys) { 225 | return hasAnyKey(f, keys.toArray(new String[0])); 226 | } 227 | 228 | /** 229 | *

Do all of these array strings exist as top-level keys? Uses the {@code ?&} operator.

230 | * 231 | *

Example: '["a", "b"]'::jsonb ?& array['a', 'b']

232 | * 233 | * @param f The JSON {@code Field} that should contain all of the {@code keys} 234 | * @param keys List of keys that all should exist in the JSON value 235 | * @return A {@code Condition} representing whether all of the {@code keys} exist 236 | * @see #hasAllKeys(Field, Collection) 237 | */ 238 | public static Condition hasAllKeys(Field f, String... keys) { 239 | return DSL.condition("{0} ??& {1}", f, keys); 240 | } 241 | 242 | /** 243 | *

Do all of these array strings exist as top-level keys? Uses the {@code ?&} operator.

244 | * 245 | *

Example: '["a", "b"]'::jsonb ?& array['a', 'b']

246 | * 247 | * @param f The JSON {@code Field} that should contain all of the {@code keys} 248 | * @param keys List of keys that all should exist in the JSON value 249 | * @return A {@code Condition} representing whether all of the {@code keys} exist 250 | * @see #hasAllKeys(Field, String...) 251 | */ 252 | public static Condition hasAllKeys(Field f, Collection keys) { 253 | return hasAllKeys(f, keys.toArray(new String[0])); 254 | } 255 | 256 | /** 257 | *

Concatenate two {@code jsonb} values into a new {@code jsonb} value using the {@code ||} operator.

258 | * 259 | *

Example: '["a", "b"]'::jsonb || '["c", "d"]'::jsonb

260 | *

Example result: ["a", "b", "c", "d"]

261 | * 262 | * @param field1 Field to concatenate {@code field2} to 263 | * @param field2 Field to concatenate to {@code field1} 264 | * @return A {@code Field} representing a concatenation of the two JSON fields 265 | */ 266 | public static Field concat(Field field1, Field field2) { 267 | return DSL.field("{0} || {1}", JSONB.class, field1, field2); 268 | } 269 | 270 | /** 271 | *

Delete key/value pair or string element from left operand. Key/value pairs are matched based on their 272 | * key value. Uses the {@code -} operator.

273 | * 274 | *

Example: '{"a": "b", "c": "d"}'::jsonb - 'a'

275 | *

Example result: {"c": "d"}

276 | * 277 | * @param f JSON {@code Field} to delete the key or element from 278 | * @param keyOrElement The key name or element value to delete from the JSON field 279 | * @return A {@code Field} representing the original field with the key or element deleted 280 | */ 281 | public static Field delete(Field f, String keyOrElement) { 282 | return DSL.field("{0} - {1}", JSONB.class, f, keyOrElement); 283 | } 284 | 285 | /** 286 | *

Delete multiple key/value pairs or string elements from left operand. Key/value pairs are matched 287 | * based on their key value. Uses the {@code -} operator.

288 | * 289 | *

Example: '{"a": "b", "c": "d", "e": "f"}'::jsonb - '{a,c}'::text[]

290 | *

Example result: {"e", "f"}

291 | * 292 | * @param f JSON {@code Field} to delete the keys or elements from 293 | * @param keysOrElements The key names or element values to delete from the JSON field 294 | * @return A {@code Field} representing the original field with the keys or elements deleted 295 | */ 296 | public static Field delete(Field f, String... keysOrElements) { 297 | return DSL.field("{0} - {1}", JSONB.class, f, DSL.array(keysOrElements)); 298 | } 299 | 300 | /** 301 | *

Delete the array element with specified index (Negative integers count from the end). Throws an error if top 302 | * level container is not an array. Uses the {@code -} operator.

303 | * 304 | *

Example: '["a", "b"]'::jsonb - 1

305 | *

Example result: ["a"]

306 | * 307 | * @param f JSON {@code Field} containing an array to delete the element from 308 | * @param index Array index to delete; negative values count from the end of the array 309 | * @return A {@code Field} representing the field with the array element removed 310 | */ 311 | public static Field deleteElement(Field f, int index) { 312 | return DSL.field("{0} - {1}", JSONB.class, f, index); 313 | } 314 | 315 | /** 316 | *

Delete the field or element with specified path (for JSON arrays, negative integers count from the end). Uses 317 | * the {@code #-} operator.

318 | * 319 | *

Example: '["a", {"b":1,"c":2}]'::jsonb #- '{1,b}'

320 | *

Example result: ["a", {"c": 2}]

321 | * 322 | * @param f JSON {@code Field} to delete the selected path from 323 | * @param path Path to the JSON element to remove 324 | * @return A {@code Field} representing the field with the chosen path removed 325 | */ 326 | public static Field deletePath(Field f, String... path) { 327 | return DSL.field("{0} #- {1}", JSONB.class, f, DSL.array(path)); 328 | } 329 | 330 | 331 | /** 332 | *

Returns the number of elements in the outermost JSON array.

333 | * 334 | *

Example: jsonb_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]')

335 | *

Example result: 5

336 | * 337 | * @param jsonField The JSON {@code Field} containing an array to measure the length of 338 | * @return Length of the array 339 | */ 340 | public static Field arrayLength(Field jsonField) { 341 | return DSL.field("jsonb_array_length({0})", Integer.class, jsonField); 342 | } 343 | 344 | /** 345 | *

Returns JSON value pointed to by {@code path} (equivalent to #> operator, ie. 346 | * {@link #objectAtPath(Field, String...)}).

347 | * 348 | *

Example: jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4')

349 | *

Example result: {"f5":99,"f6":"foo"}

350 | * 351 | * @param jsonField The JSON {@code Field} to extract the path from 352 | * @param path Path to the the object to return 353 | * @return A {@code Field} representing the object at the specified path 354 | * @see #objectAtPath(Field, String...) 355 | * @see #objectAtPath(Field, Collection) 356 | * @see #extractPath(Field, Collection) 357 | */ 358 | public static Field extractPath(Field jsonField, String... path) { 359 | return DSL.field("jsonb_extract_path({0}, VARIADIC {1})", JSONB.class, jsonField, DSL.array(path)); 360 | } 361 | 362 | /** 363 | *

Returns JSON value pointed to by {@code path} (equivalent to #> operator, ie. 364 | * {@link #objectAtPath(Field, Collection)}).

365 | * 366 | *

Example: jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4')

367 | *

Example result: {"f5":99,"f6":"foo"}

368 | * 369 | * @param jsonField The JSON {@code Field} to extract the path from 370 | * @param path Path to the the object to return 371 | * @return A {@code Field} representing the object at the specified path 372 | * @see #objectAtPath(Field, String...) 373 | * @see #objectAtPath(Field, Collection) 374 | * @see #extractPath(Field, String...) 375 | */ 376 | public static Field extractPath(Field jsonField, Collection path) { 377 | return extractPath(jsonField, path.toArray(new String[0])); 378 | } 379 | 380 | /** 381 | *

Returns JSON value pointed to by {@code path} as text (equivalent to #>> operator, ie. 382 | * {@link #objectAtPathText(Field, String...)}).

383 | * 384 | *

Example: jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6')

385 | *

Example result: foo

386 | * 387 | * @param jsonField The JSON {@code Field} to extract the path from 388 | * @param path Path to the the object to return 389 | * @return A {@code Field} representing the object at the specified path, as text 390 | * @see #objectAtPathText(Field, String...) 391 | * @see #objectAtPathText(Field, Collection) 392 | * @see #extractPathText(Field, Collection) 393 | */ 394 | public static Field extractPathText(Field jsonField, String... path) { 395 | return DSL.field("jsonb_extract_path_text({0}, VARIADIC {1})", String.class, jsonField, DSL.array(path)); 396 | } 397 | 398 | /** 399 | *

Returns JSON value pointed to by {@code path} as text (equivalent to #>> operator, ie. 400 | * {@link #objectAtPathText(Field, Collection)}).

401 | * 402 | *

Example: jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6')

403 | *

Example result: foo

404 | * 405 | * @param jsonField The JSON {@code Field} to extract the path from 406 | * @param path Path to the the object to return 407 | * @return A {@code Field} representing the object at the specified path, as text 408 | * @see #objectAtPathText(Field, String...) 409 | * @see #objectAtPathText(Field, Collection) 410 | * @see #extractPathText(Field, String...) 411 | */ 412 | public static Field extractPathText(Field jsonField, Collection path) { 413 | return extractPathText(jsonField, path.toArray(new String[0])); 414 | } 415 | 416 | /** 417 | *

Returns the type of the outermost JSON value as a text string. Possible types are {@code object}, {@code array}, 418 | * {@code string}, {@code number}, {@code boolean}, and {@code null}.

419 | * 420 | *

Example: jsonb_typeof('-123.4')

421 | *

Example result: number

422 | * 423 | * @param jsonField The JSON {@code Field} to determine the type of 424 | * @return The JSON type 425 | */ 426 | public static Field typeOf(Field jsonField) { 427 | return DSL.field("jsonb_typeof({0})", String.class, jsonField); 428 | } 429 | 430 | /** 431 | *

Returns a JSON {@code Field} with all object fields that have {@code null} values omitted. Other {@code null} 432 | * values (eg. in arrays) are untouched.

433 | * 434 | *

Example: jsonb_strip_nulls('[{"f1":1,"f2":null},2,null,3]')

435 | *

Example result: [{"f1":1},2,null,3]

436 | * 437 | * @param jsonField The JSON {@code Field} to remove {@code null} values from 438 | * @return A JSON {@code Field} with {@code null} object fields removed 439 | */ 440 | public static Field stripNulls(Field jsonField) { 441 | return DSL.field("jsonb_strip_nulls({0})", JSONB.class, jsonField); 442 | } 443 | 444 | /** 445 | *

Returns the JSON {@code Field} as indented JSON text.

446 | * 447 | *

Example: jsonb_pretty('[{"f1":1,"f2":null},2,null,3]')

448 | *

Example result:

{@code
449 |      * [
450 |      *     {
451 |      *         "f1": 1,
452 |      *         "f2": null
453 |      *     },
454 |      *     2,
455 |      *     null,
456 |      *     3
457 |      * ]
458 |      * }
459 | * 460 | * @param jsonField The JSON {@code Field} to format 461 | * @return Pretty formatted, intended String representation of the JSON {@code Field} 462 | */ 463 | public static Field pretty(Field jsonField) { 464 | return DSL.field("jsonb_pretty({0})", String.class, jsonField); 465 | } 466 | } 467 | -------------------------------------------------------------------------------- /jooq-postgresql-json/src/main/java/com/github/t9t/jooq/json/StringConverter.java: -------------------------------------------------------------------------------- 1 | package com.github.t9t.jooq.json; 2 | 3 | import org.jooq.Converter; 4 | 5 | import java.util.Objects; 6 | 7 | public class StringConverter implements Converter { 8 | @Override 9 | public String from(Object t) { 10 | return Objects.toString(t, null); 11 | } 12 | 13 | @Override 14 | public String to(String u) { 15 | return u; 16 | } 17 | 18 | @Override 19 | public Class fromType() { 20 | return Object.class; 21 | } 22 | 23 | @Override 24 | public Class toType() { 25 | return String.class; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /jooq-postgresql-json/src/main/kotlin/com/github/t9t/jooq/json/json/JsonDSL.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Operators (such as `objectAtPath`) are defined as extension functions, while PostgreSQL functions (such as 3 | * `arrayLength`) are defined as "normal" functions with the goal to make it looks like "actual" SQL). 4 | */ 5 | package com.github.t9t.jooq.json.json 6 | 7 | import com.github.t9t.jooq.json.JsonDSL 8 | import org.jetbrains.annotations.NotNull 9 | import org.jooq.Field 10 | import org.jooq.JSON 11 | 12 | /** 13 | * @see JsonDSL.field 14 | */ 15 | @NotNull 16 | fun JSON.toField(): Field = JsonDSL.field(this) 17 | 18 | /** 19 | * @see JsonDSL.arrayElement 20 | */ 21 | @NotNull 22 | fun Field.arrayElement(index: Int): Field = JsonDSL.arrayElement(this, index) 23 | 24 | /** 25 | * @see JsonDSL.arrayElementText 26 | */ 27 | @NotNull 28 | fun Field.arrayElementText(index: Int): Field = JsonDSL.arrayElementText(this, index) 29 | 30 | /** 31 | * @see JsonDSL.fieldByKey 32 | */ 33 | @NotNull 34 | fun Field.fieldByKey(key: String): Field = JsonDSL.fieldByKey(this, key) 35 | 36 | /** 37 | * @see JsonDSL.fieldByKeyText 38 | */ 39 | @NotNull 40 | fun Field.fieldByKeyText(key: String): Field = JsonDSL.fieldByKeyText(this, key) 41 | 42 | /** 43 | * @see JsonDSL.objectAtPath 44 | */ 45 | @NotNull 46 | fun Field.objectAtPath(vararg path: String): Field = JsonDSL.objectAtPath(this, *path) 47 | 48 | /** 49 | * @see JsonDSL.objectAtPath 50 | */ 51 | @NotNull 52 | fun Field.objectAtPath(path: Collection): Field = JsonDSL.objectAtPath(this, path) 53 | 54 | /** 55 | * @see JsonDSL.objectAtPathText 56 | */ 57 | @NotNull 58 | fun Field.objectAtPathText(vararg path: String): Field = JsonDSL.objectAtPathText(this, *path) 59 | 60 | /** 61 | * @see JsonDSL.objectAtPathText 62 | */ 63 | @NotNull 64 | fun Field.objectAtPathText(path: Collection): Field = JsonDSL.objectAtPathText(this, path) 65 | 66 | /** 67 | * @see JsonDSL.arrayLength 68 | */ 69 | @NotNull 70 | fun arrayLength(jsonField: Field): Field = JsonDSL.arrayLength(jsonField) 71 | 72 | /** 73 | * @see JsonDSL.extractPath 74 | */ 75 | @NotNull 76 | fun extractPath(jsonField: Field, vararg path: String): Field = JsonDSL.extractPath(jsonField, *path) 77 | 78 | /** 79 | * @see JsonDSL.extractPath 80 | */ 81 | @NotNull 82 | fun extractPath(jsonField: Field, path: Collection): Field = JsonDSL.extractPath(jsonField, path) 83 | 84 | /** 85 | * @see JsonDSL.extractPathText 86 | */ 87 | @NotNull 88 | fun extractPathText(jsonField: Field, vararg path: String): Field = JsonDSL.extractPathText(jsonField, *path) 89 | 90 | /** 91 | * @see JsonDSL.extractPathText 92 | */ 93 | @NotNull 94 | fun extractPathText(jsonField: Field, path: Collection): Field = JsonDSL.extractPathText(jsonField, path) 95 | 96 | /** 97 | * @see JsonDSL.typeOf 98 | */ 99 | @NotNull 100 | fun typeOf(jsonField: Field): Field = JsonDSL.typeOf(jsonField) 101 | 102 | /** 103 | * @see JsonDSL.stripNulls 104 | */ 105 | @NotNull 106 | fun stripNulls(jsonField: Field): Field = JsonDSL.stripNulls(jsonField) 107 | -------------------------------------------------------------------------------- /jooq-postgresql-json/src/main/kotlin/com/github/t9t/jooq/json/jsonb/JsonbDSL.kt: -------------------------------------------------------------------------------- 1 | /** 2 | * Operators (such as `objectAtPath`) are defined as extension functions, while PostgreSQL functions (such as 3 | * `arrayLength`) are defined as "normal" functions with the goal to make it looks like "actual" SQL). 4 | */ 5 | package com.github.t9t.jooq.json.jsonb 6 | 7 | import com.github.t9t.jooq.json.JsonbDSL 8 | import org.jetbrains.annotations.NotNull 9 | import org.jooq.Condition 10 | import org.jooq.Field 11 | import org.jooq.JSONB 12 | 13 | /** 14 | * @see JsonbDSL.field 15 | */ 16 | @NotNull 17 | fun JSONB.toField(): Field = JsonbDSL.field(this) 18 | 19 | /** 20 | * @see JsonbDSL.arrayElement 21 | */ 22 | @NotNull 23 | fun Field.arrayElement(index: Int): Field = JsonbDSL.arrayElement(this, index) 24 | 25 | /** 26 | * @see JsonbDSL.arrayElementText 27 | */ 28 | @NotNull 29 | fun Field.arrayElementText(index: Int): Field = JsonbDSL.arrayElementText(this, index) 30 | 31 | /** 32 | * @see JsonbDSL.fieldByKey 33 | */ 34 | @NotNull 35 | fun Field.fieldByKey(key: String): Field = JsonbDSL.fieldByKey(this, key) 36 | 37 | /** 38 | * @see JsonbDSL.fieldByKeyText 39 | */ 40 | @NotNull 41 | fun Field.fieldByKeyText(key: String): Field = JsonbDSL.fieldByKeyText(this, key) 42 | 43 | /** 44 | * @see JsonbDSL.objectAtPath 45 | */ 46 | @NotNull 47 | fun Field.objectAtPath(vararg path: String): Field = JsonbDSL.objectAtPath(this, *path) 48 | 49 | /** 50 | * @see JsonbDSL.objectAtPath 51 | */ 52 | @NotNull 53 | fun Field.objectAtPath(path: Collection): Field = JsonbDSL.objectAtPath(this, path) 54 | 55 | /** 56 | * @see JsonbDSL.objectAtPathText 57 | */ 58 | @NotNull 59 | fun Field.objectAtPathText(vararg path: String): Field = JsonbDSL.objectAtPathText(this, *path) 60 | 61 | /** 62 | * @see JsonbDSL.objectAtPathText 63 | */ 64 | @NotNull 65 | fun Field.objectAtPathText(path: Collection): Field = JsonbDSL.objectAtPathText(this, path) 66 | 67 | /** 68 | * @see JsonbDSL.contains 69 | */ 70 | @NotNull 71 | fun Field.containsJson(other: Field): Condition = JsonbDSL.contains(this, other) 72 | 73 | /** 74 | * @see JsonbDSL.containedIn 75 | */ 76 | @NotNull 77 | fun Field.containedIn(other: Field): Condition = JsonbDSL.containedIn(this, other) 78 | 79 | /** 80 | * @see JsonbDSL.hasKey 81 | */ 82 | @NotNull 83 | fun Field.hasKey(key: String): Condition = JsonbDSL.hasKey(this, key) 84 | 85 | /** 86 | * @see JsonbDSL.hasAnyKey 87 | */ 88 | @NotNull 89 | fun Field.hasAnyKey(vararg keys: String): Condition = JsonbDSL.hasAnyKey(this, *keys) 90 | 91 | /** 92 | * @see JsonbDSL.hasAnyKey 93 | */ 94 | @NotNull 95 | fun Field.hasAnyKey(keys: Collection): Condition = JsonbDSL.hasAnyKey(this, keys) 96 | 97 | /** 98 | * @see JsonbDSL.hasAllKeys 99 | */ 100 | @NotNull 101 | fun Field.hasAllKeys(vararg keys: String): Condition = JsonbDSL.hasAllKeys(this, *keys) 102 | 103 | /** 104 | * @see JsonbDSL.hasAllKeys 105 | */ 106 | @NotNull 107 | fun Field.hasAllKeys(keys: Collection): Condition = JsonbDSL.hasAllKeys(this, keys) 108 | 109 | /** 110 | * @see JsonbDSL.concat 111 | */ 112 | @NotNull 113 | fun Field.concatJson(otherField: Field): Field = JsonbDSL.concat(this, otherField) 114 | 115 | /** 116 | * @see JsonbDSL.delete 117 | */ 118 | @NotNull 119 | fun Field.delete(keyOrElement: String): Field = JsonbDSL.delete(this, keyOrElement) 120 | 121 | /** 122 | * @see JsonbDSL.delete 123 | */ 124 | @NotNull 125 | fun Field.delete(vararg keysOrElements: String): Field = JsonbDSL.delete(this, *keysOrElements) 126 | 127 | /** 128 | * @see JsonbDSL.deleteElement 129 | */ 130 | @NotNull 131 | fun Field.deleteElement(index: Int): Field = JsonbDSL.deleteElement(this, index) 132 | 133 | /** 134 | * @see JsonbDSL.deletePath 135 | */ 136 | @NotNull 137 | fun Field.deletePath(vararg path: String): Field = JsonbDSL.deletePath(this, *path) 138 | 139 | /** 140 | * @see JsonbDSL.arrayLength 141 | */ 142 | @NotNull 143 | fun arrayLength(jsonField: Field): Field = JsonbDSL.arrayLength(jsonField) 144 | 145 | /** 146 | * @see JsonbDSL.extractPath 147 | */ 148 | @NotNull 149 | fun extractPath(jsonField: Field, vararg path: String): Field = JsonbDSL.extractPath(jsonField, *path) 150 | 151 | /** 152 | * @see JsonbDSL.extractPath 153 | */ 154 | @NotNull 155 | fun extractPath(jsonField: Field, path: Collection): Field = JsonbDSL.extractPath(jsonField, *path.toTypedArray()) 156 | 157 | /** 158 | * @see JsonbDSL.extractPathText 159 | */ 160 | @NotNull 161 | fun extractPathText(jsonField: Field, vararg path: String): Field = JsonbDSL.extractPathText(jsonField, *path) 162 | 163 | /** 164 | * @see JsonbDSL.extractPathText 165 | */ 166 | @NotNull 167 | fun extractPathText(jsonField: Field, path: Collection): Field = JsonbDSL.extractPathText(jsonField, *path.toTypedArray()) 168 | 169 | /** 170 | * @see JsonbDSL.typeOf 171 | */ 172 | @NotNull 173 | fun typeOf(jsonField: Field): Field = JsonbDSL.typeOf(jsonField) 174 | 175 | /** 176 | * @see JsonbDSL.stripNulls 177 | */ 178 | @NotNull 179 | fun stripNulls(jsonField: Field): Field = JsonbDSL.stripNulls(jsonField) 180 | 181 | /** 182 | * @see JsonbDSL.pretty 183 | */ 184 | @NotNull 185 | fun pretty(jsonField: Field): Field = JsonbDSL.pretty(jsonField) 186 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.github.t9t.jooq 6 | jooq-postgresql-json-parent 7 | pom 8 | 4.0.1-SNAPSHOT 9 | 10 | jooq-postgresql-json-parent 11 | jOOQ support for PostgreSQL json & jsonb - parent pom 12 | https://github.com/t9t/jooq-postgresql-json 13 | 14 | 15 | jooq-postgresql-json 16 | integration-tests 17 | 18 | 19 | 20 | UTF-8 21 | 22 | 3.18.5 23 | 42.6.0 24 | 9.20.0 25 | 1.6.21 26 | 5.9.3 27 | 28 | 29 | 30 | 31 | 32 | com.github.t9t.jooq 33 | jooq-postgresql-json 34 | 4.0.1-SNAPSHOT 35 | 36 | 37 | 38 | org.jooq 39 | jooq 40 | ${jooq.version} 41 | 42 | 43 | 44 | org.postgresql 45 | postgresql 46 | ${postgresql.driver.version} 47 | test 48 | 49 | 50 | 51 | org.jetbrains.kotlin 52 | kotlin-stdlib 53 | ${kotlin.version} 54 | 55 | 56 | 57 | org.junit.jupiter 58 | junit-jupiter-engine 59 | ${junit5.version} 60 | test 61 | 62 | 63 | org.junit.jupiter 64 | junit-jupiter-params 65 | ${junit5.version} 66 | test 67 | 68 | 69 | 70 | com.fasterxml.jackson.core 71 | jackson-databind 72 | 2.15.2 73 | test 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | org.apache.maven.plugins 83 | maven-jar-plugin 84 | 3.3.0 85 | 86 | 87 | org.apache.maven.plugins 88 | maven-source-plugin 89 | 3.3.0 90 | 91 | 92 | org.apache.maven.plugins 93 | maven-javadoc-plugin 94 | 3.5.0 95 | 96 | 97 | org.apache.maven.plugins 98 | maven-failsafe-plugin 99 | 3.1.2 100 | 101 | 102 | io.fabric8 103 | docker-maven-plugin 104 | 0.43.0 105 | 106 | 107 | org.flywaydb 108 | flyway-maven-plugin 109 | ${flyway.version} 110 | 111 | 112 | org.jooq 113 | jooq-codegen-maven 114 | ${jooq.version} 115 | 116 | 117 | org.codehaus.mojo 118 | build-helper-maven-plugin 119 | 3.4.0 120 | 121 | 122 | 123 | org.jetbrains.kotlin 124 | kotlin-maven-plugin 125 | ${kotlin.version} 126 | 127 | 11 128 | 129 | 130 | 131 | compile 132 | 133 | compile 134 | 135 | 136 | 137 | ${project.basedir}/src/main/kotlin 138 | ${project.basedir}/src/main/java 139 | 140 | 141 | 142 | 143 | test-compile 144 | 145 | test-compile 146 | 147 | 148 | 149 | ${project.basedir}/src/test/kotlin 150 | ${project.basedir}/src/test/java 151 | 152 | ${project.basedir}/target/generated-sources/jooq 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | org.apache.maven.plugins 164 | maven-compiler-plugin 165 | 3.11.0 166 | 167 | 11 168 | 11 169 | 11 170 | 11 171 | 172 | 173 | 174 | 175 | org.apache.maven.plugins 176 | maven-release-plugin 177 | 3.0.1 178 | 179 | true 180 | v@{project.version} 181 | false 182 | release 183 | false 184 | true 185 | deploy 186 | 187 | 188 | 189 | 190 | org.apache.maven.plugins 191 | maven-deploy-plugin 192 | 3.1.1 193 | 194 | false 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | release 203 | 204 | 205 | 206 | org.apache.maven.plugins 207 | maven-gpg-plugin 208 | 3.1.0 209 | 210 | 211 | sign-artifacts 212 | install 213 | 214 | sign 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | ossrh 227 | https://oss.sonatype.org/content/repositories/snapshots 228 | 229 | 230 | ossrh 231 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 232 | 233 | 234 | 235 | 236 | 237 | The Apache Software License, Version 2.0 238 | https://www.apache.org/licenses/LICENSE-2.0.txt 239 | 240 | 241 | 242 | 243 | 244 | t9t 245 | t9t9t@protonmail.com 246 | 247 | 248 | 249 | 250 | scm:git:https://github.com/t9t/jooq-postgresql-json.git 251 | scm:git:git@github.com:t9t/jooq-postgresql-json.git 252 | https://github.com/t9t/jooq-postgresql-json 253 | HEAD 254 | 255 | 256 | 257 | GitHub 258 | https://github.com/t9t/jooq-postgresql-json/issues 259 | 260 | 261 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # ⚠ Unmaintained ⚠ 2 | Unfortunately, I no longer use jOOQ or `jooq-postgresql-json` myself, and so I don't feel comfortable maintaining this 3 | library anymore. I will no longer test the library when new jOOQ versions come out, and I won't closely follow jOOQ 4 | news anymore to keep an eye out for new features to integrate with. 5 | 6 | See also: https://github.com/t9t/jooq-postgresql-json/issues/24. 7 | 8 | # jOOQ PostgreSQL JSON support 9 | Provides jOOQ support for [PostgreSQL JSON functions and operators](https://www.postgresql.org/docs/11/functions-json.html) 10 | for `json` and `jsonb` fields. 11 | 12 | **Requires at least Java 11.** 13 | 14 | ⚠ Since version 4.0.0 this library does not include a transitive Maven dependency on `jooq` any more, you have to 15 | include both `jooq` and `jooq-postgresql-json` in your project to use it. 16 | 17 | - [Include as a Maven dependency](#include-as-a-maven-dependency) 18 | - [Usage](#usage) 19 | - [Kotlin extension functions](#kotlin) 20 | - [PostgreSQL json operator support](#postgresql-json-operator-support) 21 | - [Available PostgreSQL json processing functions](#available-postgresql-json-processing-functions) 22 | - [References](#references) 23 | - [Contributors](#contributors) 24 | - [![Javadocs](https://javadoc.io/badge/com.github.t9t.jooq/jooq-postgresql-json.svg)](https://javadoc.io/doc/com.github.t9t.jooq/jooq-postgresql-json) 25 | 26 | --- 27 | 28 | 29 | ## Include as a Maven dependency 30 | First, add the following Maven dependency: 31 | 32 | ```xml 33 | 34 | com.github.t9t.jooq 35 | jooq-postgresql-json 36 | 4.0.0 37 | 38 | ``` 39 | 40 | `jooq-postgresql-json` does not include a transitive dependency on `jooq`, so you have to include that yourself as well. 41 | 42 | ### Version matrix 43 | 44 | As for 4.0.0, this shows only which jOOQ versions are explicitly tested with this library. Minor version differences 45 | should still be compatible and newer major versions of jOOQ might still work with older versions of this library if 46 | nothing changed much in the jOOQ JSON APIs. New releases will only be created when incompatibilities with new jOOQ 47 | versions are found and fixed. 48 | 49 | See [the changelog](changelog.md) for more information about what is included in the various releases and the reason for 50 | the breaking changes. 51 | 52 | | Library version | jOOQ version | Note | 53 | | --- | --- | --- | 54 | | 4.0.0 | 3.16.x - 3.18.x | Breaking change, no longer includes a dependency on `jooq`. Tested with both jOOQ `3.16.20` (JDK 11 and 17), and `3.17.14` and `3.18.5` (JDK 17 only). | 55 | | 3.2.3 | 3.16.7 | 56 | | 3.2.2 | 3.16.6 | 57 | | 3.2.1 | 3.16.5 | 58 | | 3.2.0 | 3.16.3 | 59 | | 3.1.2 | 3.15.5 | 60 | | 3.1.1 | 3.15.4 | 61 | | 3.1.0 | 3.15.1 | 62 | | 3.0.0 | 3.14.12 | Breaking change, upgraded from Java 8 to Java 11. Java 8 no longer supported. | 63 | 64 | 65 | ## Usage 66 | 67 | Use 68 | the [`JsonDSL`](https://javadoc.io/static/com.github.t9t.jooq/jooq-postgresql-json/4.0.0/com/github/t9t/jooq/json/JsonDSL.html) 69 | and [`JsonbDSL`](https://javadoc.io/static/com.github.t9t.jooq/jooq-postgresql-json/4.0.0/com/github/t9t/jooq/json/JsonbDSL.html) 70 | classes to access the JSON functions and operators. 71 | 72 | For example, to extract a JSON nested property value as text from a `json` field: 73 | 74 | ```java 75 | /* Sample JSON: 76 | { 77 | "data": { 78 | "productCode": "Z-5521" 79 | } 80 | } 81 | */ 82 | String productCode = dsl.select(JsonDSL.extractPathText(MY_TABLE.DATA_FIELD, "data", "productCode")) 83 | .from(MY_TABLE).fetchOneInto(String.class); 84 | ``` 85 | 86 | Or for example using the `@>` operator to update a row of which a `jsonb` field contains a certain id: 87 | 88 | ```java 89 | /* Sample JSON: 90 | { 91 | "id": "1337", 92 | "name": "The Hitchhiker's Guide to the Galaxy" 93 | } 94 | */ 95 | dsl.update(MY_TABLE) 96 | .set(MY_TABLE.RATING, 100) 97 | .where(JsonbDSL.contains(MY_TABLE.DATA_FIELD, JsonbDSL.field("{\"id\": \"1337\"}"))) 98 | .execute() 99 | ``` 100 | 101 | - [`JsonDSL` Javadoc](https://javadoc.io/static/com.github.t9t.jooq/jooq-postgresql-json/4.0.0/com/github/t9t/jooq/json/JsonDSL.html) 102 | - [`JsonbDSL` Javadoc](https://javadoc.io/static/com.github.t9t.jooq/jooq-postgresql-json/4.0.0/com/github/t9t/jooq/json/JsonbDSL.html) 103 | 104 | ### Kotlin 105 | 106 | Kotlin extension functions are available for `Field` and `Field`. That means that instead of something 107 | like `JsonDSL.extractPathText(MY_TABLE.DATA_FIELD, "data", "productCode")` you can instead write: 108 | `MY_TABLE.DATA_FIELD.extractPathText("data", "productCode")`. 109 | 110 | The extension functions are available in the following packages: 111 | 112 | - [`com.github.t9t.jooq.json.json`](https://javadoc.io/static/com.github.t9t.jooq/jooq-postgresql-json/4.0.0/com/github/t9t/jooq/json/json) 113 | - [`com.github.t9t.jooq.json.jsonb`](https://javadoc.io/static/com.github.t9t.jooq/jooq-postgresql-json/4.0.0/com/github/t9t/jooq/json/jsonb) 114 | 115 | The names of extension functions match the names of the methods on `JsonDSL` and `JsonbDSL`, except for `concat` and 116 | `contains`, which are called `concatJson` and `containsJson` respectively to prevent clashes with existing methods 117 | of `Field`. 118 | 119 | ## PostgreSQL json operator support 120 | Reference: https://www.postgresql.org/docs/11/functions-json.html 121 | 122 | Operators available for both `json` (through `JsonDSL`) and `jsonb` (through `JsonbDSL`): 123 | 124 | | Op | Operand | Description | Method | 125 | | --- | --- | --- | --- | 126 | | `->` | `int` | Get array element | `arrayElement()` | 127 | | `->` | `text` | Get object field | `fieldByKey()` | 128 | | `->>` | `int` | Get array element as text | `arrayElementText()` | 129 | | `->>` | `text` | Get object field as text | `fieldByKeyText()` | 130 | | `#>` | `text[]` | Get object at path | `objectAtPath()` | 131 | | `#>>` | `text[]` | Get object at path as text | `objectAtPathText()` | 132 | 133 | Operators available only for `jsonb` (through `JsonbDSL`): 134 | 135 | 136 | | Op | Operand | Description | Method | 137 | | --- | --- | --- | --- | 138 | | `@>` | `jsonb` | Does contain value? | `contains()` | 139 | | `<@` | `jsonb` | Are entries contained? | `containedIn()` | 140 | | `?` | `text` | Does the key exist? | `hasKey()` | 141 | | ?| | `text[]` | Does any key exist? | `hasAnyKey()` | 142 | | `?&` | `text[]` | Do all keys exist? | `hasAllKeys()` | 143 | | || | `jsonb` | Concatenate values | `concat()` | 144 | | `-` | `text` | Delete key or element | `delete()` | 145 | | `-` | `text[]` | Delete multiple keys or elements | `delete()` | 146 | | `-` | `int` | Delete array element | `deleteElement()` | 147 | | `#-` | `text[]` | Delete field for path | `deletePath()` | 148 | 149 | 150 | ## Available PostgreSQL json processing functions 151 | Reference: https://www.postgresql.org/docs/11/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE 152 | 153 | Processing functions available for both `json` (through `JsonDSL`) and `jsonb` (through `JsonbDSL`): 154 | 155 | | Function | Return type | Description | Method | 156 | | --- | --- | --- | --- | 157 | | `json(b)_array_length` | `int` | Get length of JSON array | `arrayLength()` | 158 | | `json(b)_extract_path` | `json`/`jsonb` | Extract object at path (same as `#>`) | `extractPath()` | 159 | | `json(b)_extract_path_text` | `text` | Extract object at path as text (same as `#>>`) | `extractPathText()` | 160 | | `json(b)_typeof` | `text` | Get the type of a JSON field | `typeOf()` | 161 | | `json(b)_strip_nulls` | `json`/`jsonb` | Remove object fields with `null` values | `stripNulls()` | 162 | 163 | Functions only available for `jsonb` (through `JsonbDSL`): 164 | 165 | | Function | Return type | Description | Method | 166 | | --- | --- | --- | --- | 167 | | `jsonb_pretty` | `text` | Pretty format JSON field | `pretty()` | 168 | 169 | 170 | ## References 171 | - [jOOQ.org](https://www.jooq.org/) 172 | - [PostgreSQL JSON data types](https://www.postgresql.org/docs/current/datatype-json.html) 173 | 174 | 175 | ## Contributors 176 | - [@davinkevin](https://github.com/davinkevin) was kind enough to implement Kotlin extension functions ([#11](https://github.com/t9t/jooq-postgresql-json/issues/11) and [#12](https://github.com/t9t/jooq-postgresql-json/pull/12))! 177 | -------------------------------------------------------------------------------- /release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -eu 4 | 5 | if [[ $# -lt 1 ]]; then 6 | echo 'Usage: ./release.sh ' 7 | exit 1 8 | fi 9 | 10 | JAVAC_VERSION=$(javac -version |& cut -d ' ' -f 2) 11 | echo "javac version: ${JAVAC_VERSION}" 12 | 13 | if [[ ${JAVAC_VERSION} != 11* ]]; then 14 | echo 'Please use JDK 11' 15 | exit 1 16 | fi 17 | 18 | echo '=== Clean verify ===' 19 | mvn clean verify 20 | 21 | echo '=== Release prepare ===' 22 | mvn -Prelease release:clean release:prepare -DpushChanges=false -Dresume=false -DreleaseVersion=${1} 23 | 24 | echo '=== Release perform ===' 25 | mvn -Prelease release:perform 26 | 27 | echo '=== Deploy ===' 28 | mvn -Prelease deploy 29 | -------------------------------------------------------------------------------- /startdb.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | mvn -pl integration-tests docker:stop 4 | mvn -pl integration-tests docker:start flyway:migrate jooq-codegen:generate 5 | --------------------------------------------------------------------------------