mattn / go-oci8

Oracle driver for Go using database/sql
https://mattn.kaoriya.net/
MIT License
630 stars 212 forks source link

An error occurred when the time data was saved to the database #416

Open linlexing opened 3 years ago

linlexing commented 3 years ago
package main

import (
    "database/sql"
    "fmt"
    "time"

    _ "github.com/mattn/go-oci8"
)

func main() {

    db, err := sql.Open("oci8", "**")
    if err != nil {
        panic(err)
    }
    if _, err := db.Exec("create table testa(id varchar2(10) primary key,d date)"); err != nil {
        panic(err)
    }
    defer func() {
        _, err := db.Exec("drop table testa")
        if err != nil {
            panic(err)
        }
    }()
    tm, err := parseWithLocation("Asia/Shanghai", "0001-01-01 00:00:00")
    if err != nil {
        panic(err)
    }
    fmt.Println(tm)
    if _, err := db.Exec("insert into testa(id,d)values(:1,:2)", "123", tm); err != nil {
        panic(err)
    }

}

const TIME_LAYOUT = "2006-01-02 15:04:05"

func parseWithLocation(name string, timeStr string) (time.Time, error) {
    locationName := name
    if l, err := time.LoadLocation(locationName); err != nil {
        println(err.Error())
        return time.Time{}, err
    } else {
        lt, _ := time.ParseInLocation(TIME_LAYOUT, timeStr, l)
        fmt.Println(locationName, lt)
        return lt, nil
    }
}

output:

Asia/Shanghai 0001-01-01 00:00:00 +0805 LMT
0001-01-01 00:00:00 +0805 LMT
panic: timeToOCIDateTime for column 1 - error: ORA-08192: 在固定表上不允许闪回表操作

Correct if you do not have a time zone.

MichaelS11 commented 3 years ago

What happens if you try it with pure SQL inside the database? Does it work? What happens if you use the column type TIMESTAMP WITH TIME ZONE?

linlexing commented 3 years ago

There is no problem with plain SQL insert into testa(id,d)values('123', TIMESTAMP '0001-01-01 00:00:00 +8:00'), but if the data type is changed to TIMESTAMP WITH TIME ZONE and the parameters are still passed, there is a problem.

MichaelS11 commented 3 years ago

Could be a bug. Need someone that would be willing to look into it more and see about fixing it.

MichaelS11 commented 3 years ago

@mattn

I am not actively programming in Go anymore and will probably not be in the near future, so going to unwatch this repository. Will be back in your hands.

Thank you for all the wonderful code that you have written! :)

mattn commented 3 years ago

@MichaelS11 Thanks for your great help.