github.com/dolthub/go-mysql-server@v0.18.0/sql/column.go (about)

     1  // Copyright 2020-2021 Dolthub, 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package sql
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  	"strings"
    21  )
    22  
    23  // Column is the definition of a table column.
    24  // As SQL:2016 puts it:
    25  //
    26  //	A column is a named component of a table. It has a data type, a default,
    27  //	and a nullability characteristic.
    28  type Column struct {
    29  	// Name is the name of the column.
    30  	Name string
    31  	// Type is the data type of the column.
    32  	Type Type
    33  	// Default contains the default value of the column or nil if it was not explicitly defined.
    34  	Default *ColumnDefaultValue
    35  	// AutoIncrement is true if the column auto-increments.
    36  	AutoIncrement bool
    37  	// Nullable is true if the column can contain NULL values, or false
    38  	// otherwise.
    39  	Nullable bool
    40  	// Source is the name of the table this column came from.
    41  	Source string
    42  	// DatabaseSource is the name of the database this column came from.
    43  	DatabaseSource string
    44  	// PrimaryKey is true if the column is part of the primary key for its table.
    45  	PrimaryKey bool
    46  	// Comment contains the string comment for this column.
    47  	Comment string
    48  	// Extra contains any additional information to put in the `extra` column under `information_schema.columns`.
    49  	Extra string
    50  	// Generated is non-nil if the column is defined with a generated value. Mutually exclusive with Default
    51  	Generated *ColumnDefaultValue
    52  	// Virtual is true if the column is defined as a virtual column. Generated must be non-nil in this case.
    53  	// Virtual column values will be provided for write operations, in case integrators need to use them to update
    54  	// indexes, but must not be returned in rows from tables that include them.
    55  	Virtual bool
    56  	// OnUpdate contains the on update value of the column or nil if it was not explicitly defined.
    57  	OnUpdate *ColumnDefaultValue
    58  }
    59  
    60  // Check ensures the value is correct for this column.
    61  func (c *Column) Check(v interface{}) bool {
    62  	if v == nil {
    63  		return c.Nullable
    64  	}
    65  
    66  	_, _, err := c.Type.Convert(v)
    67  	return err == nil
    68  }
    69  
    70  // Equals checks whether two columns are equal.
    71  func (c *Column) Equals(c2 *Column) bool {
    72  	if c.Source != "" {
    73  		return strings.EqualFold(c.Name, c2.Name) &&
    74  			strings.EqualFold(c.Source, c2.Source) &&
    75  			strings.EqualFold(c.DatabaseSource, c2.DatabaseSource) &&
    76  			c.Nullable == c2.Nullable &&
    77  			reflect.DeepEqual(c.Default, c2.Default) &&
    78  			reflect.DeepEqual(c.Type, c2.Type)
    79  	}
    80  	return c.Name == c2.Name &&
    81  		strings.EqualFold(c.Source, c2.Source) &&
    82  		strings.EqualFold(c.DatabaseSource, c2.DatabaseSource) &&
    83  		c.Nullable == c2.Nullable &&
    84  		reflect.DeepEqual(c.Default, c2.Default) &&
    85  		reflect.DeepEqual(c.Type, c2.Type)
    86  }
    87  
    88  func (c *Column) DebugString() string {
    89  	sb := strings.Builder{}
    90  	sb.WriteString("Name: ")
    91  	sb.WriteString(c.Name)
    92  	sb.WriteString(", ")
    93  	sb.WriteString("Source: ")
    94  	sb.WriteString(c.Source)
    95  	sb.WriteString(", ")
    96  	sb.WriteString("Type: ")
    97  	sb.WriteString(c.Type.String())
    98  	sb.WriteString(", ")
    99  	sb.WriteString("PrimaryKey: ")
   100  	sb.WriteString(fmt.Sprintf("%v", c.PrimaryKey))
   101  	sb.WriteString(", ")
   102  	sb.WriteString("Nullable: ")
   103  	sb.WriteString(fmt.Sprintf("%v", c.Nullable))
   104  	sb.WriteString(", ")
   105  	sb.WriteString("Comment: ")
   106  	sb.WriteString(c.Comment)
   107  	sb.WriteString(", ")
   108  	sb.WriteString("Default: ")
   109  	sb.WriteString(DebugString(c.Default))
   110  	sb.WriteString("Generated: ")
   111  	sb.WriteString(DebugString(c.Generated))
   112  	sb.WriteString(", ")
   113  	sb.WriteString("AutoIncrement: ")
   114  	sb.WriteString(fmt.Sprintf("%v", c.AutoIncrement))
   115  	sb.WriteString(", ")
   116  	sb.WriteString("Extra: ")
   117  	sb.WriteString(c.Extra)
   118  
   119  	return sb.String()
   120  }
   121  
   122  func (c Column) Copy() *Column {
   123  	// Create a copy of the default and generated column, rather than referencing the same pointer
   124  	if c.Default != nil {
   125  		c.Default = &(*c.Default)
   126  	}
   127  	if c.Generated != nil {
   128  		c.Generated = &(*c.Generated)
   129  	}
   130  	return &c
   131  }
   132  
   133  // TableId is the unique identifier of a table or table alias in a multi-db environment.
   134  // The long-term goal is to migrate all uses of table name strings to this and minimize places where we
   135  // construct/inspect TableIDs. By treating this as an opaque identifier, it will be easier to migrate to
   136  // a system where we compute IDs once during plan building.
   137  // For aliases, DatabaseName is nil.
   138  type TableId uint16
   139  
   140  func (i TableId) IsEmpty() bool {
   141  	return i == 0
   142  }