Skip to content

Commit 49099d9

Browse files
committed
Import the driver with the dialect
1 parent 2f548ed commit 49099d9

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

dialect.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package gorm
22

33
import (
4+
"database/sql"
5+
"database/sql/driver"
46
"fmt"
57
"reflect"
68
"strings"
79
"time"
810

11+
_ "github.com/lib/pq"
12+
"github.com/lib/pq/hstore"
913
"github.com/ngorm/common"
1014
"github.com/ngorm/ngorm/model"
1115
)
@@ -157,3 +161,48 @@ func isUUID(value reflect.Value) bool {
157161
lower := strings.ToLower(typename)
158162
return "uuid" == lower || "guid" == lower
159163
}
164+
165+
type Hstore map[string]*string
166+
167+
// Value get value of Hstore
168+
func (h Hstore) Value() (driver.Value, error) {
169+
hstore := hstore.Hstore{Map: map[string]sql.NullString{}}
170+
if len(h) == 0 {
171+
return nil, nil
172+
}
173+
174+
for key, value := range h {
175+
var s sql.NullString
176+
if value != nil {
177+
s.String = *value
178+
s.Valid = true
179+
}
180+
hstore.Map[key] = s
181+
}
182+
return hstore.Value()
183+
}
184+
185+
// Scan scan value into Hstore
186+
func (h *Hstore) Scan(value interface{}) error {
187+
hstore := hstore.Hstore{}
188+
189+
if err := hstore.Scan(value); err != nil {
190+
return err
191+
}
192+
193+
if len(hstore.Map) == 0 {
194+
return nil
195+
}
196+
197+
*h = Hstore{}
198+
for k := range hstore.Map {
199+
if hstore.Map[k].Valid {
200+
s := hstore.Map[k].String
201+
(*h)[k] = &s
202+
} else {
203+
(*h)[k] = nil
204+
}
205+
}
206+
207+
return nil
208+
}

0 commit comments

Comments
 (0)