@miatechnet/node-odbc 2.4.10-multiresult.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
1
+ /*
2
+ Copyright (c) 2019, 2021 IBM
3
+ Copyright (c) 2013, Dan VerWeire<dverweire@gmail.com>
4
+ Copyright (c) 2010, Lee Smith<notwink@gmail.com>
5
+
6
+ Permission to use, copy, modify, and/or distribute this software for any
7
+ purpose with or without fee is hereby granted, provided that the above
8
+ copyright notice and this permission notice appear in all copies.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
+ */
18
+
19
+ #ifndef _SRC_ODBC_CONNECTION_H
20
+ #define _SRC_ODBC_CONNECTION_H
21
+
22
+ #include <napi.h>
23
+
24
+ class ODBCConnection : public Napi::ObjectWrap<ODBCConnection> {
25
+
26
+ // ODBCConnection AsyncWorker classes
27
+ friend class CloseAsyncWorker;
28
+ friend class CreateStatementAsyncWorker;
29
+ friend class QueryAsyncWorker;
30
+ friend class BeginTransactionAsyncWorker;
31
+ friend class EndTransactionAsyncWorker;
32
+ friend class PrimaryKeysAsyncWorker;
33
+ friend class ForeignKeysAsyncWorker;
34
+ friend class TablesAsyncWorker;
35
+ friend class ColumnsAsyncWorker;
36
+ friend class GetInfoAsyncWorker;
37
+ friend class GetAttributeAsyncWorker;
38
+ friend class CallProcedureAsyncWorker;
39
+ friend class SetIsolationLevelAsyncWorker;
40
+ friend class FetchAsyncWorker;
41
+
42
+ friend class ODBCStatement;
43
+ friend class ODBCCursor;
44
+ // ODBCStatement AsyncWorker classes
45
+ friend class PrepareAsyncWorker;
46
+ friend class BindAsyncWorker;
47
+ friend class ExecuteAsyncWorker;
48
+ friend class CloseStatementAsyncWorker;
49
+
50
+ public:
51
+
52
+ static Napi::FunctionReference constructor;
53
+ static Napi::Object Init(Napi::Env env, Napi::Object exports);
54
+
55
+ ODBCConnection(const Napi::CallbackInfo& info);
56
+ ~ODBCConnection();
57
+
58
+ private:
59
+
60
+ SQLRETURN Free();
61
+
62
+ // Functions exposed to the Node.js environment
63
+ Napi::Value Close(const Napi::CallbackInfo& info);
64
+ Napi::Value CreateStatement(const Napi::CallbackInfo& info);
65
+ Napi::Value Query(const Napi::CallbackInfo& info);
66
+ Napi::Value CallProcedure(const Napi::CallbackInfo& info);
67
+
68
+ Napi::Value BeginTransaction(const Napi::CallbackInfo& info);
69
+ Napi::Value Commit(const Napi::CallbackInfo &info);
70
+ Napi::Value Rollback(const Napi::CallbackInfo &rollback);
71
+
72
+ Napi::Value GetUsername(const Napi::CallbackInfo &info);
73
+
74
+ Napi::Value Columns(const Napi::CallbackInfo& info);
75
+ Napi::Value Tables(const Napi::CallbackInfo& info);
76
+ Napi::Value PrimaryKeys(const Napi::CallbackInfo& info);
77
+ Napi::Value ForeignKeys(const Napi::CallbackInfo& info);
78
+
79
+ Napi::Value GetConnAttr(const Napi::CallbackInfo& info);
80
+ Napi::Value SetConnAttr(const Napi::CallbackInfo& info);
81
+
82
+ Napi::Value SetIsolationLevel(const Napi::CallbackInfo &info);
83
+
84
+ // Property Getter/Setterss
85
+ Napi::Value ConnectedGetter(const Napi::CallbackInfo& info);
86
+ Napi::Value ConnectionTimeoutGetter(const Napi::CallbackInfo& info);
87
+ Napi::Value LoginTimeoutGetter(const Napi::CallbackInfo& info);
88
+ Napi::Value AutocommitGetter(const Napi::CallbackInfo& info);
89
+
90
+ Napi::Value GetInfo(const Napi::Env env, const SQLUSMALLINT option);
91
+
92
+ void ParametersToArray(Napi::Reference<Napi::Array> *napiParameters, StatementData *data, unsigned char *overwriteParameters);
93
+
94
+ bool isConnected;
95
+ bool autocommit;
96
+
97
+ int numStatements;
98
+
99
+ SQLHENV hENV;
100
+ SQLHDBC hDBC;
101
+
102
+ ConnectionOptions connectionOptions;
103
+
104
+ GetInfoResults getInfoResults;
105
+ };
106
+
107
+ Napi::Array process_data_for_napi(Napi::Env env, StatementData *data, Napi::Array napiParameters);
108
+ SQLRETURN bind_buffers(StatementData *data);
109
+ SQLRETURN prepare_for_fetch(StatementData *data);
110
+ SQLRETURN fetch_and_store(StatementData *data, bool set_position, bool *alloc_error);
111
+ SQLRETURN fetch_all_and_store(StatementData *data, bool set_position, bool *alloc_error);
112
+ SQLRETURN set_fetch_size(StatementData *data, SQLULEN fetch_size);
113
+ Napi::Value parse_query_options(Napi::Env env, Napi::Value options_value, QueryOptions *query_options);
114
+ #endif
@@ -0,0 +1,265 @@
1
+ /*
2
+ Copyright (c) 2021 IBM
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted, provided that the above
6
+ copyright notice and this permission notice appear in all copies.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+ */
16
+
17
+ #include "odbc_cursor.h"
18
+
19
+ Napi::FunctionReference ODBCCursor::constructor;
20
+
21
+ Napi::Object ODBCCursor::Init(Napi::Env env, Napi::Object exports)
22
+ {
23
+ Napi::HandleScope scope(env);
24
+
25
+ Napi::Function constructorFunction = DefineClass(env, "ODBCCursor", {
26
+ InstanceMethod("fetch", &ODBCCursor::Fetch),
27
+ InstanceMethod("close", &ODBCCursor::Close),
28
+
29
+ InstanceAccessor("noData", &ODBCCursor::MoreResultsGetter, nullptr),
30
+ });
31
+
32
+ // Attach the Database Constructor to the target object
33
+ constructor = Napi::Persistent(constructorFunction);
34
+ constructor.SuppressDestruct();
35
+
36
+ return exports;
37
+ }
38
+
39
+
40
+ ODBCCursor::ODBCCursor(const Napi::CallbackInfo& info) : Napi::ObjectWrap<ODBCCursor>(info) {
41
+ this->data = info[0].As<Napi::External<StatementData>>().Data();
42
+ this->odbcConnection = info[1].As<Napi::External<ODBCConnection>>().Data();
43
+ if (info.Length() > 2 && info[2].IsArray()) {
44
+ this->napiParametersReference = Napi::Persistent(info[2].As<Napi::Array>());
45
+ } else {
46
+ this->napiParametersReference = Napi::Persistent(Napi::Array::New(Env()));
47
+ }
48
+ this->free_statement_on_close = info[3].As<Napi::Boolean>().Value();
49
+ }
50
+
51
+ SQLRETURN ODBCCursor::Free() {
52
+
53
+ SQLRETURN return_code = SQL_SUCCESS;
54
+
55
+ if (this->free_statement_on_close && this->data)
56
+ {
57
+ if (
58
+ this->data->hstmt &&
59
+ this->data->hstmt != SQL_NULL_HANDLE
60
+ )
61
+ {
62
+ uv_mutex_lock(&ODBC::g_odbcMutex);
63
+ return_code =
64
+ SQLFreeHandle
65
+ (
66
+ SQL_HANDLE_STMT,
67
+ this->data->hstmt
68
+ );
69
+ this->data->hstmt = SQL_NULL_HANDLE;
70
+ uv_mutex_unlock(&ODBC::g_odbcMutex);
71
+ }
72
+
73
+ napiParametersReference.Reset();
74
+ delete this->data;
75
+ this->data = NULL;
76
+ }
77
+
78
+ return return_code;
79
+ }
80
+
81
+ ODBCCursor::~ODBCCursor()
82
+ {
83
+ this->Free();
84
+ }
85
+
86
+ ////////////////////////////////////////////////////////////////////////////////
87
+ // FETCH ///////////////////////////////////////////////////////////////////
88
+ ////////////////////////////////////////////////////////////////////////////////
89
+ class FetchAsyncWorker : public ODBCAsyncWorker {
90
+
91
+ private:
92
+ ODBCCursor *cursor;
93
+ StatementData *data;
94
+
95
+ public:
96
+ FetchAsyncWorker
97
+ (
98
+ ODBCCursor *cursor,
99
+ Napi::Function &callback
100
+ )
101
+ :
102
+ ODBCAsyncWorker(callback),
103
+ cursor(cursor),
104
+ data(cursor->data)
105
+ {}
106
+
107
+ ~FetchAsyncWorker() {}
108
+
109
+ void Execute() {
110
+ SQLRETURN return_code;
111
+ bool alloc_error = false;
112
+
113
+ return_code =
114
+ fetch_and_store
115
+ (
116
+ data,
117
+ true,
118
+ &alloc_error
119
+ );
120
+
121
+ if (alloc_error == true)
122
+ {
123
+ SetError("[odbc] Error allocating or reallocating memory when fetching data. No ODBC error information available.\0");
124
+ return;
125
+ }
126
+
127
+ if (!SQL_SUCCEEDED(return_code) && return_code != SQL_NO_DATA) {
128
+ if (return_code == SQL_INVALID_HANDLE) {
129
+ SetError("[odbc] Error fetching results with SQLFetch: SQL_INVALID_HANDLE\0");
130
+ return;
131
+ } else {
132
+ this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
133
+ }
134
+ SetError("[odbc] Error fetching results with SQLFetch\0");
135
+ return;
136
+ }
137
+ }
138
+
139
+ void OnOK() {
140
+
141
+ Napi::Env env = Env();
142
+ Napi::HandleScope scope(env);
143
+
144
+ Napi::Array rows =
145
+ process_data_for_napi
146
+ (
147
+ env,
148
+ data,
149
+ cursor->napiParametersReference.Value()
150
+ );
151
+
152
+ std::vector<napi_value> callbackArguments
153
+ {
154
+ env.Null(),
155
+ rows
156
+ };
157
+
158
+ Callback().Call(callbackArguments);
159
+ }
160
+ };
161
+
162
+ Napi::Value ODBCCursor::Fetch(const Napi::CallbackInfo& info) {
163
+
164
+ Napi::Env env = info.Env();
165
+ Napi::HandleScope scope(env);
166
+
167
+ Napi::Function callback = info[0].As<Napi::Function>();
168
+
169
+ FetchAsyncWorker *worker = new FetchAsyncWorker(this, callback);
170
+ worker->Queue();
171
+
172
+ return env.Undefined();
173
+ }
174
+
175
+ ////////////////////////////////////////////////////////////////////////////////
176
+ // Close ///////////////////////////////////////////////////////////////////
177
+ ////////////////////////////////////////////////////////////////////////////////
178
+ class CursorCloseAsyncWorker : public ODBCAsyncWorker {
179
+
180
+ private:
181
+ ODBCCursor *odbcCursor;
182
+ StatementData *data;
183
+
184
+ public:
185
+ CursorCloseAsyncWorker
186
+ (
187
+ ODBCCursor *cursor,
188
+ Napi::Function &callback
189
+ )
190
+ :
191
+ ODBCAsyncWorker(callback),
192
+ odbcCursor(cursor),
193
+ data(cursor->data)
194
+ {}
195
+
196
+ ~CursorCloseAsyncWorker() {}
197
+
198
+ void Execute() {
199
+
200
+ SQLRETURN return_code;
201
+
202
+ return_code =
203
+ SQLCloseCursor
204
+ (
205
+ data->hstmt // StatementHandle
206
+ );
207
+
208
+ if (!SQL_SUCCEEDED(return_code)) {
209
+ this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
210
+ SetError("[odbc] Error closing the cursor!\0");
211
+ return;
212
+ }
213
+
214
+ if (odbcCursor->free_statement_on_close)
215
+ {
216
+ return_code = odbcCursor->Free();
217
+ if (!SQL_SUCCEEDED(return_code)) {
218
+ this->errors = GetODBCErrors(SQL_HANDLE_STMT, data->hstmt);
219
+ SetError("[odbc] Error closing the Statement\0");
220
+ return;
221
+ }
222
+ }
223
+ else
224
+ {
225
+ if (data != NULL)
226
+ {
227
+ data->deleteColumns();
228
+ }
229
+ }
230
+ }
231
+
232
+ void OnOK() {
233
+
234
+ Napi::Env env = Env();
235
+ Napi::HandleScope scope(env);
236
+
237
+ std::vector<napi_value> callbackArguments;
238
+ callbackArguments.push_back(env.Null());
239
+ Callback().Call(callbackArguments);
240
+ }
241
+ };
242
+
243
+ Napi::Value ODBCCursor::Close(const Napi::CallbackInfo& info) {
244
+
245
+ Napi::Env env = info.Env();
246
+ Napi::HandleScope scope(env);
247
+
248
+ Napi::Function callback = info[0].As<Napi::Function>();
249
+
250
+ CursorCloseAsyncWorker *worker = new CursorCloseAsyncWorker(this, callback);
251
+ worker->Queue();
252
+
253
+ return env.Undefined();
254
+ }
255
+
256
+ // Return true once SQL_NO_DATA has been returned from SQLFetch. Have to do it
257
+ // this way, as a "moreResults" would have to return true if called before the
258
+ // first call to SQLFetch, but there might actually not be any more results to
259
+ // retrieve.
260
+ Napi::Value ODBCCursor::MoreResultsGetter(const Napi::CallbackInfo& info) {
261
+ Napi::Env env = info.Env();
262
+ Napi::HandleScope scope(env);
263
+
264
+ return Napi::Boolean::New(env, data->result_set_end_reached);
265
+ }
@@ -0,0 +1,52 @@
1
+ /*
2
+ Copyright (c) 2021 IBM
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted, provided that the above
6
+ copyright notice and this permission notice appear in all copies.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+ */
16
+
17
+ #ifndef _SRC_ODBC_CURSOR_H
18
+ #define _SRC_ODBC_CURSOR_H
19
+
20
+ #include <napi.h>
21
+
22
+ #include "odbc.h"
23
+ #include "odbc_connection.h"
24
+ #include "odbc_statement.h"
25
+
26
+ class ODBCCursor : public Napi::ObjectWrap<ODBCCursor>
27
+ {
28
+ friend class FetchAsyncWorker;
29
+
30
+ public:
31
+ static Napi::FunctionReference constructor;
32
+
33
+ static Napi::Object Init(Napi::Env env, Napi::Object exports);
34
+
35
+ ODBCConnection *odbcConnection;
36
+ StatementData *data;
37
+ Napi::Reference<Napi::Array> napiParametersReference;
38
+ bool free_statement_on_close;
39
+
40
+ SQLRETURN Free();
41
+
42
+ explicit ODBCCursor(const Napi::CallbackInfo& info);
43
+ ~ODBCCursor();
44
+
45
+ Napi::Value Fetch(const Napi::CallbackInfo& info);
46
+ Napi::Value Close(const Napi::CallbackInfo& info);
47
+
48
+ // Property Getter/Setters
49
+ Napi::Value MoreResultsGetter(const Napi::CallbackInfo& info);
50
+ };
51
+
52
+ #endif