You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
3.2 KiB
147 lines
3.2 KiB
6 years ago
|
// Copyright 2019 The Xorm Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
5 years ago
|
package schemas
|
||
8 years ago
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
5 years ago
|
// Table represents a database table
|
||
8 years ago
|
type Table struct {
|
||
|
Name string
|
||
|
Type reflect.Type
|
||
|
columnsSeq []string
|
||
|
columnsMap map[string][]*Column
|
||
|
columns []*Column
|
||
|
Indexes map[string]*Index
|
||
|
PrimaryKeys []string
|
||
|
AutoIncrement string
|
||
|
Created map[string]bool
|
||
|
Updated string
|
||
|
Deleted string
|
||
|
Version string
|
||
|
StoreEngine string
|
||
|
Charset string
|
||
7 years ago
|
Comment string
|
||
8 years ago
|
}
|
||
|
|
||
|
func NewEmptyTable() *Table {
|
||
|
return NewTable("", nil)
|
||
|
}
|
||
|
|
||
5 years ago
|
// NewTable creates a new Table object
|
||
8 years ago
|
func NewTable(name string, t reflect.Type) *Table {
|
||
|
return &Table{Name: name, Type: t,
|
||
|
columnsSeq: make([]string, 0),
|
||
|
columns: make([]*Column, 0),
|
||
|
columnsMap: make(map[string][]*Column),
|
||
|
Indexes: make(map[string]*Index),
|
||
|
Created: make(map[string]bool),
|
||
|
PrimaryKeys: make([]string, 0),
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func (table *Table) Columns() []*Column {
|
||
|
return table.columns
|
||
|
}
|
||
8 years ago
|
|
||
5 years ago
|
func (table *Table) ColumnsSeq() []string {
|
||
|
return table.columnsSeq
|
||
|
}
|
||
|
|
||
|
func (table *Table) columnsByName(name string) []*Column {
|
||
|
for k, cols := range table.columnsMap {
|
||
8 years ago
|
if strings.EqualFold(k, name) {
|
||
5 years ago
|
return cols
|
||
8 years ago
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (table *Table) GetColumn(name string) *Column {
|
||
|
cols := table.columnsByName(name)
|
||
|
if cols != nil {
|
||
|
return cols[0]
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (table *Table) GetColumnIdx(name string, idx int) *Column {
|
||
|
cols := table.columnsByName(name)
|
||
|
if cols != nil && idx < len(cols) {
|
||
|
return cols[idx]
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
5 years ago
|
// PKColumns reprents all primary key columns
|
||
8 years ago
|
func (table *Table) PKColumns() []*Column {
|
||
|
columns := make([]*Column, len(table.PrimaryKeys))
|
||
|
for i, name := range table.PrimaryKeys {
|
||
|
columns[i] = table.GetColumn(name)
|
||
|
}
|
||
|
return columns
|
||
|
}
|
||
|
|
||
|
func (table *Table) ColumnType(name string) reflect.Type {
|
||
|
t, _ := table.Type.FieldByName(name)
|
||
|
return t.Type
|
||
|
}
|
||
|
|
||
|
func (table *Table) AutoIncrColumn() *Column {
|
||
|
return table.GetColumn(table.AutoIncrement)
|
||
|
}
|
||
|
|
||
|
func (table *Table) VersionColumn() *Column {
|
||
|
return table.GetColumn(table.Version)
|
||
|
}
|
||
|
|
||
|
func (table *Table) UpdatedColumn() *Column {
|
||
|
return table.GetColumn(table.Updated)
|
||
|
}
|
||
|
|
||
|
func (table *Table) DeletedColumn() *Column {
|
||
|
return table.GetColumn(table.Deleted)
|
||
|
}
|
||
|
|
||
5 years ago
|
// AddColumn adds a column to table
|
||
8 years ago
|
func (table *Table) AddColumn(col *Column) {
|
||
|
table.columnsSeq = append(table.columnsSeq, col.Name)
|
||
|
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}
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
if col.IsVersion {
|
||
|
table.Version = col.Name
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// AddIndex adds an index or an unique to table
|
||
8 years ago
|
func (table *Table) AddIndex(index *Index) {
|
||
|
table.Indexes[index.Name] = index
|
||
|
}
|