Skip to content

Commit

Permalink
fix reading lob as output parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
sijms committed Mar 22, 2023
1 parent 14899f5 commit 69bf406
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 24 deletions.
113 changes: 113 additions & 0 deletions examples/TimeStamp TZ/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"database/sql"
"fmt"
go_ora "github.com/sijms/go-ora/v2"
"os"
"time"
)

func createTable(conn *sql.DB) error {
t := time.Now()
sqlText := `CREATE TABLE TEMP_TABLE_322(
ID NUMBER(10),
DATA TIMESTAMP WITH TIME ZONE
)`
_, err := conn.Exec(sqlText)
if err != nil {
return err
}
fmt.Println("Finish create table: ", time.Now().Sub(t))
return nil
}
func dropTable(conn *sql.DB) error {
t := time.Now()
_, err := conn.Exec("drop table TEMP_TABLE_322 purge")
if err != nil {
return err
}
fmt.Println("Finish drop table: ", time.Now().Sub(t))
return nil
}

func insertData(conn *sql.DB, loc string) error {
data := time.Now()
if len(loc) > 0 {
zoneLoc, err := time.LoadLocation(loc)
if err != nil {
return err
}
data = data.In(zoneLoc)
}

t := time.Now()
_, err := conn.Exec(`INSERT INTO TEMP_TABLE_322(ID, DATA) VALUES (:1 , :2)`,
1, go_ora.TimeStampTZ(data))
if err != nil {
return err
}
fmt.Println("Finish insert data: ", time.Now().Sub(t))
return nil
}

func queryData(conn *sql.DB) error {
t := time.Now()
var data time.Time
rows, err := conn.Query(`SELECT DATA FROM TEMP_TABLE_322`)
if err != nil {
return err
}
defer func() {
err = rows.Close()
if err != nil {
fmt.Println("can't close rows: ", err)
}
}()
for rows.Next() {
err = rows.Scan(&data)
if err != nil {
return err
}
fmt.Println(data, "\tLocation: ", data.Location())
}
fmt.Println("Finish query data: ", time.Now().Sub(t))
return nil
}
func main() {
conn, err := sql.Open("oracle", os.Getenv("DSN"))
if err != nil {
fmt.Println("can't open connection: ", err)
return
}
defer func() {
err = conn.Close()
if err != nil {
fmt.Println("can't close connection: ", err)
}
}()

err = createTable(conn)
if err != nil {
fmt.Println("can't create table: ", err)
return
}

defer func() {
err = dropTable(conn)
if err != nil {
fmt.Println("can't drop table: ", err)
}
}()
err = insertData(conn, "Asia/Kolkata")
//err = insertData(conn, "")
if err != nil {
fmt.Println("can't insert data: ", err)
return
}
err = queryData(conn)
if err != nil {
fmt.Println("can't query data: ", err)
return
}
}
47 changes: 23 additions & 24 deletions v2/lob.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package go_ora

import (
"bytes"
"database/sql/driver"
"errors"
"go/types"
)
Expand Down Expand Up @@ -709,26 +708,26 @@ func (val *NClob) Scan(value interface{}) error {
return nil
}

func (val *Clob) Value() (driver.Value, error) {
if val.Valid {
return val.String, nil
} else {
return nil, nil
}
}

func (val *NClob) Value() (driver.Value, error) {
if val.Valid {
return val.String, nil
} else {
return nil, nil
}
}

func (val *Blob) Value() (driver.Value, error) {
if val.Valid {
return val.Data, nil
} else {
return nil, nil
}
}
//func (val *Clob) Value() (driver.Value, error) {
// if val.Valid {
// return val.String, nil
// } else {
// return nil, nil
// }
//}
//
//func (val *NClob) Value() (driver.Value, error) {
// if val.Valid {
// return val.String, nil
// } else {
// return nil, nil
// }
//}
//
//func (val *Blob) Value() (driver.Value, error) {
// if val.Valid {
// return val.Data, nil
// } else {
// return nil, nil
// }
//}

0 comments on commit 69bf406

Please sign in to comment.