-
Notifications
You must be signed in to change notification settings - Fork 23
/
dbconn.cc
411 lines (370 loc) · 17.4 KB
/
dbconn.cc
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
/* The Source code for this program is not published or otherwise
* divested of its trade secrets, irrespective of what has been
* deposited with the U.S. Copyright Office.
*/
#include "dbconn.h"
SQLHENV DbConn::envh;
Napi::FunctionReference DbConn::constructor;
Napi::Object DbConn::Init(Napi::Env env, Napi::Object exports, SQLHENV envh2) {
Napi::HandleScope scope(env);
Napi::Function constructorfunc = DefineClass(env, "dbconn", {
InstanceMethod("setConnAttr", &DbConn::SetConnAttr),
InstanceMethod("getConnAttr", &DbConn::GetConnAttr),
InstanceMethod("conn", &DbConn::Conn),
InstanceMethod("disconn", &DbConn::Disconnect),
InstanceMethod("close", &DbConn::Close),
InstanceMethod("validStmt", &DbConn::ValidStmt),
InstanceMethod("debug", &DbConn::Debug),
InstanceMethod("isConnected", &DbConn::IsConnected),
});
constructor = Napi::Persistent(constructorfunc);
constructor.SuppressDestruct();
envh = envh2;
exports.Set("dbconn" , constructorfunc);
return exports;
}
DbConn::DbConn(const Napi::CallbackInfo& info) : Napi::ObjectWrap<DbConn>(info) {
SQLRETURN sqlReturnCode = -1;
int param = SQL_TRUE;
if(this->connAllocated == false) {
// Doc https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/cli/rzadpfnacon.htm
sqlReturnCode = SQLAllocConnect(envh, // SQLHENV henv -Environment Handle
&this->connh ); //SQLHDBC* phdbc -Pointer to connection handle
if(sqlReturnCode != SQL_SUCCESS) {
DEBUG(this, "SQLAllocConnect(%d): SQL Connection Allocation Fail", sqlReturnCode);
return;
}
this->connAllocated = true; // Any Connection Handler processing can not be allowed before this.
}
sqlReturnCode = SQLSetConnectAttr(this->connh, SQL_ATTR_AUTOCOMMIT, ¶m, 0); // Enable auto_commit by default.
}
Napi::Object DbConn::NewInstance(Napi::Value arg){
Napi::Object dbConn = constructor.New({ arg });
return dbConn;
}
/*
* DbConn::SetConnAttr
* Description: Set an attribute of a specific connection handle.
* Parameters:
* const Napi::CallbackInfo& info:
* The information passed by Napi from the JavaScript call, including
* arguments from the JavaScript function. In JavaScript, the exported
* function takes two arguments, stored on the info object:
* info[0] (Number): Attribute is the statement attribute to set.
* Refer to the attribute table for more details.
* info[1] (Number/String): Depending on the Attribute, this can be an
* integer value, or a character string.
* Return: Boolean True if no errors occured. Otherwise error is thrown.
*/
Napi::Value DbConn::SetConnAttr(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int length = info.Length();
//validation
CHECK_WITH_RETURN(length != 2, INVALID_PARAM_NUM, "Expected Two Parameters for SetConnAttr", env, env.Null())
CHECK_WITH_RETURN(!info[0].IsNumber(), INVALID_PARAM_TYPE, "Expected Parameter 1 to be a Number", env, env.Null())
CHECK_WITH_RETURN(!info[1].IsNumber() && !info[1].IsString(), INVALID_PARAM_TYPE, "Number or String Expected For Second Parameter of SetConnAttr", env, env.Null())
CHECK_WITH_RETURN(this->connAllocated == false, CONN_NOT_READY, "Need to call conn() to allocate connection on DbConn first", env, env.Null())
SQLINTEGER attr = Napi::Number(env, info[0]).Int32Value();
char* cValue;
SQLINTEGER sLen = 0;
SQLRETURN sqlReturnCode = -1;
//check if the second arg was a Number or a String
if(info[1].IsNumber() )
{
int param = Napi::Number(env, info[1]).Int32Value();
// Doc https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/cli/rzadpfnsconx.htm
sqlReturnCode = SQLSetConnectAttr(this->connh, //SQLHDBC hdbc -Connection Handle
attr, //SQLINTEGER fAttr -Connection Attr to Set
¶m, //SQLPOINTER vParam -Value for fAttr
0); //SQLINTEGER sLen -Length of input value (if string)
DEBUG(this, "SetConnAttr() attr = %d, value = %d, rc = %d\n", (int)attr, param, (int)sqlReturnCode);
}
else if(info[1].IsString())
{
std::string arg1 = Napi::String(env , info[1]).Utf8Value();
std::vector<char> newCString(arg1.begin(), arg1.end());
newCString.push_back('\0');
cValue = &newCString[0];
sLen = strlen(cValue);
sqlReturnCode = SQLSetConnectAttr(this->connh, attr, cValue, sLen);
DEBUG(this, "SetConnAttr() attr = %d, value = %s, return code = %d\n", (int)attr, cValue, (int)sqlReturnCode);
}
if(sqlReturnCode != SQL_SUCCESS){
this->throwErrMsg(SQL_HANDLE_DBC, env);
return env.Null();
}
return Napi::Boolean::New(env, 1);
}
/*
* DbConn::GetConnAttr
* Description: Returns the current settings for the specified connection option
* Parameters:
* const Napi::CallbackInfo& info:
* The information passed by Napi from the JavaScript call, including
* arguments from the JavaScript function. In JavaScript, the exported
* function takes one argument, stored on the info object:
* info[0] (Number): Attribute is the connection attribute to set.
* Return: The attribute option in the format of a Number or a String.
*
*/
Napi::Value DbConn::GetConnAttr(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int length = info.Length();
//validation
CHECK_WITH_RETURN(length != 1, INVALID_PARAM_NUM, "Expected One Parameter for getConnAttr", env, env.Null())
CHECK_WITH_RETURN(!info[0].IsNumber(), INVALID_PARAM_TYPE, "Expected Parameter 1 to be a Number", env, env.Null())
CHECK_WITH_RETURN(this->connAllocated == false, CONN_NOT_READY, "Need to call conn() to allocate connection on first", env, env.Null())
SQLINTEGER attr = Napi::Number(env, info[0]).Int32Value();
char buf[1024];
int retVal = 0;
SQLINTEGER sLen = 0;
void* pValue = (char*)&buf;
//Doc https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/cli/rzadpfngcona.htm
SQLRETURN sqlReturnCode = SQLGetConnectAttr(this->connh, //SQLHDBC hdbc -Connection handle
attr, //SQLINTEGER fAttr -Attribute to retrieve
pValue, //SQLPOINTER pvParam -Value for fAttr
sizeof(buf), //SQLINTEGER bLen -Max # of bytes store in pvParam
&sLen); //SQLINTEGER* sLen -Length of the output data.(if string)
if(!sLen) //If the returned value is a number.
{
pValue = &retVal;
sqlReturnCode = SQLGetConnectAttr(this->connh, attr, pValue, 0, &sLen);
DEBUG(this, "GetConnAttr() attr = %d, value = %d, return code = %d\n", (int)attr, *(int*)pValue, (int)sqlReturnCode);
if(sqlReturnCode == SQL_SUCCESS){
return Napi::Number::New(env , *(int*)pValue);
}
}
else //If the returned value is a string.
{
DEBUG(this, "GetConnAttr() attr = %d, value = %s, return code = %d\n", (int)attr, (char*)pValue, (int)sqlReturnCode);
if (sqlReturnCode == SQL_SUCCESS){
return Napi::String::New(env , buf);
}
}
if(sqlReturnCode != SQL_SUCCESS){
this->throwErrMsg(SQL_HANDLE_DBC, env);
}
return env.Null();
}
/*
* DbConn::Conn
* Description: Establishes a connection to the target database.
* Parameters:
* const Napi::CallbackInfo& info:
* The information passed by Napi from the JavaScript call, including
* arguments from the JavaScript function. In JavaScript, the exported
* function takes either 1-4 arguments, stored on the info object depending on the syntex:
* Syntex 1: conn(string Database)
* Syntex 2: conn(string Database, function Callback)
* Syntex 3: conn(string Database, string User, string Password)
* Syntex 4: conn(string Database, string User, string Password, function Callback)
*
*/
void DbConn::Conn(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int length = info.Length();
SQLRETURN sqlReturnCode;
//validation
if(this->connected == true || this->connAllocated == false){
return;
}
switch(length){
case 1: //conn(string Database)
if (!info[0].IsString()){
Napi::TypeError::New(env, "First Parameter Must be a String").ThrowAsJavaScriptException();
return;
}
break;
case 2: //conn(string Database, function Callback)
if (!( info[0].IsString() && info[1].IsFunction() ) ){
Napi::TypeError::New(env, "First Parameter Must be a String , Second Parameter must be a Function").ThrowAsJavaScriptException();
return;
}
break;
case 3: //conn(string Database, string User, string Password)
if (!( info[0].IsString() && info[1].IsString() && info[2].IsString() ) ){
Napi::TypeError::New(env, "All three Parameters Must be a String").ThrowAsJavaScriptException();
return;
}
break;
case 4: //conn(string Database, string User, string Password, function Callback)
if (!( info[0].IsString() && info[1].IsString() && info[2].IsString() && info[3].IsFunction() ) ){
Napi::TypeError::New(env, "First three Parameters Must be a String , last Parameter must be a Function").ThrowAsJavaScriptException();
return;
}
break;
default:
Napi::Error::New(env, "conn() takes either 1,2,3, or 4 parameters").ThrowAsJavaScriptException();
return;
}
SQLCHAR* datasource = strdup(Napi::String(env, info[0]).Utf8Value().c_str());
SQLCHAR* loginuser = NULL;
SQLCHAR* password = NULL;
if(length >= 3) {
loginuser = strdup(Napi::String(env, info[1]).Utf8Value().c_str());
password = strdup(Napi::String(env, info[2]).Utf8Value().c_str());
}
// Doc https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/cli/rzadpfnconn.htm
sqlReturnCode = SQLConnect(this->connh, //SQLHDBC Connection Handle
datasource, //SQLCHAR* szDSN -Name or alias name of the database
SQL_NTS, //SQLSMALLINT cbDSN -Length of contents of szDSN
loginuser, //SQLCHAR* szUID -Auth Name (UID)
SQL_NTS, //SQLSMALLINT cbUID -Length of contents of szUID
password, //SQLCHAR* szAuthStr -Auth String (password)
SQL_NTS );//SQLSMALLINT cbAuthStr Length of Contents of szAuthStr
DEBUG(this, "SQLConnect(%d): conn obj [%p] handler [%d]\n", sqlReturnCode, this, this->connh);
free(datasource);
if(length >= 3) {
free(loginuser);
free(password);
}
if(sqlReturnCode != SQL_SUCCESS) {
this->throwErrMsg(SQL_HANDLE_DBC, env);
SQLFreeConnect(this->connh);
return;
}
this->connected = true;
//when length is 2 or 4 need to Make Callbacks
if (length == 2 || length == 4) {
Napi::Function cb = info[length - 1].As<Napi::Function>();
cb.MakeCallback(env.Global(), {env.Null()});
}
}
/*
* DbConn::Disconnect
* Description: Ends the connection associated with the database connection handle.
* Parameters:
* const Napi::CallbackInfo& info:
* The information passed by Napi from the JavaScript call, including
* arguments from the JavaScript function. In JavaScript, the exported
* function takes 0 arguments.
* Returns: boolean true indicating success or throws an error.
*
*/
Napi::Value DbConn::Disconnect(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
SQLRETURN sqlReturnCode;
if(this->connected) {
SQLINTEGER auto_commit = 0;
sqlReturnCode = SQLGetConnectAttr(this->connh, SQL_ATTR_AUTOCOMMIT, &auto_commit, 0, NULL);
if (auto_commit != SQL_TRUE ) { // If Auto_Commit is disabled, Rollback all transactions before exit.
//Doc https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/cli/rzadpfnendtr.htm
sqlReturnCode = SQLEndTran(SQL_HANDLE_DBC, // SQLSMALLINT htype -Type of Handle
this->connh, //SQLHENV handle -Handle to use
SQL_ROLLBACK); //SQLSMALLINT fType -Wanted Action for the transaction
}
DEBUG(this, "SQLDisconnect: conn obj [%p] handler [%d]\n", this, this->connh);
//Doc https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/cli/rzadpfndconn.htm
sqlReturnCode = SQLDisconnect(this->connh); //SQLHDBC hdbc -Connection Handle
CHECK_WITH_RETURN(sqlReturnCode != SQL_SUCCESS, SQL_ERROR, "SQLDisconnect Failed", env,Napi::Boolean::New(env, 0))
this->connected = false;
}
return Napi::Boolean::New(env, 1);
}
/*
* DbConn::Close
* Description: Frees the connection object. DbConn::Disconnect() must be called before calling this function.
* Parameters:
* const Napi::CallbackInfo& info:
* The information passed by Napi from the JavaScript call, including
* arguments from the JavaScript function. In JavaScript, the exported
* function takes 0 arguments.
* Returns: boolean true indicating success or throws an error.
*
*/
Napi::Value DbConn::Close(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
SQLRETURN sqlReturnCode = -1;
if(this->connAllocated) {
DEBUG(this, "SQLFreeConnect: conn obj [%p] handler [%d]\n", this, this->connh);
//Doc https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/cli/rzadpfnfconn.htm
sqlReturnCode = SQLFreeConnect(this->connh); //SQLHDBC hdbc -Connection Handle
DEBUG(this, "SQLFreeConnect[%d]\n", sqlReturnCode);
CHECK_WITH_RETURN(sqlReturnCode != SQL_SUCCESS, SQL_ERROR,"SQLFreeConnect Failed", env, Napi::Boolean::New(env, 0));
this->connAllocated = false;
}
return Napi::Boolean::New(env, 1);
}
/*
* DbConn::ValidSmt
* Description: Checks if the SQL string is valid and interprets vendor escape clauses.
* Parameters:
* const Napi::CallbackInfo& info:
* The information passed by Napi from the JavaScript call, including
* arguments from the JavaScript function. In JavaScript, the exported
* function takes 1 argument.
* info[0] (String): The SQL String to validate.
* Returns: boolean true indicating success or throws an error.
*
*/
Napi::Value DbConn::ValidStmt(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int length = info.Length();
CHECK_WITH_RETURN(length != 1, INVALID_PARAM_NUM,"Expected 1st Parameter to be a String", env, env.Null())
CHECK_WITH_RETURN(this->connAllocated == false, CONN_NOT_READY,"Expected Connection to be allocated first, you can do so by using conn()", env, env.Null())
std::string argStr = Napi::String(env , info[0]).Utf8Value();
std::vector<char> newCString(argStr.begin(), argStr.end());
newCString.push_back('\0');
SQLCHAR* tmpSqlSt = &newCString[0];
SQLINTEGER outLen = 0;
SQLCHAR outSqlSt[2048];
// Doc https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/cli/rzadpfnnsql.htm
SQLRETURN sqlReturnCode = SQLNativeSql(this->connh, //SQL HDBC Connection Handle
tmpSqlSt, //SQLCHAR* InStatementText -Input SQL String
strlen(tmpSqlSt), //SQLINTEGER TextLength1 -Length of InStatementText
outSqlSt, //SQLCHAR* OutStatementText -Point to transformed String buffer
sizeof(outSqlSt), //SQLINTEGER BufferLength -Size of buffer -> OutStatementText
&outLen); //SQLINTGER* TextLength2 -number of bytes available to return in OutStatementText
if(sqlReturnCode != SQL_SUCCESS) {
this->throwErrMsg(SQL_HANDLE_DBC, env);
return env.Null();
}
if(outLen < sizeof(outSqlSt)){
outSqlSt[outLen] = '\0';
return Napi::String::New(env , outSqlSt);
}
}
/*
* DbConn::Debug
* Description: Turn on or Off Verbose Output:
* Parameters:
* const Napi::CallbackInfo& info:
* The information passed by Napi from the JavaScript call, including
* arguments from the JavaScript function. In JavaScript, the exported
* function takes 1 argument.
* info[0] (Boolean): true for ON false for OFF.
* Returns: boolean true/false indicating the state of the debug switch.
*
*/
Napi::Value DbConn::Debug(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int length = info.Length();
//validation
CHECK_WITH_RETURN(length != 1, INVALID_PARAM_NUM, "debug() Expected One Parameter", env, env.Null())
CHECK_WITH_RETURN(!info[0].IsBoolean(), INVALID_PARAM_TYPE, "debug() Expected 1st Parameter to be a Boolean ", env, env.Null())
this->isDebug = Napi::Boolean(env , info[0]).Value();
return Napi::Boolean(env , info[0]);
}
/*
* DbConn::IsConnected
* Description: Determine if the dbconn has a connection to the db.
* Parameters:
* const Napi::CallbackInfo& info:
* The information passed by Napi from the JavaScript call, including
* arguments from the JavaScript function. In JavaScript, the exported
* function takes 0 arguments.
* Returns: boolean true/false indicating if connected.
*
*/
Napi::Value DbConn::IsConnected(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Boolean::New(env , this->connected);
}