You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DatabaseClient now supports named parameters prefixed with a colon such as :name in addition to database-native bind markers. Named parameters thus are supported in annotated repository query methods which also increases portability of queries across database vendors.
Named parameter support unrolls collection arguments to reduce the need for argument-specific SQL statements:
db.execute()
.sql("SELECT id, name, state FROM table WHERE age IN (:ages)")
.bind("ages", Arrays.asList(35, 50));
Results in a query: SELECT id, name, state FROM table WHERE age IN (35, 50)
Collection arguments containing nested object arrays can be used to use select lists:
List<Object[]> tuples = new ArrayList<>();
tuples.add(new Object[] {"John", 35});
tuples.add(new Object[] {"Ann", 50});
db.execute()
.sql("SELECT id, name, state FROM table WHERE (name, age) IN (:tuples)")
.bind("tuples", tuples);
translates to: SELECT id, name, state FROM table WHERE (name, age) IN (('John', 35), ('Ann', 50))
Original pull request: #47.
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/r2dbc.adoc
+55-14
Original file line number
Diff line number
Diff line change
@@ -446,20 +446,61 @@ Parameter binding supports various binding strategies:
446
446
* By Index using zero-based parameter indexes.
447
447
* By Name using the placeholder name.
448
448
449
-
The following example shows parameter binding for a PostgreSQL query:
449
+
The following example shows parameter binding for a query:
450
450
451
451
[source,java]
452
452
----
453
453
db.execute()
454
-
.sql("INSERT INTO person (id, name, age) VALUES($1, $2, $3)")
455
-
.bind(0, "joe")
456
-
.bind(1, "Joe")
457
-
.bind(2, 34);
454
+
.sql("INSERT INTO person (id, name, age) VALUES(:id, :name, :age)")
455
+
.bind("id", "joe")
456
+
.bind("name", "Joe")
457
+
.bind("age", 34);
458
458
----
459
459
460
-
NOTE: If you are familiar with JDBC, then you're also familiar with `?` (question mark) bind markers.
460
+
.R2DBC Native Bind Markers
461
+
****
462
+
R2DBC uses database-native bind markers that depend on the actual database.
463
+
If you are familiar with JDBC, then you're also familiar with `?` (question mark) bind markers.
461
464
JDBC drivers translate question mark bind markers to database-native markers as part of statement execution.
462
-
Make sure to use the appropriate bind markers that are supported by your database as R2DBC requires database-native parameter bind markers.
465
+
466
+
Postgres uses indexed markers (`$1`, `$2`), SQL Server uses named bind markers prefixed with `@` as its native bind marker syntax.
467
+
Spring Data R2DBC leverages `Dialect` implementations to expand named parameters to native bind markers at the time of query execution which gives you a certain degree of query portability across various database vendors.
468
+
You can still use native bind markers if you prefer to do so.
469
+
****
470
+
471
+
The query-preprocessor unrolls named `Collection` parameters into a series of bind markers to remove the need of dynamic query creation based on the number of arguments.
472
+
Nested object arrays are expanded to allow usage of e.g. select lists.
473
+
474
+
Consider the following query:
475
+
476
+
[source,sql]
477
+
----
478
+
SELECT id, name, state FROM table WHERE (name, age) IN (('John', 35), ('Ann', 50))
479
+
----
480
+
481
+
This query can be parametrized and executed as:
482
+
483
+
[source,java]
484
+
----
485
+
List<Object[]> tuples = new ArrayList<>();
486
+
tuples.add(new Object[] {"John", 35});
487
+
tuples.add(new Object[] {"Ann", 50});
488
+
489
+
db.execute()
490
+
.sql("SELECT id, name, state FROM table WHERE (name, age) IN (:tuples)")
491
+
.bind("tuples", tuples);
492
+
----
493
+
494
+
NOTE: Usage of select lists is vendor-dependent.
495
+
496
+
A simpler variant using `IN` predicates:
497
+
498
+
[source,java]
499
+
----
500
+
db.execute()
501
+
.sql("SELECT id, name, state FROM table WHERE age IN (:ages)")
0 commit comments