forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-sql-driver/mysql/fields.go

353 lines
4.7 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This Source Code Form is subject to the terms of the Mozilla Public
2024-02-18 10:42:21 +00:00
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
2024-02-18 10:42:21 +00:00
// You can obtain one at http://mozilla.org/MPL/2.0/.
package mysql
import (
"database/sql"
"reflect"
)
func (mf *mysqlField) typeDatabaseName() string {
2024-02-18 10:42:21 +00:00
switch mf.fieldType {
2024-02-18 10:42:21 +00:00
case fieldTypeBit:
2024-02-18 10:42:21 +00:00
return "BIT"
2024-02-18 10:42:21 +00:00
case fieldTypeBLOB:
2024-02-18 10:42:21 +00:00
if mf.charSet != collations[binaryCollation] {
2024-02-18 10:42:21 +00:00
return "TEXT"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return "BLOB"
2024-02-18 10:42:21 +00:00
case fieldTypeDate:
2024-02-18 10:42:21 +00:00
return "DATE"
2024-02-18 10:42:21 +00:00
case fieldTypeDateTime:
2024-02-18 10:42:21 +00:00
return "DATETIME"
2024-02-18 10:42:21 +00:00
case fieldTypeDecimal:
2024-02-18 10:42:21 +00:00
return "DECIMAL"
2024-02-18 10:42:21 +00:00
case fieldTypeDouble:
2024-02-18 10:42:21 +00:00
return "DOUBLE"
2024-02-18 10:42:21 +00:00
case fieldTypeEnum:
2024-02-18 10:42:21 +00:00
return "ENUM"
2024-02-18 10:42:21 +00:00
case fieldTypeFloat:
2024-02-18 10:42:21 +00:00
return "FLOAT"
2024-02-18 10:42:21 +00:00
case fieldTypeGeometry:
2024-02-18 10:42:21 +00:00
return "GEOMETRY"
2024-02-18 10:42:21 +00:00
case fieldTypeInt24:
2024-02-18 10:42:21 +00:00
return "MEDIUMINT"
2024-02-18 10:42:21 +00:00
case fieldTypeJSON:
2024-02-18 10:42:21 +00:00
return "JSON"
2024-02-18 10:42:21 +00:00
case fieldTypeLong:
2024-02-18 10:42:21 +00:00
return "INT"
2024-02-18 10:42:21 +00:00
case fieldTypeLongBLOB:
2024-02-18 10:42:21 +00:00
if mf.charSet != collations[binaryCollation] {
2024-02-18 10:42:21 +00:00
return "LONGTEXT"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return "LONGBLOB"
2024-02-18 10:42:21 +00:00
case fieldTypeLongLong:
2024-02-18 10:42:21 +00:00
return "BIGINT"
2024-02-18 10:42:21 +00:00
case fieldTypeMediumBLOB:
2024-02-18 10:42:21 +00:00
if mf.charSet != collations[binaryCollation] {
2024-02-18 10:42:21 +00:00
return "MEDIUMTEXT"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return "MEDIUMBLOB"
2024-02-18 10:42:21 +00:00
case fieldTypeNewDate:
2024-02-18 10:42:21 +00:00
return "DATE"
2024-02-18 10:42:21 +00:00
case fieldTypeNewDecimal:
2024-02-18 10:42:21 +00:00
return "DECIMAL"
2024-02-18 10:42:21 +00:00
case fieldTypeNULL:
2024-02-18 10:42:21 +00:00
return "NULL"
2024-02-18 10:42:21 +00:00
case fieldTypeSet:
2024-02-18 10:42:21 +00:00
return "SET"
2024-02-18 10:42:21 +00:00
case fieldTypeShort:
2024-02-18 10:42:21 +00:00
return "SMALLINT"
2024-02-18 10:42:21 +00:00
case fieldTypeString:
2024-02-18 10:42:21 +00:00
if mf.charSet == collations[binaryCollation] {
2024-02-18 10:42:21 +00:00
return "BINARY"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return "CHAR"
2024-02-18 10:42:21 +00:00
case fieldTypeTime:
2024-02-18 10:42:21 +00:00
return "TIME"
2024-02-18 10:42:21 +00:00
case fieldTypeTimestamp:
2024-02-18 10:42:21 +00:00
return "TIMESTAMP"
2024-02-18 10:42:21 +00:00
case fieldTypeTiny:
2024-02-18 10:42:21 +00:00
return "TINYINT"
2024-02-18 10:42:21 +00:00
case fieldTypeTinyBLOB:
2024-02-18 10:42:21 +00:00
if mf.charSet != collations[binaryCollation] {
2024-02-18 10:42:21 +00:00
return "TINYTEXT"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return "TINYBLOB"
2024-02-18 10:42:21 +00:00
case fieldTypeVarChar:
2024-02-18 10:42:21 +00:00
if mf.charSet == collations[binaryCollation] {
2024-02-18 10:42:21 +00:00
return "VARBINARY"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return "VARCHAR"
2024-02-18 10:42:21 +00:00
case fieldTypeVarString:
2024-02-18 10:42:21 +00:00
if mf.charSet == collations[binaryCollation] {
2024-02-18 10:42:21 +00:00
return "VARBINARY"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return "VARCHAR"
2024-02-18 10:42:21 +00:00
case fieldTypeYear:
2024-02-18 10:42:21 +00:00
return "YEAR"
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
var (
scanTypeFloat32 = reflect.TypeOf(float32(0))
scanTypeFloat64 = reflect.TypeOf(float64(0))
scanTypeInt8 = reflect.TypeOf(int8(0))
scanTypeInt16 = reflect.TypeOf(int16(0))
scanTypeInt32 = reflect.TypeOf(int32(0))
scanTypeInt64 = reflect.TypeOf(int64(0))
2024-02-18 10:42:21 +00:00
scanTypeNullFloat = reflect.TypeOf(sql.NullFloat64{})
scanTypeNullInt = reflect.TypeOf(sql.NullInt64{})
scanTypeNullTime = reflect.TypeOf(nullTime{})
scanTypeUint8 = reflect.TypeOf(uint8(0))
scanTypeUint16 = reflect.TypeOf(uint16(0))
scanTypeUint32 = reflect.TypeOf(uint32(0))
scanTypeUint64 = reflect.TypeOf(uint64(0))
scanTypeRawBytes = reflect.TypeOf(sql.RawBytes{})
scanTypeUnknown = reflect.TypeOf(new(interface{}))
2024-02-18 10:42:21 +00:00
)
type mysqlField struct {
tableName string
name string
length uint32
flags fieldFlag
2024-02-18 10:42:21 +00:00
fieldType fieldType
decimals byte
charSet uint8
2024-02-18 10:42:21 +00:00
}
func (mf *mysqlField) scanType() reflect.Type {
2024-02-18 10:42:21 +00:00
switch mf.fieldType {
2024-02-18 10:42:21 +00:00
case fieldTypeTiny:
2024-02-18 10:42:21 +00:00
if mf.flags&flagNotNULL != 0 {
2024-02-18 10:42:21 +00:00
if mf.flags&flagUnsigned != 0 {
2024-02-18 10:42:21 +00:00
return scanTypeUint8
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return scanTypeInt8
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return scanTypeNullInt
case fieldTypeShort, fieldTypeYear:
2024-02-18 10:42:21 +00:00
if mf.flags&flagNotNULL != 0 {
2024-02-18 10:42:21 +00:00
if mf.flags&flagUnsigned != 0 {
2024-02-18 10:42:21 +00:00
return scanTypeUint16
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return scanTypeInt16
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return scanTypeNullInt
case fieldTypeInt24, fieldTypeLong:
2024-02-18 10:42:21 +00:00
if mf.flags&flagNotNULL != 0 {
2024-02-18 10:42:21 +00:00
if mf.flags&flagUnsigned != 0 {
2024-02-18 10:42:21 +00:00
return scanTypeUint32
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return scanTypeInt32
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return scanTypeNullInt
case fieldTypeLongLong:
2024-02-18 10:42:21 +00:00
if mf.flags&flagNotNULL != 0 {
2024-02-18 10:42:21 +00:00
if mf.flags&flagUnsigned != 0 {
2024-02-18 10:42:21 +00:00
return scanTypeUint64
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return scanTypeInt64
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return scanTypeNullInt
case fieldTypeFloat:
2024-02-18 10:42:21 +00:00
if mf.flags&flagNotNULL != 0 {
2024-02-18 10:42:21 +00:00
return scanTypeFloat32
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return scanTypeNullFloat
case fieldTypeDouble:
2024-02-18 10:42:21 +00:00
if mf.flags&flagNotNULL != 0 {
2024-02-18 10:42:21 +00:00
return scanTypeFloat64
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return scanTypeNullFloat
case fieldTypeDecimal, fieldTypeNewDecimal, fieldTypeVarChar,
2024-02-18 10:42:21 +00:00
fieldTypeBit, fieldTypeEnum, fieldTypeSet, fieldTypeTinyBLOB,
2024-02-18 10:42:21 +00:00
fieldTypeMediumBLOB, fieldTypeLongBLOB, fieldTypeBLOB,
2024-02-18 10:42:21 +00:00
fieldTypeVarString, fieldTypeString, fieldTypeGeometry, fieldTypeJSON,
2024-02-18 10:42:21 +00:00
fieldTypeTime:
2024-02-18 10:42:21 +00:00
return scanTypeRawBytes
case fieldTypeDate, fieldTypeNewDate,
2024-02-18 10:42:21 +00:00
fieldTypeTimestamp, fieldTypeDateTime:
2024-02-18 10:42:21 +00:00
// NullTime is always returned for more consistent behavior as it can
2024-02-18 10:42:21 +00:00
// handle both cases of parseTime regardless if the field is nullable.
2024-02-18 10:42:21 +00:00
return scanTypeNullTime
default:
2024-02-18 10:42:21 +00:00
return scanTypeUnknown
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}