-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
561 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package pgdialect | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/uptrace/bun/schema" | ||
) | ||
|
||
type HStoreValue struct { | ||
v reflect.Value | ||
|
||
append schema.AppenderFunc | ||
scan schema.ScannerFunc | ||
} | ||
|
||
// HStore accepts a map[string]string and returns a wrapper for working with PostgreSQL | ||
// hstore data type. | ||
// | ||
// For struct fields you can use hstore tag: | ||
// | ||
// Attrs map[string]string `bun:",hstore"` | ||
func HStore(vi interface{}) *HStoreValue { | ||
v := reflect.ValueOf(vi) | ||
if !v.IsValid() { | ||
panic(fmt.Errorf("bun: HStore(nil)")) | ||
} | ||
|
||
typ := v.Type() | ||
if typ.Kind() == reflect.Ptr { | ||
typ = typ.Elem() | ||
} | ||
if typ.Kind() != reflect.Map { | ||
panic(fmt.Errorf("bun: Hstore(unsupported %s)", typ)) | ||
} | ||
|
||
return &HStoreValue{ | ||
v: v, | ||
|
||
append: pgDialect.hstoreAppender(v.Type()), | ||
scan: hstoreScanner(v.Type()), | ||
} | ||
} | ||
|
||
var ( | ||
_ schema.QueryAppender = (*HStoreValue)(nil) | ||
_ sql.Scanner = (*HStoreValue)(nil) | ||
) | ||
|
||
func (h *HStoreValue) AppendQuery(fmter schema.Formatter, b []byte) ([]byte, error) { | ||
if h.append == nil { | ||
panic(fmt.Errorf("bun: HStore(unsupported %s)", h.v.Type())) | ||
} | ||
return h.append(fmter, b, h.v), nil | ||
} | ||
|
||
func (h *HStoreValue) Scan(src interface{}) error { | ||
if h.scan == nil { | ||
return fmt.Errorf("bun: HStore(unsupported %s)", h.v.Type()) | ||
} | ||
if h.v.Kind() != reflect.Ptr { | ||
return fmt.Errorf("bun: HStore(non-pointer %s)", h.v.Type()) | ||
} | ||
return h.scan(h.v.Elem(), src) | ||
} | ||
|
||
func (h *HStoreValue) Value() interface{} { | ||
if h.v.IsValid() { | ||
return h.v.Interface() | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package pgdialect | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
type hstoreParser struct { | ||
*streamParser | ||
err error | ||
} | ||
|
||
func newHStoreParser(b []byte) *hstoreParser { | ||
p := &hstoreParser{ | ||
streamParser: newStreamParser(b, 0), | ||
} | ||
if len(b) < 6 || b[0] != '"' { | ||
p.err = fmt.Errorf("bun: can't parse hstore: %q", b) | ||
} | ||
return p | ||
} | ||
|
||
func (p *hstoreParser) NextKey() (string, error) { | ||
if p.err != nil { | ||
return "", p.err | ||
} | ||
|
||
err := p.skipByte('"') | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
key, err := p.readSubstring() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
const separator = "=>" | ||
|
||
for i := range separator { | ||
err = p.skipByte(separator[i]) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
return string(key), nil | ||
} | ||
|
||
func (p *hstoreParser) NextValue() (string, error) { | ||
if p.err != nil { | ||
return "", p.err | ||
} | ||
|
||
c, err := p.readByte() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
switch c { | ||
case '"': | ||
value, err := p.readSubstring() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if p.peek() == ',' { | ||
p.skipNext() | ||
} | ||
|
||
if p.peek() == ' ' { | ||
p.skipNext() | ||
} | ||
|
||
return string(value), nil | ||
default: | ||
value := p.readSimple() | ||
if bytes.Equal(value, []byte("NULL")) { | ||
value = nil | ||
} | ||
|
||
if p.peek() == ',' { | ||
p.skipNext() | ||
} | ||
|
||
return string(value), nil | ||
} | ||
} | ||
|
||
func (p *hstoreParser) readSimple() []byte { | ||
p.unreadByte() | ||
|
||
if i := bytes.IndexByte(p.b[p.i:], ','); i >= 0 { | ||
b := p.b[p.i : p.i+i] | ||
p.i += i | ||
return b | ||
} | ||
|
||
b := p.b[p.i:len(p.b)] | ||
p.i = len(p.b) | ||
return b | ||
} | ||
|
||
func (p *hstoreParser) readSubstring() ([]byte, error) { | ||
c, err := p.readByte() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
p.buf = p.buf[:0] | ||
for { | ||
if c == '"' { | ||
break | ||
} | ||
|
||
next, err := p.readByte() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if c == '\\' { | ||
switch next { | ||
case '\\', '"': | ||
p.buf = append(p.buf, next) | ||
|
||
c, err = p.readByte() | ||
if err != nil { | ||
return nil, err | ||
} | ||
default: | ||
p.buf = append(p.buf, '\\') | ||
c = next | ||
} | ||
continue | ||
} | ||
|
||
p.buf = append(p.buf, c) | ||
c = next | ||
} | ||
|
||
return p.buf, nil | ||
} |
Oops, something went wrong.