Skip to content

Commit 8828cf9

Browse files
committed
Answer a FAQ by adding a section on custom types
1 parent 55d761f commit 8828cf9

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

doc/api.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,6 +2256,8 @@ The default query result type mappings for Oracle Database types to JavaScript t
22562256
When binding a JavaScript Date value in an `INSERT` statement, the date is also inserted as `TIMESTAMP WITH
22572257
LOCAL TIMEZONE` using OCIDateTime.
22582258
2259+
##### Fetching as String
2260+
22592261
The global [`fetchAsString`](#propdbfetchasstring) property can be
22602262
used to force all number or date columns queried by an application to
22612263
be fetched as strings instead of in native format. Allowing data to
@@ -2339,6 +2341,70 @@ represented as numbers:
23392341
To map columns returned from REF CURSORS, use `fetchAsString`. The
23402342
`fetchInfo` settings do not apply.
23412343
2344+
##### Mapping Custom Types
2345+
2346+
Datatypes such as an Oracle Locator `SDO_GEOMETRY`, or your own custom
2347+
types, cannot be fetched directly in node-oracledb. Instead, utilize
2348+
techniques such as using an intermediary PL/SQL procedure to map the
2349+
type components to scalar values, or use a pipelined table.
2350+
2351+
For example, consider a `CUSTOMERS` table having a `CUST_GEO_LOCATION`
2352+
column of type `SDO_GEOMETRY`, as created in this [example
2353+
schema](http://docs.oracle.com/cd/E17781_01/appdev.112/e18750/xe_locator.htm#XELOC560):
2354+
2355+
```sql
2356+
CREATE TABLE customers (
2357+
customer_id NUMBER,
2358+
last_name VARCHAR2(30),
2359+
cust_geo_location SDO_GEOMETRY);
2360+
2361+
INSERT INTO customers VALUES
2362+
(1001, 'Nichols', SDO_GEOMETRY(2001, 8307, SDO_POINT_TYPE (-71.48923,42.72347,NULL), NULL, NULL));
2363+
2364+
COMMIT;
2365+
```
2366+
2367+
Instead of attempting to get `CUST_GEO_LOCATION` by directly calling a
2368+
PL/SQL procedure that returns an `SDO_GEOMETRY` parameter, you could
2369+
instead get the scalar coordinates by using an intermediary PL/SQL
2370+
block that decomposes the geometry:
2371+
2372+
```javascript
2373+
. . .
2374+
var sql =
2375+
"BEGIN " +
2376+
" SELECT t.x, t.y" +
2377+
" INTO :x, :y" +
2378+
" FROM customers, TABLE(sdo_util.getvertices(customers.cust_geo_location)) t" +
2379+
" WHERE customer_id = :id;" +
2380+
"END; ";
2381+
var bindvars = {
2382+
id: 1001,
2383+
x: { type: oracledb.NUMBER, dir : oracledb.BIND_OUT },
2384+
y: { type: oracledb.NUMBER, dir : oracledb.BIND_OUT }
2385+
}
2386+
connection.execute(
2387+
sql,
2388+
bindvars,
2389+
function (err, result) {
2390+
if (err) { console.error(err.message); return; }
2391+
console.log(result.outBinds);
2392+
});
2393+
```
2394+
2395+
The output is:
2396+
2397+
```
2398+
{ x: -71.48922999999999, y: 42.72347 }
2399+
```
2400+
2401+
Note the JavaScript precision difference. In this particular example,
2402+
you may want to bind using `type: oracledb.STRING`. Output would be:
2403+
2404+
```
2405+
{ x: '-71.48923', y: '42.72347' }
2406+
```
2407+
23422408
#### <a name="rowprefetching"></a> 9.1.6 Row Prefetching
23432409
23442410
[Prefetching](http://docs.oracle.com/database/121/LNOCI/oci04sql.htm#LNOCI16355) is a query tuning feature allowing resource usage to be
@@ -2568,7 +2634,7 @@ connection.execute(
25682634
[],
25692635
{ resultSet: true },
25702636
function (err, result) {
2571-
. . .
2637+
. . .
25722638
```
25732639
25742640
The query rows can be handled using a
@@ -2710,7 +2776,7 @@ connection.execute(
27102776
if (result.rows.length === 0) { console.log("No results"); return; }
27112777

27122778
var lob = result.rows[0][0];
2713-
if (lob === null) { console.log("CLOB was NULL"); return; }
2779+
if (lob === null) { console.log("CLOB was NULL"); return; }
27142780

27152781
lob.setEncoding('utf8'); // we want text, not binary output
27162782
lob.on('error', function(err) { console.error(err); });

0 commit comments

Comments
 (0)