-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathnjsResultSet.c
369 lines (316 loc) · 12.7 KB
/
njsResultSet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (c) 2015, 2025, Oracle and/or its affiliates.
//-----------------------------------------------------------------------------
//
// This software is dual-licensed to you under the Universal Permissive License
// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
// either license.
//
// If you elect to accept the software under the Apache License, Version 2.0,
// the following applies:
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// NAME
// njsResultSet.c
//
// DESCRIPTION
// ReseultSet class implementation.
//
//-----------------------------------------------------------------------------
#include "njsModule.h"
// class methods
NJS_NAPI_METHOD_DECL_ASYNC(njsResultSet_close);
NJS_NAPI_METHOD_DECL_ASYNC(njsResultSet_getRows);
// asynchronous methods
static NJS_ASYNC_METHOD(njsResultSet_closeAsync);
static NJS_ASYNC_METHOD(njsResultSet_getRowsAsync);
// post asynchronous methods
static NJS_ASYNC_POST_METHOD(njsResultSet_getRowsPostAsync);
// finalize
static NJS_NAPI_FINALIZE(njsResultSet_finalize);
// properties defined by the class
static const napi_property_descriptor njsClassProperties[] = {
{ "close", NULL, njsResultSet_close, NULL, NULL, NULL, napi_default,
NULL },
{ "getRows", NULL, njsResultSet_getRows, NULL, NULL, NULL, napi_default,
NULL },
{ NULL, NULL, NULL, NULL, NULL, NULL, napi_default, NULL }
};
// class definition
const njsClassDef njsClassDefResultSet = {
"ResultSetImpl", sizeof(njsResultSet), njsResultSet_finalize,
njsClassProperties, false
};
// other methods used internally
static bool njsResultSet_setFetchTypes(napi_env env, njsResultSet *rs,
napi_value allMetadata);
//-----------------------------------------------------------------------------
// njsResultSet_close()
// Close the result set.
//
// PARAMETERS - NONE
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_ASYNC(njsResultSet_close, 0, NULL)
{
njsResultSet *rs = (njsResultSet*) baton->callingInstance;
baton->dpiStmtHandle = rs->handle;
rs->handle = NULL;
return njsBaton_queueWork(baton, env, "Close", njsResultSet_closeAsync,
NULL, returnValue);
}
//-----------------------------------------------------------------------------
// njsResultSet_closeAsync()
// Worker function for njsResultSet_close().
//-----------------------------------------------------------------------------
static bool njsResultSet_closeAsync(njsBaton *baton)
{
njsResultSet *rs = (njsResultSet*) baton->callingInstance;
if (dpiStmt_close(baton->dpiStmtHandle, NULL, 0) < 0) {
njsBaton_setErrorDPI(baton);
rs->handle = baton->dpiStmtHandle;
baton->dpiStmtHandle = NULL;
return false;
}
if (!rs->isNested) {
baton->queryVars = rs->queryVars;
baton->numQueryVars = rs->numQueryVars;
rs->queryVars = NULL;
rs->numQueryVars = 0;
}
return true;
}
//-----------------------------------------------------------------------------
// njsResultSet_finalize()
// Invoked when the njsResultSet object is garbage collected.
//-----------------------------------------------------------------------------
static void njsResultSet_finalize(napi_env env, void *finalizeData,
void *finalizeHint)
{
njsResultSet *rs = (njsResultSet*) finalizeData;
if (rs->handle) {
dpiStmt_release(rs->handle);
rs->handle = NULL;
}
free(rs);
}
//-----------------------------------------------------------------------------
// njsResultSet_getRows()
// Get a number of rows from the result set.
//
// PARAMETERS
// - max number of rows to fetch at this time
//-----------------------------------------------------------------------------
NJS_NAPI_METHOD_IMPL_ASYNC(njsResultSet_getRows, 2, NULL)
{
NJS_CHECK_NAPI(env, napi_get_value_uint32(env, args[0],
&baton->fetchArraySize))
NJS_CHECK_NAPI(env, napi_create_reference(env, args[1], 1,
&baton->jsExecuteOptionsRef))
return njsBaton_queueWork(baton, env, "GetRows", njsResultSet_getRowsAsync,
njsResultSet_getRowsPostAsync, returnValue);
}
//-----------------------------------------------------------------------------
// njsResultSet_getRowsAsync()
// Worker function for njsResultSet_getRows().
//-----------------------------------------------------------------------------
static bool njsResultSet_getRowsAsync(njsBaton *baton)
{
njsResultSet *rs = (njsResultSet*) baton->callingInstance;
njsVariable *var;
int moreRows;
uint32_t i;
// create ODPI-C variables, if necessary
for (i = 0; i < rs->numQueryVars; i++) {
var = &rs->queryVars[i];
if (var->dpiVarHandle && var->maxArraySize >= baton->fetchArraySize)
continue;
rs->varsDefined = false;
if (var->dpiVarHandle) {
if (dpiVar_release(var->dpiVarHandle) < 0)
return njsBaton_setErrorDPI(baton);
var->dpiVarHandle = NULL;
}
var->maxArraySize = baton->fetchArraySize;
if (!njsVariable_createBuffer(var, rs->conn, baton))
return false;
}
// perform define, if necessary
if (!rs->varsDefined) {
for (i = 0; i < rs->numQueryVars; i++) {
var = &rs->queryVars[i];
if (dpiStmt_define(rs->handle, i + 1, var->dpiVarHandle) < 0)
return njsBaton_setErrorDPI(baton);
}
rs->varsDefined = true;
}
// set fetch array size as requested
if (dpiStmt_setFetchArraySize(rs->handle, baton->fetchArraySize) < 0)
return njsBaton_setErrorDPI(baton);
// perform fetch
if (dpiStmt_fetchRows(rs->handle, baton->fetchArraySize,
&baton->bufferRowIndex, &baton->rowsFetched, &moreRows) < 0)
return njsBaton_setErrorDPI(baton);
return njsVariable_process(rs->queryVars, rs->numQueryVars,
baton->rowsFetched, baton);
}
//-----------------------------------------------------------------------------
// njsResultSet_getRowsPostAsync()
// Defines the value returned to JS.
//-----------------------------------------------------------------------------
static bool njsResultSet_getRowsPostAsync(njsBaton *baton, napi_env env,
napi_value *result)
{
njsResultSet *rs = (njsResultSet*) baton->callingInstance;
napi_value rowObj, colObj;
uint32_t row, col, i;
njsVariable *var;
// set JavaScript values to simplify creation of returned objects
if (!njsBaton_setJsValues(baton, env))
return false;
// create array
NJS_CHECK_NAPI(env, napi_create_array_with_length(env, baton->rowsFetched,
result))
// process each row
for (row = 0; row < baton->rowsFetched; row++) {
// create row
NJS_CHECK_NAPI(env, napi_create_array_with_length(env,
rs->numQueryVars, &rowObj))
// process each column
for (col = 0; col < rs->numQueryVars; col++) {
var = &rs->queryVars[col];
if (!njsVariable_getScalarValue(var, rs->conn, var->buffer, row,
baton, env, &colObj))
return false;
NJS_CHECK_NAPI(env, napi_set_element(env, rowObj, col, colObj))
}
NJS_CHECK_NAPI(env, napi_set_element(env, *result, row, rowObj))
}
// clear variables if result set was closed
if (!rs->handle && !rs->isNested) {
for (i = 0; i < rs->numQueryVars; i++)
njsVariable_free(&rs->queryVars[i]);
free(rs->queryVars);
rs->queryVars = NULL;
rs->numQueryVars = 0;
}
return true;
}
//-----------------------------------------------------------------------------
// njsResultSet_new()
// Creates a new ResultSet object given the handle and variables that have
// been built previously. It as assumed that the calling instance is a
// connection.
//-----------------------------------------------------------------------------
bool njsResultSet_new(njsBaton *baton, napi_env env, njsConnection *conn,
dpiStmt *handle, njsVariable *vars, uint32_t numVars,
napi_value *rsObj)
{
napi_value fn, temp, args[2];
njsResultSet *rs;
// create new instance
if (!njsUtils_genericNew(env, &njsClassDefResultSet,
baton->globals->jsResultSetConstructor, rsObj, (void**) &rs))
return false;
// setup the result set (calls into JavaScript)
NJS_CHECK_NAPI(env, napi_get_reference_value(env,
baton->jsExecuteOptionsRef, &args[0]))
if (!njsVariable_getMetadataMany(vars, numVars, env, &args[1]))
return false;
NJS_CHECK_NAPI(env, napi_get_named_property(env, *rsObj, "_setup", &fn))
NJS_CHECK_NAPI(env, napi_call_function(env, *rsObj, fn, 2, args, &temp))
// perform some initializations
rs->handle = handle;
rs->conn = conn;
rs->numQueryVars = numVars;
rs->queryVars = vars;
rs->fetchArraySize = baton->fetchArraySize;
rs->isNested = (baton->callingInstance != (void*) conn);
// set fetch types
if (!njsResultSet_setFetchTypes(env, rs, args[1]))
return false;
return true;
}
//-----------------------------------------------------------------------------
// njsResultSet_setFetchTypes()
// Sets the fetch types after the fetchAsString, fetchAsBuffer and fetchInfo
// rules have all been applied.
//-----------------------------------------------------------------------------
static bool njsResultSet_setFetchTypes(napi_env env, njsResultSet *rs,
napi_value allMetadata)
{
napi_value metadata, temp, fetchType;
njsVariable *var;
uint32_t i;
for (i = 0; i < rs->numQueryVars; i++) {
var = &rs->queryVars[i];
// determine fetch type to use
NJS_CHECK_NAPI(env, napi_get_element(env, allMetadata, i, &metadata))
NJS_CHECK_NAPI(env, napi_get_named_property(env, metadata, "fetchType",
&fetchType))
NJS_CHECK_NAPI(env, napi_get_named_property(env, fetchType, "num",
&temp))
NJS_CHECK_NAPI(env, napi_get_value_uint32(env, temp, &var->varTypeNum))
// if RAW data is being returned as VARCHAR, need to have twice as much
// space available to account for the hex encoding that the server does
if (var->dbTypeNum == DPI_ORACLE_TYPE_RAW &&
var->varTypeNum == DPI_ORACLE_TYPE_VARCHAR) {
var->maxSize *= 2;
}
// adjust max size to use based on fetch type and verify fetch type
switch (var->varTypeNum) {
case DPI_ORACLE_TYPE_VARCHAR:
case DPI_ORACLE_TYPE_NVARCHAR:
case DPI_ORACLE_TYPE_CHAR:
case DPI_ORACLE_TYPE_NCHAR:
case DPI_ORACLE_TYPE_RAW:
if (var->dbTypeNum == DPI_ORACLE_TYPE_JSON) {
var->maxSize = (uint32_t) -1;
} else if (var->maxSize == 0) {
var->maxSize = NJS_MAX_FETCH_AS_STRING_SIZE;
}
break;
case DPI_ORACLE_TYPE_LONG_NVARCHAR:
case DPI_ORACLE_TYPE_LONG_VARCHAR:
case DPI_ORACLE_TYPE_LONG_RAW:
case DPI_ORACLE_TYPE_XMLTYPE:
var->maxSize = (uint32_t) -1;
break;
case DPI_ORACLE_TYPE_DATE:
case DPI_ORACLE_TYPE_TIMESTAMP:
case DPI_ORACLE_TYPE_TIMESTAMP_TZ:
case DPI_ORACLE_TYPE_TIMESTAMP_LTZ:
case DPI_ORACLE_TYPE_CLOB:
case DPI_ORACLE_TYPE_NCLOB:
case DPI_ORACLE_TYPE_BLOB:
case DPI_ORACLE_TYPE_BFILE:
case DPI_ORACLE_TYPE_OBJECT:
case DPI_ORACLE_TYPE_NUMBER:
case DPI_ORACLE_TYPE_NATIVE_INT:
case DPI_ORACLE_TYPE_NATIVE_FLOAT:
case DPI_ORACLE_TYPE_NATIVE_DOUBLE:
case DPI_ORACLE_TYPE_ROWID:
case DPI_ORACLE_TYPE_STMT:
case DPI_ORACLE_TYPE_JSON:
case DPI_ORACLE_TYPE_BOOLEAN:
case DPI_ORACLE_TYPE_VECTOR:
case DPI_ORACLE_TYPE_INTERVAL_YM:
case DPI_ORACLE_TYPE_INTERVAL_DS:
break;
default:
return njsUtils_throwUnsupportedDataType(env, var->varTypeNum,
i + 1);
}
}
return true;
}