From 08801c2fe48a0b0542fdb906e0fe07effc4d0f21 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 29 Jun 2013 13:23:26 -0500 Subject: [PATCH] Select(Value|Row) require exactly one found row --- connection.go | 20 +++++++++++--------- connection_test.go | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/connection.go b/connection.go index 8cbd3545..652e2c0a 100644 --- a/connection.go +++ b/connection.go @@ -29,11 +29,13 @@ type Connection struct { txStatus byte } -type NoRowsFoundError struct { - msg string +type NotSingleRowError struct { + RowCount int64 } -func (e NoRowsFoundError) Error() string { return e.msg } +func (e NotSingleRowError) Error() string { + return fmt.Sprintf("Expected to find 1 row exactly, instead found %d", e.RowCount) +} func Connect(parameters ConnectionParameters) (c *Connection, err error) { c = new(Connection) @@ -147,6 +149,7 @@ func (c *Connection) SelectRows(sql string) (rows []map[string]interface{}, err return } +// Returns a NotSingleRowError if exactly one row is not found func (c *Connection) SelectRow(sql string) (row map[string]interface{}, err error) { var numRowsFound int64 @@ -156,14 +159,13 @@ func (c *Connection) SelectRow(sql string) (row map[string]interface{}, err erro return nil } err = c.SelectFunc(sql, onDataRow) - if err == nil { - if numRowsFound == 0 { - err = NoRowsFoundError{} - } + if err == nil && numRowsFound != 1 { + err = NotSingleRowError{RowCount: numRowsFound} } return } +// Returns a NotSingleRowError if exactly one row is not found func (c *Connection) SelectValue(sql string) (v interface{}, err error) { var numRowsFound int64 @@ -174,8 +176,8 @@ func (c *Connection) SelectValue(sql string) (v interface{}, err error) { } err = c.SelectFunc(sql, onDataRow) if err == nil { - if numRowsFound == 0 { - err = NoRowsFoundError{} + if numRowsFound != 1 { + err = NotSingleRowError{RowCount: numRowsFound} } } return diff --git a/connection_test.go b/connection_test.go index c0ba01d3..70fae107 100644 --- a/connection_test.go +++ b/connection_test.go @@ -194,8 +194,13 @@ func TestSelectRow(t *testing.T) { } _, err = conn.SelectRow("select 'Jack' as name where 1=2") - if _, ok := err.(NoRowsFoundError); !ok { - t.Error("No matching row should have returned NoRowsFoundError") + if _, ok := err.(NotSingleRowError); !ok { + t.Error("No matching row should have returned NotSingleRowError") + } + + _, err = conn.SelectRow("select * from (values ('Matthew'), ('Mark')) t") + if _, ok := err.(NotSingleRowError); !ok { + t.Error("Multiple matching rows should have returned NotSingleRowError") } } @@ -223,9 +228,14 @@ func TestConnectionSelectValue(t *testing.T) { test("select 1.23::float8", float64(1.23)) _, err := conn.SelectValue("select 'Jack' as name where 1=2") - if _, ok := err.(NoRowsFoundError); !ok { + if _, ok := err.(NotSingleRowError); !ok { t.Error("No matching row should have returned NoRowsFoundError") } + + _, err = conn.SelectValue("select * from (values ('Matthew'), ('Mark')) t") + if _, ok := err.(NotSingleRowError); !ok { + t.Error("Multiple matching rows should have returned NotSingleRowError") + } } func TestSelectValues(t *testing.T) {