FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
csv-sql/src/csv-sql.js at master · DavidTimms/csv-sql · GitHub
DavidTimms
/
csv-sql
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
4
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
csv-sql
/
src
/
csv-sql.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
161 lines (130 loc) · 4.39 KB
Breadcrumbs
csv-sql
/
src
/
csv-sql.js
Copy path
File metadata and controls
161 lines (130 loc) · 4.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
fs
from
'fs'
;
import
csv
from
'csv'
;
import
commander
from
'commander'
;
import
project
from
'../package.json'
;
import
{
preStringify
,
logStream
}
from
'./utils'
;
import
{
parseQuery
}
from
'./parser'
;
import
{
identifyAggregatesInQuery
}
from
'./aggregates'
;
import
{
performSelect
}
from
'./select'
;
import
{
performFilter
}
from
'./where'
;
import
{
GroupingStream
}
from
'./group-by'
;
import
{
OrderingStream
}
from
'./order-by'
;
import
{
performOffset
}
from
'./offset'
;
import
{
performLimit
}
from
'./limit'
;
export
function
performQuery
(
queryString
,
options
)
{
const
query
=
identifyAggregatesInQuery
(
parseQuery
(
queryString
)
)
;
//console.log(JSON.stringify(query, null, 4));
let
tableReadStream
;
if
(
query
.
from
)
{
if
(
!
fs
.
existsSync
(
query
.
from
)
)
{
throw
Error
(
`file not found: "
${
query
.
from
}
"`
)
;
}
tableReadStream
=
createEndableReadStream
(
query
.
from
)
;
}
else
{
tableReadStream
=
process
.
stdin
;
}
let
resultStream
=
tableReadStream
.
pipe
(
csv
.
parse
(
{
columns
:
true
,
delimiter
:
options
.
inSeparator
,
relax_column_count
:
true
,
}
)
)
.
pipe
(
csv
.
transform
(
performFilter
(
query
.
where
)
)
)
;
if
(
query
.
aggregates
.
length
>
0
||
query
.
groupBy
)
{
resultStream
=
resultStream
.
pipe
(
new
GroupingStream
(
query
)
)
.
pipe
(
csv
.
transform
(
performFilter
(
query
.
having
)
)
)
;
}
if
(
query
.
orderBy
)
{
resultStream
=
resultStream
.
pipe
(
new
OrderingStream
(
query
)
)
;
}
resultStream
=
resultStream
.
pipe
(
csv
.
transform
(
performOffset
(
query
)
)
)
.
pipe
(
csv
.
transform
(
performLimit
(
query
,
{
onLimitReached
:
(
)
=>
null
}
)
)
)
.
pipe
(
csv
.
transform
(
performSelect
(
query
)
)
)
;
return
resultStream
;
}
function
createEndableReadStream
(
filePath
)
{
const
fileDescriptor
=
fs
.
openSync
(
filePath
,
'r'
)
;
const
readStream
=
fs
.
createReadStream
(
null
,
{
fd
:
fileDescriptor
}
)
;
let
isStreamActive
=
true
;
readStream
.
on
(
'end'
,
(
)
=>
{
isStreamActive
=
false
;
}
)
;
// performLimit will call this function once it has been satisifed,
// to avoid processing the rest of the file
readStream
.
end
=
(
)
=>
{
return
;
if
(
isStreamActive
)
{
fs
.
closeSync
(
fileDescriptor
)
;
readStream
.
destroy
(
)
;
}
}
return
readStream
;
}
export
function
toCSV
(
rowStream
,
options
)
{
return
rowStream
.
pipe
(
preStringify
(
)
)
.
pipe
(
csv
.
stringify
(
{
header
:
options
.
header
,
delimiter
:
options
.
outSeparator
,
}
)
)
;
}
function
startRepl
(
options
)
{
const
repl
=
require
(
'repl'
)
;
const
replHistory
=
require
(
'repl.history'
)
;
const
sqlRepl
=
repl
.
start
(
{
eval
:
function
_eval
(
queryString
,
context
,
filename
,
callback
)
{
if
(
queryString
.
toLowerCase
(
)
.
trim
(
)
===
'exit'
)
{
process
.
exit
(
)
;
}
// skip empty lines
if
(
queryString
.
match
(
/
^
\s
*
$
/
)
)
{
callback
(
null
,
undefined
)
;
return
;
}
const
resultStream
=
performQuery
(
queryString
,
options
)
;
toCSV
(
resultStream
,
options
)
.
pipe
(
process
.
stdout
)
;
resultStream
.
on
(
'end'
,
(
)
=>
{
callback
(
null
,
undefined
)
;
}
)
;
}
,
ignoreUndefined
:
true
,
}
)
;
replHistory
(
sqlRepl
,
`
${
__dirname
}
/.repl_history`
)
;
}
export
function
cli
(
)
{
commander
.
version
(
project
.
version
)
.
description
(
project
.
description
)
.
arguments
(
'<query>'
)
.
option
(
'-s, --separator [string]'
,
'The CSV column separator for input and output'
)
.
option
(
'--in-separator [string]'
,
'The CSV column separator for reading input'
)
.
option
(
'--out-separator [string]'
,
'The CSV column separator for generating output'
)
.
option
(
'--no-header'
,
'Do not include a header row in the output'
)
.
parse
(
process
.
argv
)
;
const
options
=
commander
;
const
query
=
options
.
args
[
0
]
;
options
.
inSeparator
=
options
.
inSeparator
||
options
.
separator
;
options
.
outSeparator
=
options
.
outSeparator
||
options
.
separator
;
if
(
query
)
{
toCSV
(
performQuery
(
query
,
options
)
,
options
)
.
pipe
(
process
.
stdout
)
;
}
else
{
// Start a REPL if no arguments have been provided
startRepl
(
options
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL