|
26 | 26 | * typehandlernum.js |
27 | 27 | * |
28 | 28 | * 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. |
31 | 33 | * |
32 | 34 | *****************************************************************************/ |
33 | 35 |
|
@@ -60,10 +62,10 @@ if (process.env.NODE_ORACLEDB_DRIVER_MODE === 'thick') { |
60 | 62 | } |
61 | 63 |
|
62 | 64 |
|
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 |
67 | 69 | // application. |
68 | 70 |
|
69 | 71 | function fth(metaData) { |
@@ -105,14 +107,45 @@ async function run() { |
105 | 107 | const inssql = `INSERT INTO no_typehandler_tab (n_col) VALUES (:bv)`; |
106 | 108 | await connection.execute(inssql, { bv: data }); |
107 | 109 |
|
108 | | - console.log('3. Selecting the number'); |
| 110 | + // Example 1 |
109 | 111 |
|
110 | | - const result = await connection.execute( |
| 112 | + console.log('3. Selecting a formatted number'); |
| 113 | + |
| 114 | + let result = await connection.execute( |
111 | 115 | "select n_col from no_typehandler_tab", |
112 | 116 | [], |
113 | 117 | { fetchTypeHandler: fth } |
114 | 118 | ); |
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]}`); |
116 | 149 |
|
117 | 150 | } catch (err) { |
118 | 151 | console.error(err); |
|
0 commit comments