2019-06-23 15:22:43 +00:00
|
|
|
// 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.
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
package schemas
|
2016-11-03 22:16:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2020-06-15 20:46:01 +00:00
|
|
|
"strconv"
|
2016-11-03 22:16:01 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-08-27 02:17:23 +00:00
|
|
|
// Table represents a database table
|
2016-11-03 22:16:01 +00:00
|
|
|
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
|
2017-08-22 11:39:52 +00:00
|
|
|
Comment string
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// NewEmptyTable creates an empty table
|
2016-11-03 22:16:01 +00:00
|
|
|
func NewEmptyTable() *Table {
|
|
|
|
return NewTable("", nil)
|
|
|
|
}
|
|
|
|
|
2019-08-27 02:17:23 +00:00
|
|
|
// NewTable creates a new Table object
|
2016-11-03 22:16:01 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// Columns returns table's columns
|
2020-03-22 15:12:55 +00:00
|
|
|
func (table *Table) Columns() []*Column {
|
|
|
|
return table.columns
|
|
|
|
}
|
2016-11-03 22:16:01 +00:00
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// ColumnsSeq returns table's column names according sequence
|
2020-03-22 15:12:55 +00:00
|
|
|
func (table *Table) ColumnsSeq() []string {
|
|
|
|
return table.columnsSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
func (table *Table) columnsByName(name string) []*Column {
|
2021-01-05 06:28:51 +00:00
|
|
|
return table.columnsMap[strings.ToLower(name)]
|
2016-11-03 22:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// GetColumn returns column according column name, if column not found, return nil
|
2016-11-03 22:16:01 +00:00
|
|
|
func (table *Table) GetColumn(name string) *Column {
|
|
|
|
cols := table.columnsByName(name)
|
|
|
|
if cols != nil {
|
|
|
|
return cols[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-15 20:46:01 +00:00
|
|
|
// GetColumnIdx returns column according name and idx
|
2016-11-03 22:16:01 +00:00
|
|
|
func (table *Table) GetColumnIdx(name string, idx int) *Column {
|
|
|
|
cols := table.columnsByName(name)
|
|
|
|
if cols != nil && idx < len(cols) {
|
|
|
|
return cols[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-27 02:17:23 +00:00
|
|
|
// PKColumns reprents all primary key columns
|
2016-11-03 22:16:01 +00:00
|
|
|
func (table *Table) PKColumns() []*Column {
|
|
|
|
columns := make([]*Column, len(table.PrimaryKeys))
|
|
|
|
for i, name := range table.PrimaryKeys {
|
|
|
|
columns[i] = table.GetColumn(name)
|
|
|
|
}
|
|
|
|
return columns
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:32:17 +00:00
|
|
|
// ColumnType returns a column's type
|
2016-11-03 22:16:01 +00:00
|
|
|
func (table *Table) ColumnType(name string) reflect.Type {
|
|
|
|
t, _ := table.Type.FieldByName(name)
|
|
|
|
return t.Type
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:32:17 +00:00
|
|
|
// AutoIncrColumn returns autoincrement column
|
2016-11-03 22:16:01 +00:00
|
|
|
func (table *Table) AutoIncrColumn() *Column {
|
|
|
|
return table.GetColumn(table.AutoIncrement)
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:32:17 +00:00
|
|
|
// VersionColumn returns version column's information
|
2016-11-03 22:16:01 +00:00
|
|
|
func (table *Table) VersionColumn() *Column {
|
|
|
|
return table.GetColumn(table.Version)
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:32:17 +00:00
|
|
|
// UpdatedColumn returns updated column's information
|
2016-11-03 22:16:01 +00:00
|
|
|
func (table *Table) UpdatedColumn() *Column {
|
|
|
|
return table.GetColumn(table.Updated)
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:32:17 +00:00
|
|
|
// DeletedColumn returns deleted column's information
|
2016-11-03 22:16:01 +00:00
|
|
|
func (table *Table) DeletedColumn() *Column {
|
|
|
|
return table.GetColumn(table.Deleted)
|
|
|
|
}
|
|
|
|
|
2019-08-27 02:17:23 +00:00
|
|
|
// AddColumn adds a column to table
|
2016-11-03 22:16:01 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 02:17:23 +00:00
|
|
|
// AddIndex adds an index or an unique to table
|
2016-11-03 22:16:01 +00:00
|
|
|
func (table *Table) AddIndex(index *Index) {
|
|
|
|
table.Indexes[index.Name] = index
|
|
|
|
}
|
2020-06-15 20:46:01 +00:00
|
|
|
|
|
|
|
// IDOfV get id from one value of struct
|
|
|
|
func (table *Table) IDOfV(rv reflect.Value) (PK, error) {
|
|
|
|
v := reflect.Indirect(rv)
|
|
|
|
pk := make([]interface{}, len(table.PrimaryKeys))
|
|
|
|
for i, col := range table.PKColumns() {
|
|
|
|
var err error
|
|
|
|
|
2021-08-28 11:15:21 +00:00
|
|
|
pkField := v.FieldByIndex(col.FieldIndex)
|
2020-06-15 20:46:01 +00:00
|
|
|
|
|
|
|
switch pkField.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
pk[i], err = col.ConvertID(pkField.String())
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
pk[i], err = col.ConvertID(strconv.FormatInt(pkField.Int(), 10))
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
// id of uint will be converted to int64
|
|
|
|
pk[i], err = col.ConvertID(strconv.FormatUint(pkField.Uint(), 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PK(pk), nil
|
|
|
|
}
|