1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 05:50:55 +00:00

Feature: implement the time precison for time.Time type

This commit is contained in:
AllenX2018 2020-08-24 20:23:54 +08:00
parent 597c55d547
commit 7a94996e22
6 changed files with 79 additions and 57 deletions

View File

@ -66,7 +66,12 @@ checkColumn:
case TypeDateField: case TypeDateField:
col = T["time.Time-date"] col = T["time.Time-date"]
case TypeDateTimeField: case TypeDateTimeField:
if fi.timePrecision == nil {
col = T["time.Time"] col = T["time.Time"]
} else {
s := T["time.Time-precision"]
col = fmt.Sprintf(s, *fi.timePrecision)
}
case TypeBitField: case TypeBitField:
col = T["int8"] col = T["int8"]
case TypeSmallIntegerField: case TypeSmallIntegerField:

View File

@ -60,6 +60,7 @@ var mysqlTypes = map[string]string{
"uint64": "bigint unsigned", "uint64": "bigint unsigned",
"float64": "double precision", "float64": "double precision",
"float64-decimal": "numeric(%d, %d)", "float64-decimal": "numeric(%d, %d)",
"time.Time-precision": "datetime(%d)",
} }
// mysql dbBaser implementation. // mysql dbBaser implementation.

View File

@ -50,6 +50,7 @@ var oracleTypes = map[string]string{
"uint64": "INTEGER", "uint64": "INTEGER",
"float64": "NUMBER", "float64": "NUMBER",
"float64-decimal": "NUMBER(%d, %d)", "float64-decimal": "NUMBER(%d, %d)",
"time.Time-precision": "TIMESTAMP(%d)",
} }
// oracle dbBaser // oracle dbBaser

View File

@ -59,6 +59,7 @@ var postgresTypes = map[string]string{
"float64-decimal": "numeric(%d, %d)", "float64-decimal": "numeric(%d, %d)",
"json": "json", "json": "json",
"jsonb": "jsonb", "jsonb": "jsonb",
"time.Time-precision": "timestamp(%d) with time zone",
} }
// postgresql dbBaser. // postgresql dbBaser.

View File

@ -137,6 +137,7 @@ type fieldInfo struct {
isFielder bool // implement Fielder interface isFielder bool // implement Fielder interface
onDelete string onDelete string
description string description string
timePrecision *int
} }
// new field info // new field info
@ -177,7 +178,7 @@ func newFieldInfo(mi *modelInfo, field reflect.Value, sf reflect.StructField, mN
decimals := tags["decimals"] decimals := tags["decimals"]
size := tags["size"] size := tags["size"]
onDelete := tags["on_delete"] onDelete := tags["on_delete"]
precision := tags["precision"]
initial.Clear() initial.Clear()
if v, ok := tags["default"]; ok { if v, ok := tags["default"]; ok {
initial.Set(v) initial.Set(v)
@ -377,6 +378,18 @@ checkType:
fi.index = false fi.index = false
fi.unique = false fi.unique = false
case TypeTimeField, TypeDateField, TypeDateTimeField: case TypeTimeField, TypeDateField, TypeDateTimeField:
if fieldType == TypeDateTimeField {
if precision != "" {
v, e := StrTo(precision).Int()
if e != nil {
err = fmt.Errorf("convert %s to int error:%v", precision, e)
} else {
fi.timePrecision = &v
}
}
}
if attrs["auto_now"] { if attrs["auto_now"] {
fi.autoNow = true fi.autoNow = true
} else if attrs["auto_now_add"] { } else if attrs["auto_now_add"] {

View File

@ -45,6 +45,7 @@ var supportTag = map[string]int{
"on_delete": 2, "on_delete": 2,
"type": 2, "type": 2,
"description": 2, "description": 2,
"precision": 2,
} }
// get reflect.Type name with package path. // get reflect.Type name with package path.