forked from IBM/node-odbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodbc_statement.cpp
More file actions
610 lines (490 loc) · 18.6 KB
/
Copy pathodbc_statement.cpp
File metadata and controls
610 lines (490 loc) · 18.6 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
Copyright (c) 2019, 2021 IBM
Copyright (c) 2013, Dan VerWeire<dverweire@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <napi.h>
#include <time.h>
#include <string>
#include "odbc.h"
#include "odbc_connection.h"
#include "odbc_statement.h"
#include "odbc_cursor.h"
Napi::FunctionReference ODBCStatement::constructor;
Napi::Object ODBCStatement::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function constructorFunction = DefineClass(env, "ODBCStatement", {
InstanceMethod("prepare", &ODBCStatement::Prepare),
InstanceMethod("bind", &ODBCStatement::Bind),
InstanceMethod("execute", &ODBCStatement::Execute),
InstanceMethod("close", &ODBCStatement::Close),
});
// Attach the Database Constructor to the target object
constructor = Napi::Persistent(constructorFunction);
constructor.SuppressDestruct();
return exports;
}
ODBCStatement::ODBCStatement(const Napi::CallbackInfo& info) : Napi::ObjectWrap<ODBCStatement>(info) {
this->data = new StatementData();
this->odbcConnection = info[0].As<Napi::External<ODBCConnection>>().Data();
this->data->hstmt = *(info[1].As<Napi::External<SQLHSTMT>>().Data());
this->data->fetch_array = this->odbcConnection->connectionOptions.fetchArray;
this->data->maxColumnNameLength = this->odbcConnection->getInfoResults.max_column_name_length;
this->data->get_data_supports = this->odbcConnection->getInfoResults.sql_get_data_supports;
}
ODBCStatement::~ODBCStatement() {
this->Free();
}
SQLRETURN ODBCStatement::Free() {
SQLRETURN return_code = SQL_SUCCESS;
if (this->data)
{
if (
this->data->hstmt &&
this->data->hstmt != SQL_NULL_HANDLE
)
{
uv_mutex_lock(&ODBC::g_odbcMutex);
return_code =
SQLFreeHandle
(
SQL_HANDLE_STMT,
this->data->hstmt
);
this->data->hstmt = SQL_NULL_HANDLE;
uv_mutex_unlock(&ODBC::g_odbcMutex);
}
delete this->data;
this->data = NULL;
}
return return_code;
}
/******************************************************************************
********************************* PREPARE ************************************
*****************************************************************************/
// PrepareAsyncWorker, used by Prepare function (see below)
class PrepareAsyncWorker : public ODBCAsyncWorker {
private:
ODBCStatement *odbcStatement;
ODBCConnection *odbcConnection;
StatementData *data;
public:
PrepareAsyncWorker(ODBCStatement *odbcStatement, Napi::Function& callback) : ODBCAsyncWorker(callback),
odbcStatement(odbcStatement),
odbcConnection(odbcStatement->odbcConnection),
data(odbcStatement->data) {}
~PrepareAsyncWorker() {}
void Execute() {
SQLRETURN return_code;
return_code = SQLPrepare
(
data->hstmt, // StatementHandle
data->sql, // StatementText
SQL_NTS // TextLength
);
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error preparing the statement\0");
return;
}
// front-load the work of SQLNumParams here, so we can convert
// NAPI/JavaScript values to C values immediately in Bind
return_code = SQLNumParams
(
data->hstmt, // StatementHandle
&data->parameterCount // ParameterCountPtr
);
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error retrieving number of parameter markers to be bound to the statement\0");
return;
}
data->parameters = new Parameter*[data->parameterCount];
for (SQLSMALLINT i = 0; i < data->parameterCount; i++) {
data->parameters[i] = new Parameter();
}
}
void OnOK() {
Napi::Env env = Env();
Napi::HandleScope scope(env);
std::vector<napi_value> callbackArguments;
callbackArguments.push_back(env.Null());
Callback().Call(callbackArguments);
}
};
/*
* ODBCStatement:Prepare (Async)
* Description: Prepares an SQL string so that it can be bound with
* parameters and then executed.
*
* Parameters:
* const Napi::CallbackInfo& info:
* The information passed by Napi from the JavaScript call, including
* arguments from the JavaScript function. In JavaScript, the
* prepare() function takes two arguments.
*
* info[0]: String: the SQL string to prepare.
* info[1]: Function: callback function:
* function(error, result)
* error: An error object if there was a problem getting results,
* or null if operation was successful.
* result: The number of rows affected by the executed query.
*
* Return:
* Napi::Value:
* Undefined (results returned in callback).
*/
Napi::Value ODBCStatement::Prepare(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
this->napiParameters = Napi::Persistent(Napi::Array::New(env));
if(!info[0].IsString() || !info[1].IsFunction()){
Napi::TypeError::New(env, "Argument 0 must be a string , Argument 1 must be a function.").ThrowAsJavaScriptException();
return env.Null();
}
Napi::String sql = info[0].ToString();
Napi::Function callback = info[1].As<Napi::Function>();
if(this->data->hstmt == SQL_NULL_HANDLE) {
Napi::Error error = Napi::Error::New(env, "Statment handle is no longer valid. Cannot prepare SQL on an invalid statment handle.");
std::vector<napi_value> callbackArguments;
callbackArguments.push_back(error.Value());
callback.Call(callbackArguments);
return env.Undefined();
}
data->sql = ODBC::NapiStringToSQLTCHAR(sql);
PrepareAsyncWorker *worker = new PrepareAsyncWorker(this, callback);
worker->Queue();
return env.Undefined();
}
/******************************************************************************
*********************************** BIND *************************************
*****************************************************************************/
// BindAsyncWorker, used by Bind function (see below)
class BindAsyncWorker : public ODBCAsyncWorker {
public:
BindAsyncWorker(ODBCStatement *odbcStatement, Napi::Function& callback) : ODBCAsyncWorker(callback),
odbcStatement(odbcStatement),
odbcConnection(odbcStatement->odbcConnection),
data(odbcStatement->data) {}
private:
ODBCStatement *odbcStatement;
ODBCConnection *odbcConnection;
StatementData *data;
~BindAsyncWorker() { }
void Execute() {
SQLRETURN return_code;
return_code = ODBC::DescribeParameters(data->hstmt, data->parameters, data->parameterCount);
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error retrieving information about the parameters in the statement\0");
return;
}
return_code = ODBC::BindParameters(data->hstmt, data->parameters, data->parameterCount);
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error binding parameters to the statement\0");
return;
}
}
void OnOK() {
Napi::Env env = Env();
Napi::HandleScope scope(env);
std::vector<napi_value> callbackArguments;
callbackArguments.push_back(env.Null());
Callback().Call(callbackArguments);
}
};
Napi::Value ODBCStatement::Bind(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if ( !info[0].IsArray() || !info[1].IsFunction() ) {
Napi::TypeError::New(env, "Function signature is: bind(array, function)").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Array napiArray = info[0].As<Napi::Array>();
this->napiParameters = Napi::Persistent(napiArray);
Napi::Function callback = info[1].As<Napi::Function>();
if(this->data->hstmt == SQL_NULL_HANDLE) {
Napi::Error error = Napi::Error::New(env, "Statment handle is no longer valid. Cannot bind SQL on an invalid statment handle.");
std::vector<napi_value> callbackArguments;
callbackArguments.push_back(error.Value());
callback.Call(callbackArguments);
return env.Undefined();
}
// if the parameter count isnt right, end right away
if (data->parameterCount != (SQLSMALLINT)this->napiParameters.Value().Length() || data->parameters == NULL) {
std::vector<napi_value> callbackArguments;
Napi::Error error = Napi::Error::New(env, Napi::String::New(env, "[node-odbc] Error in Statement::BindAsyncWorker::Bind: The number of parameters in the prepared statement (" + std::to_string(data->parameterCount) + ") doesn't match the number of parameters passed to bind (" + std::to_string((SQLSMALLINT)this->napiParameters.Value().Length()) + "}."));
callbackArguments.push_back(error.Value());
callback.Call(callbackArguments);
return env.Undefined();
}
// converts NAPI/JavaScript values to values used by SQLBindParameter
ODBC::StoreBindValues(&napiArray, this->data->parameters);
BindAsyncWorker *worker = new BindAsyncWorker(this, callback);
worker->Queue();
return env.Undefined();
}
/******************************************************************************
********************************* EXECUTE ************************************
*****************************************************************************/
// ExecuteAsyncWorker, used by Execute function (see below)
class ExecuteAsyncWorker : public ODBCAsyncWorker {
private:
ODBCConnection *odbcConnection;
ODBCStatement *odbcStatement;
StatementData *data;
void Execute() {
SQLRETURN return_code;
// set SQL_ATTR_QUERY_TIMEOUT
if (data->query_options.timeout > 0) {
return_code =
SQLSetStmtAttr
(
data->hstmt,
SQL_ATTR_QUERY_TIMEOUT,
(SQLPOINTER) data->query_options.timeout,
IGNORED_PARAMETER
);
// It is possible that SQLSetStmtAttr returns a warning with SQLSTATE
// 01S02, indicating that the driver changed the value specified.
// Although we never use the timeout variable again (and so we don't
// REALLY need it to be correct in the code), its just good to have
// the correct value if we need it.
if (return_code == SQL_SUCCESS_WITH_INFO)
{
return_code =
SQLGetStmtAttr
(
data->hstmt,
SQL_ATTR_QUERY_TIMEOUT,
(SQLPOINTER) &data->query_options.timeout,
SQL_IS_UINTEGER,
IGNORED_PARAMETER
);
}
// Both of the SQL_ATTR_QUERY_TIMEOUT calls are combined here
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error setting the query timeout on the statement\0");
return;
}
}
return_code =
set_fetch_size
(
data,
data->query_options.fetch_size
);
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error setting the fetch size\0");
return;
}
return_code =
SQLExecute
(
data->hstmt // StatementHandle
);
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error executing the statement\0");
return;
}
if (return_code != SQL_NO_DATA) {
if (data->query_options.use_cursor)
{
if (data->query_options.cursor_name != NULL)
{
return_code =
SQLSetCursorName
(
data->hstmt,
data->query_options.cursor_name,
data->query_options.cursor_name_length
);
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error setting the cursor name on the statement\0");
return;
}
}
}
// set_fetch_size will swallow errors in the case that the driver
// doesn't implement SQL_ATTR_ROW_ARRAY_SIZE for SQLSetStmtAttr and
// the fetch size was 1. If the fetch size was set by the user to a
// value greater than 1, throw an error.
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error setting the fetch size on the statement\0");
return;
}
return_code =
prepare_for_fetch
(
data
);
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error preparing for fetch\0");
return;
}
if (!data->query_options.use_cursor)
{
bool alloc_error = false;
return_code =
fetch_all_and_store
(
data,
true,
&alloc_error
);
if (alloc_error)
{
SetError("[odbc] Error allocating or reallocating memory when fetching data. No ODBC error information available.\0");
return;
}
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error retrieving the result set from the statement\0");
return;
}
}
}
}
void OnOK() {
Napi::Env env = Env();
Napi::HandleScope scope(env);
std::vector<napi_value> callbackArguments;
if (data->query_options.use_cursor)
{
// arguments for the ODBCCursor constructor
std::vector<napi_value> cursor_arguments =
{
Napi::External<StatementData>::New(env, data),
Napi::External<ODBCConnection>::New(env, this->odbcConnection),
this->odbcStatement->napiParameters.Value(),
Napi::Boolean::New(env, false)
};
// create a new ODBCCursor object as a Napi::Value
Napi::Value cursorObject = ODBCCursor::constructor.New(cursor_arguments);
// return cursor
std::vector<napi_value> callbackArguments =
{
env.Null(),
cursorObject
};
Callback().Call(callbackArguments);
}
else
{
Napi::Array rows = process_data_for_napi(env, data, odbcStatement->napiParameters.Value());
std::vector<napi_value> callbackArguments;
callbackArguments.push_back(env.Null());
callbackArguments.push_back(rows);
Callback().Call(callbackArguments);
}
return;
}
public:
ExecuteAsyncWorker(ODBCStatement *odbcStatement, Napi::Function& callback) : ODBCAsyncWorker(callback),
odbcConnection(odbcStatement->odbcConnection),
odbcStatement(odbcStatement),
data(odbcStatement->data) {}
~ExecuteAsyncWorker() {}
};
Napi::Value ODBCStatement::Execute(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Function callback;
size_t argument_count = info.Length();
// ensuring the passed parameters are correct
if ((argument_count == 1 && info[0].IsFunction()) || (argument_count == 2 && info[1].IsFunction())) {
callback = info[argument_count - 1].As<Napi::Function>();
}
Napi::Value error;
// ensuring the passed parameters are correct
if (argument_count >= 1 && info[0].IsObject()) {
error =
parse_query_options
(
env,
info[0].As<Napi::Object>(),
&this->data->query_options
);
} else {
error =
parse_query_options
(
env,
env.Null(),
&this->data->query_options
);
}
if (!error.IsNull())
{
// Error when parsing the query options. Return the callback with the error
std::vector<napi_value> callback_argument =
{
error
};
callback.Call(callback_argument);
}
if(this->data->hstmt == SQL_NULL_HANDLE) {
Napi::Error error = Napi::Error::New(env, "Statment handle is no longer valid. Cannot execute SQL on an invalid statment handle.");
std::vector<napi_value> callbackArguments;
callbackArguments.push_back(error.Value());
callback.Call(callbackArguments);
return env.Undefined();
}
ExecuteAsyncWorker *worker = new ExecuteAsyncWorker(this, callback);
worker->Queue();
return env.Undefined();
}
/******************************************************************************
********************************** CLOSE *************************************
*****************************************************************************/
// CloseStatementAsyncWorker, used by Close function (see below)
class CloseStatementAsyncWorker : public ODBCAsyncWorker {
private:
ODBCStatement *odbcStatement;
StatementData *data;
void Execute() {
SQLRETURN return_code;
return_code = odbcStatement->Free();
if (!SQL_SUCCEEDED(return_code)) {
this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
SetError("[odbc] Error closing the Statement\0");
return;
}
}
void OnOK() {
Napi::Env env = Env();
Napi::HandleScope scope(env);
std::vector<napi_value> callbackArguments;
callbackArguments.push_back(env.Null());
Callback().Call(callbackArguments);
}
public:
CloseStatementAsyncWorker(ODBCStatement *odbcStatement, Napi::Function& callback) : ODBCAsyncWorker(callback),
odbcStatement(odbcStatement),
data(odbcStatement->data) {}
~CloseStatementAsyncWorker() {}
};
Napi::Value ODBCStatement::Close(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Function callback = info[0].As<Napi::Function>();
CloseStatementAsyncWorker *worker = new CloseStatementAsyncWorker(this, callback);
worker->Queue();
return env.Undefined();
}