| 1 |
package migrator |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql" |
| 5 |
"reflect" |
| 6 |
) |
| 7 |
|
| 8 |
// ColumnType column type implements ColumnType interface |
| 9 |
type ColumnType struct { |
| 10 |
SQLColumnType *sql.ColumnType |
| 11 |
NameValue sql.NullString |
| 12 |
DataTypeValue sql.NullString |
| 13 |
ColumnTypeValue sql.NullString |
| 14 |
PrimaryKeyValue sql.NullBool |
| 15 |
UniqueValue sql.NullBool |
| 16 |
AutoIncrementValue sql.NullBool |
| 17 |
LengthValue sql.NullInt64 |
| 18 |
DecimalSizeValue sql.NullInt64 |
| 19 |
ScaleValue sql.NullInt64 |
| 20 |
NullableValue sql.NullBool |
| 21 |
ScanTypeValue reflect.Type |
| 22 |
CommentValue sql.NullString |
| 23 |
DefaultValueValue sql.NullString |
| 24 |
} |
| 25 |
|
| 26 |
// Name returns the name or alias of the column. |
| 27 |
func (ct ColumnType) Name() string { |
| 28 |
if ct.NameValue.Valid { |
| 29 |
return ct.NameValue.String |
| 30 |
} |
| 31 |
return ct.SQLColumnType.Name() |
| 32 |
} |
| 33 |
|
| 34 |
// DatabaseTypeName returns the database system name of the column type. If an empty |
| 35 |
// string is returned, then the driver type name is not supported. |
| 36 |
// Consult your driver documentation for a list of driver data types. Length specifiers |
| 37 |
// are not included. |
| 38 |
// Common type names include "VARCHAR", "TEXT", "NVARCHAR", "DECIMAL", "BOOL", |
| 39 |
// "INT", and "BIGINT". |
| 40 |
func (ct ColumnType) DatabaseTypeName() string { |
| 41 |
if ct.DataTypeValue.Valid { |
| 42 |
return ct.DataTypeValue.String |
| 43 |
} |
| 44 |
return ct.SQLColumnType.DatabaseTypeName() |
| 45 |
} |
| 46 |
|
| 47 |
// ColumnType returns the database type of the column. like `varchar(16)` |
| 48 |
func (ct ColumnType) ColumnType() (columnType string, ok bool) { |
| 49 |
return ct.ColumnTypeValue.String, ct.ColumnTypeValue.Valid |
| 50 |
} |
| 51 |
|
| 52 |
// PrimaryKey returns the column is primary key or not. |
| 53 |
func (ct ColumnType) PrimaryKey() (isPrimaryKey bool, ok bool) { |
| 54 |
return ct.PrimaryKeyValue.Bool, ct.PrimaryKeyValue.Valid |
| 55 |
} |
| 56 |
|
| 57 |
// AutoIncrement returns the column is auto increment or not. |
| 58 |
func (ct ColumnType) AutoIncrement() (isAutoIncrement bool, ok bool) { |
| 59 |
return ct.AutoIncrementValue.Bool, ct.AutoIncrementValue.Valid |
| 60 |
} |
| 61 |
|
| 62 |
// Length returns the column type length for variable length column types |
| 63 |
func (ct ColumnType) Length() (length int64, ok bool) { |
| 64 |
if ct.LengthValue.Valid { |
| 65 |
return ct.LengthValue.Int64, true |
| 66 |
} |
| 67 |
return ct.SQLColumnType.Length() |
| 68 |
} |
| 69 |
|
| 70 |
// DecimalSize returns the scale and precision of a decimal type. |
| 71 |
func (ct ColumnType) DecimalSize() (precision int64, scale int64, ok bool) { |
| 72 |
if ct.DecimalSizeValue.Valid { |
| 73 |
return ct.DecimalSizeValue.Int64, ct.ScaleValue.Int64, true |
| 74 |
} |
| 75 |
return ct.SQLColumnType.DecimalSize() |
| 76 |
} |
| 77 |
|
| 78 |
// Nullable reports whether the column may be null. |
| 79 |
func (ct ColumnType) Nullable() (nullable bool, ok bool) { |
| 80 |
if ct.NullableValue.Valid { |
| 81 |
return ct.NullableValue.Bool, true |
| 82 |
} |
| 83 |
return ct.SQLColumnType.Nullable() |
| 84 |
} |
| 85 |
|
| 86 |
// Unique reports whether the column may be unique. |
| 87 |
func (ct ColumnType) Unique() (unique bool, ok bool) { |
| 88 |
return ct.UniqueValue.Bool, ct.UniqueValue.Valid |
| 89 |
} |
| 90 |
|
| 91 |
// ScanType returns a Go type suitable for scanning into using Rows.Scan. |
| 92 |
func (ct ColumnType) ScanType() reflect.Type { |
| 93 |
if ct.ScanTypeValue != nil { |
| 94 |
return ct.ScanTypeValue |
| 95 |
} |
| 96 |
return ct.SQLColumnType.ScanType() |
| 97 |
} |
| 98 |
|
| 99 |
// Comment returns the comment of current column. |
| 100 |
func (ct ColumnType) Comment() (value string, ok bool) { |
| 101 |
return ct.CommentValue.String, ct.CommentValue.Valid |
| 102 |
} |
| 103 |
|
| 104 |
// DefaultValue returns the default value of current column. |
| 105 |
func (ct ColumnType) DefaultValue() (value string, ok bool) { |
| 106 |
return ct.DefaultValueValue.String, ct.DefaultValueValue.Valid |
| 107 |
} |