feat: Expose scanning any/all without enforcing struct #978
      
        
          +435
        
        
          −18
        
        
          
        
      
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
High level
Exposes two functions -
ScanAllandScanSingleRowthat can be used with aRowsinstance to safely/easily bind to adestthat's a struct or a scannable.Use case
The reason for this addition is, I'm writing an wrapper around
*sqlx.DBthat essentially prepends a GTID check into a multi-statement. I'm trying to supportGetContextandSelectContextso that they can be called without any difference to the function signature when using the wrapper (and no need for awareness of wrapper logic at all).The code I've written so far that's running into this blocker is:
QueryxContextto get the results of the multi-statement queriesNextResultSet)destjust like the standardGetContextwouldrows.StructScan(dest), it panics if the provideddestisn't a structpsuedo-code
With this PR, the end state would be that I can swap my last line (
rows.StructScan(dest)) tosqlx.ScanSingleRow(rows, dest, false).I have the same issue with my
SelectContextfunction where i'll needsqlx.ScanAll.Details
This PR exposes
scanAllas a public function. The function has been unmodified, other than updating the casing (so that it's public) and removing the sentence in the comment about the desirability to make it public 🙂 all calls toscanAllhave been updated toScanAll.Additionally, this PR exposes a function called
ScanSingleRowwhich is a subset of the existingscanAnyfunction. It was slightly modified to supportRowsorRow, but should not introduce a functional difference from the existing implementation.Neither of these changes are breaking changes. They essentially expose the "scan x" functionality so that they can be called without needing to reflect on the dest before choosing to call something like
StructScan.Additional Context
If it means anything at all, this change is for code I'm writing @github. I'm hoping not to have to keep a
sqlxfork just for this delta. If there's feedback in terms of naming, or, if you'd prefer this to somehow be put under a subdirectory/package, I'm happy to iterate here! If there's a way to accomplish this with the existing code, without reinventing a chunk of sqlx reflection/scanning, I'm happy to close this PR.