-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb.go
53 lines (46 loc) · 988 Bytes
/
db.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
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
type column struct {
Name string
Type string
Default *string
IsNull bool
IsArray bool
Category string
}
func dbConnect(opts Options) (*sql.DB, error) {
db, err := sql.Open("postgres",
fmt.Sprintf(
"postgresql://%s:%s@%s:%d/%s?connect_timeout=10&application_name=gogen&sslmode=%s",
opts.User,
opts.Password,
opts.Host,
opts.Port,
opts.Database,
opts.SSLMode,
),
)
if err != nil {
return nil, err
}
return db, nil
}
func columnList(db *sql.DB, tab string) (cols []*column, err error) {
rows, err := db.Query(ColumnsQuery, tab)
if err != nil {
return nil, fmt.Errorf("unable to query data: %s", err)
}
for rows.Next() {
c := &column{}
err := rows.Scan(&c.Name, &c.Type, &c.Default, &c.IsNull, &c.IsArray, &c.Category)
if err != nil {
return nil, fmt.Errorf("unable to scan columns data: %s", err)
}
cols = append(cols, c)
}
return cols, nil
}