Fix the sql that postgres driver uses
This commit is contained in:
parent
44cff02828
commit
b37c894925
1 changed files with 28 additions and 31 deletions
|
@ -4,8 +4,6 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
// Import the postgres driver
|
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -52,9 +50,10 @@ func (p *PostgresDriver) Close() {
|
||||||
func (p *PostgresDriver) TableNames() ([]string, error) {
|
func (p *PostgresDriver) TableNames() ([]string, error) {
|
||||||
var names []string
|
var names []string
|
||||||
|
|
||||||
rows, err := p.dbConn.Query(`select table_name from
|
rows, err := p.dbConn.Query(`
|
||||||
information_schema.tables where table_schema='public'
|
select table_name from information_schema.tables
|
||||||
and table_name <> 'gorp_migrations'`)
|
where table_schema='public' and table_name not like '%migrations%'
|
||||||
|
`)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -80,8 +79,8 @@ func (p *PostgresDriver) Columns(tableName string) ([]Column, error) {
|
||||||
var columns []Column
|
var columns []Column
|
||||||
|
|
||||||
rows, err := p.dbConn.Query(`
|
rows, err := p.dbConn.Query(`
|
||||||
SELECT column_name, data_type, column_default, is_nullable from
|
select column_name, data_type, column_default, is_nullable
|
||||||
information_schema.columns WHERE table_name=$1
|
from information_schema.columns where table_name=$1
|
||||||
`, tableName)
|
`, tableName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -93,7 +92,7 @@ func (p *PostgresDriver) Columns(tableName string) ([]Column, error) {
|
||||||
var colName, colType, colDefault, isNullable string
|
var colName, colType, colDefault, isNullable string
|
||||||
var defaultPtr *string
|
var defaultPtr *string
|
||||||
if err := rows.Scan(&colName, &colType, &defaultPtr, &isNullable); err != nil {
|
if err := rows.Scan(&colName, &colType, &defaultPtr, &isNullable); err != nil {
|
||||||
return nil, fmt.Errorf("Unable to scan for table %s: %s", tableName, err)
|
return nil, fmt.Errorf("unable to scan for table %s: %s", tableName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if defaultPtr == nil {
|
if defaultPtr == nil {
|
||||||
|
@ -119,25 +118,23 @@ func (p *PostgresDriver) PrimaryKeyInfo(tableName string) (*PrimaryKey, error) {
|
||||||
pkey := &PrimaryKey{}
|
pkey := &PrimaryKey{}
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
query := `SELECT conname
|
query := `
|
||||||
FROM pg_constraint
|
select tc.constraint_name
|
||||||
WHERE conrelid =
|
from information_schema.table_constraints as tc
|
||||||
(SELECT oid
|
where tc.table_name = $1 and tc.constraint_type = 'PRIMARY KEY';`
|
||||||
FROM pg_class
|
|
||||||
WHERE relname LIKE $1)
|
|
||||||
AND contype='p';`
|
|
||||||
|
|
||||||
row := p.dbConn.QueryRow(query, tableName)
|
row := p.dbConn.QueryRow(query, tableName)
|
||||||
if err = row.Scan(&pkey.Name); err != nil {
|
if err = row.Scan(&pkey.Name); err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
queryColumns := `SELECT a.attname AS column
|
queryColumns := `
|
||||||
FROM pg_index i
|
select kcu.column_name
|
||||||
JOIN pg_attribute a ON a.attrelid = i.indrelid
|
from information_schema.key_column_usage as kcu
|
||||||
AND a.attnum = ANY(i.indkey)
|
where constraint_name = $1;`
|
||||||
WHERE i.indrelid = $1::regclass
|
|
||||||
AND i.indisprimary;`
|
|
||||||
|
|
||||||
var rows *sql.Rows
|
var rows *sql.Rows
|
||||||
if rows, err = p.dbConn.Query(queryColumns, tableName); err != nil {
|
if rows, err = p.dbConn.Query(queryColumns, tableName); err != nil {
|
||||||
|
@ -170,16 +167,16 @@ func (p *PostgresDriver) ForeignKeyInfo(tableName string) ([]ForeignKey, error)
|
||||||
var fkeys []ForeignKey
|
var fkeys []ForeignKey
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
SELECT
|
select
|
||||||
tc.constraint_name,
|
tc.constraint_name,
|
||||||
kcu.table_name as source_table,
|
kcu.table_name as source_table,
|
||||||
kcu.column_name as source_column,
|
kcu.column_name as source_column,
|
||||||
ccu.table_name as dest_table,
|
ccu.table_name as dest_table,
|
||||||
ccu.column_name as dest_column
|
ccu.column_name as dest_column
|
||||||
FROM information_schema.table_constraints as tc
|
from information_schema.table_constraints as tc
|
||||||
JOIN information_schema.key_column_usage as kcu ON tc.constraint_name = kcu.constraint_name
|
inner join information_schema.key_column_usage as kcu ON tc.constraint_name = kcu.constraint_name
|
||||||
JOIN information_schema.constraint_column_usage as ccu ON tc.constraint_name = ccu.constraint_name
|
inner join information_schema.constraint_column_usage as ccu ON tc.constraint_name = ccu.constraint_name
|
||||||
WHERE tc.table_name = $1 AND tc.constraint_type = 'FOREIGN KEY';`
|
where tc.table_name = $1 and tc.constraint_type = 'FOREIGN KEY';`
|
||||||
|
|
||||||
var rows *sql.Rows
|
var rows *sql.Rows
|
||||||
var err error
|
var err error
|
||||||
|
|
Loading…
Add table
Reference in a new issue