-
Notifications
You must be signed in to change notification settings - Fork 18k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
database/sql: add usage examples for (*Rows).NextResultSet #29973
Comments
After debugging method NextResultSet() in database/sql: // NextResultSet prepares the next result set for reading. It returns true if
// there is further result sets, or false if there is no further result set
// or if there is an error advancing to it. The Err method should be consulted
// to distinguish between the two cases.
//
// After calling NextResultSet, the Next method should always be called before
// scanning. If there are further result sets they may not have rows in the result
// set.
func (rs *Rows) NextResultSet() bool {
var doClose bool
defer func() {
if doClose {
rs.Close()
}
}()
rs.closemu.RLock()
defer rs.closemu.RUnlock()
if rs.closed {
return false
}
rs.lastcols = nil
nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet)
if !ok {
doClose = true
return false
}
// Lock the driver connection before calling the driver interface
// rowsi to prevent a Tx from rolling back the connection at the same time.
rs.dc.Lock()
defer rs.dc.Unlock()
rs.lasterr = nextResultSet.NextResultSet()
if rs.lasterr != nil {
doClose = true
return false
}
return true
} From above code error returning from function
|
The code provided has an unconditional break within the for rows.Next() {} loop. So will only print one row. |
Result sets are sets of records. Your example appears to only issue one simple query ( |
Actually the break statement must be inside error handling in my example some thing like:
In my example for simplicity i used one simple query. You can use any SELECT query try that case which i shown in example after reading first row in table suppose if there is any error in rows.Scan then i don't want to scan next row so i break from for loop. And i expect to use NextResultSet to check whether there is any further result sets in table or not from where i break it. |
Im not following your issue at all. |
@SagarPuneria, from what I can tell, I was going to point you to some examples of queries where |
@bcmills, let me clear, i am new to database, it seems you got confused. NextResultSet example was already given in db.Query example. Lets come to the point, the reason behind escalating this issue is that i want to know in which case NextResultSet return true? I tried all the cases using mysql database it is always returning false. If so then what is use case of implementing NextResultSet? Before giving your answer, i would suggest you to do sample program on NextResultSet using mysql database where it can return true case. I hope you got my Question. |
Calls a stored procedure that returns multiple resultsets on mysql using github.com/go-sql-driver/mysql https://gist.github.com/renthraysk/861bfb6f64e1b5d8e0d8b4d6e8b95be0 |
@renthraysk, yes you are right. Once it returns multiple resultsets it means it reaches the EOF when there are no more result sets then NextResultSet return false as per golang internal code. But my question in which case the API NextResultSet can return true case using mysql database? Suppose If NextResultSet API never return true then what is the use case of implementing this API ? |
CC @bcmills, @renthraysk func (*Rows) Next() bool: func (*Rows) NextResultSet() bool: Now in MySQL database:
Why NextResultSet method is NOT behaving exactly as designed and documentation?As per documentation provided for type RowsNextResultSet interface, it is mentioned that NextResultSet should return io.EOF when there are no more result sets. Solution:I think it would be better to refactor error handling code Finally I would suggest that change either the documentation or code. If you want to change the code defined in NextResultSet method: i would suggest to refactor error handling code something like:
|
Its was my big mistake. There is NO issue in NextResultSet method. I was working with different databases using golang sql package. I was thinking this same behavior with MySQL using (github.com/go-sql-driver/mysql) Eventually i found unexpected behavior (or bug) of lib/pq I filed an issue report on lib/pq. Thank you @bcmills, @renthraysk for investigating. |
What version of Go are you using (
go version
)?go version go1.11.5 windows/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?Windows 10, 64 bit
What did you do?
I am using func (rs *Rows) NextResultSet() bool in my code to check whether there is further result sets in table and in my table there is more one than records. So immediately after reading one record i am returning from
for rows.Next() {}
to test NextResultSet() booldb Query example
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: