2016-04-04 10:27:51 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 GitHub Inc.
|
2016-05-16 09:09:17 +00:00
|
|
|
See https://github.com/github/gh-ost/blob/master/LICENSE
|
2016-04-04 10:27:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2016-09-07 12:24:11 +00:00
|
|
|
"strconv"
|
2016-04-04 10:27:51 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-10-13 11:12:33 +00:00
|
|
|
type ColumnType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
UnknownColumnType ColumnType = iota
|
|
|
|
TimestampColumnType = iota
|
|
|
|
DateTimeColumnType = iota
|
2017-01-10 07:57:42 +00:00
|
|
|
EnumColumnType = iota
|
|
|
|
MediumIntColumnType = iota
|
2017-08-22 08:51:20 +00:00
|
|
|
JSONColumnType = iota
|
2016-10-13 11:12:33 +00:00
|
|
|
)
|
|
|
|
|
2017-01-10 07:57:42 +00:00
|
|
|
const maxMediumintUnsigned int32 = 16777215
|
|
|
|
|
2016-10-13 11:12:33 +00:00
|
|
|
type TimezoneConvertion struct {
|
|
|
|
ToTimezone string
|
|
|
|
}
|
|
|
|
|
2016-09-07 12:24:11 +00:00
|
|
|
type Column struct {
|
2016-10-13 11:12:33 +00:00
|
|
|
Name string
|
|
|
|
IsUnsigned bool
|
|
|
|
Charset string
|
|
|
|
Type ColumnType
|
|
|
|
timezoneConversion *TimezoneConvertion
|
2016-09-07 12:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Column) convertArg(arg interface{}) interface{} {
|
|
|
|
if s, ok := arg.(string); ok {
|
2016-09-08 07:37:22 +00:00
|
|
|
// string, charset conversion
|
|
|
|
if encoding, ok := charsetEncodingMap[this.Charset]; ok {
|
|
|
|
arg, _ = encoding.NewDecoder().String(s)
|
2016-09-07 12:24:11 +00:00
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
if this.IsUnsigned {
|
|
|
|
if i, ok := arg.(int8); ok {
|
|
|
|
return uint8(i)
|
|
|
|
}
|
|
|
|
if i, ok := arg.(int16); ok {
|
|
|
|
return uint16(i)
|
|
|
|
}
|
|
|
|
if i, ok := arg.(int32); ok {
|
2017-01-10 07:57:42 +00:00
|
|
|
if this.Type == MediumIntColumnType {
|
|
|
|
// problem with mediumint is that it's a 3-byte type. There is no compatible golang type to match that.
|
|
|
|
// So to convert from negative to positive we'd need to convert the value manually
|
|
|
|
if i >= 0 {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
return uint32(maxMediumintUnsigned + i + 1)
|
|
|
|
}
|
2016-09-07 12:24:11 +00:00
|
|
|
return uint32(i)
|
|
|
|
}
|
|
|
|
if i, ok := arg.(int64); ok {
|
|
|
|
return strconv.FormatUint(uint64(i), 10)
|
|
|
|
}
|
|
|
|
if i, ok := arg.(int); ok {
|
|
|
|
return uint(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewColumns(names []string) []Column {
|
|
|
|
result := make([]Column, len(names))
|
|
|
|
for i := range names {
|
|
|
|
result[i].Name = names[i]
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseColumns(names string) []Column {
|
|
|
|
namesArray := strings.Split(names, ",")
|
|
|
|
return NewColumns(namesArray)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ColumnsMap maps a column name onto its ordinal position
|
2016-04-11 15:27:16 +00:00
|
|
|
type ColumnsMap map[string]int
|
|
|
|
|
2016-08-17 04:49:20 +00:00
|
|
|
func NewEmptyColumnsMap() ColumnsMap {
|
2016-04-11 15:27:16 +00:00
|
|
|
columnsMap := make(map[string]int)
|
2016-08-17 04:49:20 +00:00
|
|
|
return ColumnsMap(columnsMap)
|
|
|
|
}
|
|
|
|
|
2016-09-07 12:24:11 +00:00
|
|
|
func NewColumnsMap(orderedColumns []Column) ColumnsMap {
|
2016-08-17 04:49:20 +00:00
|
|
|
columnsMap := NewEmptyColumnsMap()
|
2016-09-07 12:24:11 +00:00
|
|
|
for i, column := range orderedColumns {
|
|
|
|
columnsMap[column.Name] = i
|
2016-04-11 15:27:16 +00:00
|
|
|
}
|
2016-08-17 04:49:20 +00:00
|
|
|
return columnsMap
|
2016-04-11 15:27:16 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 10:27:51 +00:00
|
|
|
// ColumnList makes for a named list of columns
|
2016-04-11 15:27:16 +00:00
|
|
|
type ColumnList struct {
|
2016-09-07 12:24:11 +00:00
|
|
|
columns []Column
|
|
|
|
Ordinals ColumnsMap
|
2016-04-11 15:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewColumnList creates an object given ordered list of column names
|
|
|
|
func NewColumnList(names []string) *ColumnList {
|
|
|
|
result := &ColumnList{
|
2016-09-07 12:24:11 +00:00
|
|
|
columns: NewColumns(names),
|
2016-04-11 15:27:16 +00:00
|
|
|
}
|
2016-09-07 12:24:11 +00:00
|
|
|
result.Ordinals = NewColumnsMap(result.columns)
|
2016-04-11 15:27:16 +00:00
|
|
|
return result
|
|
|
|
}
|
2016-04-04 10:27:51 +00:00
|
|
|
|
|
|
|
// ParseColumnList parses a comma delimited list of column names
|
2016-09-07 12:24:11 +00:00
|
|
|
func ParseColumnList(names string) *ColumnList {
|
2016-04-11 15:27:16 +00:00
|
|
|
result := &ColumnList{
|
2016-09-07 12:24:11 +00:00
|
|
|
columns: ParseColumns(names),
|
2016-04-11 15:27:16 +00:00
|
|
|
}
|
2016-09-07 12:24:11 +00:00
|
|
|
result.Ordinals = NewColumnsMap(result.columns)
|
2016-04-11 15:27:16 +00:00
|
|
|
return result
|
2016-04-04 10:27:51 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 12:24:11 +00:00
|
|
|
func (this *ColumnList) Columns() []Column {
|
|
|
|
return this.columns
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ColumnList) Names() []string {
|
|
|
|
names := make([]string, len(this.columns))
|
|
|
|
for i := range this.columns {
|
|
|
|
names[i] = this.columns[i].Name
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2016-10-13 11:12:33 +00:00
|
|
|
func (this *ColumnList) GetColumn(columnName string) *Column {
|
|
|
|
if ordinal, ok := this.Ordinals[columnName]; ok {
|
|
|
|
return &this.columns[ordinal]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-17 04:49:20 +00:00
|
|
|
func (this *ColumnList) SetUnsigned(columnName string) {
|
2016-10-13 11:12:33 +00:00
|
|
|
this.GetColumn(columnName).IsUnsigned = true
|
2016-08-17 04:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ColumnList) IsUnsigned(columnName string) bool {
|
2016-10-13 11:12:33 +00:00
|
|
|
return this.GetColumn(columnName).IsUnsigned
|
2016-09-07 12:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ColumnList) SetCharset(columnName string, charset string) {
|
2016-10-13 11:12:33 +00:00
|
|
|
this.GetColumn(columnName).Charset = charset
|
2016-09-07 12:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ColumnList) GetCharset(columnName string) string {
|
2016-10-13 11:12:33 +00:00
|
|
|
return this.GetColumn(columnName).Charset
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ColumnList) SetColumnType(columnName string, columnType ColumnType) {
|
|
|
|
this.GetColumn(columnName).Type = columnType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ColumnList) GetColumnType(columnName string) ColumnType {
|
|
|
|
return this.GetColumn(columnName).Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ColumnList) SetConvertDatetimeToTimestamp(columnName string, toTimezone string) {
|
|
|
|
this.GetColumn(columnName).timezoneConversion = &TimezoneConvertion{ToTimezone: toTimezone}
|
2016-08-17 04:49:20 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 07:33:53 +00:00
|
|
|
func (this *ColumnList) HasTimezoneConversion(columnName string) bool {
|
|
|
|
return this.GetColumn(columnName).timezoneConversion != nil
|
|
|
|
}
|
|
|
|
|
2016-04-04 10:27:51 +00:00
|
|
|
func (this *ColumnList) String() string {
|
2016-09-07 12:24:11 +00:00
|
|
|
return strings.Join(this.Names(), ",")
|
2016-04-04 10:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ColumnList) Equals(other *ColumnList) bool {
|
2016-09-07 12:24:11 +00:00
|
|
|
return reflect.DeepEqual(this.Columns, other.Columns)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ColumnList) EqualsByNames(other *ColumnList) bool {
|
|
|
|
return reflect.DeepEqual(this.Names(), other.Names())
|
2016-04-04 10:27:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 15:27:16 +00:00
|
|
|
// IsSubsetOf returns 'true' when column names of this list are a subset of
|
|
|
|
// another list, in arbitrary order (order agnostic)
|
|
|
|
func (this *ColumnList) IsSubsetOf(other *ColumnList) bool {
|
2016-09-07 12:24:11 +00:00
|
|
|
for _, column := range this.columns {
|
|
|
|
if _, exists := other.Ordinals[column.Name]; !exists {
|
2016-04-11 15:27:16 +00:00
|
|
|
return false
|
|
|
|
}
|
2016-04-08 12:35:06 +00:00
|
|
|
}
|
2016-04-11 15:27:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ColumnList) Len() int {
|
2016-09-07 12:24:11 +00:00
|
|
|
return len(this.columns)
|
2016-04-08 12:35:06 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 10:27:51 +00:00
|
|
|
// UniqueKey is the combination of a key's name and columns
|
|
|
|
type UniqueKey struct {
|
2016-04-18 17:57:18 +00:00
|
|
|
Name string
|
|
|
|
Columns ColumnList
|
|
|
|
HasNullable bool
|
|
|
|
IsAutoIncrement bool
|
2016-04-04 10:27:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 16:44:54 +00:00
|
|
|
// IsPrimary checks if this unique key is primary
|
2016-04-04 10:27:51 +00:00
|
|
|
func (this *UniqueKey) IsPrimary() bool {
|
|
|
|
return this.Name == "PRIMARY"
|
|
|
|
}
|
|
|
|
|
2016-04-11 15:27:16 +00:00
|
|
|
func (this *UniqueKey) Len() int {
|
|
|
|
return this.Columns.Len()
|
|
|
|
}
|
|
|
|
|
2016-04-04 10:27:51 +00:00
|
|
|
func (this *UniqueKey) String() string {
|
2016-04-18 17:57:18 +00:00
|
|
|
description := this.Name
|
|
|
|
if this.IsAutoIncrement {
|
2016-08-05 15:46:38 +00:00
|
|
|
description = fmt.Sprintf("%s (auto_increment)", description)
|
2016-04-18 17:57:18 +00:00
|
|
|
}
|
2016-09-07 12:24:11 +00:00
|
|
|
return fmt.Sprintf("%s: %s; has nullable: %+v", description, this.Columns.Names(), this.HasNullable)
|
2016-04-04 10:27:51 +00:00
|
|
|
}
|
2016-04-04 16:19:46 +00:00
|
|
|
|
|
|
|
type ColumnValues struct {
|
|
|
|
abstractValues []interface{}
|
|
|
|
ValuesPointers []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewColumnValues(length int) *ColumnValues {
|
|
|
|
result := &ColumnValues{
|
|
|
|
abstractValues: make([]interface{}, length),
|
|
|
|
ValuesPointers: make([]interface{}, length),
|
|
|
|
}
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
result.ValuesPointers[i] = &result.abstractValues[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-04-06 16:44:54 +00:00
|
|
|
func ToColumnValues(abstractValues []interface{}) *ColumnValues {
|
|
|
|
result := &ColumnValues{
|
|
|
|
abstractValues: abstractValues,
|
|
|
|
ValuesPointers: make([]interface{}, len(abstractValues)),
|
|
|
|
}
|
|
|
|
for i := 0; i < len(abstractValues); i++ {
|
|
|
|
result.ValuesPointers[i] = &result.abstractValues[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-04-04 16:19:46 +00:00
|
|
|
func (this *ColumnValues) AbstractValues() []interface{} {
|
|
|
|
return this.abstractValues
|
|
|
|
}
|
|
|
|
|
2016-04-07 13:57:12 +00:00
|
|
|
func (this *ColumnValues) StringColumn(index int) string {
|
|
|
|
val := this.AbstractValues()[index]
|
|
|
|
if ints, ok := val.([]uint8); ok {
|
|
|
|
return string(ints)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%+v", val)
|
|
|
|
}
|
|
|
|
|
2016-04-04 16:19:46 +00:00
|
|
|
func (this *ColumnValues) String() string {
|
|
|
|
stringValues := []string{}
|
2016-04-07 13:57:12 +00:00
|
|
|
for i := range this.AbstractValues() {
|
|
|
|
stringValues = append(stringValues, this.StringColumn(i))
|
2016-04-04 16:19:46 +00:00
|
|
|
}
|
|
|
|
return strings.Join(stringValues, ",")
|
|
|
|
}
|