use gob to instead json to store pk & added json type
This commit is contained in:
parent
be6e7ac47d
commit
8cc025cd8e
19
cache.go
19
cache.go
|
@ -1,10 +1,11 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
"bytes"
|
||||||
|
"encoding/gob"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -47,16 +48,20 @@ type Cacher interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeIds(ids []PK) (string, error) {
|
func encodeIds(ids []PK) (string, error) {
|
||||||
b, err := json.Marshal(ids)
|
buf := new(bytes.Buffer)
|
||||||
if err != nil {
|
enc := gob.NewEncoder(buf)
|
||||||
return "", err
|
err := enc.Encode(ids)
|
||||||
}
|
|
||||||
return string(b), nil
|
return buf.String(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func decodeIds(s string) ([]PK, error) {
|
func decodeIds(s string) ([]PK, error) {
|
||||||
pks := make([]PK, 0)
|
pks := make([]PK, 0)
|
||||||
err := json.Unmarshal([]byte(s), &pks)
|
|
||||||
|
dec := gob.NewDecoder(bytes.NewBufferString(s))
|
||||||
|
err := dec.Decode(&pks)
|
||||||
|
|
||||||
return pks, err
|
return pks, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
type.go
4
type.go
|
@ -101,6 +101,8 @@ var (
|
||||||
Serial = "SERIAL"
|
Serial = "SERIAL"
|
||||||
BigSerial = "BIGSERIAL"
|
BigSerial = "BIGSERIAL"
|
||||||
|
|
||||||
|
Json = "JSON"
|
||||||
|
|
||||||
SqlTypes = map[string]int{
|
SqlTypes = map[string]int{
|
||||||
Bit: NUMERIC_TYPE,
|
Bit: NUMERIC_TYPE,
|
||||||
TinyInt: NUMERIC_TYPE,
|
TinyInt: NUMERIC_TYPE,
|
||||||
|
@ -112,6 +114,7 @@ var (
|
||||||
|
|
||||||
Enum: TEXT_TYPE,
|
Enum: TEXT_TYPE,
|
||||||
Set: TEXT_TYPE,
|
Set: TEXT_TYPE,
|
||||||
|
Json: TEXT_TYPE,
|
||||||
|
|
||||||
Char: TEXT_TYPE,
|
Char: TEXT_TYPE,
|
||||||
Varchar: TEXT_TYPE,
|
Varchar: TEXT_TYPE,
|
||||||
|
@ -229,7 +232,6 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Type2SQLType(t reflect.Type) (st SQLType) {
|
func Type2SQLType(t reflect.Type) (st SQLType) {
|
||||||
|
|
||||||
switch k := t.Kind(); k {
|
switch k := t.Kind(); k {
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
||||||
st = SQLType{Int, 0, 0}
|
st = SQLType{Int, 0, 0}
|
||||||
|
|
Loading…
Reference in New Issue