This repository was archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
151 lines (130 loc) · 4.18 KB
/
index.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
const edge = require('edge-js');
const data = edge.func(__dirname + '/Data.cs');
const COMMAND_TYPES = {
QUERY: 'query',
SCALAR: 'scalar',
COMMAND: 'command',
PROCEDURE: 'procedure',
PROCEDURE_SCALAR: 'procedure_scalar'
};
const PARAMETER_DIRECTIONS = {
INPUT: 1,
OUTPUT: 2,
INPUT_OUTPUT: 3,
RETURN_VALUE: 6
};
const CONNECTION_TYPES = {
OLEDB: 'oledb',
SQL: 'sql',
ODBC: 'odbc'
};
function executePromise(constring, contype, commands) {
if (!commands)
return Promise.reject('The commands argument is required.');
if (!Array.isArray(commands))
return Promise.reject('Commands argument must be an array type.');
if (commands.length === 0)
return Promise.reject('There must be more than one transaction to execute.');
for(let i = 0; i < commands.length; i++) {
let command = commands[i];
if (!command)
return Promise.reject("Command object must be defined.")
if (command.query == null || command.query.length === 0)
return Promise.reject('Command string cannot be null or empty.');
if (command.params != null && !Array.isArray(command.params))
command.params = [command.params];
if (command.params) {
if (!Array.isArray(command.params))
return Promise.reject('Params must be an array type.');
for(let i = 0; i < command.params.length; i++) {
if (Array.isArray(command.params[i]))
return Promise.reject('Params cannot contain sub-arrays.');
}
}
else
command.params = [];
}
return new Promise((resolve, reject) => {
let options = {
constring: constring,
connection: contype,
commands: commands
};
data(options, (err, data) => {
if (err)
return reject(err);
return resolve(data);
});
});
}
class Connection {
constructor(constring, contype) {
if (constring == null || constring.trim() === '')
throw new Error('constring must not be null or empty');
if (contype == null || contype.trim() === '')
contype = CONNECTION_TYPES.OLEDB;
this.connectionString = constring;
this.connectionType = contype;
}
query(command, params) {
return executePromise(this.connectionString, this.connectionType, [
{
query: command,
params: params,
type: COMMAND_TYPES.QUERY
}
]);
}
scalar(command, params) {
return executePromise(this.connectionString, this.connectionType, [
{
query: command,
params: params,
type: COMMAND_TYPES.SCALAR
}
]);
}
execute(command, params) {
return executePromise(this.connectionString, this.connectionType, [
{
query: command,
params: params,
type: COMMAND_TYPES.COMMAND
}
]);
}
procedure(command, params) {
return executePromise(this.connectionString, this.connectionType, [
{
query: command,
params: params,
type: COMMAND_TYPES.PROCEDURE
}
]);
}
procedureScalar(command, params) {
return executePromise(this.connectionString, this.connectionType, [
{
query: command,
params: params,
type: COMMAND_TYPES.PROCEDURE_SCALAR
}
]);
}
transaction(commands) {
return executePromise(this.connectionString, this.connectionType, commands)
}
}
module.exports = {
COMMAND_TYPES: COMMAND_TYPES,
PARAMETER_DIRECTIONS: PARAMETER_DIRECTIONS,
oledbConnection(connectionString) {
return new Connection(connectionString, CONNECTION_TYPES.OLEDB);
},
odbcConnection(connectionString) {
return new Connection(connectionString, CONNECTION_TYPES.ODBC);
},
sqlConnection(connectionString) {
return new Connection(connectionString, CONNECTION_TYPES.SQL);
}
};