github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/model/model.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package model
    15  
    16  import (
    17  	"strings"
    18  
    19  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    20  )
    21  
    22  // SchemaState is the state for schema elements.
    23  type SchemaState byte
    24  
    25  const (
    26  	// StateNone means this schema element is absent and can't be used.
    27  	StateNone SchemaState = iota
    28  	// StateDeleteOnly means we can only delete items for this schema element.
    29  	StateDeleteOnly
    30  	// StateWriteOnly means we can use any write operation on this schema element,
    31  	// but outer can't read the changed data.
    32  	StateWriteOnly
    33  	// StateWriteReorganization means we are re-organizating whole data after write only state.
    34  	StateWriteReorganization
    35  	// StateDeleteReorganization means we are re-organizating whole data after delete only state.
    36  	StateDeleteReorganization
    37  	// StatePublic means this schema element is ok for all write and read operations.
    38  	StatePublic
    39  )
    40  
    41  // String implements fmt.Stringer interface.
    42  func (s SchemaState) String() string {
    43  	switch s {
    44  	case StateDeleteOnly:
    45  		return "delete only"
    46  	case StateWriteOnly:
    47  		return "write only"
    48  	case StateWriteReorganization:
    49  		return "write reorganization"
    50  	case StateDeleteReorganization:
    51  		return "delete reorganization"
    52  	case StatePublic:
    53  		return "public"
    54  	default:
    55  		return "none"
    56  	}
    57  }
    58  
    59  // ColumnInfo provides meta data describing of a table column.
    60  type ColumnInfo struct {
    61  	ID              int64       `json:"id"`
    62  	Name            CIStr       `json:"name"`
    63  	Offset          int         `json:"offset"`
    64  	DefaultValue    interface{} `json:"default"`
    65  	types.FieldType `json:"type"`
    66  	State           SchemaState `json:"state"`
    67  	Comment         string      `json:"comment"`
    68  }
    69  
    70  // Clone clones ColumnInfo.
    71  func (c *ColumnInfo) Clone() *ColumnInfo {
    72  	nc := *c
    73  	return &nc
    74  }
    75  
    76  // TableInfo provides meta data describing a DB table.
    77  type TableInfo struct {
    78  	ID      int64  `json:"id"`
    79  	Name    CIStr  `json:"name"`
    80  	Charset string `json:"charset"`
    81  	Collate string `json:"collate"`
    82  	// Columns are listed in the order in which they appear in the schema.
    83  	Columns     []*ColumnInfo `json:"cols"`
    84  	Indices     []*IndexInfo  `json:"index_info"`
    85  	ForeignKeys []*FKInfo     `json:"fk_info"`
    86  	State       SchemaState   `json:"state"`
    87  	PKIsHandle  bool          `json:"pk_is_handle"`
    88  	Comment     string        `json:"comment"`
    89  	AutoIncID   int64         `json:"auto_inc_id"`
    90  }
    91  
    92  // Clone clones TableInfo.
    93  func (t *TableInfo) Clone() *TableInfo {
    94  	nt := *t
    95  	nt.Columns = make([]*ColumnInfo, len(t.Columns))
    96  	nt.Indices = make([]*IndexInfo, len(t.Indices))
    97  	nt.ForeignKeys = make([]*FKInfo, len(t.ForeignKeys))
    98  
    99  	for i := range t.Columns {
   100  		nt.Columns[i] = t.Columns[i].Clone()
   101  	}
   102  
   103  	for i := range t.Indices {
   104  		nt.Indices[i] = t.Indices[i].Clone()
   105  	}
   106  
   107  	for i := range t.ForeignKeys {
   108  		nt.ForeignKeys[i] = t.ForeignKeys[i].Clone()
   109  	}
   110  
   111  	return &nt
   112  }
   113  
   114  // IndexColumn provides index column info.
   115  type IndexColumn struct {
   116  	Name   CIStr `json:"name"`   // Index name
   117  	Offset int   `json:"offset"` // Index offset
   118  	Length int   `json:"length"` // Index length
   119  }
   120  
   121  // Clone clones IndexColumn.
   122  func (i *IndexColumn) Clone() *IndexColumn {
   123  	ni := *i
   124  	return &ni
   125  }
   126  
   127  // IndexType is the type of index
   128  type IndexType int
   129  
   130  // String implements Stringer interface.
   131  func (t IndexType) String() string {
   132  	switch t {
   133  	case IndexTypeBtree:
   134  		return "BTREE"
   135  	case IndexTypeHash:
   136  		return "HASH"
   137  	}
   138  	return ""
   139  }
   140  
   141  // IndexTypes
   142  const (
   143  	IndexTypeBtree IndexType = iota + 1
   144  	IndexTypeHash
   145  )
   146  
   147  // IndexInfo provides meta data describing a DB index.
   148  // It corresponds to the statement `CREATE INDEX Name ON Table (Column);`
   149  // See: https://dev.mysql.com/doc/refman/5.7/en/create-index.html
   150  type IndexInfo struct {
   151  	ID      int64          `json:"id"`
   152  	Name    CIStr          `json:"idx_name"`   // Index name.
   153  	Table   CIStr          `json:"tbl_name"`   // Table name.
   154  	Columns []*IndexColumn `json:"idx_cols"`   // Index columns.
   155  	Unique  bool           `json:"is_unique"`  // Whether the index is unique.
   156  	Primary bool           `json:"is_primary"` // Whether the index is primary key.
   157  	State   SchemaState    `json:"state"`
   158  	Comment string         `json:"comment"`    // Comment
   159  	Tp      IndexType      `json:"index_type"` // Index type: Btree or Hash
   160  }
   161  
   162  // Clone clones IndexInfo.
   163  func (index *IndexInfo) Clone() *IndexInfo {
   164  	ni := *index
   165  	ni.Columns = make([]*IndexColumn, len(index.Columns))
   166  	for i := range index.Columns {
   167  		ni.Columns[i] = index.Columns[i].Clone()
   168  	}
   169  	return &ni
   170  }
   171  
   172  // FKInfo provides meta data describing a foreign key constraint.
   173  type FKInfo struct {
   174  	ID       int64       `json:"id"`
   175  	Name     CIStr       `json:"fk_name"`
   176  	RefTable CIStr       `json:"ref_table"`
   177  	RefCols  []CIStr     `json:"ref_cols"`
   178  	Cols     []CIStr     `json:"cols"`
   179  	OnDelete int         `json:"on_delete"`
   180  	OnUpdate int         `json:"on_update"`
   181  	State    SchemaState `json:"state"`
   182  }
   183  
   184  // Clone clones FKInfo.
   185  func (fk *FKInfo) Clone() *FKInfo {
   186  	nfk := *fk
   187  
   188  	nfk.RefCols = make([]CIStr, len(fk.RefCols))
   189  	nfk.Cols = make([]CIStr, len(fk.Cols))
   190  	copy(nfk.RefCols, fk.RefCols)
   191  	copy(nfk.Cols, fk.Cols)
   192  
   193  	return &nfk
   194  }
   195  
   196  // DBInfo provides meta data describing a DB.
   197  type DBInfo struct {
   198  	ID      int64        `json:"id"`      // Database ID
   199  	Name    CIStr        `json:"db_name"` // DB name.
   200  	Charset string       `json:"charset"`
   201  	Collate string       `json:"collate"`
   202  	Tables  []*TableInfo `json:"-"` // Tables in the DB.
   203  	State   SchemaState  `json:"state"`
   204  }
   205  
   206  // Clone clones DBInfo.
   207  func (db *DBInfo) Clone() *DBInfo {
   208  	newInfo := *db
   209  	newInfo.Tables = make([]*TableInfo, len(db.Tables))
   210  	for i := range db.Tables {
   211  		newInfo.Tables[i] = db.Tables[i].Clone()
   212  	}
   213  	return &newInfo
   214  }
   215  
   216  // CIStr is case insensitive string.
   217  type CIStr struct {
   218  	O string `json:"O"` // Original string.
   219  	L string `json:"L"` // Lower case string.
   220  }
   221  
   222  // String implements fmt.Stringer interface.
   223  func (cis CIStr) String() string {
   224  	return cis.O
   225  }
   226  
   227  // NewCIStr creates a new CIStr.
   228  func NewCIStr(s string) (cs CIStr) {
   229  	cs.O = s
   230  	cs.L = strings.ToLower(s)
   231  	return
   232  }