schema: split 'column', 'index' and 'schema' into separate files from 'table'
This commit is contained in:
parent
8c29d455ff
commit
f8664ed514
61
pkg/schema/column.go
Normal file
61
pkg/schema/column.go
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package schema
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Column represents a single column in a table.
|
||||||
|
type Column struct {
|
||||||
|
TableName string `db:"table_name"`
|
||||||
|
Name string `db:"column_name"`
|
||||||
|
Type string `db:"column_type"`
|
||||||
|
IsNotNull bool `db:"notnull"`
|
||||||
|
HasDefaultValue bool `db:"has_default_value"`
|
||||||
|
DefaultValue string `db:"dflt_value"`
|
||||||
|
IsPrimaryKey bool `db:"is_primary_key"`
|
||||||
|
PrimaryKeyRank uint `db:"primary_key_rank"`
|
||||||
|
IsForeignKey bool `db:"is_foreign_key"`
|
||||||
|
ForeignKeyTargetTable string `db:"fk_target_table"`
|
||||||
|
ForeignKeyTargetColumn string `db:"fk_target_column"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsNullableForeignKey is a helper function.
|
||||||
|
func (c Column) IsNullableForeignKey() bool {
|
||||||
|
return !c.IsNotNull && !c.IsPrimaryKey && c.IsForeignKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Column) IsNonCodeTableForeignKey() bool {
|
||||||
|
return c.IsForeignKey && strings.HasSuffix(c.Name, "_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Column) GoFieldName() string {
|
||||||
|
if c.Name == "rowid" {
|
||||||
|
return "ID"
|
||||||
|
}
|
||||||
|
if c.IsNonCodeTableForeignKey() {
|
||||||
|
return textutils.SnakeToCamel(strings.TrimSuffix(c.Name, "_id")) + "ID"
|
||||||
|
}
|
||||||
|
return textutils.SnakeToCamel(c.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GoVarName returns the name of a local variable for this column, e.g., when used as a function parameter.
|
||||||
|
func (c Column) GoVarName() string {
|
||||||
|
if c.Name == "rowid" {
|
||||||
|
return strings.ToLower(c.TableName)[0:1] + "ID"
|
||||||
|
// TODO: Or should it just be "id"??
|
||||||
|
}
|
||||||
|
// For foreign keys, use first letter of the target type and "ID". "UserID" => "uID"
|
||||||
|
if c.IsNonCodeTableForeignKey() {
|
||||||
|
return strings.ToLower(c.ForeignKeyTargetTable)[0:1] + "ID"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, just use the whole name
|
||||||
|
return c.LongGoVarName()
|
||||||
|
}
|
||||||
|
|
||||||
|
// LongGoVarName returns a lowercased version of the field name (Pascal => Camel).
|
||||||
|
func (c Column) LongGoVarName() string {
|
||||||
|
return textutils.CamelToPascal(c.GoFieldName())
|
||||||
|
}
|
||||||
10
pkg/schema/index.go
Normal file
10
pkg/schema/index.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package schema
|
||||||
|
|
||||||
|
type Index struct {
|
||||||
|
Name string `db:"index_name"`
|
||||||
|
TableName string `db:"table_name"`
|
||||||
|
Columns []string
|
||||||
|
IsUnique bool `db:"is_unique"`
|
||||||
|
// TODO: `where ...` for partial indexes
|
||||||
|
// TODO: identify columns that are expressions
|
||||||
|
}
|
||||||
6
pkg/schema/schema.go
Normal file
6
pkg/schema/schema.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package schema
|
||||||
|
|
||||||
|
type Schema struct {
|
||||||
|
Tables map[string]Table
|
||||||
|
Indexes map[string]Index
|
||||||
|
}
|
||||||
@ -2,65 +2,8 @@ package schema
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Column represents a single column in a table.
|
|
||||||
type Column struct {
|
|
||||||
TableName string `db:"table_name"`
|
|
||||||
Name string `db:"column_name"`
|
|
||||||
Type string `db:"column_type"`
|
|
||||||
IsNotNull bool `db:"notnull"`
|
|
||||||
HasDefaultValue bool `db:"has_default_value"`
|
|
||||||
DefaultValue string `db:"dflt_value"`
|
|
||||||
IsPrimaryKey bool `db:"is_primary_key"`
|
|
||||||
PrimaryKeyRank uint `db:"primary_key_rank"`
|
|
||||||
IsForeignKey bool `db:"is_foreign_key"`
|
|
||||||
ForeignKeyTargetTable string `db:"fk_target_table"`
|
|
||||||
ForeignKeyTargetColumn string `db:"fk_target_column"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsNullableForeignKey is a helper function.
|
|
||||||
func (c Column) IsNullableForeignKey() bool {
|
|
||||||
return !c.IsNotNull && !c.IsPrimaryKey && c.IsForeignKey
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c Column) IsNonCodeTableForeignKey() bool {
|
|
||||||
return c.IsForeignKey && strings.HasSuffix(c.Name, "_id")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c Column) GoFieldName() string {
|
|
||||||
if c.Name == "rowid" {
|
|
||||||
return "ID"
|
|
||||||
}
|
|
||||||
if c.IsNonCodeTableForeignKey() {
|
|
||||||
return textutils.SnakeToCamel(strings.TrimSuffix(c.Name, "_id")) + "ID"
|
|
||||||
}
|
|
||||||
return textutils.SnakeToCamel(c.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GoVarName returns the name of a local variable for this column, e.g., when used as a function parameter.
|
|
||||||
func (c Column) GoVarName() string {
|
|
||||||
if c.Name == "rowid" {
|
|
||||||
return strings.ToLower(c.TableName)[0:1] + "ID"
|
|
||||||
// TODO: Or should it just be "id"??
|
|
||||||
}
|
|
||||||
// For foreign keys, use first letter of the target type and "ID". "UserID" => "uID"
|
|
||||||
if c.IsNonCodeTableForeignKey() {
|
|
||||||
return strings.ToLower(c.ForeignKeyTargetTable)[0:1] + "ID"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, just use the whole name
|
|
||||||
return c.LongGoVarName()
|
|
||||||
}
|
|
||||||
|
|
||||||
// LongGoVarName returns a lowercased version of the field name (Pascal => Camel).
|
|
||||||
func (c Column) LongGoVarName() string {
|
|
||||||
return textutils.CamelToPascal(c.GoFieldName())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Table is a single SQLite table.
|
// Table is a single SQLite table.
|
||||||
type Table struct {
|
type Table struct {
|
||||||
TableName string `db:"name"`
|
TableName string `db:"name"`
|
||||||
@ -118,17 +61,3 @@ func (t Table) HasAutoTimestamps() (hasCreatedAt bool, hasUpdatedAt bool) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type Index struct {
|
|
||||||
Name string `db:"index_name"`
|
|
||||||
TableName string `db:"table_name"`
|
|
||||||
Columns []string
|
|
||||||
IsUnique bool `db:"is_unique"`
|
|
||||||
// TODO: `where ...` for partial indexes
|
|
||||||
// TODO: identify columns that are expressions
|
|
||||||
}
|
|
||||||
|
|
||||||
type Schema struct {
|
|
||||||
Tables map[string]Table
|
|
||||||
Indexes map[string]Index
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user