Skip to content

Commit

Permalink
add WrapRefCursor which convert RefCursor to sql.Rows
Browse files Browse the repository at this point in the history
  • Loading branch information
sijms committed Aug 24, 2023
1 parent c1f4c03 commit d92bac6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
20 changes: 15 additions & 5 deletions examples/refcursor/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"database/sql"
"flag"
"fmt"
Expand Down Expand Up @@ -103,9 +104,11 @@ func usage() {
fmt.Println()
}

func queryCursor(cursor *go_ora.RefCursor) error {
func queryCursor(conn *sql.DB, cursor *go_ora.RefCursor) error {
t := time.Now()
rows, err := cursor.Query()
rows, err := go_ora.WrapRefCursor(context.Background(), conn, cursor)

//rows, err := cursor.Query()
if err != nil {
return err
}
Expand All @@ -115,14 +118,21 @@ func queryCursor(cursor *go_ora.RefCursor) error {
val float64
date time.Time
)

for rows.Next_() {
defer rows.Close()
for rows.Next() {
err = rows.Scan(&id, &name, &val, &date)
if err != nil {
return err
}
fmt.Println("ID: ", id, "\tName: ", name, "\tval: ", val, "\tDate: ", date)
}
//for rows.Next_() {
// err = rows.Scan(&id, &name, &val, &date)
// if err != nil {
// return err
// }
// fmt.Println("ID: ", id, "\tName: ", name, "\tval: ", val, "\tDate: ", date)
//}
fmt.Println("Finish query RefCursor: ", time.Now().Sub(t))
return nil
}
Expand Down Expand Up @@ -201,7 +211,7 @@ func main() {
}
}()

err = queryCursor(&cursor)
err = queryCursor(conn, &cursor)
if err != nil {
fmt.Println("Can't query RefCursor", err)
}
Expand Down
20 changes: 20 additions & 0 deletions v2/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ const (
//PROXY LogonMode = 0x400
)

// from GODROR
const wrapResultset = "--WRAP_RESULTSET--"

// Querier is the QueryContext of sql.Conn.
type Querier interface {
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
}

/////

type NLSData struct {
Calender string `db:"p_nls_calendar,,40,out"`
Comp string `db:"p_nls_comp,,40,out"`
Expand Down Expand Up @@ -1086,7 +1096,17 @@ func (conn *Connection) QueryRowContext(ctx context.Context, query string, args
return dataSet
}

func WrapRefCursor(ctx context.Context, q Querier, cursor *RefCursor) (*sql.Rows, error) {
rows, err := cursor.Query()
if err != nil {
return nil, err
}
return q.QueryContext(ctx, wrapResultset, rows)
}
func (conn *Connection) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
if query == wrapResultset {
return args[0].Value.(driver.Rows), nil
}
stmt := NewStmt(query, conn)
stmt.autoClose = true
rows, err := stmt.QueryContext(ctx, args)
Expand Down
4 changes: 0 additions & 4 deletions v2/parameter_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,6 @@ func (par *ParameterInfo) encodeValue(val driver.Value, size int, connection *Co
}
par.MaxLen = par.MaxCharLen * converters.MaxBytePerChar(conv.GetLangID())
}
//if par.DataType == NUMBER {
// par.Precision = 38
// par.Scale = 0xff
//}
if par.DataType == RAW {
if par.MaxLen < size {
par.MaxLen = size
Expand Down

0 comments on commit d92bac6

Please sign in to comment.