From e27511a4c59624f8b98cfb6abdeec95a7b35d53c Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 20 Apr 2013 11:30:06 -0500 Subject: [PATCH] Extract Connection.Select* to own file --- conn.go | 64 -------------- conn_test.go | 136 ------------------------------ connection_select_value.go | 70 ++++++++++++++++ connection_select_value_test.go | 142 ++++++++++++++++++++++++++++++++ 4 files changed, 212 insertions(+), 200 deletions(-) create mode 100644 connection_select_value.go create mode 100644 connection_select_value_test.go diff --git a/conn.go b/conn.go index d429a151..b6e7f654 100644 --- a/conn.go +++ b/conn.go @@ -142,70 +142,6 @@ func (c *Connection) SelectRows(sql string) (rows []map[string]string, err error return } -func (c *Connection) SelectString(sql string) (s string, err error) { - onDataRow := func(r *messageReader, _ []fieldDescription) error { - var null bool - s, null = c.rxDataRowFirstValue(r) - if null { - return errors.New("Unexpected NULL") - } - return nil - } - err = c.SelectFunc(sql, onDataRow) - return -} - -func (c *Connection) selectInt(sql string, size int) (i int64, err error) { - var s string - s, err = c.SelectString(sql) - if err != nil { - return - } - - i, err = strconv.ParseInt(s, 10, size) - return -} - -func (c *Connection) SelectInt64(sql string) (i int64, err error) { - return c.selectInt(sql, 64) -} - -func (c *Connection) SelectInt32(sql string) (i int32, err error) { - var i64 int64 - i64, err = c.selectInt(sql, 32) - i = int32(i64) - return -} - -func (c *Connection) SelectInt16(sql string) (i int16, err error) { - var i64 int64 - i64, err = c.selectInt(sql, 16) - i = int16(i64) - return -} - -func (c *Connection) selectFloat(sql string, size int) (f float64, err error) { - var s string - s, err = c.SelectString(sql) - if err != nil { - return - } - - f, err = strconv.ParseFloat(s, size) - return -} - -func (c *Connection) SelectFloat64(sql string) (f float64, err error) { - return c.selectFloat(sql, 64) -} - -func (c *Connection) SelectFloat32(sql string) (f float32, err error) { - var f64 float64 - f64, err = c.selectFloat(sql, 32) - f = float32(f64) - return -} - func (c *Connection) SelectAllString(sql string) (strings []string, err error) { strings = make([]string, 0, 8) onDataRow := func(r *messageReader, _ []fieldDescription) error { diff --git a/conn_test.go b/conn_test.go index bf6279f6..59527985 100644 --- a/conn_test.go +++ b/conn_test.go @@ -158,142 +158,6 @@ func TestSelectRows(t *testing.T) { } } -func TestSelectString(t *testing.T) { - conn := getSharedConnection() - - s, err := conn.SelectString("select 'foo'") - if err != nil { - t.Error("Unable to select string: " + err.Error()) - } else if s != "foo" { - t.Error("Received incorrect string") - } - - _, err = conn.SelectString("select null") - if err == nil { - t.Error("Should have received error on null") - } -} - -func TestSelectInt64(t *testing.T) { - conn := getSharedConnection() - - i, err := conn.SelectInt64("select 1") - if err != nil { - t.Fatal("Unable to select int64: " + err.Error()) - } - - if i != 1 { - t.Error("Received incorrect int64") - } - - i, err = conn.SelectInt64("select power(2,65)::numeric") - if err == nil || !strings.Contains(err.Error(), "value out of range") { - t.Error("Expected value out of range error when selecting number greater than max int64") - } - - i, err = conn.SelectInt64("select -power(2,65)::numeric") - if err == nil || !strings.Contains(err.Error(), "value out of range") { - t.Error("Expected value out of range error when selecting number less than min int64") - } - - _, err = conn.SelectInt64("select null") - if err == nil || !strings.Contains(err.Error(), "NULL") { - t.Error("Should have received error on null") - } -} - -func TestSelectInt32(t *testing.T) { - conn := getSharedConnection() - - i, err := conn.SelectInt32("select 1") - if err != nil { - t.Fatal("Unable to select int32: " + err.Error()) - } - - if i != 1 { - t.Error("Received incorrect int32") - } - - i, err = conn.SelectInt32("select power(2,33)::numeric") - if err == nil || !strings.Contains(err.Error(), "value out of range") { - t.Error("Expected value out of range error when selecting number greater than max int32") - } - - i, err = conn.SelectInt32("select -power(2,33)::numeric") - if err == nil || !strings.Contains(err.Error(), "value out of range") { - t.Error("Expected value out of range error when selecting number less than min int32") - } - - _, err = conn.SelectInt32("select null") - if err == nil || !strings.Contains(err.Error(), "NULL") { - t.Error("Should have received error on null") - } -} - -func TestSelectInt16(t *testing.T) { - conn := getSharedConnection() - - i, err := conn.SelectInt16("select 1") - if err != nil { - t.Fatal("Unable to select int16: " + err.Error()) - } - - if i != 1 { - t.Error("Received incorrect int16") - } - - i, err = conn.SelectInt16("select power(2,17)::numeric") - if err == nil || !strings.Contains(err.Error(), "value out of range") { - t.Error("Expected value out of range error when selecting number greater than max int16") - } - - i, err = conn.SelectInt16("select -power(2,17)::numeric") - if err == nil || !strings.Contains(err.Error(), "value out of range") { - t.Error("Expected value out of range error when selecting number less than min int16") - } - - _, err = conn.SelectInt16("select null") - if err == nil || !strings.Contains(err.Error(), "NULL") { - t.Error("Should have received error on null") - } -} - -func TestSelectFloat64(t *testing.T) { - conn := getSharedConnection() - - f, err := conn.SelectFloat64("select 1.23") - if err != nil { - t.Fatal("Unable to select float64: " + err.Error()) - } - - if f != 1.23 { - t.Error("Received incorrect float64") - } - - _, err = conn.SelectFloat64("select null") - if err == nil || !strings.Contains(err.Error(), "NULL") { - t.Error("Should have received error on null") - } -} - -func TestSelectFloat32(t *testing.T) { - conn := getSharedConnection() - - f, err := conn.SelectFloat32("select 1.23") - if err != nil { - t.Fatal("Unable to select float32: " + err.Error()) - } - - if f != 1.23 { - t.Error("Received incorrect float32") - } - - _, err = conn.SelectFloat32("select null") - if err == nil || !strings.Contains(err.Error(), "NULL") { - t.Error("Should have received error on null") - } -} - func TestSelectAllString(t *testing.T) { conn := getSharedConnection() diff --git a/connection_select_value.go b/connection_select_value.go new file mode 100644 index 00000000..84a667d0 --- /dev/null +++ b/connection_select_value.go @@ -0,0 +1,70 @@ +package pgx + +import ( + "errors" + "strconv" +) + +func (c *Connection) SelectString(sql string) (s string, err error) { + onDataRow := func(r *messageReader, _ []fieldDescription) error { + var null bool + s, null = c.rxDataRowFirstValue(r) + if null { + return errors.New("Unexpected NULL") + } + return nil + } + err = c.SelectFunc(sql, onDataRow) + return +} + +func (c *Connection) selectInt(sql string, size int) (i int64, err error) { + var s string + s, err = c.SelectString(sql) + if err != nil { + return + } + + i, err = strconv.ParseInt(s, 10, size) + return +} + +func (c *Connection) SelectInt64(sql string) (i int64, err error) { + return c.selectInt(sql, 64) +} + +func (c *Connection) SelectInt32(sql string) (i int32, err error) { + var i64 int64 + i64, err = c.selectInt(sql, 32) + i = int32(i64) + return +} + +func (c *Connection) SelectInt16(sql string) (i int16, err error) { + var i64 int64 + i64, err = c.selectInt(sql, 16) + i = int16(i64) + return +} + +func (c *Connection) selectFloat(sql string, size int) (f float64, err error) { + var s string + s, err = c.SelectString(sql) + if err != nil { + return + } + + f, err = strconv.ParseFloat(s, size) + return +} + +func (c *Connection) SelectFloat64(sql string) (f float64, err error) { + return c.selectFloat(sql, 64) +} + +func (c *Connection) SelectFloat32(sql string) (f float32, err error) { + var f64 float64 + f64, err = c.selectFloat(sql, 32) + f = float32(f64) + return +} diff --git a/connection_select_value_test.go b/connection_select_value_test.go new file mode 100644 index 00000000..66646927 --- /dev/null +++ b/connection_select_value_test.go @@ -0,0 +1,142 @@ +package pgx + +import ( + "strings" + "testing" +) + +func TestSelectString(t *testing.T) { + conn := getSharedConnection() + + s, err := conn.SelectString("select 'foo'") + if err != nil { + t.Error("Unable to select string: " + err.Error()) + } else if s != "foo" { + t.Error("Received incorrect string") + } + + _, err = conn.SelectString("select null") + if err == nil { + t.Error("Should have received error on null") + } +} + +func TestSelectInt64(t *testing.T) { + conn := getSharedConnection() + + i, err := conn.SelectInt64("select 1") + if err != nil { + t.Fatal("Unable to select int64: " + err.Error()) + } + + if i != 1 { + t.Error("Received incorrect int64") + } + + i, err = conn.SelectInt64("select power(2,65)::numeric") + if err == nil || !strings.Contains(err.Error(), "value out of range") { + t.Error("Expected value out of range error when selecting number greater than max int64") + } + + i, err = conn.SelectInt64("select -power(2,65)::numeric") + if err == nil || !strings.Contains(err.Error(), "value out of range") { + t.Error("Expected value out of range error when selecting number less than min int64") + } + + _, err = conn.SelectInt64("select null") + if err == nil || !strings.Contains(err.Error(), "NULL") { + t.Error("Should have received error on null") + } +} + +func TestSelectInt32(t *testing.T) { + conn := getSharedConnection() + + i, err := conn.SelectInt32("select 1") + if err != nil { + t.Fatal("Unable to select int32: " + err.Error()) + } + + if i != 1 { + t.Error("Received incorrect int32") + } + + i, err = conn.SelectInt32("select power(2,33)::numeric") + if err == nil || !strings.Contains(err.Error(), "value out of range") { + t.Error("Expected value out of range error when selecting number greater than max int32") + } + + i, err = conn.SelectInt32("select -power(2,33)::numeric") + if err == nil || !strings.Contains(err.Error(), "value out of range") { + t.Error("Expected value out of range error when selecting number less than min int32") + } + + _, err = conn.SelectInt32("select null") + if err == nil || !strings.Contains(err.Error(), "NULL") { + t.Error("Should have received error on null") + } +} + +func TestSelectInt16(t *testing.T) { + conn := getSharedConnection() + + i, err := conn.SelectInt16("select 1") + if err != nil { + t.Fatal("Unable to select int16: " + err.Error()) + } + + if i != 1 { + t.Error("Received incorrect int16") + } + + i, err = conn.SelectInt16("select power(2,17)::numeric") + if err == nil || !strings.Contains(err.Error(), "value out of range") { + t.Error("Expected value out of range error when selecting number greater than max int16") + } + + i, err = conn.SelectInt16("select -power(2,17)::numeric") + if err == nil || !strings.Contains(err.Error(), "value out of range") { + t.Error("Expected value out of range error when selecting number less than min int16") + } + + _, err = conn.SelectInt16("select null") + if err == nil || !strings.Contains(err.Error(), "NULL") { + t.Error("Should have received error on null") + } +} + +func TestSelectFloat64(t *testing.T) { + conn := getSharedConnection() + + f, err := conn.SelectFloat64("select 1.23") + if err != nil { + t.Fatal("Unable to select float64: " + err.Error()) + } + + if f != 1.23 { + t.Error("Received incorrect float64") + } + + _, err = conn.SelectFloat64("select null") + if err == nil || !strings.Contains(err.Error(), "NULL") { + t.Error("Should have received error on null") + } +} + +func TestSelectFloat32(t *testing.T) { + conn := getSharedConnection() + + f, err := conn.SelectFloat32("select 1.23") + if err != nil { + t.Fatal("Unable to select float32: " + err.Error()) + } + + if f != 1.23 { + t.Error("Received incorrect float32") + } + + _, err = conn.SelectFloat32("select null") + if err == nil || !strings.Contains(err.Error(), "NULL") { + t.Error("Should have received error on null") + } +}