2014-04-08 12:57:04 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// database table
|
|
|
|
type Table struct {
|
|
|
|
Name string
|
|
|
|
Type reflect.Type
|
|
|
|
columnsSeq []string
|
|
|
|
columns map[string]*Column
|
|
|
|
Indexes map[string]*Index
|
|
|
|
PrimaryKeys []string
|
|
|
|
AutoIncrement string
|
|
|
|
Created map[string]bool
|
|
|
|
Updated string
|
|
|
|
Version string
|
2014-04-11 07:38:09 +00:00
|
|
|
Cacher Cacher
|
2014-04-08 12:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (table *Table) Columns() map[string]*Column {
|
|
|
|
return table.columns
|
|
|
|
}
|
|
|
|
|
|
|
|
func (table *Table) ColumnsSeq() []string {
|
|
|
|
return table.columnsSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEmptyTable() *Table {
|
|
|
|
return &Table{columnsSeq: make([]string, 0),
|
|
|
|
columns: make(map[string]*Column),
|
|
|
|
Indexes: make(map[string]*Index),
|
|
|
|
Created: make(map[string]bool),
|
|
|
|
PrimaryKeys: make([]string, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTable(name string, t reflect.Type) *Table {
|
|
|
|
return &Table{Name: name, Type: t,
|
|
|
|
columnsSeq: make([]string, 0),
|
|
|
|
columns: make(map[string]*Column),
|
|
|
|
Indexes: make(map[string]*Index),
|
|
|
|
Created: make(map[string]bool),
|
|
|
|
PrimaryKeys: make([]string, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (table *Table) GetColumn(name string) *Column {
|
|
|
|
return table.columns[strings.ToLower(name)]
|
|
|
|
}
|
|
|
|
|
|
|
|
// if has primary key, return column
|
|
|
|
func (table *Table) PKColumns() []*Column {
|
|
|
|
columns := make([]*Column, 0)
|
|
|
|
for _, name := range table.PrimaryKeys {
|
|
|
|
columns = append(columns, table.GetColumn(name))
|
|
|
|
}
|
|
|
|
return columns
|
|
|
|
}
|
|
|
|
|
|
|
|
func (table *Table) AutoIncrColumn() *Column {
|
|
|
|
return table.GetColumn(table.AutoIncrement)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (table *Table) VersionColumn() *Column {
|
|
|
|
return table.GetColumn(table.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a column to table
|
|
|
|
func (table *Table) AddColumn(col *Column) {
|
|
|
|
table.columnsSeq = append(table.columnsSeq, col.Name)
|
|
|
|
table.columns[strings.ToLower(col.Name)] = col
|
|
|
|
if col.IsPrimaryKey {
|
|
|
|
table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
|
|
|
|
}
|
|
|
|
if col.IsAutoIncrement {
|
|
|
|
table.AutoIncrement = col.Name
|
|
|
|
}
|
|
|
|
if col.IsCreated {
|
|
|
|
table.Created[col.Name] = true
|
|
|
|
}
|
|
|
|
if col.IsUpdated {
|
|
|
|
table.Updated = col.Name
|
|
|
|
}
|
|
|
|
if col.IsVersion {
|
|
|
|
table.Version = col.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add an index or an unique to table
|
|
|
|
func (table *Table) AddIndex(index *Index) {
|
|
|
|
table.Indexes[index.Name] = index
|
|
|
|
}
|