summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/lib/pq/hstore
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/lib/pq/hstore')
-rw-r--r--vendor/github.com/lib/pq/hstore/hstore.go118
-rw-r--r--vendor/github.com/lib/pq/hstore/hstore_test.go148
2 files changed, 0 insertions, 266 deletions
diff --git a/vendor/github.com/lib/pq/hstore/hstore.go b/vendor/github.com/lib/pq/hstore/hstore.go
deleted file mode 100644
index f1470db14..000000000
--- a/vendor/github.com/lib/pq/hstore/hstore.go
+++ /dev/null
@@ -1,118 +0,0 @@
-package hstore
-
-import (
- "database/sql"
- "database/sql/driver"
- "strings"
-)
-
-// Hstore is a wrapper for transferring Hstore values back and forth easily.
-type Hstore struct {
- Map map[string]sql.NullString
-}
-
-// escapes and quotes hstore keys/values
-// s should be a sql.NullString or string
-func hQuote(s interface{}) string {
- var str string
- switch v := s.(type) {
- case sql.NullString:
- if !v.Valid {
- return "NULL"
- }
- str = v.String
- case string:
- str = v
- default:
- panic("not a string or sql.NullString")
- }
-
- str = strings.Replace(str, "\\", "\\\\", -1)
- return `"` + strings.Replace(str, "\"", "\\\"", -1) + `"`
-}
-
-// Scan implements the Scanner interface.
-//
-// Note h.Map is reallocated before the scan to clear existing values. If the
-// hstore column's database value is NULL, then h.Map is set to nil instead.
-func (h *Hstore) Scan(value interface{}) error {
- if value == nil {
- h.Map = nil
- return nil
- }
- h.Map = make(map[string]sql.NullString)
- var b byte
- pair := [][]byte{{}, {}}
- pi := 0
- inQuote := false
- didQuote := false
- sawSlash := false
- bindex := 0
- for bindex, b = range value.([]byte) {
- if sawSlash {
- pair[pi] = append(pair[pi], b)
- sawSlash = false
- continue
- }
-
- switch b {
- case '\\':
- sawSlash = true
- continue
- case '"':
- inQuote = !inQuote
- if !didQuote {
- didQuote = true
- }
- continue
- default:
- if !inQuote {
- switch b {
- case ' ', '\t', '\n', '\r':
- continue
- case '=':
- continue
- case '>':
- pi = 1
- didQuote = false
- continue
- case ',':
- s := string(pair[1])
- if !didQuote && len(s) == 4 && strings.ToLower(s) == "null" {
- h.Map[string(pair[0])] = sql.NullString{String: "", Valid: false}
- } else {
- h.Map[string(pair[0])] = sql.NullString{String: string(pair[1]), Valid: true}
- }
- pair[0] = []byte{}
- pair[1] = []byte{}
- pi = 0
- continue
- }
- }
- }
- pair[pi] = append(pair[pi], b)
- }
- if bindex > 0 {
- s := string(pair[1])
- if !didQuote && len(s) == 4 && strings.ToLower(s) == "null" {
- h.Map[string(pair[0])] = sql.NullString{String: "", Valid: false}
- } else {
- h.Map[string(pair[0])] = sql.NullString{String: string(pair[1]), Valid: true}
- }
- }
- return nil
-}
-
-// Value implements the driver Valuer interface. Note if h.Map is nil, the
-// database column value will be set to NULL.
-func (h Hstore) Value() (driver.Value, error) {
- if h.Map == nil {
- return nil, nil
- }
- parts := []string{}
- for key, val := range h.Map {
- thispart := hQuote(key) + "=>" + hQuote(val)
- parts = append(parts, thispart)
- }
- return []byte(strings.Join(parts, ",")), nil
-}
diff --git a/vendor/github.com/lib/pq/hstore/hstore_test.go b/vendor/github.com/lib/pq/hstore/hstore_test.go
deleted file mode 100644
index 1c9f2bd49..000000000
--- a/vendor/github.com/lib/pq/hstore/hstore_test.go
+++ /dev/null
@@ -1,148 +0,0 @@
-package hstore
-
-import (
- "database/sql"
- "os"
- "testing"
-
- _ "github.com/lib/pq"
-)
-
-type Fatalistic interface {
- Fatal(args ...interface{})
-}
-
-func openTestConn(t Fatalistic) *sql.DB {
- datname := os.Getenv("PGDATABASE")
- sslmode := os.Getenv("PGSSLMODE")
-
- if datname == "" {
- os.Setenv("PGDATABASE", "pqgotest")
- }
-
- if sslmode == "" {
- os.Setenv("PGSSLMODE", "disable")
- }
-
- conn, err := sql.Open("postgres", "")
- if err != nil {
- t.Fatal(err)
- }
-
- return conn
-}
-
-func TestHstore(t *testing.T) {
- db := openTestConn(t)
- defer db.Close()
-
- // quitely create hstore if it doesn't exist
- _, err := db.Exec("CREATE EXTENSION IF NOT EXISTS hstore")
- if err != nil {
- t.Skipf("Skipping hstore tests - hstore extension create failed: %s", err.Error())
- }
-
- hs := Hstore{}
-
- // test for null-valued hstores
- err = db.QueryRow("SELECT NULL::hstore").Scan(&hs)
- if err != nil {
- t.Fatal(err)
- }
- if hs.Map != nil {
- t.Fatalf("expected null map")
- }
-
- err = db.QueryRow("SELECT $1::hstore", hs).Scan(&hs)
- if err != nil {
- t.Fatalf("re-query null map failed: %s", err.Error())
- }
- if hs.Map != nil {
- t.Fatalf("expected null map")
- }
-
- // test for empty hstores
- err = db.QueryRow("SELECT ''::hstore").Scan(&hs)
- if err != nil {
- t.Fatal(err)
- }
- if hs.Map == nil {
- t.Fatalf("expected empty map, got null map")
- }
- if len(hs.Map) != 0 {
- t.Fatalf("expected empty map, got len(map)=%d", len(hs.Map))
- }
-
- err = db.QueryRow("SELECT $1::hstore", hs).Scan(&hs)
- if err != nil {
- t.Fatalf("re-query empty map failed: %s", err.Error())
- }
- if hs.Map == nil {
- t.Fatalf("expected empty map, got null map")
- }
- if len(hs.Map) != 0 {
- t.Fatalf("expected empty map, got len(map)=%d", len(hs.Map))
- }
-
- // a few example maps to test out
- hsOnePair := Hstore{
- Map: map[string]sql.NullString{
- "key1": {String: "value1", Valid: true},
- },
- }
-
- hsThreePairs := Hstore{
- Map: map[string]sql.NullString{
- "key1": {String: "value1", Valid: true},
- "key2": {String: "value2", Valid: true},
- "key3": {String: "value3", Valid: true},
- },
- }
-
- hsSmorgasbord := Hstore{
- Map: map[string]sql.NullString{
- "nullstring": {String: "NULL", Valid: true},
- "actuallynull": {String: "", Valid: false},
- "NULL": {String: "NULL string key", Valid: true},
- "withbracket": {String: "value>42", Valid: true},
- "withequal": {String: "value=42", Valid: true},
- `"withquotes1"`: {String: `this "should" be fine`, Valid: true},
- `"withquotes"2"`: {String: `this "should\" also be fine`, Valid: true},
- "embedded1": {String: "value1=>x1", Valid: true},
- "embedded2": {String: `"value2"=>x2`, Valid: true},
- "withnewlines": {String: "\n\nvalue\t=>2", Valid: true},
- "<<all sorts of crazy>>": {String: `this, "should,\" also, => be fine`, Valid: true},
- },
- }
-
- // test encoding in query params, then decoding during Scan
- testBidirectional := func(h Hstore) {
- err = db.QueryRow("SELECT $1::hstore", h).Scan(&hs)
- if err != nil {
- t.Fatalf("re-query %d-pair map failed: %s", len(h.Map), err.Error())
- }
- if hs.Map == nil {
- t.Fatalf("expected %d-pair map, got null map", len(h.Map))
- }
- if len(hs.Map) != len(h.Map) {
- t.Fatalf("expected %d-pair map, got len(map)=%d", len(h.Map), len(hs.Map))
- }
-
- for key, val := range hs.Map {
- otherval, found := h.Map[key]
- if !found {
- t.Fatalf(" key '%v' not found in %d-pair map", key, len(h.Map))
- }
- if otherval.Valid != val.Valid {
- t.Fatalf(" value %v <> %v in %d-pair map", otherval, val, len(h.Map))
- }
- if otherval.String != val.String {
- t.Fatalf(" value '%v' <> '%v' in %d-pair map", otherval.String, val.String, len(h.Map))
- }
- }
- }
-
- testBidirectional(hsOnePair)
- testBidirectional(hsThreePairs)
- testBidirectional(hsSmorgasbord)
-}