Skip to content

Commit 7fc73f0

Browse files
committed
Update Example based on comment in Issue #745 by Kubo Takehiro
1 parent f11e53e commit 7fc73f0

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,6 @@ File Name | Description
108108
[`sessiontagging2.js`](sessiontagging2.js) | More complex example of pooled connection tagging for setting session state
109109
[`soda1.js`](soda1.js) | Basic Simple Oracle Document Access (SODA) example
110110
[`typehandlerdate.js`](typehandlerdate.js) | Show how a type handler can format a queried date in a locale-specific way
111-
[`typehandlernum.js`](typehandlernum.js) | Show how a type handler can format a queried number in a locale-specific way
111+
[`typehandlernum.js`](typehandlernum.js) | Show how a type handler can alter queried numbers
112112
[`version.js`](version.js) | Shows the node-oracledb version attributes
113113
[`webapp.js`](webapp.js) | A simple web application using a connection pool

examples/typehandlernum.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
* typehandlernum.js
2727
*
2828
* DESCRIPTION
29-
* Show how a type handler can format a queried number in a locale-specific
30-
* way.
29+
* Show how a type handler can alter queried numbers
30+
* - formating numbers in a locale-specific way.
31+
* - altering the conversion between Oracle's decimal format and Node.js's
32+
* binary format.
3133
*
3234
*****************************************************************************/
3335

@@ -60,10 +62,10 @@ if (process.env.NODE_ORACLEDB_DRIVER_MODE === 'thick') {
6062
}
6163

6264

63-
// The fetch type handler is called once per column in the SELECT list.
64-
// If the metadata name & type tests are satified, then the returned
65-
// converter function is enabled for that column. Data in this column will
66-
// be processed by the converter function before it is returned to the
65+
// This fetch type handler is called once per column in the SELECT list of
66+
// example 1. If the metadata name & type tests are satified, then the
67+
// returned converter function is enabled for that column. Data in this column
68+
// will be processed by the converter function before it is returned to the
6769
// application.
6870

6971
function fth(metaData) {
@@ -105,14 +107,45 @@ async function run() {
105107
const inssql = `INSERT INTO no_typehandler_tab (n_col) VALUES (:bv)`;
106108
await connection.execute(inssql, { bv: data });
107109

108-
console.log('3. Selecting the number');
110+
// Example 1
109111

110-
const result = await connection.execute(
112+
console.log('3. Selecting a formatted number');
113+
114+
let result = await connection.execute(
111115
"select n_col from no_typehandler_tab",
112116
[],
113117
{ fetchTypeHandler: fth }
114118
);
115-
console.log(`Column ${result.metaData[0].name} is formatted as ${result.rows[0][0]}`);
119+
console.log(` Column ${result.metaData[0].name} is formatted as ${result.rows[0][0]}`);
120+
121+
// Example 2
122+
123+
// In Thick mode, the default conversion from Oracle's decimal number
124+
// format to Node.js's binary format may not be desirable. For example the
125+
// number 0.94 may be fetched as 0.9400000000000001. An alternative is to
126+
// fetch numbers as strings from the database and then convert to floats in
127+
// Node.js. This example shows the type handler in-line in the execute()
128+
// call. Thin mode does not need the handler.
129+
130+
console.log('4. Selecting a number where the default Thick mode decimal-to-binary format conversion may not be desired');
131+
132+
result = await connection.execute(
133+
"SELECT 0.94 AS col1, 0.94 AS col2 FROM dual", [], {
134+
fetchTypeHandler: function(metaData) {
135+
if (metaData.name == 'COL2' && metaData.dbType == oracledb.DB_TYPE_NUMBER) {
136+
const converter = (v) => {
137+
if (v !== null)
138+
v = parseFloat(v);
139+
return v;
140+
};
141+
return {type: oracledb.STRING, converter: converter};
142+
}
143+
}
144+
}
145+
);
146+
147+
// In Thick mode, the two values will differ
148+
console.log(` Raw number is ${result.rows[0][0]}. Number converted is ${result.rows[0][1]}`);
116149

117150
} catch (err) {
118151
console.error(err);

0 commit comments

Comments
 (0)