2019-07-27 10:40:30 +02:00
|
|
|
package metadata
|
2019-05-24 13:13:13 +02:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
2019-07-04 17:54:15 +02:00
|
|
|
"github.com/go-jet/jet/internal/utils"
|
2019-05-24 13:13:13 +02:00
|
|
|
)
|
|
|
|
|
|
2019-08-14 12:50:31 +02:00
|
|
|
// TableMetaData metadata struct
|
|
|
|
|
type TableMetaData struct {
|
2019-05-24 13:13:13 +02:00
|
|
|
SchemaName string
|
|
|
|
|
name string
|
|
|
|
|
PrimaryKeys map[string]bool
|
2019-08-14 12:50:31 +02:00
|
|
|
Columns []ColumnMetaData
|
2019-05-24 13:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// Name returns table info name
|
2019-08-14 12:50:31 +02:00
|
|
|
func (t TableMetaData) Name() string {
|
2019-05-24 13:13:13 +02:00
|
|
|
return t.name
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// IsPrimaryKey returns if column is a part of primary key
|
2019-08-14 12:50:31 +02:00
|
|
|
func (t TableMetaData) IsPrimaryKey(column string) bool {
|
2019-07-18 18:42:03 +02:00
|
|
|
return t.PrimaryKeys[column]
|
2019-05-24 13:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// MutableColumns returns list of mutable columns for table
|
2019-08-14 12:50:31 +02:00
|
|
|
func (t TableMetaData) MutableColumns() []ColumnMetaData {
|
|
|
|
|
ret := []ColumnMetaData{}
|
2019-06-30 11:53:35 +02:00
|
|
|
|
|
|
|
|
for _, column := range t.Columns {
|
|
|
|
|
if t.IsPrimaryKey(column.Name) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = append(ret, column)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// GetImports returns model imports for table.
|
2019-08-14 12:50:31 +02:00
|
|
|
func (t TableMetaData) GetImports() []string {
|
2019-05-24 13:13:13 +02:00
|
|
|
imports := map[string]string{}
|
|
|
|
|
|
|
|
|
|
for _, column := range t.Columns {
|
2019-08-14 12:50:31 +02:00
|
|
|
columnType := column.GoBaseType
|
2019-05-24 13:13:13 +02:00
|
|
|
|
|
|
|
|
switch columnType {
|
|
|
|
|
case "time.Time":
|
|
|
|
|
imports["time.Time"] = "time"
|
|
|
|
|
case "uuid.UUID":
|
|
|
|
|
imports["uuid.UUID"] = "github.com/google/uuid"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := []string{}
|
|
|
|
|
|
|
|
|
|
for _, packageImport := range imports {
|
|
|
|
|
ret = append(ret, packageImport)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// GoStructName returns go struct name for sql builder
|
2019-08-14 12:50:31 +02:00
|
|
|
func (t TableMetaData) GoStructName() string {
|
2019-07-04 17:54:15 +02:00
|
|
|
return utils.ToGoIdentifier(t.name) + "Table"
|
2019-05-24 13:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
// GetTableInfo returns table info metadata
|
2019-08-14 12:50:31 +02:00
|
|
|
func GetTableInfo(db *sql.DB, querySet DialectQuerySet, schemaName, tableName string) (tableInfo TableMetaData, err error) {
|
2019-05-24 13:13:13 +02:00
|
|
|
|
|
|
|
|
tableInfo.SchemaName = schemaName
|
|
|
|
|
tableInfo.name = tableName
|
|
|
|
|
|
2019-07-27 10:40:30 +02:00
|
|
|
tableInfo.PrimaryKeys, err = getPrimaryKeys(db, querySet, schemaName, tableName)
|
2019-05-24 13:13:13 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 12:50:31 +02:00
|
|
|
tableInfo.Columns, err = getColumnsMetaData(db, querySet, schemaName, tableName)
|
2019-05-24 13:13:13 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 12:50:31 +02:00
|
|
|
func getPrimaryKeys(db *sql.DB, querySet DialectQuerySet, schemaName, tableName string) (map[string]bool, error) {
|
2019-07-27 10:40:30 +02:00
|
|
|
|
|
|
|
|
rows, err := db.Query(querySet.PrimaryKeysQuery(), schemaName, tableName)
|
2019-05-24 13:13:13 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
primaryKeyMap := map[string]bool{}
|
|
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
primaryKey := ""
|
|
|
|
|
err := rows.Scan(&primaryKey)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
primaryKeyMap[primaryKey] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return primaryKeyMap, nil
|
|
|
|
|
}
|