This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
200 lines (159 loc) · 4.11 KB
/
main.go
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"context"
"fmt"
"time"
"utilware/logger"
"github.com/ClarkQAQ/gpgsql"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
)
func main() {
logger.GlobalWriter().Level(logger.DefaultLevelNoColor)
logger.Info("target: %s, arch: %s, version: %s, sha256: %s",
gpgsql.ReleaseTarget(), gpgsql.ReleaseArch(), gpgsql.ReleaseVersion(), gpgsql.ReleaseSha256())
g, e := gpgsql.New()
if e != nil {
logger.Fatal("new gpgsql error: %s", e.Error())
}
g.Logger(nil)
if e := g.Username("postgres").
Password("postgres").
Data("data/"); e != nil {
logger.Fatal("add data failed: %s", e.Error())
}
if t, e := g.IsEmptyData(); e != nil {
logger.Fatal("is empty data failed: %s", e.Error())
} else if t {
if e := g.Initdb(context.Background(), &gpgsql.InitdbOptions{
Encoding: "UTF8",
Locale: "en_US.UTF-8",
AuthMethod: "password",
}); e != nil {
logger.Fatal("initdb failed: %s", e.Error())
}
}
{
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if e := g.Stop(ctx); e != nil {
logger.Fatal("stop failed: %s", e.Error())
}
}
{
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if e := g.Start(ctx, &gpgsql.PostgreSqlOptions{
Wait: 3 * time.Second,
Timeout: 5 * time.Second,
}); e != nil {
logger.Fatal("postgresql start failed: %s", e.Error())
}
logger.Info("postgresql is running")
logger.Debug("postgresql listening on %s", g.ListenAddr())
}
defer func() {
if e := g.Stop(context.Background()); e != nil {
logger.Fatal("postgresql stop failed: %s", e.Error())
}
logger.Info("postgresql stop success")
}()
{
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel()
if e := PgTest(ctx, g); e != nil {
logger.Fatal("postgresql pg test failed: %s", e.Error())
}
}
{
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
defer cancel()
if e := SqlTest(ctx, g); e != nil {
logger.Fatal("postgresql sql test failed: %s", e.Error())
}
}
}
func SqlTest(ctx context.Context, g *gpgsql.GpgsqlRuntime) error {
db, e := g.DB(g.GetUsername())
if e != nil {
return e
}
row := db.QueryRow(`
SELECT
"user_model"."id",
"user_model"."name"
FROM "user_models" AS "user_model"
WHERE (id = 1)
`)
user := &UserModel{}
if e := row.Scan(&user.Id, &user.Name); e != nil {
return e
}
logger.Info("sql user: %s", user)
return nil
}
type UserModel struct {
Id int64
Name string
Emails []string
}
func (u UserModel) String() string {
return fmt.Sprintf("User<%d %s %v>", u.Id, u.Name, u.Emails)
}
func PgTest(ctx context.Context, g *gpgsql.GpgsqlRuntime) error {
db := pg.Connect(&pg.Options{
Network: "tcp",
Addr: g.ListenAddr(),
User: g.GetUsername(),
Password: g.GetPassword(),
Database: g.GetUsername(),
})
defer db.Close()
db.AddQueryHook(&DebugHook{Verbose: true, EmptyLine: true})
if e := db.Model(&UserModel{}).CreateTable(&orm.CreateTableOptions{
IfNotExists: true,
}); e != nil {
return e
}
if _, e := db.Model(&UserModel{
Name: "admin",
Emails: []string{"admin@localhost", "admin@localhost"},
}).Where("id = ?", 1).SelectOrInsert(); e != nil {
return e
}
if e := db.Model(&UserModel{}).Where("id > ?", 0).ForEach(func(u *UserModel) error {
logger.Info("pg user: %s", u)
return nil
}); e != nil {
return e
}
return nil
}
type DebugHook struct {
Verbose bool
EmptyLine bool
}
func (h *DebugHook) BeforeQuery(ctx context.Context, evt *pg.QueryEvent) (context.Context, error) {
return ctx, nil
}
func (h *DebugHook) AfterQuery(ctx context.Context, evt *pg.QueryEvent) error {
q, e := evt.FormattedQuery()
if e != nil {
return e
}
if evt.Err != nil {
logger.Printf("[pgdebug] %s\r\n[%s] %s", time.Since(evt.StartTime), q, evt.Err)
} else if h.Verbose {
if evt.Result != nil {
logger.Printf("[pgdebug] %s aff: %d ret: %d\r\n%s",
time.Since(evt.StartTime), evt.Result.RowsAffected(),
evt.Result.RowsReturned(), q)
} else {
logger.Printf("[pgdebug] %s\r\n%s", time.Since(evt.StartTime), q)
}
}
if h.EmptyLine {
fmt.Println()
}
return nil
}