forked from segment-boneyard/source-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
100 lines (80 loc) · 2.65 KB
/
driver.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
package main
import (
"bytes"
"fmt"
"strings"
"github.com/Sirupsen/logrus"
_ "github.com/jackc/pgx/stdlib"
"github.com/jmoiron/sqlx"
"github.com/segment-sources/sqlsource/domain"
)
type tableDescriptionRow struct {
Catalog string `db:"table_catalog"`
SchemaName string `db:"table_schema"`
TableName string `db:"table_name"`
ColumnName string `db:"column_name"`
IsPrimary bool `db:"is_primary_key"`
}
type Postgres struct {
Connection *sqlx.DB
}
func (p *Postgres) Init(c *domain.Config) error {
var extraOptions bytes.Buffer
if len(c.ExtraOptions) > 0 {
extraOptions.WriteRune('?')
extraOptions.WriteString(strings.Join(c.ExtraOptions, "&"))
}
connectionString := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s%s",
c.Username, c.Password, c.Hostname, c.Port, c.Database, extraOptions.String(),
)
db, err := sqlx.Connect("pgx", connectionString)
if err != nil {
return err
}
p.Connection = db
return nil
}
func (p *Postgres) Scan(t *domain.Table) (*sqlx.Rows, error) {
query := fmt.Sprintf("SELECT %s FROM %q.%q", t.ColumnToSQL(), t.SchemaName, t.TableName)
logrus.Debugf("Executing query: %v", query)
return p.Connection.Queryx(query)
}
func (p *Postgres) Transform(row map[string]interface{}) map[string]interface{} {
return row
}
func (p *Postgres) Describe() (*domain.Description, error) {
describeQuery := `
with o_1 as (SELECT
_s.nspname AS table_schema,
_t.relname AS table_name,
c.conkey AS column_positions
FROM pg_catalog.pg_constraint c
LEFT JOIN pg_catalog.pg_class _t ON c.conrelid = _t.oid
LEFT JOIN pg_catalog.pg_class referenced_table ON c.confrelid = referenced_table.oid
LEFT JOIN pg_catalog.pg_namespace _s ON _t.relnamespace = _s.oid
LEFT JOIN pg_catalog.pg_namespace referenced_schema ON referenced_table.relnamespace = referenced_schema.oid
WHERE c.contype = 'p')
select c.table_catalog, c.table_schema, c.table_name, c.column_name, CASE WHEN c.ordinal_position = ANY(o_1.column_positions) THEN true ELSE false END as "is_primary_key"
FROM o_1 LEFT JOIN information_schema.columns c
ON o_1.table_schema = c.table_schema
AND o_1.table_name = c.table_name;
`
res := domain.NewDescription()
rows, err := p.Connection.Queryx(describeQuery)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
row := &tableDescriptionRow{}
if err := rows.StructScan(row); err != nil {
return nil, err
}
res.AddColumn(&domain.Column{Name: row.ColumnName, Schema: row.SchemaName, Table: row.TableName, IsPrimaryKey: row.IsPrimary})
}
if err := rows.Err(); err != nil {
return nil, err
}
return res, nil
}