-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (119 loc) · 4.17 KB
/
index.js
File metadata and controls
130 lines (119 loc) · 4.17 KB
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
const spawn = require('child_process').spawn
const csvparse = require('csv-parse')
const isWin = process.platform === 'win32'
const isMac = process.platform === 'darwin'
const isLinux = process.platform === 'linux'
if (isMac) {
// Useful for Electron apps as GUI apps on macOS doesn't inherit
// the $PATH defined in your dotfiles (.bashrc/.bash_profile/.zshrc/etc).
require('fix-path')()
}
/**
* @sql - SQL Statement to execute
* @conProps - username/password@databaseName using TNS names
* @callback - callback function to pass results/error
* @bDebug - enable debug output to console
* @maxTimeout - maximum time the function is waiting for results from SQLPLus process
**/
module.exports = function (sql, connProps, callback, bDebug, maxTimeout) {
if (typeof sql !== 'string') {
callback(new Error('Please provide first argument: {string} i.e. SELECT ID, NAME FROM USERS'))
}
if (typeof connProps !== 'string') {
callback(new Error('Please specify second argument: {string} i.e. USER/PWD@TNS_NAME'))
}
debuglog(`process.platform: ${process.platform}`)
const commandString = 'sqlplus -s ' + connProps
let shellApp // default shell app
if (isWin) {
shellApp = process.env.comspec || 'cmd.exe'
} else {
shellApp = process.env.SHELL || '/bin/bash'
}
const shellAppCmdArg = isWin ? '/c' : '-c'
debuglog(`shellApp: ${shellApp}`)
debuglog(`shellAppCmdArg: ${shellAppCmdArg}`)
debuglog(`commandString: ${commandString}`)
debuglog(`sql: ${sqlWrap(sql)}`)
let output = ''
let stderr = '' // error of command itself, for example "ORA-" not included
function onOutput (data, isError) {
output += data.toString()
}
// http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
const sqlPlusProcess = spawn(shellApp, [shellAppCmdArg, commandString])
sqlPlusProcess.stdout.on('data', onOutput)
sqlPlusProcess.stderr.on('data', function (data) {
onOutput(data)
stderr += data.toString()
})
sqlPlusProcess.on('exit', finish)
sqlPlusProcess.on('error', finish)
const exitTimeout = setTimeout(finish, maxTimeout || 10000)
// pass SQL script to SQLPlus via stdin
sqlPlusProcess.stdin.write(sqlWrap(sql))
// sqlPlusProcess.stdin.end()
function finish (exitCode) {
clearTimeout(exitTimeout)
debuglog(`stderr: ${stderr}`)
let resultError = ''
let bEmpty = false
if (typeof exitCode === 'undefined') {
resultError += 'Command timed out'
sqlPlusProcess.kill('SIGKILL')
}
if (stderr) {
resultError += `STDERR ${stderr}`
}
if (output.indexOf('SP2-') === 0) {
// SP2-0158: unknown SET option "CSV"
// - means that client version is less than 12.2
resultError += output
}
if (output.indexOf('ORA-') !== -1) {
resultError += output
}
if (output.indexOf('sqlplus') === 0) {
// 'sqlplus' is not recognized as an internal or external command,
// operable program or batch file.
resultError += output
}
if (output === '') {
bEmpty = true
}
debuglog('EXITCODE: ' + exitCode)
debuglog('COMMAND OUTPUT: ' + output)
if (output !== '' && resultError === '') {
const colNamesArray = output.split(/\r\n?|\n/, 2)[1].split('"').join('').split(',')
const csvparseOpt = {
columns: colNamesArray,
skip_lines_with_empty_values: true,
from: 2 // first line is blank, second is headings
}
csvparse(output, csvparseOpt, function (parseErr, data) {
if (parseErr) {
console.log(`sqlplus result CSV parsing error: ${parseErr} OUTPUT: «${output}»`)
}
callback(parseErr || resultError, data)
})
} else {
callback(resultError, [], bEmpty)
}
}
/**
* Adding output properties for SQLPlus to ensure CSV parser will work
* @sql - SQL Statement to execute
**/
function sqlWrap (sql) {
let formatSettings = 'SET MARKUP CSV ON\n'
formatSettings += 'SET FEEDBACK OFF\n'
formatSettings += 'SET PAGESIZE 50000\n'
formatSettings += 'SET LINESIZE 32767\n'
return formatSettings + sql + ';\nexit\n'
}
function debuglog () {
if (bDebug) {
console.log.apply(console, arguments)
}
}
}