@@ -515,16 +515,8 @@ func (s *Statement) FreeResult() (err os.Error) {
515
515
if ! s .checkResult () {
516
516
return & ClientError {CR_NO_RESULT_SET , CR_NO_RESULT_SET_STR }
517
517
}
518
- // Check for unread rows
519
- if ! s .result .allRead {
520
- // Read all rows
521
- err = s .getAllRows ()
522
- if err != nil {
523
- return
524
- }
525
- }
526
- // Unset the result
527
- s .result = nil
518
+ // Free the current result set
519
+ s .freeAll (false )
528
520
return
529
521
}
530
522
@@ -557,6 +549,9 @@ func (s *Statement) NextResult() (more bool, err os.Error) {
557
549
// Read result from server
558
550
s .c .sequence ++
559
551
_ , err = s .getResult (PACKET_OK | PACKET_ERROR | PACKET_RESULT )
552
+ if err != nil || s .result == nil {
553
+ return
554
+ }
560
555
// Store fields
561
556
err = s .getFields ()
562
557
return
@@ -575,9 +570,13 @@ func (s *Statement) Reset() (err os.Error) {
575
570
return & ClientError {CR_NO_PREPARE_STMT , CR_NO_PREPARE_STMT_STR }
576
571
}
577
572
// Pre-run checks
578
- if ! s .c .checkConn () || s . checkResult () {
573
+ if ! s .c .checkConn () {
579
574
return & ClientError {CR_COMMANDS_OUT_OF_SYNC , CR_COMMANDS_OUT_OF_SYNC_STR }
580
575
}
576
+ // Free any results
577
+ if s .checkResult () {
578
+ err = s .freeAll (true )
579
+ }
581
580
// Reset client
582
581
s .reset ()
583
582
// Send command
@@ -724,3 +723,48 @@ func (s *Statement) getResult(types packetType) (eof bool, err os.Error) {
724
723
}
725
724
return
726
725
}
726
+
727
+ // Free any result sets waiting to be read
728
+ func (s * Statement ) freeAll (next bool ) (err os.Error ) {
729
+ // Check for unread rows
730
+ if ! s .result .allRead {
731
+ // Read all rows
732
+ err = s .getAllRows ()
733
+ if err != nil {
734
+ return
735
+ }
736
+ }
737
+ // Unset the result
738
+ s .result = nil
739
+ // Check for next result
740
+ if next {
741
+ for {
742
+ // Check if more results exist
743
+ if ! s .c .MoreResults () {
744
+ break
745
+ }
746
+ // Get next result
747
+ s .c .sequence ++
748
+ _ , err = s .getResult (PACKET_OK | PACKET_ERROR | PACKET_RESULT )
749
+ if err != nil {
750
+ return
751
+ }
752
+ if s .result == nil {
753
+ continue
754
+ }
755
+ // Set result mode to RESULT_FREE
756
+ s .result .mode = RESULT_FREE
757
+ // Read fields
758
+ err = s .getFields ()
759
+ if err != nil {
760
+ return
761
+ }
762
+ // Read rows
763
+ err = s .getAllRows ()
764
+ if err != nil {
765
+ return
766
+ }
767
+ }
768
+ }
769
+ return
770
+ }
0 commit comments