Skip to content

Commit 361521e

Browse files
Document OBJECT support
1 parent 84341e7 commit 361521e

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,120 @@ Publisher<Integer[]> arrayMapExample(Result result) {
618618
}
619619
```
620620

621+
### OBJECT
622+
Oracle Database supports `OBJECT` as a user defined type. A `CREATE TYPE`
623+
command is used to define an `OBJECT` type:
624+
```sql
625+
CREATE TYPE PET AS OBJECT(
626+
name VARCHAR(128),
627+
species VARCHAR(128),
628+
weight NUMBER,
629+
birthday DATE)
630+
```
631+
Oracle R2DBC defines `oracle.r2dbc.OracleR2dbcType.ObjectType` as a `Type` for
632+
representing user defined `OBJECT` types. A `Parameter` with a type of
633+
`ObjectType` may be used to bind `OBJECT` values to a `Statement`.
634+
635+
Use an `Object[]` to bind the attribute values of an `OBJECT` by index:
636+
```java
637+
Publisher<Result> objectArrayBindExample(Connection connection) {
638+
Statement statement =
639+
connection.createStatement("INSERT INTO petTable VALUES (:petObject)");
640+
641+
// Bind the attributes of the PET OBJECT defined above
642+
ObjectType objectType = OracleR2dbcTypes.objectType("PET");
643+
Object[] attributeValues = {
644+
"Derby",
645+
"Dog",
646+
22.8,
647+
LocalDate.of(2015, 11, 07)
648+
};
649+
statement.bind("petObject", Parameters.in(objectType, attributeValues));
650+
651+
return statement.execute();
652+
}
653+
```
654+
655+
Use a `Map<String,Object>` to bind the attribute values of an `OBJECT` by name:
656+
```java
657+
Publisher<Result> objectMapBindExample(Connection connection) {
658+
Statement statement =
659+
connection.createStatement("INSERT INTO petTable VALUES (:petObject)");
660+
661+
// Bind the attributes of the PET OBJECT defined above
662+
ObjectType objectType = OracleR2dbcTypes.objectType("PET");
663+
Map<String,Object> attributeValues = Map.of(
664+
"name", "Derby",
665+
"species", "Dog",
666+
"weight", 22.8,
667+
"birthday", LocalDate.of(2015, 11, 07));
668+
statement.bind("petObject", Parameters.in(objectType, attributeValues));
669+
670+
return statement.execute();
671+
}
672+
```
673+
A `Parameter` with a type of `ObjectType` must be used when binding OUT
674+
parameters of `OBJECT` types for a PL/SQL call:
675+
```java
676+
Publisher<Result> objectOutBindExample(Connection connection) {
677+
Statement statement =
678+
connection.createStatement("BEGIN; getPet(:petObject); END;");
679+
680+
ObjectType objectType = OracleR2dbcTypes.objectType("PET");
681+
statement.bind("petObject", Parameters.out(objectType));
682+
683+
return statement.execute();
684+
}
685+
```
686+
`OBJECT` values may be consumed from a `Row` or `OutParameter` as an
687+
`oracle.r2dbc.OracleR2dbcObject`. The `OracleR2dbcObject` interface is a subtype
688+
of `io.r2dbc.spi.Readable`. Attribute values may be accessed using the standard
689+
`get` methods of `Readable`. The `get` methods of `OracleR2dbcObject` support
690+
alll SQL to Java type mappings defined by the
691+
[R2DBC Specification](https://r2dbc.io/spec/1.0.0.RELEASE/spec/html/#datatypes.mapping):
692+
```java
693+
Publisher<Pet> objectMapExample(Result result) {
694+
return result.map(row -> {
695+
OracleR2dbcObject oracleObject = row.get(0, OracleR2dbcObject.class);
696+
return new Pet(
697+
oracleObject.get("name", String.class),
698+
oracleObject.get("species", String.class),
699+
oracleObject.get("weight", Float.class),
700+
oracleObject.get("birthday", LocalDate.class));
701+
});
702+
}
703+
```
704+
705+
Instances of `OracleR2dbcObject` may be passed directly to `Statement` bind
706+
methods:
707+
```java
708+
Publisher<Result> objectBindExample(
709+
OracleR2dbcObject oracleObject, Connection connection) {
710+
Statement statement =
711+
connection.createStatement("INSERT INTO petTable VALUES (:petObject)");
712+
713+
statement.bind("petObject", oracleObject);
714+
715+
return statement.execute();
716+
}
717+
```
718+
Attribute metadata is exposed by the `getMetadata` method of
719+
`OracleR2dbcObject`:
720+
```java
721+
void printObjectMetadata(OracleR2dbcObject oracleObject) {
722+
OracleR2dbcObjectMetadata metadata = oracleObject.getMetadata();
723+
OracleR2dbcTypes.ObjectType objectType = metadata.getObjectType();
724+
725+
System.out.println("Object Type: " + objectType);
726+
metadata.getAttributeMetadatas()
727+
.stream()
728+
.forEach(attributeMetadata -> {
729+
System.out.println("\tAttribute Name: " + attributeMetadata.getName()));
730+
System.out.println("\tAttribute Type: " + attributeMetadata.getType()));
731+
});
732+
}
733+
```
734+
621735
### REF Cursor
622736
Use the `oracle.r2dbc.OracleR2dbcTypes.REF_CURSOR` type to bind `SYS_REFCURSOR` out
623737
parameters:

0 commit comments

Comments
 (0)