go-xorm / xorm

Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm
BSD 3-Clause "New" or "Revised" License
6.67k stars 754 forks source link

Oracle DateTime Insert ORA-01861/ORA-01843 Bug #799

Open jjjachyty opened 6 years ago

jjjachyty commented 6 years ago

ORA-01861: literal does not match format string ORA-01843: not a valid month\n"

Can be temporary modified engine.go 1589 line

// formatTime format time as column type
func (engine *Engine) formatTime(sqlTypeName string, t time.Time) (v interface{}) {
    switch sqlTypeName {
    case core.Time:
        s := t.Format("2006-01-02 15:04:05") //time.RFC3339
        v = s[11:19]
    case core.Date:
        v = t.Format("2006-01-02")
    case core.DateTime, core.TimeStamp:

            //v = t.Format("2006-01-02 15:04:05")
        //oracle DateTime Bug @ByJanly
        return t

    case core.TimeStampz:
        if engine.dialect.DBType() == core.MSSQL {
            v = t.Format("2006-01-02T15:04:05.9999999Z07:00")
        } else {
            v = t.Format(time.RFC3339Nano)
        }
    case core.BigInt, core.Int:
        v = t.Unix()
    default:
        v = t
    }
    return
}
yixin19 commented 5 years ago

This bug still exist, while inserting a type time.Time

jjjachyty commented 5 years ago

@yixin19 can you see my mark //oracle DateTime Bug @ByJanly?

yixin19 commented 5 years ago

@yixin19 can you see my mark //oracle DateTime Bug @ByJanly?

Yes, you mean that i have to modify the engine.go file. But when i need to switch between databases, it's not easy.

jjjachyty commented 5 years ago

@yixin19 can you see my mark //oracle DateTime Bug @ByJanly?

Yes, you mean that i have to modify the engine.go file. But when i need to switch between databases, it's not easy.

you can switch and case your db drivers or if oracle

yixin19 commented 5 years ago

@yixin19 can you see my mark //oracle DateTime Bug @ByJanly?

Yes, you mean that i have to modify the engine.go file. But when i need to switch between databases, it's not easy.

you can switch and case your db drivers or if oracle

okay thx

dawndiy commented 5 years ago

If you use goracle driver, the driver can process time.Time, as the code from @jjjachyty you can directly return t so that goracle can process it. If you don't want modify the code from xorm, you can create the *sql.DB and use goracle.NewSessionIniter to reset "NLS_DATE_FORMAT" with "yyyy-mm-dd hh24:mi:ss", then It will works!

engine, err := xorm.NewEngine("goracle", dsn)
ctr, err := goracle.NewConnector(dsn, goracle.NewSessionIniter(map[string]string{
    "NLS_DATE_FORMAT": "yyyy-mm-dd hh24:mi:ss",
}))
engine.DB().DB = sql.OpenDB(ctr)