-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Checking if a result/result set is nil #46
Comments
I think you should be able to compare the return value of |
That works—thanks! I had database/sql as a side-effect import (since I’m leaning on sqlx) and didn’t spot that. Might have to dig through the database/sql API a little more. Additionally: it looks like the best way to do this when using sqlx.Queryx to pull into a slice of structs is check if On 26 Jan 2014, at 11:38 AM, Kamil Kisiel notifications@github.com wrote:
|
This is somewhat related to the above—I'm pulling a set of results from the DB (using Postgres' full text search capabilities): rows, err := db.Queryx(`SELECT * FROM listings
WHERE tsv @@ plainto_tsquery('pg_catalog.english', $1)
ORDER BY expiry_date DESC LIMIT $2`, searchTerm, pageLimit) // pageLimit is 10 in this case
if rows.Next() == false { // or rows.Rows.Next() ?
...
}
err = sqlx.StructScan(&rows.Rows, &results)
if err != nil {
...
} I'm doing some testing and exactly one record matches the search term provided. Running the query directly (via the CLI) results the expected row (singular). When running this query using sqlx however, If I change it to ensure that at least two (2) rows are returned, In the case where I'm expecting anywhere between 0 and ∞ results, how should I be confirming that there are > 0 rows returned before calling |
You want to know if there are rows before you go to scan? Is this to save on allocation? You can use the If you want to avoid that allocation, you could make a zero-length rows, err := db.Queryx(...)
results := make([]MyType, 0, 0)
err = rows.StructScan(&rows.Rows, &results) Keep in mind that I'm thinking very strongly of changing Edit Also, in my tests, |
@jmoiron Thanks for looking into this. I'm not particularly fussed on saving on allocation (but it is a nice thing to do). I'm already allocating the result slice as the function returns You're right that I could replace I was definitely running into that last gotcha though since I was checking the value of rows, err := db.Queryx(`SELECT id, title, company, location FROM listings WHERE tsv @@ plainto_tsquery('pg_catalog.english', $1) ORDER BY expiry_date DESC LIMIT $2`, searchTerm, pageLimit)
if err != nil {
return ...
}
defer rows.Close()
if rows.Next() == false {
return ...
}
err = sqlx.StructScan(&rows.Rows, &results)
if err != nil {
return ...
} Thanks again for investigating/helping out. |
Ah yes, for rows.Next() {
...
} It sorta assumes that you've processed as far as the cursor has gone. This is unlike Going to close this as I think everything is working more or less as it should. Glad I could help! |
Is there a simple way to do this? It'd be nice to be able to delineate between driver errors (no relation, column doesn't exist, etc) and a "no rows in result set" error without having to do string comparison.
e.g. A DB error will pass an error that will throw a HTTP 500 to the user and log it; a record that doesn't will just through a HTTP 404.
Something like the below:
The text was updated successfully, but these errors were encountered: