-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathodbc.js
270 lines (210 loc) · 7.04 KB
/
odbc.js
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
/*
Copyright (c) 2010, Lee Smith <[email protected]>
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.
*/
var odbc;
try {
odbc = require("./odbc_bindings");
} catch (e) {
try {
odbc = require("./build/default/odbc_bindings");
} catch (e) {
odbc = require("./build/Release/odbc_bindings");
}
}
var Database = exports.Database = function () {
var self = this;
var db = new odbc.Database();
db.executing = false;
db.connected = false;
db.queue = [];
db.__proto__ = Database.prototype;
return db;
};
Database.prototype = {
__proto__: odbc.Database.prototype,
constructor: Database,
};
Database.prototype.processQueue = function () {
var self = this;
if (!self.queue) self.queue = [];
if (self.connected && !self.executing && self.queue.length) {
var currentQuery = self.queue[0];
self.executing = true;
currentQuery.method.apply(currentQuery.context, currentQuery.args); //TODO: we need to make sure we aren't sending any extra arguments to the cpp method
}
};
Database.prototype.query = function(sql, params, callback) {
var self = this, args = [];
if (callback == null) {
callback = params; // no parameters supplied
params = null;
}
if (!self.connected) {
return callback( { message : "Connection not open." }, [], false );
}
if (!self.queue) self.queue = [];
args.push(sql);
if (params) {
args.push(params);
}
args.push(function (error, rows, morefollowing) {
//check to see if this is the last result set returned
if (!morefollowing) {
self.queue.shift();
self.executing = false;
}
if (callback) callback.apply(self, arguments);
self.processQueue();
});
self.queue.push({
context : self,
method : self.dispatchQuery,
args : args
});
self.processQueue();
};
Database.prototype.open = function(connectionString, callback) {
var self = this;
if (self.connected) {
return callback( { message : "Connection already open." }, [], false);
}
self.dispatchOpen(connectionString, function (err) {
self.connected = true;
self.processQueue();
return callback(err);
});
};
/**
*
* We must queue the close. If we don't then we may close during the middle of a query which
* could cause a segfault or other madness
*
**/
Database.prototype.close = function(callback) {
var self = this;
if (!self.queue) self.queue = [];
self.queue.push({
context : self,
method : self.dispatchClose,
args : [function (err) {
self.queue = [];
self.connected = false;
self.executing = false;
if (err && !callback) throw err;
else if (callback) callback(err)
}]
});
self.processQueue();
};
Database.prototype.tables = function(catalog, schema, table, type, callback) {
var self = this;
if (!self.queue) self.queue = [];
self.queue.push({
context : self,
method : self.dispatchTables,
catalog : (arguments.length > 1) ? catalog : "",
schema : (arguments.length > 2) ? schema : "",
table : (arguments.length > 3) ? table : "",
type : (arguments.length > 4) ? type : "",
callback : (arguments.length == 5) ? callback : arguments[arguments.length - 1],
args : arguments
});
self.processQueue();
};
Database.prototype.columns = function(catalog, schema, table, column, callback) {
var self = this;
if (!self.queue) self.queue = [];
self.queue.push({
context : self,
method : self.dispatchColumns,
catalog : (arguments.length > 1) ? catalog : "",
schema : (arguments.length > 2) ? schema : "",
table : (arguments.length > 3) ? table : "",
column : (arguments.length > 4) ? column : "",
callback : (arguments.length == 5) ? callback : arguments[arguments.length - 1],
args : arguments
});
self.processQueue();
};
Database.prototype.describe = function(obj, callback) {
var self = this;
if (typeof(callback) != "function") {
throw({
error : "[node-odbc] Missing Arguments",
message : "You must specify a callback function in order for the describe method to work."
});
return false;
}
if (typeof(obj) != "object") {
callback({
error : "[node-odbc] Missing Arguments",
message : "You must pass an object as argument 0 if you want anything productive to happen in the describe method."
}, []);
return false;
}
if (!obj.database) {
callback({
error : "[node-odbc] Missing Arguments",
message : "The object you passed did not contain a database property. This is required for the describe method to work."
}, []);
return false;
}
//set some defaults if they weren't passed
obj.schema = obj.schema || "%";
obj.type = obj.type || "table";
if (obj.table && obj.column) {
//get the column details
self.columns(obj.database, obj.schema, obj.table, obj.column, callback);
}
else if (obj.table) {
//get the columns in the table
self.columns(obj.database, obj.schema, obj.table, "%", callback);
}
else {
//get the tables in the database
self.tables(obj.database, obj.schema, null, obj.type || "table", callback);
}
};
var Pool = exports.Pool = function () {
var self = this;
self.connectionPool = {};
}
Pool.prototype.open = function (connectionString, callback) {
var self = this;
//check to see if we already have a connection for this connection string
if (self.connectionPool[connectionString] && self.connectionPool[connectionString].length) {
callback(null, self.connectionPool[connectionString].shift());
}
else {
var db = new Database();
db.realClose = db.close;
db.close = function (cb) {
//call back early, we can do the rest of this stuff after the client thinks
//that the connection is closed.
cb(null);
//close the connection for real
//this will kill any temp tables or anything that might be a security issue.
db.realClose( function () {
//re-open the connection using the connection string
db.open(connectionString, function (error) {
//add this clean connection to the connection pool
self.connectionPool[connectionString] = self.connectionPool[connectionString] || [];
self.connectionPool[connectionString].push(db);
});
});
};
db.open(connectionString, function (error) {
callback(error, db);
});
}
};