-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql
More file actions
executable file
·75 lines (62 loc) · 1.45 KB
/
sql
File metadata and controls
executable file
·75 lines (62 loc) · 1.45 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
#!/usr/bin/env node
const {parserSync} = require('shargs')
const {array, command, number, string, subcommand} = require('shargs/opts')
const {cast} = require('shargs/parser')
const csv = array(['csv'])
const group = subcommand([
string('by', ['BY'])
])
const order = subcommand([
string('by', ['BY'])
])
const opts = [
csv('select', ['SELECT']),
csv('from', ['FROM']),
string('where', ['WHERE']),
group('group', ['GROUP']),
string('having', ['HAVING']),
order('order', ['ORDER']),
number('limit', ['LIMIT'])
]
const sql = command('sql', opts)
const stages = {
opts: [
csvToArray,
cast
],
args: [
withBy('group', 'groupBy'),
withBy('order', 'orderBy')
]
}
const argv = process.argv.slice(2)
const {args} = parserSync(stages)(sql)(argv)
console.log(args)
process.exit(0)
function csvToArray ({errs, opts}) {
return {
errs,
opts: opts.map(opt => {
if (opt.values && opt.values[0] && opt.types && opt.types[0] === 'csv') {
const values = opt.values[0].split(',').map(str => str.trim())
const types = Array.from({length: values.length}, () => 'string')
return {...opt, types, values}
} else {
return opt
}
})
}
}
function withBy (from, to) {
return ({errs, args}) => {
const {[from]: group, ...rest} = args
if (group) {
if (group._) rest._ = group._
if (group.by) rest[to] = group.by
}
return {
errs,
args: rest
}
}
}