forked from polothy/pg2mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.go
90 lines (73 loc) · 1.64 KB
/
postgres.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
package pg2mysql
import (
"database/sql"
"fmt"
_ "github.com/lib/pq" // register postgres driver
)
func NewPostgreSQLDB(
database string,
username string,
password string,
host string,
port int,
sslMode string,
) DB {
dsn := fmt.Sprintf("dbname=%s host=%s port=%d sslmode=%s", database, host, port, sslMode)
if username != "" {
dsn = fmt.Sprintf("%s user=%s", dsn, username)
}
if password != "" {
dsn = fmt.Sprintf("%s password=%s", dsn, password)
}
return &postgreSQLDB{
dsn: dsn,
dbName: database,
}
}
type postgreSQLDB struct {
dbName string
db *sql.DB
dsn string
}
func (p *postgreSQLDB) Open() error {
db, err := sql.Open("postgres", p.dsn)
if err != nil {
return err
}
p.db = db
return nil
}
func (p *postgreSQLDB) Close() error {
return p.db.Close()
}
func (p *postgreSQLDB) GetSchemaRows() (*sql.Rows, error) {
stmt := `
SELECT t1.table_name,
t1.column_name,
t1.data_type,
t1.character_maximum_length
FROM information_schema.columns t1
JOIN information_schema.tables t2
ON t2.table_name = t1.table_name
AND t2.table_type = 'BASE TABLE'
WHERE t1.table_schema = 'public'
AND t1.table_name NOT IN ('schema_migrations')
AND t1.table_catalog = $1`
rows, err := p.db.Query(stmt, p.dbName)
if err != nil {
return nil, err
}
return rows, nil
}
func (p *postgreSQLDB) DB() *sql.DB {
return p.db
}
func (p *postgreSQLDB) ColumnNameForSelect(name string) string {
return name
}
func (p *postgreSQLDB) EnableConstraints() error {
panic("not implemented")
}
func (p *postgreSQLDB) DisableConstraints() error {
panic("not implemented")
}