81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
package transformers
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
func mssqlUuidToBigEndian(mssqlUuid []byte) ([]byte, error) {
|
|
if len(mssqlUuid) != 16 {
|
|
return nil, errors.New("Invalid uuid")
|
|
}
|
|
|
|
pgUuid := make([]byte, 16)
|
|
pgUuid[0], pgUuid[1], pgUuid[2], pgUuid[3] = mssqlUuid[3], mssqlUuid[2], mssqlUuid[1], mssqlUuid[0]
|
|
pgUuid[4], pgUuid[5] = mssqlUuid[5], mssqlUuid[4]
|
|
pgUuid[6], pgUuid[7] = mssqlUuid[7], mssqlUuid[6]
|
|
copy(pgUuid[8:], mssqlUuid[8:])
|
|
|
|
return pgUuid, nil
|
|
}
|
|
|
|
const sridFlag = 0x20000000
|
|
|
|
func wkbToEwkbWithSrid(geometry []byte, srid int) ([]byte, error) {
|
|
if len(geometry) < 5 {
|
|
return nil, errors.New("Invalid wkb")
|
|
}
|
|
|
|
var byteOrder binary.ByteOrder
|
|
if geometry[0] == 0 {
|
|
byteOrder = binary.BigEndian
|
|
} else {
|
|
byteOrder = binary.LittleEndian
|
|
}
|
|
|
|
wkbType := byteOrder.Uint32(geometry[1:5])
|
|
if wkbType&sridFlag != 0 {
|
|
return geometry, nil
|
|
}
|
|
|
|
ewkbType := wkbType | sridFlag
|
|
|
|
result := make([]byte, len(geometry)+4)
|
|
|
|
result[0] = geometry[0]
|
|
|
|
byteOrder.PutUint32(result[1:5], ewkbType)
|
|
|
|
byteOrder.PutUint32(result[5:9], uint32(srid))
|
|
|
|
copy(result[9:], geometry[5:])
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func ensureUTC(t time.Time) time.Time {
|
|
if t.Location() == time.UTC {
|
|
return t
|
|
}
|
|
|
|
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.UTC)
|
|
}
|
|
|
|
func ToInt64(v any) (int64, bool) {
|
|
switch t := v.(type) {
|
|
case int:
|
|
return int64(t), true
|
|
case int8:
|
|
return int64(t), true
|
|
case int16:
|
|
return int64(t), true
|
|
case int32:
|
|
return int64(t), true
|
|
case int64:
|
|
return int64(t), true
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|