forked from pen-lang/pen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.pen
81 lines (68 loc) · 1.46 KB
/
main.pen
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
import Core'Number
import Core'String
import Os'Context as Os
import Os'Environment
import Os'File
import Os'Process
import Sql'Pool
import Sql'Pool'Options { Options }
import Sql'Value { Value }
import 'arguments { Arguments }
import 'command
main = \(ctx context) none {
if e = run(ctx) as none {
none
} else {
_ = File'Write(
ctx.Os,
File'StdErr(),
if s = source(e) as string { s } else { "unknown error" } + "\n",
)
Process'Exit(ctx.Os, 1)
}
}
run = \(ctx context) none | error {
args = arguments'Parse(Environment'Arguments(ctx.Os))?
p = Pool'New(
ctx.Sql,
args.Uri,
Options{
MinConnections: 1,
MaxConnections: 1,
ConnectTimeout: 1000,
},
)?
if _ = args.Command as command'Query {
xss = Pool'Query(ctx.Sql, p, args.Query, [Value])?
print(
ctx.Os,
String'Join(
[string
String'Join([string valueString(x()) for x in xs()], ", ")
for xs in xss
],
"\n",
),
)?
} else if command'Execute {
n = Pool'Execute(ctx.Sql, p, args.Query, [Value])?
print(ctx.Os, Number'String(n))?
}
print(ctx.Os, "\n")?
none
}
valueString = \(v Value) string {
if v = v as boolean {
if v { "true" } else { "false" }
} else if number {
Number'String(v)
} else if string {
"'" + v + "'"
} else if none {
"null"
}
}
print = \(ctx Os'Context, s string) none | error {
File'Write(ctx, File'StdOut(), s)?
none
}