2016-06-23 08:09:56 +02:00
|
|
|
// Package bdb supplies the sql(b)oiler (d)ata(b)ase abstractions.
|
|
|
|
package bdb
|
2016-02-23 09:27:32 +01:00
|
|
|
|
2016-06-13 00:34:57 +02:00
|
|
|
import "github.com/pkg/errors"
|
2016-04-03 09:15:35 +02:00
|
|
|
|
2016-03-23 05:25:57 +01:00
|
|
|
// Interface for a database driver. Functionality required to support a specific
|
|
|
|
// database type (eg, MySQL, Postgres etc.)
|
|
|
|
type Interface interface {
|
2016-08-16 12:38:31 +02:00
|
|
|
TableNames(exclude []string) ([]string, error)
|
2016-04-03 09:15:35 +02:00
|
|
|
Columns(tableName string) ([]Column, error)
|
|
|
|
PrimaryKeyInfo(tableName string) (*PrimaryKey, error)
|
|
|
|
ForeignKeyInfo(tableName string) ([]ForeignKey, error)
|
2016-03-23 05:25:57 +01:00
|
|
|
|
2016-04-03 09:15:35 +02:00
|
|
|
// TranslateColumnType takes a Database column type and returns a go column type.
|
2016-03-23 06:05:23 +01:00
|
|
|
TranslateColumnType(Column) Column
|
2016-02-24 10:17:58 +01:00
|
|
|
|
2016-08-14 01:27:34 +02:00
|
|
|
// UseLastInsertID should return true if the driver is capable of using
|
|
|
|
// the sql.Exec result's LastInsertId
|
|
|
|
UseLastInsertID() bool
|
|
|
|
|
2016-02-23 09:27:32 +01:00
|
|
|
// Open the database connection
|
|
|
|
Open() error
|
|
|
|
// Close the database connection
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2016-08-16 12:38:31 +02:00
|
|
|
// Tables returns the metadata for all tables, minus the tables
|
|
|
|
// specified in the exclude slice.
|
|
|
|
func Tables(db Interface, exclude ...string) ([]Table, error) {
|
2016-04-03 09:15:35 +02:00
|
|
|
var err error
|
2016-08-15 14:42:40 +02:00
|
|
|
|
2016-08-16 12:38:31 +02:00
|
|
|
names, err := db.TableNames(exclude)
|
2016-08-15 14:42:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "unable to get table names")
|
2016-04-03 09:15:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var tables []Table
|
|
|
|
for _, name := range names {
|
|
|
|
t := Table{Name: name}
|
|
|
|
|
|
|
|
if t.Columns, err = db.Columns(name); err != nil {
|
2016-06-13 00:34:57 +02:00
|
|
|
return nil, errors.Wrapf(err, "unable to fetch table column info (%s)", name)
|
2016-04-03 09:15:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range t.Columns {
|
|
|
|
t.Columns[i] = db.TranslateColumnType(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.PKey, err = db.PrimaryKeyInfo(name); err != nil {
|
2016-06-13 00:34:57 +02:00
|
|
|
return nil, errors.Wrapf(err, "unable to fetch table pkey info (%s)", name)
|
2016-04-03 09:15:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if t.FKeys, err = db.ForeignKeyInfo(name); err != nil {
|
2016-06-13 00:34:57 +02:00
|
|
|
return nil, errors.Wrapf(err, "unable to fetch table fkey info (%s)", name)
|
2016-04-03 09:15:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setIsJoinTable(&t)
|
|
|
|
|
|
|
|
tables = append(tables, t)
|
|
|
|
}
|
|
|
|
|
2016-07-12 17:09:26 +02:00
|
|
|
// Relationships have a dependency on foreign key nullability.
|
|
|
|
for i := range tables {
|
|
|
|
tbl := &tables[i]
|
2016-07-15 21:09:32 +02:00
|
|
|
setForeignKeyConstraints(tbl, tables)
|
2016-07-12 17:09:26 +02:00
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
for i := range tables {
|
|
|
|
tbl := &tables[i]
|
|
|
|
setRelationships(tbl, tables)
|
|
|
|
}
|
|
|
|
|
2016-04-03 09:15:35 +02:00
|
|
|
return tables, nil
|
|
|
|
}
|
|
|
|
|
2016-08-13 16:07:22 +02:00
|
|
|
// setIsJoinTable if there are:
|
2016-06-23 08:09:56 +02:00
|
|
|
// A composite primary key involving two columns
|
2016-04-03 10:02:41 +02:00
|
|
|
// Both primary key columns are also foreign keys
|
2016-04-03 09:15:35 +02:00
|
|
|
func setIsJoinTable(t *Table) {
|
2016-04-03 10:02:41 +02:00
|
|
|
if t.PKey == nil || len(t.PKey.Columns) != 2 || len(t.FKeys) < 2 {
|
2016-04-03 09:15:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range t.PKey.Columns {
|
|
|
|
found := false
|
|
|
|
for _, f := range t.FKeys {
|
|
|
|
if c == f.Column {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.IsJoinTable = true
|
|
|
|
}
|
2016-07-09 18:53:21 +02:00
|
|
|
|
2016-07-15 21:09:32 +02:00
|
|
|
func setForeignKeyConstraints(t *Table, tables []Table) {
|
2016-07-09 18:53:21 +02:00
|
|
|
for i, fkey := range t.FKeys {
|
2016-07-12 17:09:26 +02:00
|
|
|
localColumn := t.GetColumn(fkey.Column)
|
|
|
|
foreignTable := GetTable(tables, fkey.ForeignTable)
|
|
|
|
foreignColumn := foreignTable.GetColumn(fkey.ForeignColumn)
|
2016-07-09 18:53:21 +02:00
|
|
|
|
2016-07-12 17:09:26 +02:00
|
|
|
t.FKeys[i].Nullable = localColumn.Nullable
|
2016-07-15 21:09:32 +02:00
|
|
|
t.FKeys[i].Unique = localColumn.Unique
|
2016-07-12 17:09:26 +02:00
|
|
|
t.FKeys[i].ForeignColumnNullable = foreignColumn.Nullable
|
2016-07-15 21:09:32 +02:00
|
|
|
t.FKeys[i].ForeignColumnUnique = foreignColumn.Unique
|
2016-07-09 18:53:21 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 08:49:42 +02:00
|
|
|
|
|
|
|
func setRelationships(t *Table, tables []Table) {
|
|
|
|
t.ToManyRelationships = toManyRelationships(*t, tables)
|
|
|
|
}
|