Skip to content

Commit

Permalink
map RefCursor to Sql.Rows
Browse files Browse the repository at this point in the history
  • Loading branch information
sijms committed Apr 9, 2023
1 parent 05a44fb commit 898231b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
69 changes: 38 additions & 31 deletions v2/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,25 +634,6 @@ func (stmt *defaultStmt) read(dataSet *DataSet) error {
return err
}
switch msg {
//case 4:
// stmt.connection.session.Summary, err = network.NewSummary(session)
// if err != nil {
// return err
// }
// stmt.connection.connOption.Tracer.Printf("Summary: RetCode:%d, Error Message:%q", stmt.connection.session.Summary.RetCode, string(stmt.connection.session.Summary.ErrorMessage))
//
// stmt.cursorID = stmt.connection.session.Summary.CursorID
// stmt.disableCompression = stmt.connection.session.Summary.Flags&0x20 != 0
// if stmt.connection.session.HasError() {
// if stmt.connection.session.Summary.RetCode == 1403 {
// stmt._hasMoreRows = false
// stmt.connection.session.Summary = nil
// } else {
// return stmt.connection.session.GetError()
// }
//
// }
// loop = false
case 6:
//_, err = session.GetByte()
err = dataSet.load(session)
Expand Down Expand Up @@ -918,15 +899,6 @@ func (stmt *defaultStmt) read(dataSet *DataSet) error {
}
}
dataSet.setBitVector(bitVector)
//case 23:
// opCode, err := session.GetByte()
// if err != nil {
// return err
// }
// err = stmt.connection.getServerNetworkInformation(opCode)
// if err != nil {
// return err
// }
default:
err = stmt.connection.readResponse(msg)
if err != nil {
Expand Down Expand Up @@ -1362,6 +1334,17 @@ func (stmt *defaultStmt) calculateColumnValue(col *ParameterInfo) error {
//if col.DataType == OCIBlobLocator || col.DataType == OCIClobLocator {
// stmt._hasBLOB = true
//}
if col.DataType == REFCURSOR {
var cursor = new(RefCursor)
cursor.connection = stmt.connection
cursor.parent = stmt
err := cursor.load()
if err != nil {
return err
}
col.Value = cursor
return nil
}
if col.DataType == XMLType {
if col.TypeName == "XMLTYPE" {
return errors.New("unsupported data type: XMLTYPE")
Expand Down Expand Up @@ -1424,6 +1407,7 @@ func (stmt *defaultStmt) calculateColumnValue(col *ParameterInfo) error {
}
return nil
}

return col.decodeColumnValue(stmt.connection)
}

Expand Down Expand Up @@ -1651,7 +1635,10 @@ func (stmt *Stmt) _exec(args []driver.NamedValue) (*QueryResult, error) {
stmt.connection.connOption.Tracer.Printf(" %d:\n%v", x, args[x])
}
if useNamedPars {
stmt.useNamedParameters()
err = stmt.useNamedParameters()
if err != nil {
return nil, err
}
}
defer func() {
_ = stmt.freeTemporaryLobs()
Expand Down Expand Up @@ -1681,7 +1668,10 @@ func (stmt *Stmt) _exec(args []driver.NamedValue) (*QueryResult, error) {

// useNamedParameters: re-arrange parameters according parameter defined in sql text
func (stmt *Stmt) useNamedParameters() error {
names := parseSqlText(stmt.text)
names, err := parseSqlText(stmt.text)
if err != nil {
return err
}
var parCollection = make([]ParameterInfo, 0, len(names))
for x := 0; x < len(names); x++ {
found := false
Expand Down Expand Up @@ -1825,7 +1815,10 @@ func (stmt *Stmt) Query_(namedArgs []driver.NamedValue) (*DataSet, error) {
}

if useNamedPars {
stmt.useNamedParameters()
err := stmt.useNamedParameters()
if err != nil {
return nil, err
}
}

failOver := stmt.connection.connOption.Failover
Expand Down Expand Up @@ -1913,6 +1906,20 @@ func (stmt *Stmt) _query() (*DataSet, error) {
if err != nil {
return nil, err
}
// deal with ref cursor
for colIndex, col := range dataSet.Cols {
if col.DataType == REFCURSOR {
if len(dataSet.rows) == 1 {
if cursor, ok := dataSet.rows[0][colIndex].(*RefCursor); ok {
dataSet.rows[0][colIndex], err = cursor.Query()
if err != nil {
return nil, err
}
}

}
}
}
// deal with lobs
if stmt._hasBLOB {
if stmt.connection.connOption.Lob == 0 {
Expand Down
8 changes: 4 additions & 4 deletions v2/ref_cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func (cursor *RefCursor) load() error {
if err != nil {
return err
}
_, err = session.GetInt(2, true, true)
if err != nil {
return err
}
//_, err = session.GetInt(2, true, true)
//if err != nil {
// return err
//}
return nil
}
func (cursor *RefCursor) getExeOptions() int {
Expand Down
23 changes: 9 additions & 14 deletions v2/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package go_ora

import "strings"
import (
"regexp"
"strings"
)

func parseSqlText(text string) []string {
func parseSqlText(text string) ([]string, error) {
index := 0
length := len(text)
skip := false
Expand Down Expand Up @@ -46,17 +49,9 @@ func parseSqlText(text string) []string {
}
}
refinedSql := strings.TrimSpace(string(output))
split := func(r rune) bool {
return r == ' ' || r == '\t' || r == ',' || r == '+' || r == '-' ||
r == '*' || r == '/' || r == '<' || r == '>' ||
r == '=' || r == '|' || r == '(' || r == ')'
reg, err := regexp.Compile(`:\w+`)
if err != nil {
return nil, err
}
pars := make([]string, 0, 10)
words := strings.FieldsFunc(refinedSql, split)
for _, word := range words {
if len(word) > 1 && word[0] == ':' {
pars = append(pars, word[1:])
}
}
return pars
return reg.FindAllString(refinedSql, -1), nil
}

0 comments on commit 898231b

Please sign in to comment.