From 50e5776fc586a90e1b4325e3c84a50c2e58142e3 Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Sun, 21 Jul 2024 00:19:19 +0700 Subject: [PATCH 1/4] fix: improve oracle --- .../data-access/jdbc/parameter-handling.adoc | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc b/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc index b0035ea95466..e00d05153c71 100644 --- a/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc +++ b/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc @@ -209,7 +209,7 @@ are passed in as a parameter to the stored procedure. The `SqlReturnType` interface has a single method (named `getTypeValue`) that must be implemented. This interface is used as part of the declaration of an `SqlOutParameter`. -The following example shows returning the value of an Oracle `STRUCT` object of the user +The following example shows returning the value of a `java.sql.Struct` object of the user declared type `ITEM_TYPE`: [tabs] @@ -222,9 +222,9 @@ Java:: public TestItemStoredProcedure(DataSource dataSource) { // ... - declareParameter(new SqlOutParameter("item", OracleTypes.STRUCT, "ITEM_TYPE", + declareParameter(new SqlOutParameter("item", Types.STRUCT, "ITEM_TYPE", (CallableStatement cs, int colIndx, int sqlType, String typeName) -> { - STRUCT struct = (STRUCT) cs.getObject(colIndx); + Struct struct = (Struct) cs.getObject(colIndx); Object[] attr = struct.getAttributes(); TestItem item = new TestItem(); item.setId(((Number) attr[0]).longValue()); @@ -258,8 +258,8 @@ Kotlin:: You can use `SqlTypeValue` to pass the value of a Java object (such as `TestItem`) to a stored procedure. The `SqlTypeValue` interface has a single method (named `createTypeValue`) that you must implement. The active connection is passed in, and you -can use it to create database-specific objects, such as `StructDescriptor` instances -or `ArrayDescriptor` instances. The following example creates a `StructDescriptor` instance: +can use it to create database-specific objects, such as `java.sql.Struct` instances +or `java.sql.Array` instances. The following example creates a `java.sql.Struct` instance: [tabs] ====== @@ -272,14 +272,12 @@ Java:: SqlTypeValue value = new AbstractSqlTypeValue() { protected Object createTypeValue(Connection conn, int sqlType, String typeName) throws SQLException { - StructDescriptor itemDescriptor = new StructDescriptor(typeName, conn); - Struct item = new STRUCT(itemDescriptor, conn, - new Object[] { + var item = new Object[] { testItem.getId(), testItem.getDescription(), new java.sql.Date(testItem.getExpirationDate().getTime()) - }); - return item; + }; + return connection.createStruct(typeName, objects); } }; ---- @@ -307,7 +305,7 @@ You can now add this `SqlTypeValue` to the `Map` that contains the input paramet Another use for the `SqlTypeValue` is passing in an array of values to an Oracle stored procedure. Oracle has its own internal `ARRAY` class that must be used in this case, and you can use the `SqlTypeValue` to create an instance of the Oracle `ARRAY` and populate -it with values from the Java `ARRAY`, as the following example shows: +it with values from the Java `java.sql.Array`, as the following example shows: [tabs] ====== @@ -319,9 +317,7 @@ Java:: SqlTypeValue value = new AbstractSqlTypeValue() { protected Object createTypeValue(Connection conn, int sqlType, String typeName) throws SQLException { - ArrayDescriptor arrayDescriptor = new ArrayDescriptor(typeName, conn); - ARRAY idArray = new ARRAY(arrayDescriptor, conn, ids); - return idArray; + return conn.unwrap(OracleConnection.class).createOracleArray(typeName, ids); } }; ---- From 362ff308c1b8718995ee795ef05e076bc99ef925 Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Sun, 21 Jul 2024 00:31:08 +0700 Subject: [PATCH 2/4] fix: revert line 212 --- .../modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc b/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc index e00d05153c71..d20b69371ec5 100644 --- a/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc +++ b/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc @@ -209,7 +209,7 @@ are passed in as a parameter to the stored procedure. The `SqlReturnType` interface has a single method (named `getTypeValue`) that must be implemented. This interface is used as part of the declaration of an `SqlOutParameter`. -The following example shows returning the value of a `java.sql.Struct` object of the user +The following example shows returning the value of an Oracle `STRUCT` object of the user declared type `ITEM_TYPE`: [tabs] From 315c705efbbaa1e197aff83535502477ac33f4fe Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Sun, 21 Jul 2024 00:41:09 +0700 Subject: [PATCH 3/4] fix: update variable --- .../modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc b/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc index d20b69371ec5..5879673aa08f 100644 --- a/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc +++ b/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc @@ -277,7 +277,7 @@ Java:: testItem.getDescription(), new java.sql.Date(testItem.getExpirationDate().getTime()) }; - return connection.createStruct(typeName, objects); + return connection.createStruct(typeName, item); } }; ---- From 91376670241b921771a791ffecf619c9534c4e05 Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Sun, 21 Jul 2024 00:46:35 +0700 Subject: [PATCH 4/4] fix: update note --- .../ROOT/pages/data-access/jdbc/parameter-handling.adoc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc b/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc index 5879673aa08f..8e4a215b4343 100644 --- a/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc +++ b/framework-docs/modules/ROOT/pages/data-access/jdbc/parameter-handling.adoc @@ -341,5 +341,8 @@ Kotlin:: ---- ====== - +[NOTE] +==== +Use `unwrap(OracleConnection.class)` method if connection is not an OracleConnection's instance +====