Skip to content

Commit 8df0ca6

Browse files
committed
Update Using DDL/SQL to 0.7.0 API
1 parent c5fac0b commit 8df0ca6

File tree

2 files changed

+31
-65
lines changed

2 files changed

+31
-65
lines changed

articles/ecosystem/java_jdbc/using_ddl.md

+3-27
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ DDL operations can be executed using the `db-do-commands` function. The general
99
approach is:
1010

1111
```clojure
12-
(j/db-do-commands db-spec sql-command-1 sql-command-2 .. sql-command-n)
12+
(j/db-do-commands db-spec [sql-command-1 sql-command-2 .. sql-command-n])
1313
```
1414

1515
The commands are executed as a single, batched statement, wrapped in a
1616
transaction. If you want to avoid the transaction, use this approach:
1717

1818
```clojure
19-
(j/db-do-commands db-spec false sql-command-1 sql-command-2 .. sql-command-n)
19+
(j/db-do-commands db-spec false [sql-command-1 sql-command-2 .. sql-command-n])
2020
```
2121

2222
### Creating tables
@@ -35,22 +35,6 @@ several of the SQL functions.
3535
{:table-spec "ENGINE=InnoDB"
3636
:entities clojure.string/upper-case})
3737
```
38-
39-
Note that the vector of columns syntax is new in version 0.5.6. Earlier
40-
versions just accepted each column spec as a separate argument followed by the
41-
keyword arguments unrolled, but that was deprecated in 0.5.5 and will be
42-
removed in 0.6.0:
43-
44-
```clojure
45-
(j/create-table-ddl :fruit
46-
[:name "varchar(32)" :primary :key]
47-
[:appearance "varchar(32)"]
48-
[:cost :int]
49-
[:grade :real]
50-
:table-spec "ENGINE=InnoDB"
51-
:entities clojure.string/upper-case)
52-
```
53-
5438
This will generate:
5539

5640
```clojure
@@ -73,13 +57,6 @@ optional `:entities` option to generate DDL to drop a table.
7357
(j/drop-table-ddl :fruit {:entities clojure.string/upper-case}) ; drop table FRUIT
7458
```
7559

76-
Prior to version 0.5.0, the following syntax was accepted (but it is now
77-
deprecated and will be removed in 0.6.0):
78-
79-
```clojure
80-
(j/drop-table-ddl :fruit :entities clojure.string/upper-case)
81-
```
82-
8360
This will generate:
8461

8562
```clojure
@@ -94,8 +71,7 @@ java.jdbc provides two functions for working with database metadata:
9471
connection
9572
* `metadata-result` for turning metadata results into Clojure data structures
9673

97-
For example (for versions prior to 0.3.3 you need to wrap the `metadata-result`
98-
with `doall`):
74+
For example:
9975

10076
```clojure
10177
(j/with-db-metadata [md db-spec]

articles/ecosystem/java_jdbc/using_sql.md

+28-38
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ of data with values in the same order as the columns.
4646
;; ...)
4747
```
4848

49-
Note: prior to version 0.5.5 options could be specified as top-level (unrolled)
50-
keyword/value arguments but that does not compose well and was deprecated in
51-
0.5.5 (and support will be removed in 0.6.0).
52-
5349
### Processing a result set lazily
5450

5551
Since `query` returns a fully realized result set, it can be difficult to
@@ -163,44 +159,44 @@ a map of the generated keys will be returned for each insert (as a sequence).
163159
In the latter case, a single, batched insert will be performed and a sequence
164160
of row insert counts will be returned (generally a sequence of ones).
165161

166-
If you use `insert!` and specify each row as a map of columns and their values,
162+
If you use `insert-multi!` and specify each row as a map of columns and their values,
167163
then you can specify a mixture of complete and partial rows, and you will get
168164
back the generated keys for each row (assuming the database has that
169165
capability).
170166

171167
```clojure
172-
(j/insert! db-spec :fruit
173-
{:name "Pomegranate" :appearance "fresh" :cost 585}
174-
{:name "Kiwifruit" :grade 93})
168+
(j/insert-multi! db-spec :fruit
169+
[{:name "Pomegranate" :appearance "fresh" :cost 585}
170+
{:name "Kiwifruit" :grade 93}])
175171
;; returns a sequence of database-specific maps, e.g., for MySQL:
176172
;; ({generated_key 51} {generated_key 52})
177173
```
178174

179-
If you use `insert!` and specify the columns you wish to insert followed by
175+
If you use `insert-multi!` and specify the columns you wish to insert followed by
180176
each row as a vector of column values, then you must specify the same columns
181177
in each row, and you will not get generated keys back, just row counts. If you
182178
wish to insert complete rows, you may omit the column name vector (passing
183179
`nil` instead) but your rows must match the natural order of columns in your
184180
table so be careful!
185181

186182
```clojure
187-
(j/insert! db-spec :fruit
188-
nil ; column names not supplied
189-
[1 "Apple" "red" 59 87]
190-
[2 "Banana" "yellow" 29 92.2]
191-
[3 "Peach" "fuzzy" 139 90.0]
192-
[4 "Orange" "juicy" 89 88.6])
183+
(j/insert-multi! db-spec :fruit
184+
nil ; column names not supplied
185+
[[1 "Apple" "red" 59 87]
186+
[2 "Banana" "yellow" 29 92.2]
187+
[3 "Peach" "fuzzy" 139 90.0]
188+
[4 "Orange" "juicy" 89 88.6]])
193189
;; (1 1 1 1) - row counts modified
194190
```
195191

196192
It is generally safer to specify the columns you wish to insert so you can
197193
control the order, and choose to omit certain columns:
198194

199195
```clojure
200-
(j/insert! db-spec :fruit
201-
[:name :cost]
202-
["Mango" 722]
203-
["Feijoa" 441])
196+
(j/insert-multi! db-spec :fruit
197+
[:name :cost]
198+
[["Mango" 722]
199+
["Feijoa" 441]])
204200
;; (1 1) - row counts modified
205201
```
206202

@@ -275,9 +271,6 @@ Possible values for `:isolation` are `:none`, `:read-committed`,
275271
`:read-uncommitted`, `:repeatable-read`, and `:serializable`. Be aware that not
276272
all databases support all isolation levels.
277273

278-
Note: prior to version 0.5.5, the `:isolation` option could be specified
279-
without `{ }` but that was deprecated in 0.5.5 (and will be removed in 0.6.0).
280-
281274
In addition, you can also set the current transaction-aware connection to
282275
rollback, and reset that setting, as well as test whether the connection is
283276
currently set to rollback, using the following functions:
@@ -325,10 +318,10 @@ examples.
325318

326319
```clojure
327320
(j/with-db-transaction [t-con db-spec]
328-
(j/insert! t-con :fruit
329-
[:name :appearance]
330-
["Grape" "yummy"]
331-
["Pear" "bruised"])
321+
(j/insert-multi! t-con :fruit
322+
[:name :appearance]
323+
[["Grape" "yummy"]
324+
["Pear" "bruised"]])
332325
;; At this point the insert! call is complete, but the transaction is
333326
;; not. The exception will cause it to roll back leaving the database
334327
;; untouched.
@@ -344,10 +337,10 @@ commit:
344337
;; is-rollback-only false
345338
(j/db-set-rollback-only! t-con)
346339
;; the following insert will be rolled back when the transaction ends:
347-
(j/insert! t-con :fruit
348-
[:name :appearance]
349-
["Grape" "yummy"]
350-
["Pear" "bruised"])
340+
(j/insert!-multi t-con :fruit
341+
[:name :appearance]
342+
[["Grape" "yummy"]
343+
["Pear" "bruised"]])
351344
(prn "is-rollback-only" (j/db-is-rollback-only t-con))
352345
;; is-rollback-only true
353346
;; the following will display the inserted rows:
@@ -397,15 +390,17 @@ keywords:
397390

398391
For several databases, you will often want entities to be quoted in some way
399392
(sometimes referred to as "stropping"). A utility function `quoted` is provided
400-
that accepts either a single character or a vector pair of characters, and
393+
that accepts either a single character, a vector pair of characters, or a keyword
394+
as a symbolic name for the type of quoting you want (`:ansi`, `:mysql`,
395+
`:oracle`, `:sqlserver`), and
401396
returns a function suitable for use with the `:entities` option.
402397

403398
For example:
404399

405400
```clojure
406401
(j/insert! db-spec :fruit
407402
{:name "Apple" :appearance "Round" :cost 99}
408-
:options {:entities (j/quoted \`)})
403+
{:entities (j/quoted \`)}) ; or (j/quoted :mysql)
409404
```
410405

411406
will execute:
@@ -420,7 +415,7 @@ with the parameters `"Apple", "Round", "99"` whereas:
420415
```clojure
421416
(j/insert! db-spec :fruit
422417
{:name "Apple" :appearance "Round" :cost 99}
423-
:options {:entities (j/quoted [\[ \]])})
418+
{:entities (j/quoted [\[ \]])}) ; or (j/quoted :sqlserver)
424419
```
425420

426421
will execute:
@@ -432,11 +427,6 @@ INSERT INTO [fruit] ( [name], [appearance], [cost] )
432427

433428
with the parameters `"Apple", "Round", "99"`.
434429

435-
Note that `insert!` and `create-table-ddl` are the only functions in version
436-
0.5.5 that require `:options` as a "flag" to introduce the options map. In all
437-
the other functions, the options map is simply the last argument in the call
438-
(and can be omitted when the defaults are acceptable).
439-
440430
## Protocol extensions for transforming values
441431

442432
By default, java.jdbc leaves it up to Java interop and the JDBC driver library

0 commit comments

Comments
 (0)