xorm/table.go

153 lines
3.0 KiB
Go
Raw Normal View History

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
2014-05-23 06:17:56 +00:00
columnsMap map[string][]*Column
columns []*Column
2014-04-08 12:57:04 +00:00
Indexes map[string]*Index
PrimaryKeys []string
AutoIncrement string
Created map[string]bool
Updated string
Deleted string
2014-04-08 12:57:04 +00:00
Version string
2014-04-11 07:38:09 +00:00
Cacher Cacher
2014-08-30 14:14:12 +00:00
StoreEngine string
Charset string
Comment string
2014-04-08 12:57:04 +00:00
}
2014-05-23 06:17:56 +00:00
func (table *Table) Columns() []*Column {
2014-04-08 12:57:04 +00:00
return table.columns
}
func (table *Table) ColumnsSeq() []string {
return table.columnsSeq
}
func NewEmptyTable() *Table {
2014-05-23 06:17:56 +00:00
return NewTable("", nil)
2014-04-08 12:57:04 +00:00
}
func NewTable(name string, t reflect.Type) *Table {
return &Table{Name: name, Type: t,
columnsSeq: make([]string, 0),
2014-05-23 06:17:56 +00:00
columns: make([]*Column, 0),
columnsMap: make(map[string][]*Column),
2014-04-08 12:57:04 +00:00
Indexes: make(map[string]*Index),
Created: make(map[string]bool),
PrimaryKeys: make([]string, 0),
}
}
func (table *Table) columnsByName(name string) []*Column {
n := len(name)
for k := range table.columnsMap {
if len(k) != n {
continue
}
if strings.EqualFold(k, name) {
return table.columnsMap[k]
}
}
return nil
}
2014-04-08 12:57:04 +00:00
func (table *Table) GetColumn(name string) *Column {
cols := table.columnsByName(name)
if cols != nil {
return cols[0]
2014-05-23 06:17:56 +00:00
}
2014-05-23 06:17:56 +00:00
return nil
}
func (table *Table) GetColumnIdx(name string, idx int) *Column {
cols := table.columnsByName(name)
if cols != nil && idx < len(cols) {
return cols[idx]
2014-05-23 06:17:56 +00:00
}
2014-05-23 06:17:56 +00:00
return nil
2014-04-08 12:57:04 +00:00
}
// if has primary key, return column
func (table *Table) PKColumns() []*Column {
2015-02-22 16:00:10 +00:00
columns := make([]*Column, len(table.PrimaryKeys))
for i, name := range table.PrimaryKeys {
columns[i] = table.GetColumn(name)
2014-04-08 12:57:04 +00:00
}
return columns
}
2015-02-22 16:00:10 +00:00
func (table *Table) ColumnType(name string) reflect.Type {
t, _ := table.Type.FieldByName(name)
return t.Type
}
2014-04-08 12:57:04 +00:00
func (table *Table) AutoIncrColumn() *Column {
return table.GetColumn(table.AutoIncrement)
}
func (table *Table) VersionColumn() *Column {
return table.GetColumn(table.Version)
}
2014-05-23 06:17:56 +00:00
func (table *Table) UpdatedColumn() *Column {
return table.GetColumn(table.Updated)
}
func (table *Table) DeletedColumn() *Column {
return table.GetColumn(table.Deleted)
2014-11-03 13:02:45 +00:00
}
2014-04-08 12:57:04 +00:00
// add a column to table
func (table *Table) AddColumn(col *Column) {
table.columnsSeq = append(table.columnsSeq, col.Name)
2014-05-23 06:17:56 +00:00
table.columns = append(table.columns, col)
colName := strings.ToLower(col.Name)
if c, ok := table.columnsMap[colName]; ok {
table.columnsMap[colName] = append(c, col)
} else {
table.columnsMap[colName] = []*Column{col}
}
2014-04-08 12:57:04 +00:00
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.IsDeleted {
table.Deleted = col.Name
}
2014-04-08 12:57:04 +00:00
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
}