|
| 1 | +# Overview |
| 2 | +SQL4Json is allows you to select, filter, aggregate your json data with using nearly same syntax of SQL Select query. Here it is fully fledged query example. |
| 3 | +```sql |
| 4 | +SELECT user.name AS userName, |
| 5 | +SUM(value) AS total, |
| 6 | +COUNT(value) AS cnt, |
| 7 | +COUNT(*) AS count_including_nulls, |
| 8 | +MAX(value) AS max_val, |
| 9 | +MIN(value) AS min_val, |
| 10 | +AVG(value) AS avg_val |
| 11 | + |
| 12 | +FROM $r.nested.object |
| 13 | + |
| 14 | +WHERE ((a > 5 AND x.z < 10) OR c = 20) |
| 15 | + AND age IS NOT NULL |
| 16 | + AND isActive = true |
| 17 | + AND isDeleted IS NULL |
| 18 | + AND LOWER(name, 'en-US') LIKE '%cahit%' |
| 19 | + AND (some.number >= 20 OR COALESCE(some.other.number, 0) <= 10) |
| 20 | + AND TO_DATE(lasLogin, 'yyyy-MM-dd HH:mm:ss') < NOW() |
| 21 | + |
| 22 | +GROUP BY user.name |
| 23 | + |
| 24 | +HAVING total > 5 OR avg_val < 3 |
| 25 | + |
| 26 | +ORDER BY total DESC, userName |
| 27 | +``` |
| 28 | + |
| 29 | +# Get it! |
| 30 | + |
| 31 | +## Maven |
| 32 | +To use the package, you need to add following dependency: |
| 33 | +```xml |
| 34 | +<dependency> |
| 35 | + <groupId>io.github.mnesimiyilmaz</groupId> |
| 36 | + <artifactId>sql4json</artifactId> |
| 37 | + <version>0.0.1</version> |
| 38 | +</dependency> |
| 39 | +``` |
| 40 | + |
| 41 | +## Non-Maven |
| 42 | +For non-Maven use cases, you download jars from [packages](https://github.com/mnesimiyilmaz/sql4json/packages). |
| 43 | + |
| 44 | + |
| 45 | +# Use it! |
| 46 | + |
| 47 | +Usage starts with creation of `SQL4JsonInput` instance. You can create an instance using the following methods: |
| 48 | + |
| 49 | +Serializable Object |
| 50 | +```java |
| 51 | +SQL4JsonInput input = SQL4JsonInput.fromObject("SELECT * FROM $r", yourObject); |
| 52 | +``` |
| 53 | +or Json String |
| 54 | +```java |
| 55 | +SQL4JsonInput input = SQL4JsonInput.fromJsonString("SELECT * FROM $r", "jsonString"); |
| 56 | +``` |
| 57 | +or `JsonNode` Supplier |
| 58 | +```java |
| 59 | +SQL4JsonInput input = SQL4JsonInput.fromJsonNodeSupplier("SELECT * FROM $r", () -> new ObjectNode(null)); |
| 60 | +``` |
| 61 | +or `JsonNode` |
| 62 | +```java |
| 63 | +SQL4JsonInput input = new SQL4JsonInput("SELECT * FROM $r", new ObjectNode(null)); |
| 64 | +``` |
| 65 | + |
| 66 | +Than create an instance of `SQL4JsonProcessor` and pass `SQL4JsonInput` as constructor parameter and lastly call `getResult()` method. |
| 67 | +```java |
| 68 | +SQL4JsonProcessor processor = new SQL4JsonProcessor(input); |
| 69 | +JsonNode result = processor.getResult(); |
| 70 | +``` |
| 71 | + |
| 72 | +Voila! Now you can convert `result` to any type you want via using `ObjectMapper`. |
| 73 | + |
| 74 | +# Syntax & Supported Operations |
| 75 | + |
| 76 | +## Selecting Fields |
| 77 | +Like in SQL if you want to select all fields, you can simply put an asterisk. |
| 78 | +```sql |
| 79 | +select * from $r |
| 80 | +``` |
| 81 | +If you want to select specific field, object or array you just need to write path of field. Moreover you can give alias for fields. |
| 82 | +```sql |
| 83 | +SELECT fullname, |
| 84 | + id AS userId, |
| 85 | + username AS user.username |
| 86 | + some.field.in.some.object AS user.someData |
| 87 | + object.field AS renamedObject |
| 88 | + array.field AS renamed.data.arrayField |
| 89 | +FROM $r |
| 90 | +``` |
| 91 | +The example above will be give output like below |
| 92 | +```json |
| 93 | +[ |
| 94 | + { |
| 95 | + fullname: "Mucahit Nesimi YILMAZ", |
| 96 | + userId: 1, |
| 97 | + user: { |
| 98 | + username: "mucahit" |
| 99 | + someData: 123 |
| 100 | + }, |
| 101 | + renamedObject: { |
| 102 | + ...object fields here... |
| 103 | + }, |
| 104 | + renamed: { |
| 105 | + data: { |
| 106 | + arrayField: [1, 2, 3] |
| 107 | + } |
| 108 | + } |
| 109 | + }, |
| 110 | + ...other objects... |
| 111 | +] |
| 112 | +``` |
| 113 | + |
| 114 | +## FROM $r |
| 115 | +`$r` is reserved keyword for SQL4Json. You must add `$r` to from section of your query. You can expand `$r` for scenarios for like below. |
| 116 | + |
| 117 | +Let's say you have json like this |
| 118 | +```json |
| 119 | +{ |
| 120 | + responseStatus: 200, |
| 121 | + errors: null, |
| 122 | + response: { |
| 123 | + data: [...list of objects...] |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | +You can select `data` directly by typing the path of `data` after the `$r` expression. |
| 128 | +```sql |
| 129 | +SELECT * FROM $r.response.data |
| 130 | +``` |
| 131 | + |
| 132 | +## WHERE Clause |
| 133 | +You can filter data using complex conditions. You can chain your conditions using `AND` and `OR`, and you can use comparison operators, `IS NULL`, `IS NOT NULL` and `LIKE`. |
| 134 | +```sql |
| 135 | +SELECT * FROM $r |
| 136 | +WHERE ((a > 5 AND x.z < 10) OR c = 20) |
| 137 | +AND age IS NOT NULL |
| 138 | +AND isActive = true |
| 139 | +AND isDeleted IS NULL |
| 140 | +AND name LIKE '%cahit%' |
| 141 | +AND (some.number >= 20 OR some.other.number <= 10) |
| 142 | +``` |
| 143 | + |
| 144 | +## GROUP BY and HAVING |
| 145 | +You can aggregate your data using aggregation functions such as `COUNT`, `MIN`, `MAX`, `SUM` and `AVG`. |
| 146 | +```sql |
| 147 | +SELECT |
| 148 | +field1, field2, |
| 149 | +SUM(value) AS total, |
| 150 | +COUNT(value) AS cnt, |
| 151 | +COUNT(*) AS count_including_nulls, |
| 152 | +MAX(value) AS max_val, |
| 153 | +MIN(value) AS min_val, |
| 154 | +AVG(value) AS avg_val |
| 155 | +FROM $r |
| 156 | +GROUP BY field1, field2 |
| 157 | +HAVING avg_val > 10 AND max_val < 100 |
| 158 | +``` |
| 159 | +**Note 1:** All aggregation functions other than `COUNT(*)`, do calculation over non null data. |
| 160 | + |
| 161 | +**Note 2:** Field names in `HAVING`, **must** be aliases that you determined in select. |
| 162 | + |
| 163 | +## ORDER BY |
| 164 | +You can sort data by specific fields. |
| 165 | +```sql |
| 166 | +SELECT * FROM $r |
| 167 | +ORDER BY someObject.field, someField DESC, otherField ASC |
| 168 | +``` |
| 169 | +If you want to sort grouped data you should use aliases in `ORDER BY`. |
| 170 | +```sql |
| 171 | +SELECT field1, |
| 172 | +SUM(value) AS total |
| 173 | +FROM $r |
| 174 | +GROUP BY field1, |
| 175 | +HAVING total > 10 |
| 176 | +ORDER BY total DESC, field1 ASC |
| 177 | +``` |
| 178 | +## Nested Queries |
| 179 | +In order to process your data step by step you can write nested queries. Execution order of nested queries is from **bottom** to **top**. You can express nested query with using `>>>` operator. |
| 180 | +```sql |
| 181 | +SELECT name, COUNT(*) as userCount FROM $r GROUP BY name ORDER BY userCount DESC |
| 182 | + >>> |
| 183 | +SELECT name, age FROM $r WHERE id > 0 |
| 184 | + >>> |
| 185 | +SELECT objectField FROM $r.data WHERE groupName = 'users' |
| 186 | +``` |
| 187 | + |
| 188 | +## Functions |
| 189 | +### Lower & Upper |
| 190 | +You can use `LOWER` and `UPPER` functions for `string` fields. These functions takes optional lang code parameter. JVM default will be used if you don't pass lang code. |
| 191 | +```sql |
| 192 | +SELECT LOWER(name) AS name |
| 193 | +FROM $r |
| 194 | +WHERE UPPER(username, 'en-US') = 'MUCAHIT' |
| 195 | +ORDER BY LOWER(name, 'tr-TR') |
| 196 | +``` |
| 197 | + |
| 198 | +### Coalesce |
| 199 | +Returns second parameter value if first one is null. |
| 200 | +```sql |
| 201 | +SELECT * FROM $r WHERE COALESCE(num, 0) > -1 |
| 202 | +``` |
| 203 | + |
| 204 | +### TO_DATE |
| 205 | +Convert date strings to `LocalDate` or `LocalDateTime` instance to in order to compare or sort. This function takes optional pattern parameter. |
| 206 | +```sql |
| 207 | +SELECT * FROM $r |
| 208 | +WHERE TO_DATE(lasLogin, 'yyyy-MM-dd HH:mm:ss') > TO_DATE('2023-25-10 03:00:00') |
| 209 | +``` |
| 210 | + |
| 211 | +### NOW |
| 212 | +Returns `LocalDateTime` instance of current date time. |
| 213 | +```sql |
| 214 | +SELECT * FROM $r WHERE TO_DATE(lasLogin, 'yyyy-MM-dd HH:mm:ss') < NOW() |
| 215 | +``` |
0 commit comments