Skip to content
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

[BUG] SelectOne does not return ErrNoRows when selecting into a value #150

Closed
robfig opened this issue Mar 18, 2014 · 5 comments
Closed
Labels
Milestone

Comments

@robfig
Copy link
Contributor

robfig commented Mar 18, 2014

The documentation for SelectOne says this:

// SelectOne executes the given query (which should be a SELECT statement)
// and binds the result to holder, which must be a pointer.
//
// If no row is found, an an error (sql.ErrNoRows specifically) will be returned
//
// If more than one row is found, an error will be returned.

However, if the caller selects into a primitive, e.g.

var value int
var err = dbm.SelectOne(&value, "select 1 from foo where id = ?", id) 

err will be nil even if no row is found. That is because the code delegates to selectVal for that case, which does not have that requirement.

func SelectOne(m *DbMap, e SqlExecutor, holder interface{}, query string, args ...interface{}) error {
    t := reflect.TypeOf(holder)
    ...
    if t.Kind() == reflect.Struct {
        if list != nil && len(list) > 0 {
            ...
        } else {
            // No rows found, return a proper error.
            return sql.ErrNoRows
        }
        return nil
    }

    return selectVal(e, holder, query, args...)
}

func selectVal(e SqlExecutor, holder interface{}, query string, args ...interface{}) error {
    ...
    rows, err := e.query(query, args...)
    if err != nil {
        return err
    }
    defer rows.Close()

    if rows.Next() {
        err = rows.Scan(holder)
        if err != nil {
            return err
        }
    }

    return nil
}
@coopernurse
Copy link
Contributor

Good catch Rob.

Presumably we don't want the semantics of SelectInt, SelectFloat, SelectStr, etc to change.

If we want those functions to continue to return zero values, then we could:

  • modify selectVal() to return sql.ErrNoRows (fixes SelectOne())
  • modify the other selectVal() callers to ignore sql.ErrNoRows if err is non-nil

Another option is to return a 2nd value from selectVal() that indicates whether a row was found, but that doesn't seem necessary.

Any opinions?

@robfig
Copy link
Contributor Author

robfig commented Mar 18, 2014

Yeah -- too bad since it seems like returning ErrNoRows could be helpful for SelectInt too, but I think that ship has sailed

I think your bulleted plan is the right one. It is explicit and roughly the smallest change possible. I think that I or one of my colleagues can provide a fix before long

coopernurse pushed a commit that referenced this issue Mar 23, 2014
… funcs to ignore this error and continue to return zeroVal (per docs). This fixes SelectOne() for pointers to primitive values.
JonPulfer pushed a commit to JonPulfer/gorp that referenced this issue Mar 31, 2014
…Select* funcs to ignore this error and continue to return zeroVal (per docs). This fixes SelectOne() for pointers to primitive values.
@seantalts
Copy link

is this fixed in the latest gorp?

@coopernurse
Copy link
Contributor

I haven't merged it to master yet. If folks think it looks good I'll promote it to master this week.

@coopernurse coopernurse added this to the v1.6 milestone May 14, 2014
@coopernurse
Copy link
Contributor

Hi,

This issue has been resolved in develop. Please test your project
using the latest from github.com/coopernurse/gorp's develop branch
and post a comment on this ticket if things look good to you:

https://github.com/coopernurse/gorp/issues/170

If nothing is found, v1.6 will be merged to master on May 23

thanks, -- James

mattc58 pushed a commit to mattc58/gorp that referenced this issue May 21, 2014
…se migrations

commit 50ff6b2
Merge: 96ea999 def0f72
Author: Matt Culbreth <matthew@ionicsecurity.com>
Date:   Wed May 21 17:12:50 2014 -0400

    fixed merge issue with mattc58/gorp for transaction support

commit 96ea999
Author: Matt Culbreth <matthew@ionicsecurity.com>
Date:   Wed May 21 17:09:31 2014 -0400

    Changed DbMap and createTables() to be able to support Transactions in addtion to Connections. This is for Goose migration support.

    commit 1e2e414
    Author: Matt Culbreth <matthew@ionicsecurity.com>
    Date:   Fri Apr 11 09:20:37 2014 -0400

        fixed else statement

    commit 4fc09f6
    Author: Matt Culbreth <matthew@ionicsecurity.com>
    Date:   Fri Apr 11 09:18:40 2014 -0400

        changed dropTableImpl to use the transaction if it's there

    commit 25cbcab
    Author: Matt Culbreth <mattculbreth@gmail.com>
    Date:   Fri Apr 11 12:56:10 2014 +0000

        * added Tx to DbMap
        * added logic in createTables() to switch between Tx and DbMap for the Exec

commit def0f72
Author: James Cooper <jamespcooper@gmail.com>
Date:   Mon May 19 07:31:13 2014 -0700

    clean up dialect receiver var name. add QuerySuffix() to SqlServerDialect

commit 325f5a7
Author: James Cooper <jamespcooper@gmail.com>
Date:   Mon May 19 07:30:45 2014 -0700

    ensure that dialect structs comply with interface

commit 096714f
Author: James Cooper <jamespcooper@gmail.com>
Date:   Mon May 19 07:30:05 2014 -0700

    add sql server driver to README

commit ef29a36
Author: James Cooper <jamespcooper@gmail.com>
Date:   Fri May 16 13:22:59 2014 -0700

    readme: add notes about SQL Server and Oracle support

commit 214aeb9
Author: James Cooper <jamespcooper@gmail.com>
Date:   Fri May 16 13:10:19 2014 -0700

    Added docs for DbMap.TableFor

commit 4593fad
Author: Samuel Nelson <valczir.darkvein@gmail.com>
Date:   Fri May 16 13:27:27 2014 -0600

    Export TableMap.Columns and DbMap.TableFor()

commit 728a08e
Author: Alex Guerrieri <klaidliadon@gmail.com>
Date:   Fri May 16 16:51:28 2014 +0200

    Oracle diver develop, new method QuerySuffix() in dialect, better args string

commit d69be84
Author: Pierre <pierre@prinetti.it>
Date:   Fri May 16 10:23:29 2014 +0200

    fix QuoteField for SqlServerDialect

    from blank to dbl-quote

commit 354af19
Author: Pierre Prinetti <pierre@prinetti.it>
Date:   Thu May 15 10:56:09 2014 +0200

    add support for SQL Server

commit 108d32d
Author: Harley Laue <losinggeneration@gmail.com>
Date:   Wed May 7 16:29:35 2014 -0500

    MySQL certainly does have schemas

    * I'd wager it was a copy/paste job from SQLite.
    * http://dev.mysql.com/doc/refman/5.1/en/create-database.html

commit 86bc8f6
Author: Samuel Nelson <valczir.darkvein@gmail.com>
Date:   Thu Apr 17 15:34:29 2014 -0600

    More clarification

commit 059256e
Author: Samuel Nelson <valczir.darkvein@gmail.com>
Date:   Thu Apr 17 15:32:41 2014 -0600

    Minor documentation clarification

commit 2e7bcc3
Author: Samuel Nelson <valczir.darkvein@gmail.com>
Date:   Thu Apr 17 15:31:01 2014 -0600

    Support non-integer autoincrement fields in postgres

commit 9259f03
Author: Mike Thompson <michael.p.thompson@gmail.com>
Date:   Mon Apr 7 16:06:36 2014 -0700

    Update statements to handle Non-incrementing PK's
    The change in this if statement allows for the driver to correctly
    handle non-auto-incrementing primary keys. The check has been changed to
    if it is auto-incrementing or transient.

commit ed5dce5
Author: Mike Thompson <michael.p.thompson@gmail.com>
Date:   Mon Apr 7 15:02:48 2014 -0700

    Initial test to show failure
    This initial test, against this code base, shows that "update" against a
    table, with only one column, where that coumn is the primary key, and is
    not auto-incrementing, will fail.

commit b795354
Author: umisama <umisama@fe2o3.jp>
Date:   Mon Mar 17 19:35:29 2014 +0900

    Add Pre/Post functions to godoc

commit f1c93ef
Author: umisama <umisama@fe2o3.jp>
Date:   Mon Mar 17 19:11:23 2014 +0900

    Modified Pre/Post functions into using interface

commit b5ce3b9
Author: zhengjia <jiazheng@live.com>
Date:   Wed Mar 5 21:38:57 2014 -0600

    Add support for alias column

commit eeb38f7
Author: James Cooper <jamespcooper@gmail.com>
Date:   Wed May 14 13:11:44 2014 -0700

    TestSetUniqueTogether: fix sqlite test expectation (force err string to lower)

commit f296a21
Author: James Cooper <jamespcooper@gmail.com>
Date:   Sun Mar 23 07:53:33 2014 -0700

    go-gorp#150 - modify selectVal() to return sql.ErrNoRows, and modify Select* funcs to ignore this error and continue to return zeroVal (per docs).  This fixes SelectOne() for pointers to primitive values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants