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

Merge pull request #4186 from AllenX2018/Feature-datetime-precision

Feature: implement the time precison for time.Time type
This commit is contained in:
Ming Deng 2020-08-25 21:05:08 +08:00 committed by GitHub
commit cceecad8c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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:
col = T["time.Time"] if fi.timePrecision == nil {
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

@ -42,24 +42,25 @@ var mysqlOperators = map[string]string{
// mysql column field types. // mysql column field types.
var mysqlTypes = map[string]string{ var mysqlTypes = map[string]string{
"auto": "AUTO_INCREMENT NOT NULL PRIMARY KEY", "auto": "AUTO_INCREMENT NOT NULL PRIMARY KEY",
"pk": "NOT NULL PRIMARY KEY", "pk": "NOT NULL PRIMARY KEY",
"bool": "bool", "bool": "bool",
"string": "varchar(%d)", "string": "varchar(%d)",
"string-char": "char(%d)", "string-char": "char(%d)",
"string-text": "longtext", "string-text": "longtext",
"time.Time-date": "date", "time.Time-date": "date",
"time.Time": "datetime", "time.Time": "datetime",
"int8": "tinyint", "int8": "tinyint",
"int16": "smallint", "int16": "smallint",
"int32": "integer", "int32": "integer",
"int64": "bigint", "int64": "bigint",
"uint8": "tinyint unsigned", "uint8": "tinyint unsigned",
"uint16": "smallint unsigned", "uint16": "smallint unsigned",
"uint32": "integer unsigned", "uint32": "integer unsigned",
"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

@ -33,23 +33,24 @@ var oracleOperators = map[string]string{
// oracle column field types. // oracle column field types.
var oracleTypes = map[string]string{ var oracleTypes = map[string]string{
"pk": "NOT NULL PRIMARY KEY", "pk": "NOT NULL PRIMARY KEY",
"bool": "bool", "bool": "bool",
"string": "VARCHAR2(%d)", "string": "VARCHAR2(%d)",
"string-char": "CHAR(%d)", "string-char": "CHAR(%d)",
"string-text": "VARCHAR2(%d)", "string-text": "VARCHAR2(%d)",
"time.Time-date": "DATE", "time.Time-date": "DATE",
"time.Time": "TIMESTAMP", "time.Time": "TIMESTAMP",
"int8": "INTEGER", "int8": "INTEGER",
"int16": "INTEGER", "int16": "INTEGER",
"int32": "INTEGER", "int32": "INTEGER",
"int64": "INTEGER", "int64": "INTEGER",
"uint8": "INTEGER", "uint8": "INTEGER",
"uint16": "INTEGER", "uint16": "INTEGER",
"uint32": "INTEGER", "uint32": "INTEGER",
"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

@ -39,26 +39,27 @@ var postgresOperators = map[string]string{
// postgresql column field types. // postgresql column field types.
var postgresTypes = map[string]string{ var postgresTypes = map[string]string{
"auto": "serial NOT NULL PRIMARY KEY", "auto": "serial NOT NULL PRIMARY KEY",
"pk": "NOT NULL PRIMARY KEY", "pk": "NOT NULL PRIMARY KEY",
"bool": "bool", "bool": "bool",
"string": "varchar(%d)", "string": "varchar(%d)",
"string-char": "char(%d)", "string-char": "char(%d)",
"string-text": "text", "string-text": "text",
"time.Time-date": "date", "time.Time-date": "date",
"time.Time": "timestamp with time zone", "time.Time": "timestamp with time zone",
"int8": `smallint CHECK("%COL%" >= -127 AND "%COL%" <= 128)`, "int8": `smallint CHECK("%COL%" >= -127 AND "%COL%" <= 128)`,
"int16": "smallint", "int16": "smallint",
"int32": "integer", "int32": "integer",
"int64": "bigint", "int64": "bigint",
"uint8": `smallint CHECK("%COL%" >= 0 AND "%COL%" <= 255)`, "uint8": `smallint CHECK("%COL%" >= 0 AND "%COL%" <= 255)`,
"uint16": `integer CHECK("%COL%" >= 0)`, "uint16": `integer CHECK("%COL%" >= 0)`,
"uint32": `bigint CHECK("%COL%" >= 0)`, "uint32": `bigint CHECK("%COL%" >= 0)`,
"uint64": `bigint CHECK("%COL%" >= 0)`, "uint64": `bigint CHECK("%COL%" >= 0)`,
"float64": "double precision", "float64": "double precision",
"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.