qbs icon indicating copy to clipboard operation
qbs copied to clipboard

Return errors in QueryRow properly

Open codemac opened this issue 10 years ago • 9 comments

  • oracle.go: Handle QueryRow multiple return values
  • postgres.go: Handle QueryRow multiple return values
  • qbs.go: Return an error during QueryRow

This fixes a panic in the qbs.Count and qbs.ContainsValue implementation

Signed-off-by: Jeff Mickey [email protected]

codemac avatar Feb 10 '15 02:02 codemac

See #54 for details of why this is necessary.

codemac avatar Feb 10 '15 02:02 codemac

Thank you for reporting the panic issue, but I can not merge this PR because it is an API change, ti will break the previous working code.

Can we just check if the row is nil and return 0 in the "Count" method?

coocood avatar Feb 10 '15 03:02 coocood

Well, you certainly could, but I think the other function (doQueryRow, doQueryRows) all return errors as well, so this seems like a natural implementation. You don't document what QueryRow will return on error, and I think maintaining hidden behavior is a bad choice. Semantic change requiring two different code changes (an if row == nil or if err != nil) means you're changing the API either way.

You're more than welcome to make changes to your code however you wish, this took me very little time to fix.

codemac avatar Feb 10 '15 03:02 codemac

I just remembered that I didn't return error in "QueryRow" method because I follow the standard library "database.sql" package . So the error shoud be set in the "Row", and the "Row" should never be nil.

Would you please change it this way, then I will merge it as soon as possible.

Thank you.

coocood avatar Feb 10 '15 04:02 coocood

On February 9, 2015 8:05:37 PM PST, Ewan Chou [email protected] wrote:

I just remembered that I didn't return error in "QueryRow" method because I follow the standard library "database.sql" package . So the error shoud be set in the "Row", and the "Row" should never be nil.

Would you please change it this way, then I will merge it as soon as possible.

Thank you.


Reply to this email directly or view it on GitHub: https://github.com/coocood/qbs/pull/55#issuecomment-73641128

Ahhh that makes much more sense now that you explain it. Will change it accordingly. // mickey

codemac avatar Feb 10 '15 04:02 codemac

Problem - the err in sql.Row is unexported. I don't know how to set the error in Row. As long as a tx.Prepare is tied up in this API, I don't think it's possible without our own Row type.

codemac avatar Feb 10 '15 19:02 codemac

We can set the error via reflection.

coocood avatar Feb 11 '15 01:02 coocood

It's unexported, you can't reflect on it... which struct are you talking about reflecting on? sql.Row.err is private to the stdlib. I think I'm misunderstanding the issue.

codemac avatar Feb 11 '15 01:02 codemac

Sorry, I didn't write go reflection for a whiel and confused it with JAVA reflection.

Then if we failed to prepare, we can just call db.QueryRow,, if tx is not nil, we call tx.QueryRow.

coocood avatar Feb 11 '15 02:02 coocood