2019-03-24 14:57:29 +01:00
|
|
|
package psql
|
|
|
|
|
|
|
|
import (
|
2020-04-10 08:27:43 +02:00
|
|
|
"database/sql"
|
2019-03-24 14:57:29 +01:00
|
|
|
"fmt"
|
2019-12-02 16:52:22 +01:00
|
|
|
"strconv"
|
2019-03-24 14:57:29 +01:00
|
|
|
"strings"
|
|
|
|
|
2019-09-30 07:48:29 +02:00
|
|
|
"github.com/jackc/pgtype"
|
2019-03-24 14:57:29 +01:00
|
|
|
)
|
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
type DBInfo struct {
|
2020-04-23 02:51:14 +02:00
|
|
|
Version int
|
|
|
|
Tables []DBTable
|
|
|
|
Columns [][]DBColumn
|
|
|
|
Functions []DBFunction
|
2020-04-23 07:42:16 +02:00
|
|
|
colMap map[string]map[string]*DBColumn
|
2019-12-09 07:48:18 +01:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
func GetDBInfo(db *sql.DB) (*DBInfo, error) {
|
2019-12-09 07:48:18 +01:00
|
|
|
di := &DBInfo{}
|
|
|
|
var version string
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
err := db.QueryRow(`SHOW server_version_num`).Scan(&version)
|
2019-12-09 07:48:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error fetching version: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
di.Version, err = strconv.Atoi(version)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
di.Tables, err = GetTables(db)
|
2019-12-09 07:48:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:42:16 +02:00
|
|
|
for _, t := range di.Tables {
|
2020-04-10 08:27:43 +02:00
|
|
|
cols, err := GetColumns(db, "public", t.Name)
|
2019-12-09 07:48:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
di.Columns = append(di.Columns, cols)
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:42:16 +02:00
|
|
|
di.colMap = newColMap(di.Tables, di.Columns)
|
|
|
|
|
2020-04-23 02:51:14 +02:00
|
|
|
di.Functions, err = GetFunctions(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
return di, nil
|
|
|
|
}
|
|
|
|
|
2020-04-23 07:42:16 +02:00
|
|
|
func newColMap(tables []DBTable, columns [][]DBColumn) map[string]map[string]*DBColumn {
|
|
|
|
cm := make(map[string]map[string]*DBColumn, len(tables))
|
|
|
|
|
|
|
|
for i, t := range tables {
|
|
|
|
cols := columns[i]
|
|
|
|
cm[t.Key] = make(map[string]*DBColumn, len(cols))
|
|
|
|
|
|
|
|
for n, c := range cols {
|
|
|
|
cm[t.Key][c.Key] = &columns[i][n]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cm
|
|
|
|
}
|
|
|
|
|
2020-01-28 06:26:53 +01:00
|
|
|
func (di *DBInfo) AddTable(t DBTable, cols []DBColumn) {
|
|
|
|
t.ID = di.Tables[len(di.Tables)-1].ID
|
|
|
|
|
|
|
|
di.Tables = append(di.Tables, t)
|
2020-04-23 07:42:16 +02:00
|
|
|
di.colMap[t.Key] = make(map[string]*DBColumn, len(cols))
|
2020-01-28 06:26:53 +01:00
|
|
|
|
|
|
|
for i := range cols {
|
|
|
|
cols[i].ID = int16(i)
|
|
|
|
c := &cols[i]
|
2020-04-23 07:42:16 +02:00
|
|
|
di.colMap[t.Key][c.Key] = c
|
2020-01-28 06:26:53 +01:00
|
|
|
}
|
|
|
|
di.Columns = append(di.Columns, cols)
|
|
|
|
}
|
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
func (di *DBInfo) GetColumn(table, column string) (*DBColumn, bool) {
|
2020-04-23 07:42:16 +02:00
|
|
|
v, ok := di.colMap[strings.ToLower(table)][strings.ToLower(column)]
|
2019-12-09 07:48:18 +01:00
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
type DBTable struct {
|
2019-12-09 07:48:18 +01:00
|
|
|
ID int
|
2019-09-26 06:35:31 +02:00
|
|
|
Name string
|
2019-12-09 07:48:18 +01:00
|
|
|
Key string
|
2019-09-26 06:35:31 +02:00
|
|
|
Type string
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
func GetTables(db *sql.DB) ([]DBTable, error) {
|
2019-06-14 06:32:15 +02:00
|
|
|
sqlStmt := `
|
2019-09-29 21:20:59 +02:00
|
|
|
SELECT
|
|
|
|
c.relname as "name",
|
|
|
|
CASE c.relkind WHEN 'r' THEN 'table'
|
2019-09-26 06:35:31 +02:00
|
|
|
WHEN 'v' THEN 'view'
|
|
|
|
WHEN 'm' THEN 'materialized view'
|
|
|
|
WHEN 'f' THEN 'foreign table'
|
|
|
|
END as "type"
|
2019-06-14 06:32:15 +02:00
|
|
|
FROM pg_catalog.pg_class c
|
2019-09-29 21:20:59 +02:00
|
|
|
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
2019-06-14 06:32:15 +02:00
|
|
|
WHERE c.relkind IN ('r','v','m','f','')
|
2019-10-01 06:00:15 +02:00
|
|
|
AND n.nspname <> ('pg_catalog')
|
|
|
|
AND n.nspname <> ('information_schema')
|
|
|
|
AND n.nspname !~ ('^pg_toast')
|
2019-09-29 21:20:59 +02:00
|
|
|
AND pg_catalog.pg_table_is_visible(c.oid);`
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
var tables []DBTable
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
rows, err := db.Query(sqlStmt)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error fetching tables: %s", err)
|
|
|
|
}
|
2019-09-26 06:35:31 +02:00
|
|
|
defer rows.Close()
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
for i := 0; rows.Next(); i++ {
|
|
|
|
t := DBTable{ID: i}
|
2019-09-26 06:35:31 +02:00
|
|
|
err = rows.Scan(&t.Name, &t.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-09 07:48:18 +01:00
|
|
|
t.Key = strings.ToLower(t.Name)
|
2019-12-25 07:24:30 +01:00
|
|
|
if t.Key != "schema_migrations" && t.Key != "ar_internal_metadata" {
|
|
|
|
tables = append(tables, t)
|
|
|
|
}
|
2019-09-26 06:35:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return tables, nil
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type DBColumn struct {
|
2019-10-01 06:00:15 +02:00
|
|
|
ID int16
|
2019-09-26 06:35:31 +02:00
|
|
|
Name string
|
2019-12-09 07:48:18 +01:00
|
|
|
Key string
|
2019-09-26 06:35:31 +02:00
|
|
|
Type string
|
2019-12-09 07:48:18 +01:00
|
|
|
Array bool
|
2019-09-26 06:35:31 +02:00
|
|
|
NotNull bool
|
|
|
|
PrimaryKey bool
|
|
|
|
UniqueKey bool
|
|
|
|
FKeyTable string
|
2019-10-01 06:00:15 +02:00
|
|
|
FKeyColID []int16
|
2019-09-26 06:35:31 +02:00
|
|
|
fKeyColID pgtype.Int2Array
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
func GetColumns(db *sql.DB, schema, table string) ([]DBColumn, error) {
|
2019-06-14 06:32:15 +02:00
|
|
|
sqlStmt := `
|
2019-09-29 21:20:59 +02:00
|
|
|
SELECT
|
|
|
|
f.attnum AS id,
|
|
|
|
f.attname AS name,
|
|
|
|
f.attnotnull AS notnull,
|
|
|
|
pg_catalog.format_type(f.atttypid,f.atttypmod) AS type,
|
2019-12-09 07:48:18 +01:00
|
|
|
CASE
|
|
|
|
WHEN f.attndims != 0 THEN true
|
2020-02-03 07:21:07 +01:00
|
|
|
WHEN right(pg_catalog.format_type(f.atttypid,f.atttypmod), 2) = '[]' THEN true
|
2019-12-09 07:48:18 +01:00
|
|
|
ELSE false
|
|
|
|
END AS array,
|
2019-09-29 21:20:59 +02:00
|
|
|
CASE
|
|
|
|
WHEN p.contype = ('p'::char) THEN true
|
|
|
|
ELSE false
|
|
|
|
END AS primarykey,
|
|
|
|
CASE
|
|
|
|
WHEN p.contype = ('u'::char) THEN true
|
|
|
|
ELSE false
|
|
|
|
END AS uniquekey,
|
|
|
|
CASE
|
|
|
|
WHEN p.contype = ('f'::char) THEN g.relname
|
|
|
|
ELSE ''::text
|
|
|
|
END AS foreignkey,
|
|
|
|
CASE
|
2020-02-07 07:12:14 +01:00
|
|
|
WHEN p.contype = ('f'::char) THEN p.confkey::int2[]
|
2019-09-29 21:20:59 +02:00
|
|
|
ELSE ARRAY[]::int2[]
|
|
|
|
END AS foreignkey_fieldnum
|
|
|
|
FROM pg_attribute f
|
|
|
|
JOIN pg_class c ON c.oid = f.attrelid
|
|
|
|
LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum
|
|
|
|
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
|
|
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)
|
|
|
|
LEFT JOIN pg_class AS g ON p.confrelid = g.oid
|
2020-02-03 07:21:07 +01:00
|
|
|
WHERE c.relkind IN ('r', 'v', 'm', 'f')
|
2019-09-29 21:20:59 +02:00
|
|
|
AND n.nspname = $1 -- Replace with Schema name
|
|
|
|
AND c.relname = $2 -- Replace with table name
|
|
|
|
AND f.attnum > 0
|
|
|
|
AND f.attisdropped = false
|
|
|
|
ORDER BY id;`
|
2019-09-26 06:35:31 +02:00
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
rows, err := db.Query(sqlStmt, schema, table)
|
2019-06-14 06:32:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error fetching columns: %s", err)
|
|
|
|
}
|
2019-09-26 06:35:31 +02:00
|
|
|
defer rows.Close()
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
cmap := make(map[int16]DBColumn)
|
2019-09-29 21:20:59 +02:00
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
for rows.Next() {
|
|
|
|
c := DBColumn{}
|
2019-12-12 06:47:56 +01:00
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
err = rows.Scan(&c.ID, &c.Name, &c.NotNull, &c.Type, &c.Array, &c.PrimaryKey, &c.UniqueKey, &c.FKeyTable, &c.fKeyColID)
|
2019-09-26 06:35:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-29 21:20:59 +02:00
|
|
|
|
|
|
|
if v, ok := cmap[c.ID]; ok {
|
|
|
|
if c.PrimaryKey {
|
|
|
|
v.PrimaryKey = true
|
2019-12-25 07:24:30 +01:00
|
|
|
v.UniqueKey = true
|
2019-09-29 21:20:59 +02:00
|
|
|
}
|
|
|
|
if c.NotNull {
|
|
|
|
v.NotNull = true
|
|
|
|
}
|
|
|
|
if c.UniqueKey {
|
|
|
|
v.UniqueKey = true
|
|
|
|
}
|
2019-12-09 07:48:18 +01:00
|
|
|
if c.Array {
|
|
|
|
v.Array = true
|
|
|
|
}
|
2019-12-12 06:47:56 +01:00
|
|
|
if len(c.FKeyTable) != 0 {
|
|
|
|
v.FKeyTable = c.FKeyTable
|
|
|
|
}
|
|
|
|
if c.fKeyColID.Elements != nil {
|
|
|
|
v.fKeyColID = c.fKeyColID
|
|
|
|
err := v.fKeyColID.AssignTo(&v.FKeyColID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmap[c.ID] = v
|
2019-09-29 21:20:59 +02:00
|
|
|
} else {
|
2019-10-01 06:00:15 +02:00
|
|
|
err := c.fKeyColID.AssignTo(&c.FKeyColID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-09 07:48:18 +01:00
|
|
|
c.Key = strings.ToLower(c.Name)
|
2019-12-25 07:24:30 +01:00
|
|
|
if c.PrimaryKey {
|
|
|
|
c.UniqueKey = true
|
|
|
|
}
|
2019-12-09 07:48:18 +01:00
|
|
|
cmap[c.ID] = c
|
2019-09-29 21:20:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
cols := make([]DBColumn, 0, len(cmap))
|
2019-12-12 06:47:56 +01:00
|
|
|
for i := range cmap {
|
|
|
|
cols = append(cols, cmap[i])
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 06:35:31 +02:00
|
|
|
return cols, nil
|
2019-06-14 06:32:15 +02:00
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
|
2020-04-23 02:51:14 +02:00
|
|
|
type DBFunction struct {
|
|
|
|
Name string
|
|
|
|
Params []DBFuncParam
|
|
|
|
}
|
|
|
|
|
|
|
|
type DBFuncParam struct {
|
|
|
|
ID int
|
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetFunctions(db *sql.DB) ([]DBFunction, error) {
|
|
|
|
sqlStmt := `
|
|
|
|
SELECT
|
|
|
|
routines.routine_name,
|
|
|
|
parameters.specific_name,
|
|
|
|
parameters.data_type,
|
|
|
|
parameters.parameter_name,
|
|
|
|
parameters.ordinal_position
|
|
|
|
FROM
|
|
|
|
information_schema.routines
|
|
|
|
RIGHT JOIN
|
|
|
|
information_schema.parameters
|
|
|
|
ON (routines.specific_name = parameters.specific_name and parameters.ordinal_position IS NOT NULL)
|
|
|
|
WHERE
|
|
|
|
routines.specific_schema = 'public'
|
|
|
|
ORDER BY
|
|
|
|
routines.routine_name, parameters.ordinal_position;`
|
|
|
|
|
|
|
|
rows, err := db.Query(sqlStmt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error fetching functions: %s", err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var funcs []DBFunction
|
|
|
|
fm := make(map[string]int)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var fn, fid string
|
|
|
|
fp := DBFuncParam{}
|
|
|
|
|
|
|
|
err = rows.Scan(&fn, &fid, &fp.Type, &fp.Name, &fp.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := fm[fid]; ok {
|
|
|
|
funcs[i].Params = append(funcs[i].Params, fp)
|
|
|
|
} else {
|
|
|
|
funcs = append(funcs, DBFunction{Name: fn, Params: []DBFuncParam{fp}})
|
|
|
|
fm[fid] = len(funcs) - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return funcs, nil
|
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
// func GetValType(type string) qcode.ValType {
|
|
|
|
// switch {
|
|
|
|
// case "bigint", "integer", "smallint", "numeric", "bigserial":
|
|
|
|
// return qcode.ValInt
|
|
|
|
// case "double precision", "real":
|
|
|
|
// return qcode.ValFloat
|
|
|
|
// case ""
|
|
|
|
// }
|
|
|
|
// }
|