Add database alias table and fix wrong warning (#1947)
fix #1831 Reviewed-on: https://gitea.com/xorm/xorm/pulls/1947 Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-committed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
parent
a38b1fddb3
commit
717e4a0d21
|
@ -43,6 +43,7 @@ type Dialect interface {
|
||||||
Init(*URI) error
|
Init(*URI) error
|
||||||
URI() *URI
|
URI() *URI
|
||||||
SQLType(*schemas.Column) string
|
SQLType(*schemas.Column) string
|
||||||
|
Alias(string) string // return what a sql type's alias of
|
||||||
FormatBytes(b []byte) string
|
FormatBytes(b []byte) string
|
||||||
Version(ctx context.Context, queryer core.Queryer) (*schemas.Version, error)
|
Version(ctx context.Context, queryer core.Queryer) (*schemas.Version, error)
|
||||||
|
|
||||||
|
@ -80,6 +81,11 @@ type Base struct {
|
||||||
quoter schemas.Quoter
|
quoter schemas.Quoter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Alias returned col itself
|
||||||
|
func (db *Base) Alias(col string) string {
|
||||||
|
return col
|
||||||
|
}
|
||||||
|
|
||||||
// Quoter returns the current database Quoter
|
// Quoter returns the current database Quoter
|
||||||
func (db *Base) Quoter() schemas.Quoter {
|
func (db *Base) Quoter() schemas.Quoter {
|
||||||
return db.quoter
|
return db.quoter
|
||||||
|
|
|
@ -190,6 +190,21 @@ func (db *mysql) Init(uri *URI) error {
|
||||||
return db.Base.Init(db, uri)
|
return db.Base.Init(db, uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
mysqlColAliases = map[string]string{
|
||||||
|
"numeric": "decimal",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Alias returns a alias of column
|
||||||
|
func (db *mysql) Alias(col string) string {
|
||||||
|
v, ok := mysqlColAliases[strings.ToLower(col)]
|
||||||
|
if ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return col
|
||||||
|
}
|
||||||
|
|
||||||
func (db *mysql) Version(ctx context.Context, queryer core.Queryer) (*schemas.Version, error) {
|
func (db *mysql) Version(ctx context.Context, queryer core.Queryer) (*schemas.Version, error) {
|
||||||
rows, err := queryer.QueryContext(ctx, "SELECT @@VERSION")
|
rows, err := queryer.QueryContext(ctx, "SELECT @@VERSION")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -778,12 +778,24 @@ var (
|
||||||
var (
|
var (
|
||||||
// DefaultPostgresSchema default postgres schema
|
// DefaultPostgresSchema default postgres schema
|
||||||
DefaultPostgresSchema = "public"
|
DefaultPostgresSchema = "public"
|
||||||
|
postgresColAliases = map[string]string{
|
||||||
|
"numeric": "decimal",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type postgres struct {
|
type postgres struct {
|
||||||
Base
|
Base
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Alias returns a alias of column
|
||||||
|
func (db *postgres) Alias(col string) string {
|
||||||
|
v, ok := postgresColAliases[strings.ToLower(col)]
|
||||||
|
if ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return col
|
||||||
|
}
|
||||||
|
|
||||||
func (db *postgres) Init(uri *URI) error {
|
func (db *postgres) Init(uri *URI) error {
|
||||||
db.quoter = postgresQuoter
|
db.quoter = postgresQuoter
|
||||||
return db.Base.Init(db, uri)
|
return db.Base.Init(db, uri)
|
||||||
|
|
|
@ -286,6 +286,19 @@ func TestSyncTable3(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSyncTable4(t *testing.T) {
|
||||||
|
type SyncTable6 struct {
|
||||||
|
Id int64
|
||||||
|
Qty float64 `xorm:"numeric(36,2)"`
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, PrepareEngine())
|
||||||
|
|
||||||
|
assert.NoError(t, testEngine.Sync2(new(SyncTable6)))
|
||||||
|
|
||||||
|
assert.NoError(t, testEngine.Sync2(new(SyncTable6)))
|
||||||
|
}
|
||||||
|
|
||||||
func TestIsTableExist(t *testing.T) {
|
func TestIsTableExist(t *testing.T) {
|
||||||
assert.NoError(t, PrepareEngine())
|
assert.NoError(t, PrepareEngine())
|
||||||
|
|
||||||
|
|
|
@ -368,3 +368,9 @@ func SQLType2Type(st SQLType) reflect.Type {
|
||||||
return reflect.TypeOf("")
|
return reflect.TypeOf("")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SQLTypeName returns sql type name
|
||||||
|
func SQLTypeName(tp string) string {
|
||||||
|
fields := strings.Split(tp, "(")
|
||||||
|
return fields[0]
|
||||||
|
}
|
||||||
|
|
|
@ -336,8 +336,10 @@ func (session *Session) Sync2(beans ...interface{}) error {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !(strings.HasPrefix(curType, expectedType) && curType[len(expectedType)] == '(') {
|
if !(strings.HasPrefix(curType, expectedType) && curType[len(expectedType)] == '(') {
|
||||||
engine.logger.Warnf("Table %s column %s db type is %s, struct type is %s",
|
if !strings.EqualFold(schemas.SQLTypeName(curType), engine.dialect.Alias(schemas.SQLTypeName(expectedType))) {
|
||||||
tbNameWithSchema, col.Name, curType, expectedType)
|
engine.logger.Warnf("Table %s column %s db type is %s, struct type is %s",
|
||||||
|
tbNameWithSchema, col.Name, curType, expectedType)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if expectedType == schemas.Varchar {
|
} else if expectedType == schemas.Varchar {
|
||||||
|
|
Loading…
Reference in New Issue