sijms / go-ora

Pure go oracle client
MIT License
786 stars 174 forks source link

How to scan RefCursor to map #428

Closed dzwvip closed 1 year ago

dzwvip commented 1 year ago

func queryCursor(crows goora.RefCursor) (list []map[string]interface{}, n int) { srows, err := crows.Query() if err != nil { } defer srows.Close() defer crows.Close() columns := srows.Columns() //转换Oracle列名 小写 for i := 0; i < len(columns); i++ { columns[i] = strings.ToLower(columns[i]) } columnLength := len(columns) cache := make([]interface{}, columnLength) //临时存储每行数据地址 for index := range cache { //为每一列初始化一个指针 var a interface{} cache[index] = &a } n = 0 //scan to map for srows.Next() { _ = srows.Scan(cache...) item := make(map[string]interface{}) for i, data := range cache { item[columns[i]] = data.(*interface{}) //取实际类型 } list = append(list, item) n++ }

result is nil

sijms commented 1 year ago

there is an error return from rows.Scan(cache...)

sijms commented 1 year ago

fixed in next release

dzwvip commented 1 year ago

If RefCursor can be directly converted to sqlrows, it can be directly scan to map and struct using gorm; The type of driver.rows used by the output cursor of the Godror library; You can use the godror.WrapRows function to convert to sql.rows

dzwvip commented 1 year ago

fixed in next release

tested ,ok

sijms commented 1 year ago

mapping RefCursor to sql.Rows is supported please return to docs and this example

dzwvip commented 1 year ago

Just the implicit conversion that occurs internally when the cursor returned by the query function, and how to convert if the cursor returned by the stored procedure is executed

sijms commented 1 year ago

I define a new function WrapRefCursor in last commit same as WrapRows that convert *RefCursor into *sql.Rows

dzwvip commented 1 year ago

I don't see this function in the latest v2.7.16

sijms commented 1 year ago

last commit still not release. it will be release in v2.7.17

sijms commented 1 year ago

released