adding sqlserver, mysql to function
This commit is contained in:
parent
0b5404da32
commit
706912ec97
@ -8,8 +8,10 @@ import (
|
|||||||
type Database string
|
type Database string
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Postgres Database = "postgres"
|
Postgres Database = "postgres"
|
||||||
Sqlite Database = "sqlite"
|
Sqlite Database = "sqlite"
|
||||||
|
Mysql Database = "mysql"
|
||||||
|
SqlServer Database = "sqlserver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Function interface {
|
type Function interface {
|
||||||
@ -33,6 +35,10 @@ func (fnc FuncDateWithoutTime) GetSql() string {
|
|||||||
fnc.SQL = fmt.Sprintf(" to_char(%s, %s) as %s", fnc.Field, "'YYYY-MM-DD'", alias)
|
fnc.SQL = fmt.Sprintf(" to_char(%s, %s) as %s", fnc.Field, "'YYYY-MM-DD'", alias)
|
||||||
case Sqlite:
|
case Sqlite:
|
||||||
fnc.SQL = fmt.Sprintf(" strftime(%s, %s) as %s", "'%Y-%m-%d' ", fnc.Field, alias)
|
fnc.SQL = fmt.Sprintf(" strftime(%s, %s) as %s", "'%Y-%m-%d' ", fnc.Field, alias)
|
||||||
|
case Mysql:
|
||||||
|
fnc.SQL = fmt.Sprintf(" DATE_FORMAT(%s, %s) as %s", fnc.Field, "'%Y-%m-%d' ", alias)
|
||||||
|
case SqlServer:
|
||||||
|
fnc.SQL = fmt.Sprintf(" FORMAT(%s, %s) as %s", fnc.Field, "'yyyy-MM-dd' ", alias)
|
||||||
default:
|
default:
|
||||||
log.Print("database not implemented yet for this function")
|
log.Print("database not implemented yet for this function")
|
||||||
return ""
|
return ""
|
||||||
|
@ -1360,7 +1360,7 @@ func TestQueryResetNullValue(t *testing.T) {
|
|||||||
AssertEqual(t, q2, qs[1])
|
AssertEqual(t, q2, qs[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFuncDateWihtoutTimeSQLITE(t *testing.T) {
|
func TestFuncDateWihtoutTime(t *testing.T) {
|
||||||
users := []User{
|
users := []User{
|
||||||
*GetUser("find", Config{}),
|
*GetUser("find", Config{}),
|
||||||
*GetUser("find", Config{}),
|
*GetUser("find", Config{}),
|
||||||
@ -1376,8 +1376,10 @@ func TestFuncDateWihtoutTimeSQLITE(t *testing.T) {
|
|||||||
Birthday string
|
Birthday string
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
var fn clause.FuncDateWithoutTime = clause.FuncDateWithoutTime{Database: "sqlite", Field: "birthday"}
|
|
||||||
|
var fn clause.FuncDateWithoutTime = clause.FuncDateWithoutTime{Database: clause.Database(DB.Dialector.Name()), Field: "birthday"}
|
||||||
var sql string = fn.GetSql()
|
var sql string = fn.GetSql()
|
||||||
|
|
||||||
if err := DB.Model(&User{}).Select([]string{sql, "name"}).Where("name = ?", "find").First(&first).Error; err != nil {
|
if err := DB.Model(&User{}).Select([]string{sql, "name"}).Where("name = ?", "find").First(&first).Error; err != nil {
|
||||||
t.Errorf("errors happened when query first: %v", err)
|
t.Errorf("errors happened when query first: %v", err)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user