github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/table/table.go (about) 1 // Copyright 2013 The ql Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSES/QL-LICENSE file. 4 5 // Copyright 2015 PingCAP, Inc. 6 // 7 // Licensed under the Apache License, Version 2.0 (the "License"); 8 // you may not use this file except in compliance with the License. 9 // You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an "AS IS" BASIS, 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package table 19 20 import ( 21 "github.com/insionng/yougam/libraries/juju/errors" 22 "github.com/insionng/yougam/libraries/pingcap/tidb/column" 23 "github.com/insionng/yougam/libraries/pingcap/tidb/context" 24 "github.com/insionng/yougam/libraries/pingcap/tidb/evaluator" 25 "github.com/insionng/yougam/libraries/pingcap/tidb/kv" 26 "github.com/insionng/yougam/libraries/pingcap/tidb/meta/autoid" 27 "github.com/insionng/yougam/libraries/pingcap/tidb/model" 28 "github.com/insionng/yougam/libraries/pingcap/tidb/mysql" 29 "github.com/insionng/yougam/libraries/pingcap/tidb/util/types" 30 ) 31 32 // RecordIterFunc is used for low-level record iteration. 33 type RecordIterFunc func(h int64, rec []types.Datum, cols []*column.Col) (more bool, err error) 34 35 // Table is used to retrieve and modify rows in table. 36 type Table interface { 37 // IterRecords iterates records in the table and calls fn. 38 IterRecords(ctx context.Context, startKey kv.Key, cols []*column.Col, fn RecordIterFunc) error 39 40 // RowWithCols returns a row that contains the given cols. 41 RowWithCols(ctx context.Context, h int64, cols []*column.Col) ([]types.Datum, error) 42 43 // Row returns a row for all columns. 44 Row(ctx context.Context, h int64) ([]types.Datum, error) 45 46 // Cols returns the columns of the table which is used in select. 47 Cols() []*column.Col 48 49 // Indices returns the indices of the table. 50 Indices() []*column.IndexedCol 51 52 // RecordPrefix returns the record key prefix. 53 RecordPrefix() kv.Key 54 55 // IndexPrefix returns the index key prefix. 56 IndexPrefix() kv.Key 57 58 // FirstKey returns the first key. 59 FirstKey() kv.Key 60 61 // RecordKey returns the key in KV storage for the column. 62 RecordKey(h int64, col *column.Col) kv.Key 63 64 // Truncate truncates the table. 65 Truncate(ctx context.Context) (err error) 66 67 // AddRecord inserts a row into the table. 68 AddRecord(ctx context.Context, r []types.Datum) (recordID int64, err error) 69 70 // UpdateRecord updates a row in the table. 71 UpdateRecord(ctx context.Context, h int64, currData []types.Datum, newData []types.Datum, touched map[int]bool) error 72 73 // RemoveRecord removes a row in the table. 74 RemoveRecord(ctx context.Context, h int64, r []types.Datum) error 75 76 // AllocAutoID allocates an auto_increment ID for a new row. 77 AllocAutoID() (int64, error) 78 79 // RebaseAutoID rebases the auto_increment ID base. 80 // If allocIDs is true, it will allocate some IDs and save to the cache. 81 // If allocIDs is false, it will not allocate IDs. 82 RebaseAutoID(newBase int64, allocIDs bool) error 83 84 // Meta returns TableInfo. 85 Meta() *model.TableInfo 86 87 // LockRow locks a row. 88 LockRow(ctx context.Context, h int64, forRead bool) error 89 90 // Seek returns the handle greater or equal to h. 91 Seek(ctx context.Context, h int64) (handle int64, found bool, err error) 92 } 93 94 // TableFromMeta builds a table.Table from *model.TableInfo. 95 // Currently, it is assigned to tables.TableFromMeta in tidb package's init function. 96 var TableFromMeta func(alloc autoid.Allocator, tblInfo *model.TableInfo) (Table, error) 97 98 // GetColDefaultValue gets default value of the column. 99 func GetColDefaultValue(ctx context.Context, col *model.ColumnInfo) (types.Datum, bool, error) { 100 // Check no default value flag. 101 if mysql.HasNoDefaultValueFlag(col.Flag) && col.Tp != mysql.TypeEnum { 102 return types.Datum{}, false, errors.Errorf("Field '%s' doesn't have a default value", col.Name) 103 } 104 105 // Check and get timestamp/datetime default value. 106 if col.Tp == mysql.TypeTimestamp || col.Tp == mysql.TypeDatetime { 107 if col.DefaultValue == nil { 108 return types.Datum{}, true, nil 109 } 110 111 value, err := evaluator.GetTimeValue(ctx, col.DefaultValue, col.Tp, col.Decimal) 112 if err != nil { 113 return types.Datum{}, true, errors.Errorf("Field '%s' get default value fail - %s", col.Name, errors.Trace(err)) 114 } 115 return value, true, nil 116 } else if col.Tp == mysql.TypeEnum { 117 // For enum type, if no default value and not null is set, 118 // the default value is the first element of the enum list 119 if col.DefaultValue == nil && mysql.HasNotNullFlag(col.Flag) { 120 return types.NewDatum(col.FieldType.Elems[0]), true, nil 121 } 122 } 123 124 return types.NewDatum(col.DefaultValue), true, nil 125 } 126 127 // MockTableFromMeta only serves for test. 128 var MockTableFromMeta func(tableInfo *model.TableInfo) Table