2016-02-23 09:27:32 +01:00
|
|
|
package dbdrivers
|
|
|
|
|
2016-04-03 09:15:35 +02:00
|
|
|
import "fmt"
|
|
|
|
|
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-04-03 09:15:35 +02:00
|
|
|
TableNames() ([]string, error)
|
|
|
|
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-02-23 09:27:32 +01:00
|
|
|
// Open the database connection
|
|
|
|
Open() error
|
2016-02-24 10:17:58 +01:00
|
|
|
|
2016-02-23 09:27:32 +01:00
|
|
|
// Close the database connection
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2016-03-23 05:25:57 +01:00
|
|
|
// Table metadata from the database schema.
|
|
|
|
type Table struct {
|
|
|
|
Name string
|
|
|
|
Columns []Column
|
|
|
|
|
2016-03-23 07:02:11 +01:00
|
|
|
PKey *PrimaryKey
|
|
|
|
FKeys []ForeignKey
|
|
|
|
|
2016-03-23 05:25:57 +01:00
|
|
|
IsJoinTable bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Column holds information about a database column.
|
2016-03-23 06:05:23 +01:00
|
|
|
// Types are Go types, converted by TranslateColumnType.
|
2016-03-23 05:25:57 +01:00
|
|
|
type Column struct {
|
2016-04-03 09:15:35 +02:00
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
IsNullable bool
|
2016-02-23 09:27:32 +01:00
|
|
|
}
|
2016-03-23 07:02:11 +01:00
|
|
|
|
|
|
|
// PrimaryKey represents a primary key constraint in a database
|
|
|
|
type PrimaryKey struct {
|
|
|
|
Name string
|
|
|
|
Columns []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForeignKey represents a foreign key constraint in a database
|
|
|
|
type ForeignKey struct {
|
|
|
|
Name string
|
|
|
|
Column string
|
|
|
|
|
|
|
|
ForeignTable string
|
|
|
|
ForeignColumn string
|
|
|
|
}
|
2016-04-03 09:15:35 +02:00
|
|
|
|
|
|
|
// Tables returns the table metadata for the given tables, or all tables if
|
|
|
|
// no tables are provided.
|
|
|
|
func Tables(db Interface, names ...string) ([]Table, error) {
|
|
|
|
var err error
|
|
|
|
if len(names) == 0 {
|
|
|
|
if names, err = db.TableNames(); err != nil {
|
|
|
|
fmt.Println("Unable to get table names.")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var tables []Table
|
|
|
|
for _, name := range names {
|
|
|
|
t := Table{Name: name}
|
|
|
|
|
|
|
|
if t.Columns, err = db.Columns(name); err != nil {
|
|
|
|
return nil, err
|
|
|
|
fmt.Println("Unable to get columnss.")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range t.Columns {
|
|
|
|
t.Columns[i] = db.TranslateColumnType(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.PKey, err = db.PrimaryKeyInfo(name); err != nil {
|
|
|
|
fmt.Println("Unable to get primary key info.")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.FKeys, err = db.ForeignKeyInfo(name); err != nil {
|
|
|
|
fmt.Println("Unable to get foreign key info.")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsJoinTable(&t)
|
|
|
|
|
|
|
|
tables = append(tables, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tables, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setIsJoinTable(t *Table) {
|
|
|
|
if t.PKey == nil {
|
|
|
|
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
|
|
|
|
}
|