2016-06-23 08:09:56 +02:00
|
|
|
package drivers
|
2016-02-23 09:27:32 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
2016-06-19 23:46:10 +02:00
|
|
|
// Side-effect import sql driver
|
2016-02-23 09:27:32 +01:00
|
|
|
_ "github.com/lib/pq"
|
2016-06-23 08:48:49 +02:00
|
|
|
"github.com/nullbio/sqlboiler/bdb"
|
2016-02-23 09:27:32 +01:00
|
|
|
)
|
|
|
|
|
2016-02-24 10:17:58 +01:00
|
|
|
// PostgresDriver holds the database connection string and a handle
|
|
|
|
// to the database connection.
|
2016-02-23 09:27:32 +01:00
|
|
|
type PostgresDriver struct {
|
|
|
|
connStr string
|
|
|
|
dbConn *sql.DB
|
|
|
|
}
|
|
|
|
|
2016-02-24 10:17:58 +01:00
|
|
|
// NewPostgresDriver takes the database connection details as parameters and
|
|
|
|
// returns a pointer to a PostgresDriver object. Note that it is required to
|
|
|
|
// call PostgresDriver.Open() and PostgresDriver.Close() to open and close
|
|
|
|
// the database connection once an object has been obtained.
|
2016-02-23 09:27:32 +01:00
|
|
|
func NewPostgresDriver(user, pass, dbname, host string, port int) *PostgresDriver {
|
|
|
|
driver := PostgresDriver{
|
|
|
|
connStr: fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%d",
|
|
|
|
user, pass, dbname, host, port),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &driver
|
|
|
|
}
|
|
|
|
|
2016-02-24 10:17:58 +01:00
|
|
|
// Open opens the database connection using the connection string
|
2016-03-23 06:05:23 +01:00
|
|
|
func (p *PostgresDriver) Open() error {
|
2016-02-23 09:27:32 +01:00
|
|
|
var err error
|
2016-03-23 06:05:23 +01:00
|
|
|
p.dbConn, err = sql.Open("postgres", p.connStr)
|
2016-02-23 09:27:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-24 10:17:58 +01:00
|
|
|
// Close closes the database connection
|
2016-03-23 06:05:23 +01:00
|
|
|
func (p *PostgresDriver) Close() {
|
|
|
|
p.dbConn.Close()
|
2016-02-23 09:27:32 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 23:46:10 +02:00
|
|
|
// TableNames connects to the postgres database and
|
2016-02-24 10:17:58 +01:00
|
|
|
// retrieves all table names from the information_schema where the
|
|
|
|
// table schema is public. It excludes common migration tool tables
|
|
|
|
// such as gorp_migrations
|
2016-04-03 09:15:35 +02:00
|
|
|
func (p *PostgresDriver) TableNames() ([]string, error) {
|
2016-03-23 06:05:23 +01:00
|
|
|
var names []string
|
2016-02-23 09:27:32 +01:00
|
|
|
|
2016-06-14 16:53:36 +02:00
|
|
|
rows, err := p.dbConn.Query(`
|
|
|
|
select table_name from information_schema.tables
|
2016-06-27 08:53:17 +02:00
|
|
|
where table_schema = 'public' and table_name not like '%migrations%'
|
2016-06-14 16:53:36 +02:00
|
|
|
`)
|
2016-02-23 09:27:32 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
2016-03-23 06:05:23 +01:00
|
|
|
var name string
|
|
|
|
if err := rows.Scan(&name); err != nil {
|
2016-02-23 09:27:32 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-23 06:05:23 +01:00
|
|
|
names = append(names, name)
|
2016-02-23 09:27:32 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 06:05:23 +01:00
|
|
|
return names, nil
|
2016-02-23 09:27:32 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 23:46:10 +02:00
|
|
|
// Columns takes a table name and attempts to retrieve the table information
|
2016-02-24 10:17:58 +01:00
|
|
|
// from the database information_schema.columns. It retrieves the column names
|
2016-03-23 06:05:23 +01:00
|
|
|
// and column types and returns those as a []Column after TranslateColumnType()
|
2016-02-24 10:17:58 +01:00
|
|
|
// converts the SQL types to Go types, for example: "varchar" to "string"
|
2016-06-23 08:48:49 +02:00
|
|
|
func (p *PostgresDriver) Columns(tableName string) ([]bdb.Column, error) {
|
|
|
|
var columns []bdb.Column
|
2016-03-18 16:27:55 +01:00
|
|
|
|
2016-03-23 06:05:23 +01:00
|
|
|
rows, err := p.dbConn.Query(`
|
2016-06-14 16:53:36 +02:00
|
|
|
select column_name, data_type, column_default, is_nullable
|
2016-06-27 08:53:17 +02:00
|
|
|
from information_schema.columns
|
|
|
|
where table_name=$1 and table_schema = 'public'
|
2016-03-18 16:27:55 +01:00
|
|
|
`, tableName)
|
2016-02-23 09:27:32 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
2016-04-26 11:27:00 +02:00
|
|
|
var colName, colType, colDefault, isNullable string
|
2016-05-17 12:00:56 +02:00
|
|
|
var defaultPtr *string
|
|
|
|
if err := rows.Scan(&colName, &colType, &defaultPtr, &isNullable); err != nil {
|
2016-06-14 16:53:36 +02:00
|
|
|
return nil, fmt.Errorf("unable to scan for table %s: %s", tableName, err)
|
2016-05-17 12:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if defaultPtr == nil {
|
|
|
|
colDefault = ""
|
|
|
|
} else {
|
|
|
|
colDefault = *defaultPtr
|
2016-02-23 09:27:32 +01:00
|
|
|
}
|
2016-05-17 12:00:56 +02:00
|
|
|
|
2016-06-23 08:48:49 +02:00
|
|
|
column := bdb.Column{
|
2016-04-03 09:15:35 +02:00
|
|
|
Name: colName,
|
|
|
|
Type: colType,
|
2016-04-26 11:27:00 +02:00
|
|
|
Default: colDefault,
|
2016-04-03 09:15:35 +02:00
|
|
|
IsNullable: isNullable == "YES",
|
|
|
|
}
|
2016-03-23 06:05:23 +01:00
|
|
|
columns = append(columns, column)
|
2016-02-23 09:27:32 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 06:05:23 +01:00
|
|
|
return columns, nil
|
2016-02-23 09:27:32 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 23:46:10 +02:00
|
|
|
// PrimaryKeyInfo looks up the primary key for a table.
|
2016-06-23 08:48:49 +02:00
|
|
|
func (p *PostgresDriver) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
|
|
|
|
pkey := &bdb.PrimaryKey{}
|
2016-03-23 07:02:11 +01:00
|
|
|
var err error
|
|
|
|
|
2016-06-14 16:53:36 +02:00
|
|
|
query := `
|
|
|
|
select tc.constraint_name
|
|
|
|
from information_schema.table_constraints as tc
|
2016-06-27 08:53:17 +02:00
|
|
|
where tc.table_name = $1 and tc.constraint_type = 'PRIMARY KEY' and tc.table_schema = 'public';`
|
2016-03-23 07:02:11 +01:00
|
|
|
|
|
|
|
row := p.dbConn.QueryRow(query, tableName)
|
|
|
|
if err = row.Scan(&pkey.Name); err != nil {
|
2016-06-14 16:53:36 +02:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-03-23 07:02:11 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:53:36 +02:00
|
|
|
queryColumns := `
|
|
|
|
select kcu.column_name
|
|
|
|
from information_schema.key_column_usage as kcu
|
2016-06-27 08:53:17 +02:00
|
|
|
where constraint_name = $1 and table_schema = 'public';`
|
2016-03-23 07:02:11 +01:00
|
|
|
|
|
|
|
var rows *sql.Rows
|
2016-06-20 01:53:45 +02:00
|
|
|
if rows, err = p.dbConn.Query(queryColumns, pkey.Name); err != nil {
|
2016-03-23 07:02:11 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-04 12:28:58 +02:00
|
|
|
var columns []string
|
2016-03-23 07:02:11 +01:00
|
|
|
for rows.Next() {
|
|
|
|
var column string
|
|
|
|
|
|
|
|
err = rows.Scan(&column)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-04 12:28:58 +02:00
|
|
|
|
|
|
|
columns = append(columns, column)
|
2016-03-23 07:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-20 01:53:45 +02:00
|
|
|
pkey.Columns = columns
|
|
|
|
|
2016-03-23 07:02:11 +01:00
|
|
|
return pkey, nil
|
|
|
|
}
|
|
|
|
|
2016-06-19 23:46:10 +02:00
|
|
|
// ForeignKeyInfo retrieves the foreign keys for a given table name.
|
2016-06-23 08:48:49 +02:00
|
|
|
func (p *PostgresDriver) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) {
|
|
|
|
var fkeys []bdb.ForeignKey
|
2016-03-23 07:02:11 +01:00
|
|
|
|
|
|
|
query := `
|
2016-06-14 16:53:36 +02:00
|
|
|
select
|
|
|
|
tc.constraint_name,
|
|
|
|
kcu.table_name as source_table,
|
|
|
|
kcu.column_name as source_column,
|
|
|
|
ccu.table_name as dest_table,
|
|
|
|
ccu.column_name as dest_column
|
|
|
|
from information_schema.table_constraints as tc
|
|
|
|
inner join information_schema.key_column_usage as kcu ON tc.constraint_name = kcu.constraint_name
|
|
|
|
inner join information_schema.constraint_column_usage as ccu ON tc.constraint_name = ccu.constraint_name
|
2016-07-02 00:52:19 +02:00
|
|
|
where tc.table_name = $1 and tc.constraint_type = 'FOREIGN KEY' and tc.table_schema = 'public';`
|
2016-03-23 07:02:11 +01:00
|
|
|
|
|
|
|
var rows *sql.Rows
|
|
|
|
var err error
|
|
|
|
if rows, err = p.dbConn.Query(query, tableName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
2016-06-23 08:48:49 +02:00
|
|
|
var fkey bdb.ForeignKey
|
2016-03-23 07:02:11 +01:00
|
|
|
var sourceTable string
|
|
|
|
|
|
|
|
err = rows.Scan(&fkey.Name, &sourceTable, &fkey.Column, &fkey.ForeignTable, &fkey.ForeignColumn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-19 23:46:10 +02:00
|
|
|
|
|
|
|
fkeys = append(fkeys, fkey)
|
2016-03-23 07:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fkeys, nil
|
|
|
|
}
|
|
|
|
|
2016-03-23 06:05:23 +01:00
|
|
|
// TranslateColumnType converts postgres database types to Go types, for example
|
2016-02-24 10:17:58 +01:00
|
|
|
// "varchar" to "string" and "bigint" to "int64". It returns this parsed data
|
2016-03-23 05:25:57 +01:00
|
|
|
// as a Column object.
|
2016-06-23 08:48:49 +02:00
|
|
|
func (p *PostgresDriver) TranslateColumnType(c bdb.Column) bdb.Column {
|
2016-03-23 05:25:57 +01:00
|
|
|
if c.IsNullable {
|
|
|
|
switch c.Type {
|
2016-06-02 23:07:51 +02:00
|
|
|
case "bigint", "bigserial":
|
|
|
|
c.Type = "null.Int64"
|
|
|
|
case "integer", "serial":
|
|
|
|
c.Type = "null.Int32"
|
|
|
|
case "smallint", "smallserial":
|
|
|
|
c.Type = "null.Int16"
|
|
|
|
case "decimal", "numeric", "double precision", "money":
|
|
|
|
c.Type = "null.Float64"
|
|
|
|
case "real":
|
|
|
|
c.Type = "null.Float32"
|
2016-03-01 12:26:00 +01:00
|
|
|
case "bit", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
|
2016-03-23 05:25:57 +01:00
|
|
|
c.Type = "null.String"
|
2016-07-06 08:02:35 +02:00
|
|
|
case "bytea":
|
|
|
|
c.Type = "[]byte"
|
2016-03-01 12:26:00 +01:00
|
|
|
case "boolean":
|
2016-03-23 05:25:57 +01:00
|
|
|
c.Type = "null.Bool"
|
2016-03-01 12:26:00 +01:00
|
|
|
case "date", "interval", "time", "timestamp without time zone", "timestamp with time zone":
|
2016-03-23 05:25:57 +01:00
|
|
|
c.Type = "null.Time"
|
2016-03-01 12:26:00 +01:00
|
|
|
default:
|
2016-03-23 05:25:57 +01:00
|
|
|
c.Type = "null.String"
|
2016-03-01 12:26:00 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-03-23 05:25:57 +01:00
|
|
|
switch c.Type {
|
2016-06-02 23:07:51 +02:00
|
|
|
case "bigint", "bigserial":
|
2016-03-23 05:25:57 +01:00
|
|
|
c.Type = "int64"
|
2016-06-02 23:07:51 +02:00
|
|
|
case "integer", "serial":
|
|
|
|
c.Type = "int32"
|
|
|
|
case "smallint", "smallserial":
|
|
|
|
c.Type = "int16"
|
|
|
|
case "decimal", "numeric", "double precision", "money":
|
|
|
|
c.Type = "float64"
|
|
|
|
case "real":
|
|
|
|
c.Type = "float32"
|
2016-03-01 12:26:00 +01:00
|
|
|
case "bit", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
|
2016-03-23 05:25:57 +01:00
|
|
|
c.Type = "string"
|
2016-03-01 12:26:00 +01:00
|
|
|
case "bytea":
|
2016-03-23 05:25:57 +01:00
|
|
|
c.Type = "[]byte"
|
2016-03-01 12:26:00 +01:00
|
|
|
case "boolean":
|
2016-03-23 05:25:57 +01:00
|
|
|
c.Type = "bool"
|
2016-03-01 12:26:00 +01:00
|
|
|
case "date", "interval", "time", "timestamp without time zone", "timestamp with time zone":
|
2016-03-23 05:25:57 +01:00
|
|
|
c.Type = "time.Time"
|
2016-03-01 12:26:00 +01:00
|
|
|
default:
|
2016-03-23 05:25:57 +01:00
|
|
|
c.Type = "string"
|
2016-03-01 12:26:00 +01:00
|
|
|
}
|
2016-02-23 09:27:32 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:25:57 +01:00
|
|
|
return c
|
2016-02-23 09:27:32 +01:00
|
|
|
}
|