add column type unsigned
This commit is contained in:
parent
0c1b815227
commit
50deb79581
|
@ -354,10 +354,13 @@ func (db *mysql) GetColumns(queryer core.Queryer, ctx context.Context, tableName
|
||||||
cts := strings.Split(colType, "(")
|
cts := strings.Split(colType, "(")
|
||||||
colName := cts[0]
|
colName := cts[0]
|
||||||
colType = strings.ToUpper(colName)
|
colType = strings.ToUpper(colName)
|
||||||
|
extra = strings.ToUpper(extra)
|
||||||
var len1, len2 int
|
var len1, len2 int
|
||||||
if len(cts) == 2 {
|
if len(cts) == 2 {
|
||||||
idx := strings.Index(cts[1], ")")
|
idx := strings.Index(cts[1], ")")
|
||||||
if colType == schemas.Enum && cts[1][0] == '\'' { // enum
|
switch colType {
|
||||||
|
case schemas.Enum:
|
||||||
|
if cts[1][0] == '\'' {
|
||||||
options := strings.Split(cts[1][0:idx], ",")
|
options := strings.Split(cts[1][0:idx], ",")
|
||||||
col.EnumOptions = make(map[string]int)
|
col.EnumOptions = make(map[string]int)
|
||||||
for k, v := range options {
|
for k, v := range options {
|
||||||
|
@ -365,7 +368,9 @@ func (db *mysql) GetColumns(queryer core.Queryer, ctx context.Context, tableName
|
||||||
v = strings.Trim(v, "'")
|
v = strings.Trim(v, "'")
|
||||||
col.EnumOptions[v] = k
|
col.EnumOptions[v] = k
|
||||||
}
|
}
|
||||||
} else if colType == schemas.Set && cts[1][0] == '\'' {
|
}
|
||||||
|
case schemas.Set:
|
||||||
|
if cts[1][0] == '\'' {
|
||||||
options := strings.Split(cts[1][0:idx], ",")
|
options := strings.Split(cts[1][0:idx], ",")
|
||||||
col.SetOptions = make(map[string]int)
|
col.SetOptions = make(map[string]int)
|
||||||
for k, v := range options {
|
for k, v := range options {
|
||||||
|
@ -373,7 +378,8 @@ func (db *mysql) GetColumns(queryer core.Queryer, ctx context.Context, tableName
|
||||||
v = strings.Trim(v, "'")
|
v = strings.Trim(v, "'")
|
||||||
col.SetOptions[v] = k
|
col.SetOptions[v] = k
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
default:
|
||||||
lens := strings.Split(cts[1][0:idx], ",")
|
lens := strings.Split(cts[1][0:idx], ",")
|
||||||
len1, err = strconv.Atoi(strings.TrimSpace(lens[0]))
|
len1, err = strconv.Atoi(strings.TrimSpace(lens[0]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -387,19 +393,30 @@ func (db *mysql) GetColumns(queryer core.Queryer, ctx context.Context, tableName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if colType == "FLOAT UNSIGNED" {
|
|
||||||
colType = "FLOAT"
|
switch colType {
|
||||||
}
|
case "TINYINT UNSIGNED":
|
||||||
if colType == "DOUBLE UNSIGNED" {
|
colType = schemas.TinyIntUnsigned
|
||||||
colType = "DOUBLE"
|
case "SMALLINT UNSIGNED":
|
||||||
|
colType = schemas.SmallIntUnsigned
|
||||||
|
case "MEDIUMINT UNSIGNED":
|
||||||
|
colType = schemas.MediumIntUnsigned
|
||||||
|
case "INT UNSIGNED":
|
||||||
|
colType = schemas.IntUnsigned
|
||||||
|
case "BIGINT UNSIGNED":
|
||||||
|
colType = schemas.BigIntUnsigned
|
||||||
|
case "FLOAT UNSIGNED":
|
||||||
|
colType = schemas.FloatUnsigned
|
||||||
|
case "DOUBLE UNSIGNED":
|
||||||
|
colType = schemas.DoubleUnsigned
|
||||||
}
|
}
|
||||||
|
|
||||||
col.Length = len1
|
col.Length = len1
|
||||||
col.Length2 = len2
|
col.Length2 = len2
|
||||||
if _, ok := schemas.SqlTypes[colType]; ok {
|
if _, ok := schemas.SqlTypes[colType]; !ok {
|
||||||
col.SQLType = schemas.SQLType{Name: colType, DefaultLength: len1, DefaultLength2: len2}
|
|
||||||
} else {
|
|
||||||
return nil, nil, fmt.Errorf("Unknown colType %v", colType)
|
return nil, nil, fmt.Errorf("Unknown colType %v", colType)
|
||||||
}
|
}
|
||||||
|
col.SQLType = schemas.SQLType{Name: colType, DefaultLength: len1, DefaultLength2: len2}
|
||||||
|
|
||||||
if colKey == "PRI" {
|
if colKey == "PRI" {
|
||||||
col.IsPrimaryKey = true
|
col.IsPrimaryKey = true
|
||||||
|
@ -408,7 +425,7 @@ func (db *mysql) GetColumns(queryer core.Queryer, ctx context.Context, tableName
|
||||||
// col.is
|
// col.is
|
||||||
}
|
}
|
||||||
|
|
||||||
if extra == "auto_increment" {
|
if extra == "AUTO_INCREMENT" {
|
||||||
col.IsAutoIncrement = true
|
col.IsAutoIncrement = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
engine.go
14
engine.go
|
@ -61,6 +61,10 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return newEngine(driverName, dataSourceName, dialect, db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newEngine(driverName, dataSourceName string, dialect dialects.Dialect, db *core.DB) (*Engine, error) {
|
||||||
cacherMgr := caches.NewManager()
|
cacherMgr := caches.NewManager()
|
||||||
mapper := names.NewCacheMapper(new(names.SnakeMapper))
|
mapper := names.NewCacheMapper(new(names.SnakeMapper))
|
||||||
tagParser := tags.NewParser("xorm", dialect, mapper, mapper, cacherMgr)
|
tagParser := tags.NewParser("xorm", dialect, mapper, mapper, cacherMgr)
|
||||||
|
@ -88,7 +92,7 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
||||||
engine.SetLogger(log.NewLoggerAdapter(logger))
|
engine.SetLogger(log.NewLoggerAdapter(logger))
|
||||||
|
|
||||||
runtime.SetFinalizer(engine, func(engine *Engine) {
|
runtime.SetFinalizer(engine, func(engine *Engine) {
|
||||||
engine.Close()
|
_ = engine.Close()
|
||||||
})
|
})
|
||||||
|
|
||||||
return engine, nil
|
return engine, nil
|
||||||
|
@ -101,6 +105,14 @@ func NewEngineWithParams(driverName string, dataSourceName string, params map[st
|
||||||
return engine, err
|
return engine, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewEngineWithDialectAndDB new a db manager according to the parameter.
|
||||||
|
// If you do not want to use your own dialect or db, please use NewEngine.
|
||||||
|
// For creating dialect, you can call dialects.OpenDialect. And, for creating db,
|
||||||
|
// you can call core.Open or core.FromDB.
|
||||||
|
func NewEngineWithDialectAndDB(driverName, dataSourceName string, dialect dialects.Dialect, db *core.DB) (*Engine, error) {
|
||||||
|
return newEngine(driverName, dataSourceName, dialect, db)
|
||||||
|
}
|
||||||
|
|
||||||
// EnableSessionID if enable session id
|
// EnableSessionID if enable session id
|
||||||
func (engine *Engine) EnableSessionID(enable bool) {
|
func (engine *Engine) EnableSessionID(enable bool) {
|
||||||
engine.logSessionID = enable
|
engine.logSessionID = enable
|
||||||
|
|
|
@ -71,11 +71,16 @@ func (s *SQLType) IsJson() bool {
|
||||||
var (
|
var (
|
||||||
Bit = "BIT"
|
Bit = "BIT"
|
||||||
TinyInt = "TINYINT"
|
TinyInt = "TINYINT"
|
||||||
|
TinyIntUnsigned = "TINYINT_UNSIGNED"
|
||||||
SmallInt = "SMALLINT"
|
SmallInt = "SMALLINT"
|
||||||
|
SmallIntUnsigned = "SMALLINT_UNSIGNED"
|
||||||
MediumInt = "MEDIUMINT"
|
MediumInt = "MEDIUMINT"
|
||||||
|
MediumIntUnsigned = "MEDIUMINT_UNSIGNED"
|
||||||
Int = "INT"
|
Int = "INT"
|
||||||
|
IntUnsigned = "INT_UNSIGNED"
|
||||||
Integer = "INTEGER"
|
Integer = "INTEGER"
|
||||||
BigInt = "BIGINT"
|
BigInt = "BIGINT"
|
||||||
|
BigIntUnsigned = "BIGINT_UNSIGNED"
|
||||||
|
|
||||||
Enum = "ENUM"
|
Enum = "ENUM"
|
||||||
Set = "SET"
|
Set = "SET"
|
||||||
|
@ -109,7 +114,9 @@ var (
|
||||||
|
|
||||||
Real = "REAL"
|
Real = "REAL"
|
||||||
Float = "FLOAT"
|
Float = "FLOAT"
|
||||||
|
FloatUnsigned = "FLOAT_UNSIGNED"
|
||||||
Double = "DOUBLE"
|
Double = "DOUBLE"
|
||||||
|
DoubleUnsigned = "DOUBLE_UNSIGNED"
|
||||||
|
|
||||||
Binary = "BINARY"
|
Binary = "BINARY"
|
||||||
VarBinary = "VARBINARY"
|
VarBinary = "VARBINARY"
|
||||||
|
@ -133,11 +140,16 @@ var (
|
||||||
SqlTypes = map[string]int{
|
SqlTypes = map[string]int{
|
||||||
Bit: NUMERIC_TYPE,
|
Bit: NUMERIC_TYPE,
|
||||||
TinyInt: NUMERIC_TYPE,
|
TinyInt: NUMERIC_TYPE,
|
||||||
|
TinyIntUnsigned: NUMERIC_TYPE,
|
||||||
SmallInt: NUMERIC_TYPE,
|
SmallInt: NUMERIC_TYPE,
|
||||||
|
SmallIntUnsigned: NUMERIC_TYPE,
|
||||||
MediumInt: NUMERIC_TYPE,
|
MediumInt: NUMERIC_TYPE,
|
||||||
|
MediumIntUnsigned: NUMERIC_TYPE,
|
||||||
Int: NUMERIC_TYPE,
|
Int: NUMERIC_TYPE,
|
||||||
|
IntUnsigned: NUMERIC_TYPE,
|
||||||
Integer: NUMERIC_TYPE,
|
Integer: NUMERIC_TYPE,
|
||||||
BigInt: NUMERIC_TYPE,
|
BigInt: NUMERIC_TYPE,
|
||||||
|
BigIntUnsigned: NUMERIC_TYPE,
|
||||||
|
|
||||||
Enum: TEXT_TYPE,
|
Enum: TEXT_TYPE,
|
||||||
Set: TEXT_TYPE,
|
Set: TEXT_TYPE,
|
||||||
|
@ -169,7 +181,9 @@ var (
|
||||||
Numeric: NUMERIC_TYPE,
|
Numeric: NUMERIC_TYPE,
|
||||||
Real: NUMERIC_TYPE,
|
Real: NUMERIC_TYPE,
|
||||||
Float: NUMERIC_TYPE,
|
Float: NUMERIC_TYPE,
|
||||||
|
FloatUnsigned: NUMERIC_TYPE,
|
||||||
Double: NUMERIC_TYPE,
|
Double: NUMERIC_TYPE,
|
||||||
|
DoubleUnsigned: NUMERIC_TYPE,
|
||||||
Money: NUMERIC_TYPE,
|
Money: NUMERIC_TYPE,
|
||||||
SmallMoney: NUMERIC_TYPE,
|
SmallMoney: NUMERIC_TYPE,
|
||||||
|
|
||||||
|
|
|
@ -250,11 +250,9 @@ func (session *Session) Sync2(beans ...interface{}) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var tbName string
|
tbName := engine.TableName(bean)
|
||||||
if len(session.statement.AltTableName) > 0 {
|
if len(session.statement.AltTableName) > 0 {
|
||||||
tbName = session.statement.AltTableName
|
tbName = session.statement.AltTableName
|
||||||
} else {
|
|
||||||
tbName = engine.TableName(bean)
|
|
||||||
}
|
}
|
||||||
tbNameWithSchema := engine.tbNameWithSchema(tbName)
|
tbNameWithSchema := engine.tbNameWithSchema(tbName)
|
||||||
|
|
||||||
|
|
|
@ -205,8 +205,6 @@ func (parser *Parser) Parse(v reflect.Value) (*schemas.Table, error) {
|
||||||
}
|
}
|
||||||
if j < len(tags)-1 {
|
if j < len(tags)-1 {
|
||||||
ctx.nextTag = tags[j+1]
|
ctx.nextTag = tags[j+1]
|
||||||
} else {
|
|
||||||
ctx.nextTag = ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if h, ok := parser.handlers[ctx.tagName]; ok {
|
if h, ok := parser.handlers[ctx.tagName]; ok {
|
||||||
|
|
17
tags/tag.go
17
tags/tag.go
|
@ -225,6 +225,22 @@ func CommentTagHandler(ctx *Context) error {
|
||||||
|
|
||||||
// SQLTypeTagHandler describes SQL Type tag handler
|
// SQLTypeTagHandler describes SQL Type tag handler
|
||||||
func SQLTypeTagHandler(ctx *Context) error {
|
func SQLTypeTagHandler(ctx *Context) error {
|
||||||
|
switch ctx.tagName {
|
||||||
|
case schemas.TinyIntUnsigned:
|
||||||
|
ctx.col.SQLType = schemas.SQLType{Name: "TINYINT UNSIGNED"}
|
||||||
|
case schemas.SmallIntUnsigned:
|
||||||
|
ctx.col.SQLType = schemas.SQLType{Name: "SMALLINT UNSIGNED"}
|
||||||
|
case schemas.MediumIntUnsigned:
|
||||||
|
ctx.col.SQLType = schemas.SQLType{Name: "MEDIUMINT UNSIGNED"}
|
||||||
|
case schemas.IntUnsigned:
|
||||||
|
ctx.col.SQLType = schemas.SQLType{Name: "INT UNSIGNED"}
|
||||||
|
case schemas.BigIntUnsigned:
|
||||||
|
ctx.col.SQLType = schemas.SQLType{Name: "BIGINT UNSIGNED"}
|
||||||
|
case schemas.FloatUnsigned:
|
||||||
|
ctx.col.SQLType = schemas.SQLType{Name: "FLOAT UNSIGNED"}
|
||||||
|
case schemas.DoubleUnsigned:
|
||||||
|
ctx.col.SQLType = schemas.SQLType{Name: "DOUBLE UNSIGNED"}
|
||||||
|
default:
|
||||||
ctx.col.SQLType = schemas.SQLType{Name: ctx.tagName}
|
ctx.col.SQLType = schemas.SQLType{Name: ctx.tagName}
|
||||||
if len(ctx.params) > 0 {
|
if len(ctx.params) > 0 {
|
||||||
if ctx.tagName == schemas.Enum {
|
if ctx.tagName == schemas.Enum {
|
||||||
|
@ -260,6 +276,7 @@ func SQLTypeTagHandler(ctx *Context) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue