forked from magomi/node-oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoracle.js
70 lines (60 loc) · 1.87 KB
/
oracle.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
var bindings = require("../build/Release/oracle_bindings");
var oracle = new bindings.OracleClient();
function getSettings(settings) {
settings = settings || {
hostname: '127.0.0.1',
database: 'XE'
};
settings.hostname = settings.hostname || settings.host;
settings.user = settings.user || settings.username;
return settings;
}
exports.connect = function(settings, callback) {
settings = getSettings(settings);
oracle.connect(settings, callback);
}
exports.connectSync = function(settings) {
settings = getSettings(settings);
return oracle.connectSync(settings);
}
exports.OutParam = bindings.OutParam;
exports.OCCIINT = 0;
exports.OCCISTRING = 1;
exports.OCCIDOUBLE = 2;
exports.OCCIFLOAT = 3;
exports.OCCICURSOR = 4;
exports.OCCICLOB = 5;
exports.OCCIDATE = 6;
exports.OCCITIMESTAMP = 7;
exports.OCCINUMBER = 8;
exports.OCCIBLOB = 9;
// Reader is implemented in JS around a C++ handle
// This is easier and also more efficient because we don't cross the JS/C++ boundary
// every time we read a record.
function Reader(handle) {
this._handle = handle;
this._error = null;
this._rows = [];
}
Reader.prototype.nextRows = function() {
this._handle.nextRows.apply(this._handle, arguments);
}
Reader.prototype.nextRow = function(cb) {
var self = this;
if (!self._handle || self._error || (self._rows && self._rows.length > 0)) {
process.nextTick(function() {
cb(self._error, self._rows && self._rows.shift());
});
} else {
// nextRows willl use the prefetch row count as window size
self._handle.nextRows(function(err, result) {
self._error = err || self._error;
self._rows = result;
if (err || result.length === 0) self._handle = null;
cb(self._error, self._rows && self._rows.shift());
});
}
};
bindings.Connection.prototype.reader = function(sql, args) {
return new Reader(this.readerHandle(sql, args));
}