mirror of https://github.com/go-gorm/gorm.git
Adding json type for mssql dialect, similar to postgres.Jsonb (#1934)
* Adding json type for mssql dialect, similar to postgres.Jsonb * Adding proper comments
This commit is contained in:
parent
0fd395ab37
commit
dbb25e9487
|
@ -1,12 +1,16 @@
|
|||
package mssql
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
// Importing mssql driver package only in dialect file, otherwide not needed
|
||||
_ "github.com/denisenkom/go-mssqldb"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
@ -201,3 +205,27 @@ func currentDatabaseAndTable(dialect gorm.Dialect, tableName string) (string, st
|
|||
}
|
||||
return dialect.CurrentDatabase(), tableName
|
||||
}
|
||||
|
||||
// JSON type to support easy handling of JSON data in character table fields
|
||||
// using golang json.RawMessage for deferred decoding/encoding
|
||||
type JSON struct {
|
||||
json.RawMessage
|
||||
}
|
||||
|
||||
// Value get value of JSON
|
||||
func (j JSON) Value() (driver.Value, error) {
|
||||
if len(j.RawMessage) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return j.MarshalJSON()
|
||||
}
|
||||
|
||||
// Scan scan value into JSON
|
||||
func (j *JSON) Scan(value interface{}) error {
|
||||
str, ok := value.(string)
|
||||
if !ok {
|
||||
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value (strcast):", value))
|
||||
}
|
||||
bytes := []byte(str)
|
||||
return json.Unmarshal(bytes, j)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue