Update to github.com/lafriks/xormstore@v1.3.0 (#8317)
parent
3a7e3dbfb4
commit
149758c912
@ -0,0 +1,453 @@ |
||||
package mssql |
||||
|
||||
import ( |
||||
"fmt" |
||||
"net" |
||||
"net/url" |
||||
"os" |
||||
"strconv" |
||||
"strings" |
||||
"time" |
||||
"unicode" |
||||
) |
||||
|
||||
type connectParams struct { |
||||
logFlags uint64 |
||||
port uint64 |
||||
host string |
||||
instance string |
||||
database string |
||||
user string |
||||
password string |
||||
dial_timeout time.Duration |
||||
conn_timeout time.Duration |
||||
keepAlive time.Duration |
||||
encrypt bool |
||||
disableEncryption bool |
||||
trustServerCertificate bool |
||||
certificate string |
||||
hostInCertificate string |
||||
hostInCertificateProvided bool |
||||
serverSPN string |
||||
workstation string |
||||
appname string |
||||
typeFlags uint8 |
||||
failOverPartner string |
||||
failOverPort uint64 |
||||
packetSize uint16 |
||||
} |
||||
|
||||
func parseConnectParams(dsn string) (connectParams, error) { |
||||
var p connectParams |
||||
|
||||
var params map[string]string |
||||
if strings.HasPrefix(dsn, "odbc:") { |
||||
parameters, err := splitConnectionStringOdbc(dsn[len("odbc:"):]) |
||||
if err != nil { |
||||
return p, err |
||||
} |
||||
params = parameters |
||||
} else if strings.HasPrefix(dsn, "sqlserver://") { |
||||
parameters, err := splitConnectionStringURL(dsn) |
||||
if err != nil { |
||||
return p, err |
||||
} |
||||
params = parameters |
||||
} else { |
||||
params = splitConnectionString(dsn) |
||||
} |
||||
|
||||
strlog, ok := params["log"] |
||||
if ok { |
||||
var err error |
||||
p.logFlags, err = strconv.ParseUint(strlog, 10, 64) |
||||
if err != nil { |
||||
return p, fmt.Errorf("Invalid log parameter '%s': %s", strlog, err.Error()) |
||||
} |
||||
} |
||||
server := params["server"] |
||||
parts := strings.SplitN(server, `\`, 2) |
||||
p.host = parts[0] |
||||
if p.host == "." || strings.ToUpper(p.host) == "(LOCAL)" || p.host == "" { |
||||
p.host = "localhost" |
||||
} |
||||
if len(parts) > 1 { |
||||
p.instance = parts[1] |
||||
} |
||||
p.database = params["database"] |
||||
p.user = params["user id"] |
||||
p.password = params["password"] |
||||
|
||||
p.port = 1433 |
||||
strport, ok := params["port"] |
||||
if ok { |
||||
var err error |
||||
p.port, err = strconv.ParseUint(strport, 10, 16) |
||||
if err != nil { |
||||
f := "Invalid tcp port '%v': %v" |
||||
return p, fmt.Errorf(f, strport, err.Error()) |
||||
} |
||||
} |
||||
|
||||
// https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-the-network-packet-size-server-configuration-option
|
||||
// Default packet size remains at 4096 bytes
|
||||
p.packetSize = 4096 |
||||
strpsize, ok := params["packet size"] |
||||
if ok { |
||||
var err error |
||||
psize, err := strconv.ParseUint(strpsize, 0, 16) |
||||
if err != nil { |
||||
f := "Invalid packet size '%v': %v" |
||||
return p, fmt.Errorf(f, strpsize, err.Error()) |
||||
} |
||||
|
||||
// Ensure packet size falls within the TDS protocol range of 512 to 32767 bytes
|
||||
// NOTE: Encrypted connections have a maximum size of 16383 bytes. If you request
|
||||
// a higher packet size, the server will respond with an ENVCHANGE request to
|
||||
// alter the packet size to 16383 bytes.
|
||||
p.packetSize = uint16(psize) |
||||
if p.packetSize < 512 { |
||||
p.packetSize = 512 |
||||
} else if p.packetSize > 32767 { |
||||
p.packetSize = 32767 |
||||
} |
||||
} |
||||
|
||||
// https://msdn.microsoft.com/en-us/library/dd341108.aspx
|
||||
//
|
||||
// Do not set a connection timeout. Use Context to manage such things.
|
||||
// Default to zero, but still allow it to be set.
|
||||
if strconntimeout, ok := params["connection timeout"]; ok { |
||||
timeout, err := strconv.ParseUint(strconntimeout, 10, 64) |
||||
if err != nil { |
||||
f := "Invalid connection timeout '%v': %v" |
||||
return p, fmt.Errorf(f, strconntimeout, err.Error()) |
||||
} |
||||
p.conn_timeout = time.Duration(timeout) * time.Second |
||||
} |
||||
p.dial_timeout = 15 * time.Second |
||||
if strdialtimeout, ok := params["dial timeout"]; ok { |
||||
timeout, err := strconv.ParseUint(strdialtimeout, 10, 64) |
||||
if err != nil { |
||||
f := "Invalid dial timeout '%v': %v" |
||||
return p, fmt.Errorf(f, strdialtimeout, err.Error()) |
||||
} |
||||
p.dial_timeout = time.Duration(timeout) * time.Second |
||||
} |
||||
|
||||
// default keep alive should be 30 seconds according to spec:
|
||||
// https://msdn.microsoft.com/en-us/library/dd341108.aspx
|
||||
p.keepAlive = 30 * time.Second |
||||
if keepAlive, ok := params["keepalive"]; ok { |
||||
timeout, err := strconv.ParseUint(keepAlive, 10, 64) |
||||
if err != nil { |
||||
f := "Invalid keepAlive value '%s': %s" |
||||
return p, fmt.Errorf(f, keepAlive, err.Error()) |
||||
} |
||||
p.keepAlive = time.Duration(timeout) * time.Second |
||||
} |
||||
encrypt, ok := params["encrypt"] |
||||
if ok { |
||||
if strings.EqualFold(encrypt, "DISABLE") { |
||||
p.disableEncryption = true |
||||
} else { |
||||
var err error |
||||
p.encrypt, err = strconv.ParseBool(encrypt) |
||||
if err != nil { |
||||
f := "Invalid encrypt '%s': %s" |
||||
return p, fmt.Errorf(f, encrypt, err.Error()) |
||||
} |
||||
} |
||||
} else { |
||||
p.trustServerCertificate = true |
||||
} |
||||
trust, ok := params["trustservercertificate"] |
||||
if ok { |
||||
var err error |
||||
p.trustServerCertificate, err = strconv.ParseBool(trust) |
||||
if err != nil { |
||||
f := "Invalid trust server certificate '%s': %s" |
||||
return p, fmt.Errorf(f, trust, err.Error()) |
||||
} |
||||
} |
||||
p.certificate = params["certificate"] |
||||
p.hostInCertificate, ok = params["hostnameincertificate"] |
||||
if ok { |
||||
p.hostInCertificateProvided = true |
||||
} else { |
||||
p.hostInCertificate = p.host |
||||
p.hostInCertificateProvided = false |
||||
} |
||||
|
||||
serverSPN, ok := params["serverspn"] |
||||
if ok { |
||||
p.serverSPN = serverSPN |
||||
} else { |
||||
p.serverSPN = fmt.Sprintf("MSSQLSvc/%s:%d", p.host, p.port) |
||||
} |
||||
|
||||
workstation, ok := params["workstation id"] |
||||
if ok { |
||||
p.workstation = workstation |
||||
} else { |
||||
workstation, err := os.Hostname() |
||||
if err == nil { |
||||
p.workstation = workstation |
||||
} |
||||
} |
||||
|
||||
appname, ok := params["app name"] |
||||
if !ok { |
||||
appname = "go-mssqldb" |
||||
} |
||||
p.appname = appname |
||||
|
||||
appintent, ok := params["applicationintent"] |
||||
if ok { |
||||
if appintent == "ReadOnly" { |
||||
p.typeFlags |= fReadOnlyIntent |
||||
} |
||||
} |
||||
|
||||
failOverPartner, ok := params["failoverpartner"] |
||||
if ok { |
||||
p.failOverPartner = failOverPartner |
||||
} |
||||
|
||||
failOverPort, ok := params["failoverport"] |
||||
if ok { |
||||
var err error |
||||
p.failOverPort, err = strconv.ParseUint(failOverPort, 0, 16) |
||||
if err != nil { |
||||
f := "Invalid tcp port '%v': %v" |
||||
return p, fmt.Errorf(f, failOverPort, err.Error()) |
||||
} |
||||
} |
||||
|
||||
return p, nil |
||||
} |
||||
|
||||
func splitConnectionString(dsn string) (res map[string]string) { |
||||
res = map[string]string{} |
||||
parts := strings.Split(dsn, ";") |
||||
for _, part := range parts { |
||||
if len(part) == 0 { |
||||
continue |
||||
} |
||||
lst := strings.SplitN(part, "=", 2) |
||||
name := strings.TrimSpace(strings.ToLower(lst[0])) |
||||
if len(name) == 0 { |
||||
continue |
||||
} |
||||
var value string = "" |
||||
if len(lst) > 1 { |
||||
value = strings.TrimSpace(lst[1]) |
||||
} |
||||
res[name] = value |
||||
} |
||||
return res |
||||
} |
||||
|
||||
// Splits a URL of the form sqlserver://username:password@host/instance?param1=value¶m2=value
|
||||
func splitConnectionStringURL(dsn string) (map[string]string, error) { |
||||
res := map[string]string{} |
||||
|
||||
u, err := url.Parse(dsn) |
||||
if err != nil { |
||||
return res, err |
||||
} |
||||
|
||||
if u.Scheme != "sqlserver" { |
||||
return res, fmt.Errorf("scheme %s is not recognized", u.Scheme) |
||||
} |
||||
|
||||
if u.User != nil { |
||||
res["user id"] = u.User.Username() |
||||
p, exists := u.User.Password() |
||||
if exists { |
||||
res["password"] = p |
||||
} |
||||
} |
||||
|
||||
host, port, err := net.SplitHostPort(u.Host) |
||||
if err != nil { |
||||
host = u.Host |
||||
} |
||||
|
||||
if len(u.Path) > 0 { |
||||
res["server"] = host + "\\" + u.Path[1:] |
||||
} else { |
||||
res["server"] = host |
||||
} |
||||
|
||||
if len(port) > 0 { |
||||
res["port"] = port |
||||
} |
||||
|
||||
query := u.Query() |
||||
for k, v := range query { |
||||
if len(v) > 1 { |
||||
return res, fmt.Errorf("key %s provided more than once", k) |
||||
} |
||||
res[strings.ToLower(k)] = v[0] |
||||
} |
||||
|
||||
return res, nil |
||||
} |
||||
|
||||
// Splits a URL in the ODBC format
|
||||
func splitConnectionStringOdbc(dsn string) (map[string]string, error) { |
||||
res := map[string]string{} |
||||
|
||||
type parserState int |
||||
const ( |
||||
// Before the start of a key
|
||||
parserStateBeforeKey parserState = iota |
||||
|
||||
// Inside a key
|
||||
parserStateKey |
||||
|
||||
// Beginning of a value. May be bare or braced
|
||||
parserStateBeginValue |
||||
|
||||
// Inside a bare value
|
||||
parserStateBareValue |
||||
|
||||
// Inside a braced value
|
||||
parserStateBracedValue |
||||
|
||||
// A closing brace inside a braced value.
|
||||
// May be the end of the value or an escaped closing brace, depending on the next character
|
||||
parserStateBracedValueClosingBrace |
||||
|
||||
// After a value. Next character should be a semicolon or whitespace.
|
||||
parserStateEndValue |
||||
) |
||||
|
||||
var state = parserStateBeforeKey |
||||
|
||||
var key string |
||||
var value string |
||||
|
||||
for i, c := range dsn { |
||||
switch state { |
||||
case parserStateBeforeKey: |
||||
switch { |
||||
case c == '=': |
||||
return res, fmt.Errorf("Unexpected character = at index %d. Expected start of key or semi-colon or whitespace.", i) |
||||
case !unicode.IsSpace(c) && c != ';': |
||||
state = parserStateKey |
||||
key += string(c) |
||||
} |
||||
|
||||
case parserStateKey: |
||||
switch c { |
||||
case '=': |
||||
key = normalizeOdbcKey(key) |
||||
state = parserStateBeginValue |
||||
|
||||
case ';': |
||||
// Key without value
|
||||
key = normalizeOdbcKey(key) |
||||
res[key] = value |
||||
key = "" |
||||
value = "" |
||||
state = parserStateBeforeKey |
||||
|
||||
default: |
||||
key += string(c) |
||||
} |
||||
|
||||
case parserStateBeginValue: |
||||
switch { |
||||
case c == '{': |
||||
state = parserStateBracedValue |
||||
case c == ';': |
||||
// Empty value
|
||||
res[key] = value |
||||
key = "" |
||||
state = parserStateBeforeKey |
||||
case unicode.IsSpace(c): |
||||
// Ignore whitespace
|
||||
default: |
||||
state = parserStateBareValue |
||||
value += string(c) |
||||
} |
||||
|
||||
case parserStateBareValue: |
||||
if c == ';' { |
||||
res[key] = strings.TrimRightFunc(value, unicode.IsSpace) |
||||
key = "" |
||||
value = "" |
||||
state = parserStateBeforeKey |
||||
} else { |
||||
value += string(c) |
||||
} |
||||
|
||||
case parserStateBracedValue: |
||||
if c == '}' { |
||||
state = parserStateBracedValueClosingBrace |
||||
} else { |
||||
value += string(c) |
||||
} |
||||
|
||||
case parserStateBracedValueClosingBrace: |
||||
if c == '}' { |
||||
// Escaped closing brace
|
||||
value += string(c) |
||||
state = parserStateBracedValue |
||||
continue |
||||
} |
||||
|
||||
// End of braced value
|
||||
res[key] = value |
||||
key = "" |
||||
value = "" |
||||
|
||||
// This character is the first character past the end,
|
||||
// so it needs to be parsed like the parserStateEndValue state.
|
||||
state = parserStateEndValue |
||||
switch { |
||||
case c == ';': |
||||
state = parserStateBeforeKey |
||||
case unicode.IsSpace(c): |
||||
// Ignore whitespace
|
||||
default: |
||||
return res, fmt.Errorf("Unexpected character %c at index %d. Expected semi-colon or whitespace.", c, i) |
||||
} |
||||
|
||||
case parserStateEndValue: |
||||
switch { |
||||
case c == ';': |
||||
state = parserStateBeforeKey |
||||
case unicode.IsSpace(c): |
||||
// Ignore whitespace
|
||||
default: |
||||
return res, fmt.Errorf("Unexpected character %c at index %d. Expected semi-colon or whitespace.", c, i) |
||||
} |
||||
} |
||||
} |
||||
|
||||
switch state { |
||||
case parserStateBeforeKey: // Okay
|
||||
case parserStateKey: // Unfinished key. Treat as key without value.
|
||||
key = normalizeOdbcKey(key) |
||||
res[key] = value |
||||
case parserStateBeginValue: // Empty value
|
||||
res[key] = value |
||||
case parserStateBareValue: |
||||
res[key] = strings.TrimRightFunc(value, unicode.IsSpace) |
||||
case parserStateBracedValue: |
||||
return res, fmt.Errorf("Unexpected end of braced value at index %d.", len(dsn)) |
||||
case parserStateBracedValueClosingBrace: // End of braced value
|
||||
res[key] = value |
||||
case parserStateEndValue: // Okay
|
||||
} |
||||
|
||||
return res, nil |
||||
} |
||||
|
||||
// Normalizes the given string as an ODBC-format key
|
||||
func normalizeOdbcKey(s string) string { |
||||
return strings.ToLower(strings.TrimRightFunc(s, unicode.IsSpace)) |
||||
} |
@ -1,131 +0,0 @@ |
||||
package mssql |
||||
|
||||
import ( |
||||
"encoding/binary" |
||||
"errors" |
||||
"math" |
||||
"math/big" |
||||
) |
||||
|
||||
// http://msdn.microsoft.com/en-us/library/ee780893.aspx
|
||||
type Decimal struct { |
||||
integer [4]uint32 |
||||
positive bool |
||||
prec uint8 |
||||
scale uint8 |
||||
} |
||||
|
||||
var scaletblflt64 [39]float64 |
||||
|
||||
func (d Decimal) ToFloat64() float64 { |
||||
val := float64(0) |
||||
for i := 3; i >= 0; i-- { |
||||
val *= 0x100000000 |
||||
val += float64(d.integer[i]) |
||||
} |
||||
if !d.positive { |
||||
val = -val |
||||
} |
||||
if d.scale != 0 { |
||||
val /= scaletblflt64[d.scale] |
||||
} |
||||
return val |
||||
} |
||||
|
||||
const autoScale = 100 |
||||
|
||||
func Float64ToDecimal(f float64) (Decimal, error) { |
||||
return Float64ToDecimalScale(f, autoScale) |
||||
} |
||||
|
||||
func Float64ToDecimalScale(f float64, scale uint8) (Decimal, error) { |
||||
var dec Decimal |
||||
if math.IsNaN(f) { |
||||
return dec, errors.New("NaN") |
||||
} |
||||
if math.IsInf(f, 0) { |
||||
return dec, errors.New("Infinity can't be converted to decimal") |
||||
} |
||||
dec.positive = f >= 0 |
||||
if !dec.positive { |
||||
f = math.Abs(f) |
||||
} |
||||
if f > 3.402823669209385e+38 { |
||||
return dec, errors.New("Float value is out of range") |
||||
} |
||||
dec.prec = 20 |
||||
var integer float64 |
||||
for dec.scale = 0; dec.scale <= scale; dec.scale++ { |
||||
integer = f * scaletblflt64[dec.scale] |
||||
_, frac := math.Modf(integer) |
||||
if frac == 0 && scale == autoScale { |
||||
break |
||||
} |
||||
} |
||||
for i := 0; i < 4; i++ { |
||||
mod := math.Mod(integer, 0x100000000) |
||||
integer -= mod |
||||
integer /= 0x100000000 |
||||
dec.integer[i] = uint32(mod) |
||||
} |
||||
return dec, nil |
||||
} |
||||
|
||||
func init() { |
||||
var acc float64 = 1 |
||||
for i := 0; i <= 38; i++ { |
||||
scaletblflt64[i] = acc |
||||
acc *= 10 |
||||
} |
||||
} |
||||
|
||||
func (d Decimal) BigInt() big.Int { |
||||
bytes := make([]byte, 16) |
||||
binary.BigEndian.PutUint32(bytes[0:4], d.integer[3]) |
||||
binary.BigEndian.PutUint32(bytes[4:8], d.integer[2]) |
||||
binary.BigEndian.PutUint32(bytes[8:12], d.integer[1]) |
||||
binary.BigEndian.PutUint32(bytes[12:16], d.integer[0]) |
||||
var x big.Int |
||||
x.SetBytes(bytes) |
||||
if !d.positive { |
||||
x.Neg(&x) |
||||
} |
||||
return x |
||||
} |
||||
|
||||
func (d Decimal) Bytes() []byte { |
||||
x := d.BigInt() |
||||
return scaleBytes(x.String(), d.scale) |
||||
} |
||||
|
||||
func (d Decimal) UnscaledBytes() []byte { |
||||
x := d.BigInt() |
||||
return x.Bytes() |
||||
} |
||||
|
||||
func scaleBytes(s string, scale uint8) []byte { |
||||
z := make([]byte, 0, len(s)+1) |
||||
if s[0] == '-' || s[0] == '+' { |
||||
z = append(z, byte(s[0])) |
||||
s = s[1:] |
||||
} |
||||
pos := len(s) - int(scale) |
||||
if pos <= 0 { |
||||
z = append(z, byte('0')) |
||||
} else if pos > 0 { |
||||
z = append(z, []byte(s[:pos])...) |
||||
} |
||||
if scale > 0 { |
||||
z = append(z, byte('.')) |
||||
for pos < 0 { |
||||
z = append(z, byte('0')) |
||||
pos++ |
||||
} |
||||
z = append(z, []byte(s[pos:])...) |
||||
} |
||||
return z |
||||
} |
||||
|
||||
func (d Decimal) String() string { |
||||
return string(d.Bytes()) |
||||
} |
@ -0,0 +1,252 @@ |
||||
package decimal |
||||
|
||||
import ( |
||||
"encoding/binary" |
||||
"errors" |
||||
"fmt" |
||||
"math" |
||||
"math/big" |
||||
"strings" |
||||
) |
||||
|
||||
// Decimal represents decimal type in the Microsoft Open Specifications: http://msdn.microsoft.com/en-us/library/ee780893.aspx
|
||||
type Decimal struct { |
||||
integer [4]uint32 // Little-endian
|
||||
positive bool |
||||
prec uint8 |
||||
scale uint8 |
||||
} |
||||
|
||||
var ( |
||||
scaletblflt64 [39]float64 |
||||
int10 big.Int |
||||
int1e5 big.Int |
||||
) |
||||
|
||||
func init() { |
||||
var acc float64 = 1 |
||||
for i := 0; i <= 38; i++ { |
||||
scaletblflt64[i] = acc |
||||
acc *= 10 |
||||
} |
||||
|
||||
int10.SetInt64(10) |
||||
int1e5.SetInt64(1e5) |
||||
} |
||||
|
||||
const autoScale = 100 |
||||
|
||||
// SetInteger sets the ind'th element in the integer array
|
||||
func (d *Decimal) SetInteger(integer uint32, ind uint8) { |
||||
d.integer[ind] = integer |
||||
} |
||||
|
||||
// SetPositive sets the positive member
|
||||
func (d *Decimal) SetPositive(positive bool) { |
||||
d.positive = positive |
||||
} |
||||
|
||||
// SetPrec sets the prec member
|
||||
func (d *Decimal) SetPrec(prec uint8) { |
||||
d.prec = prec |
||||
} |
||||
|
||||
// SetScale sets the scale member
|
||||
func (d *Decimal) SetScale(scale uint8) { |
||||
d.scale = scale |
||||
} |
||||
|
||||
// IsPositive returns true if the Decimal is positive
|
||||
func (d *Decimal) IsPositive() bool { |
||||
return d.positive |
||||
} |
||||
|
||||
// ToFloat64 converts decimal to a float64
|
||||
func (d Decimal) ToFloat64() float64 { |
||||
val := float64(0) |
||||
for i := 3; i >= 0; i-- { |
||||
val *= 0x100000000 |
||||
val += float64(d.integer[i]) |
||||
} |
||||
if !d.positive { |
||||
val = -val |
||||
} |
||||
if d.scale != 0 { |
||||
val /= scaletblflt64[d.scale] |
||||
} |
||||
return val |
||||
} |
||||
|
||||
// BigInt converts decimal to a bigint
|
||||
func (d Decimal) BigInt() big.Int { |
||||
bytes := make([]byte, 16) |
||||
binary.BigEndian.PutUint32(bytes[0:4], d.integer[3]) |
||||
binary.BigEndian.PutUint32(bytes[4:8], d.integer[2]) |
||||
binary.BigEndian.PutUint32(bytes[8:12], d.integer[1]) |
||||
binary.BigEndian.PutUint32(bytes[12:16], d.integer[0]) |
||||
var x big.Int |
||||
x.SetBytes(bytes) |
||||
if !d.positive { |
||||
x.Neg(&x) |
||||
} |
||||
return x |
||||
} |
||||
|
||||
// Bytes converts decimal to a scaled byte slice
|
||||
func (d Decimal) Bytes() []byte { |
||||
x := d.BigInt() |
||||
return ScaleBytes(x.String(), d.scale) |
||||
} |
||||
|
||||
// UnscaledBytes converts decimal to a unscaled byte slice
|
||||
func (d Decimal) UnscaledBytes() []byte { |
||||
x := d.BigInt() |
||||
return x.Bytes() |
||||
} |
||||
|
||||
// String converts decimal to a string
|
||||
func (d Decimal) String() string { |
||||
return string(d.Bytes()) |
||||
} |
||||
|
||||
// Float64ToDecimal converts float64 to decimal
|
||||
func Float64ToDecimal(f float64) (Decimal, error) { |
||||
return Float64ToDecimalScale(f, autoScale) |
||||
} |
||||
|
||||
// Float64ToDecimalScale converts float64 to decimal; user can specify the scale
|
||||
func Float64ToDecimalScale(f float64, scale uint8) (Decimal, error) { |
||||
var dec Decimal |
||||
if math.IsNaN(f) { |
||||
return dec, errors.New("NaN") |
||||
} |
||||
if math.IsInf(f, 0) { |
||||
return dec, errors.New("Infinity can't be converted to decimal") |
||||
} |
||||
dec.positive = f >= 0 |
||||
if !dec.positive { |
||||
f = math.Abs(f) |
||||
} |
||||
if f > 3.402823669209385e+38 { |
||||
return dec, errors.New("Float value is out of range") |
||||
} |
||||
dec.prec = 20 |
||||
var integer float64 |
||||
for dec.scale = 0; dec.scale <= scale; dec.scale++ { |
||||
integer = f * scaletblflt64[dec.scale] |
||||
_, frac := math.Modf(integer) |
||||
if frac == 0 && scale == autoScale { |
||||
break |
||||
} |
||||
} |
||||
for i := 0; i < 4; i++ { |
||||
mod := math.Mod(integer, 0x100000000) |
||||
integer -= mod |
||||
integer /= 0x100000000 |
||||
dec.integer[i] = uint32(mod) |
||||
if mod-math.Trunc(mod) >= 0.5 { |
||||
dec.integer[i] = uint32(mod) + 1 |
||||
} |
||||
} |
||||
return dec, nil |
||||
} |
||||
|
||||
// Int64ToDecimalScale converts float64 to decimal; user can specify the scale
|
||||
func Int64ToDecimalScale(v int64, scale uint8) Decimal { |
||||
positive := v >= 0 |
||||
if !positive { |
||||
if v == math.MinInt64 { |
||||
// Special case - can't negate
|
||||
return Decimal{ |
||||
integer: [4]uint32{0, 0x80000000, 0, 0}, |
||||
positive: false, |
||||
prec: 20, |
||||
scale: 0, |
||||
} |
||||
} |
||||
v = -v |
||||
} |
||||
return Decimal{ |
||||
integer: [4]uint32{uint32(v), uint32(v >> 32), 0, 0}, |
||||
positive: positive, |
||||
prec: 20, |
||||
scale: scale, |
||||
} |
||||
} |
||||
|
||||
// StringToDecimalScale converts string to decimal
|
||||
func StringToDecimalScale(v string, outScale uint8) (Decimal, error) { |
||||
var r big.Int |
||||
var unscaled string |
||||
var inScale int |
||||
|
||||
point := strings.LastIndexByte(v, '.') |
||||
if point == -1 { |
||||
inScale = 0 |
||||
unscaled = v |
||||
} else { |
||||
inScale = len(v) - point - 1 |
||||
unscaled = v[:point] + v[point+1:] |
||||
} |
||||
if inScale > math.MaxUint8 { |
||||
return Decimal{}, fmt.Errorf("can't parse %q as a decimal number: scale too large", v) |
||||
} |
||||
|
||||
_, ok := r.SetString(unscaled, 10) |
||||
if !ok { |
||||
return Decimal{}, fmt.Errorf("can't parse %q as a decimal number", v) |
||||
} |
||||
|
||||
if inScale > int(outScale) { |
||||
return Decimal{}, fmt.Errorf("can't parse %q as a decimal number: scale %d is larger than the scale %d of the target column", v, inScale, outScale) |
||||
} |
||||
for inScale < int(outScale) { |
||||
if int(outScale)-inScale >= 5 { |
||||
r.Mul(&r, &int1e5) |
||||
inScale += 5 |
||||
} else { |
||||
r.Mul(&r, &int10) |
||||
inScale++ |
||||
} |
||||
} |
||||
|
||||
bytes := r.Bytes() |
||||
if len(bytes) > 16 { |
||||
return Decimal{}, fmt.Errorf("can't parse %q as a decimal number: precision too large", v) |
||||
} |
||||
var out [4]uint32 |
||||
for i, b := range bytes { |
||||
pos := len(bytes) - i - 1 |
||||
out[pos/4] += uint32(b) << uint(pos%4*8) |
||||
} |
||||
return Decimal{ |
||||
integer: out, |
||||
positive: r.Sign() >= 0, |
||||
prec: 20, |
||||
scale: uint8(inScale), |
||||
}, nil |
||||
} |
||||
|
||||
// ScaleBytes converts a stringified decimal to a scaled byte slice
|
||||
func ScaleBytes(s string, scale uint8) []byte { |
||||
z := make([]byte, 0, len(s)+1) |
||||
if s[0] == '-' || s[0] == '+' { |
||||
z = append(z, byte(s[0])) |
||||
s = s[1:] |
||||
} |
||||
pos := len(s) - int(scale) |
||||
if pos <= 0 { |
||||
z = append(z, byte('0')) |
||||
} else if pos > 0 { |
||||
z = append(z, []byte(s[:pos])...) |
||||
} |
||||
if scale > 0 { |
||||
z = append(z, byte('.')) |
||||
for pos < 0 { |
||||
z = append(z, byte('0')) |
||||
pos++ |
||||
} |
||||
z = append(z, []byte(s[pos:])...) |
||||
} |
||||
return z |
||||
} |
@ -0,0 +1,73 @@ |
||||
# Contributing |
||||
|
||||
1. Sign one of the contributor license agreements below. |
||||
|
||||
#### Running |
||||
|
||||
Once you've done the necessary setup, you can run the integration tests by |
||||
running: |
||||
|
||||
``` sh |
||||
$ go test -v github.com/golang-sql/civil |
||||
``` |
||||
|
||||
## Contributor License Agreements |
||||
|
||||
Before we can accept your pull requests you'll need to sign a Contributor |
||||
License Agreement (CLA): |
||||
|
||||
- **If you are an individual writing original source code** and **you own the |
||||
intellectual property**, then you'll need to sign an [individual CLA][indvcla]. |
||||
- **If you work for a company that wants to allow you to contribute your |
||||
work**, then you'll need to sign a [corporate CLA][corpcla]. |
||||
|
||||
You can sign these electronically (just scroll to the bottom). After that, |
||||
we'll be able to accept your pull requests. |
||||
|
||||
## Contributor Code of Conduct |
||||
|
||||
As contributors and maintainers of this project, |
||||
and in the interest of fostering an open and welcoming community, |
||||
we pledge to respect all people who contribute through reporting issues, |
||||
posting feature requests, updating documentation, |
||||
submitting pull requests or patches, and other activities. |
||||
|
||||
We are committed to making participation in this project |
||||
a harassment-free experience for everyone, |
||||
regardless of level of experience, gender, gender identity and expression, |
||||
sexual orientation, disability, personal appearance, |
||||
body size, race, ethnicity, age, religion, or nationality. |
||||
|
||||
Examples of unacceptable behavior by participants include: |
||||
|
||||
* The use of sexualized language or imagery |
||||
* Personal attacks |
||||
* Trolling or insulting/derogatory comments |
||||
* Public or private harassment |
||||
* Publishing other's private information, |
||||
such as physical or electronic |
||||
addresses, without explicit permission |
||||
* Other unethical or unprofessional conduct. |
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or reject |
||||
comments, commits, code, wiki edits, issues, and other contributions |
||||
that are not aligned to this Code of Conduct. |
||||
By adopting this Code of Conduct, |
||||
project maintainers commit themselves to fairly and consistently |
||||
applying these principles to every aspect of managing this project. |
||||
Project maintainers who do not follow or enforce the Code of Conduct |
||||
may be permanently removed from the project team. |
||||
|
||||
This code of conduct applies both within project spaces and in public spaces |
||||
when an individual is representing the project or its community. |
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior |
||||
may be reported by opening an issue |
||||
or contacting one or more of the project maintainers. |
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, |
||||
available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) |
||||
|
||||
[gcloudcli]: https://developers.google.com/cloud/sdk/gcloud/ |
||||
[indvcla]: https://developers.google.com/open-source/cla/individual |
||||
[corpcla]: https://developers.google.com/open-source/cla/corporate |
@ -0,0 +1,202 @@ |
||||
|
||||
Apache License |
||||
Version 2.0, January 2004 |
||||
http://www.apache.org/licenses/ |
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION |
||||
|
||||
1. Definitions. |
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction, |
||||
and distribution as defined by Sections 1 through 9 of this document. |
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by |
||||
the copyright owner that is granting the License. |
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all |
||||
other entities that control, are controlled by, or are under common |
||||
control with that entity. For the purposes of this definition, |
||||
"control" means (i) the power, direct or indirect, to cause the |
||||
direction or management of such entity, whether by contract or |
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the |
||||
outstanding shares, or (iii) beneficial ownership of such entity. |
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity |
||||
exercising permissions granted by this License. |
||||
|
||||
"Source" form shall mean the preferred form for making modifications, |
||||
including but not limited to software source code, documentation |
||||
source, and configuration files. |
||||
|
||||
"Object" form shall mean any form resulting from mechanical |
||||
transformation or translation of a Source form, including but |
||||
not limited to compiled object code, generated documentation, |
||||
and conversions to other media types. |
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or |
||||
Object form, made available under the License, as indicated by a |
||||
copyright notice that is included in or attached to the work |
||||
(an example is provided in the Appendix below). |
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object |
||||
form, that is based on (or derived from) the Work and for which the |
||||
editorial revisions, annotations, elaborations, or other modifications |
||||
represent, as a whole, an original work of authorship. For the purposes |
||||
of this License, Derivative Works shall not include works that remain |
||||
separable from, or merely link (or bind by name) to the interfaces of, |
||||
the Work and Derivative Works thereof. |
||||
|
||||
"Contribution" shall mean any work of authorship, including |
||||
the original version of the Work and any modifications or additions |
||||
to that Work or Derivative Works thereof, that is intentionally |
||||
submitted to Licensor for inclusion in the Work by the copyright owner |
||||
or by an individual or Legal Entity authorized to submit on behalf of |
||||
the copyright owner. For the purposes of this definition, "submitted" |
||||
means any form of electronic, verbal, or written communication sent |
||||
to the Licensor or its representatives, including but not limited to |
||||
communication on electronic mailing lists, source code control systems, |
||||
and issue tracking systems that are managed by, or on behalf of, the |
||||
Licensor for the purpose of discussing and improving the Work, but |
||||
excluding communication that is conspicuously marked or otherwise |
||||
designated in writing by the copyright owner as "Not a Contribution." |
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity |
||||
on behalf of whom a Contribution has been received by Licensor and |
||||
subsequently incorporated within the Work. |
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of |
||||
this License, each Contributor hereby grants to You a perpetual, |
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable |
||||
copyright license to reproduce, prepare Derivative Works of, |
||||
publicly display, publicly perform, sublicense, and distribute the |
||||
Work and such Derivative Works in Source or Object form. |
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of |
||||
this License, each Contributor hereby grants to You a perpetual, |
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable |
||||
(except as stated in this section) patent license to make, have made, |
||||
use, offer to sell, sell, import, and otherwise transfer the Work, |
||||
where such license applies only to those patent claims licensable |
||||
by such Contributor that are necessarily infringed by their |
||||
Contribution(s) alone or by combination of their Contribution(s) |
||||
with the Work to which such Contribution(s) was submitted. If You |
||||
institute patent litigation against any entity (including a |
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work |
||||
or a Contribution incorporated within the Work constitutes direct |
||||
or contributory patent infringement, then any patent licenses |
||||
granted to You under this License for that Work shall terminate |
||||
as of the date such litigation is filed. |
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the |
||||
Work or Derivative Works thereof in any medium, with or without |
||||
modifications, and in Source or Object form, provided that You |
||||
meet the following conditions: |
||||
|
||||
(a) You must give any other recipients of the Work or |
||||
Derivative Works a copy of this License; and |
||||
|
||||
(b) You must cause any modified files to carry prominent notices |
||||
stating that You changed the files; and |
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works |
||||
that You distribute, all copyright, patent, trademark, and |
||||
attribution notices from the Source form of the Work, |
||||
excluding those notices that do not pertain to any part of |
||||
the Derivative Works; and |
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its |
||||
distribution, then any Derivative Works that You distribute must |
||||
include a readable copy of the attribution notices contained |
||||
within such NOTICE file, excluding those notices that do not |
||||
pertain to any part of the Derivative Works, in at least one |
||||
of the following places: within a NOTICE text file distributed |
||||
as part of the Derivative Works; within the Source form or |
||||
documentation, if provided along with the Derivative Works; or, |
||||
within a display generated by the Derivative Works, if and |
||||
wherever such third-party notices normally appear. The contents |
||||
of the NOTICE file are for informational purposes only and |
||||
do not modify the License. You may add Your own attribution |
||||
notices within Derivative Works that You distribute, alongside |
||||
or as an addendum to the NOTICE text from the Work, provided |
||||
that such additional attribution notices cannot be construed |
||||
as modifying the License. |
||||
|
||||
You may add Your own copyright statement to Your modifications and |
||||
may provide additional or different license terms and conditions |
||||
for use, reproduction, or distribution of Your modifications, or |
||||
for any such Derivative Works as a whole, provided Your use, |
||||
reproduction, and distribution of the Work otherwise complies with |
||||
the conditions stated in this License. |
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise, |
||||
any Contribution intentionally submitted for inclusion in the Work |
||||
by You to the Licensor shall be under the terms and conditions of |
||||
this License, without any additional terms or conditions. |
||||
Notwithstanding the above, nothing herein shall supersede or modify |
||||
the terms of any separate license agreement you may have executed |
||||
with Licensor regarding such Contributions. |
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade |
||||
names, trademarks, service marks, or product names of the Licensor, |
||||
except as required for reasonable and customary use in describing the |
||||
origin of the Work and reproducing the content of the NOTICE file. |
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or |
||||
agreed to in writing, Licensor provides the Work (and each |
||||
Contributor provides its Contributions) on an "AS IS" BASIS, |
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
||||
implied, including, without limitation, any warranties or conditions |
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A |
||||
PARTICULAR PURPOSE. You are solely responsible for determining the |
||||
appropriateness of using or redistributing the Work and assume any |
||||
risks associated with Your exercise of permissions under this License. |
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory, |
||||
whether in tort (including negligence), contract, or otherwise, |
||||
unless required by applicable law (such as deliberate and grossly |
||||
negligent acts) or agreed to in writing, shall any Contributor be |
||||
liable to You for damages, including any direct, indirect, special, |
||||
incidental, or consequential damages of any character arising as a |
||||
result of this License or out of the use or inability to use the |
||||
Work (including but not limited to damages for loss of goodwill, |
||||
work stoppage, computer failure or malfunction, or any and all |
||||
other commercial damages or losses), even if such Contributor |
||||
has been advised of the possibility of such damages. |
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing |
||||
the Work or Derivative Works thereof, You may choose to offer, |
||||
and charge a fee for, acceptance of support, warranty, indemnity, |
||||
or other liability obligations and/or rights consistent with this |
||||
License. However, in accepting such obligations, You may act only |
||||
on Your own behalf and on Your sole responsibility, not on behalf |
||||
of any other Contributor, and only if You agree to indemnify, |
||||
defend, and hold each Contributor harmless for any liability |
||||
incurred by, or claims asserted against, such Contributor by reason |
||||
of your accepting any such warranty or additional liability. |
||||
|
||||
END OF TERMS AND CONDITIONS |
||||
|
||||
APPENDIX: How to apply the Apache License to your work. |
||||
|
||||
To apply the Apache License to your work, attach the following |
||||
boilerplate notice, with the fields enclosed by brackets "[]" |
||||
replaced with your own identifying information. (Don't include |
||||
the brackets!) The text should be enclosed in the appropriate |
||||
comment syntax for the file format. We also recommend that a |
||||
file or class name and description of purpose be included on the |
||||
same "printed page" as the copyright notice for easier |
||||
identification within third-party archives. |
||||
|
||||
Copyright [yyyy] [name of copyright owner] |
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
you may not use this file except in compliance with the License. |
||||
You may obtain a copy of the License at |
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
||||
Unless required by applicable law or agreed to in writing, software |
||||
distributed under the License is distributed on an "AS IS" BASIS, |
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
See the License for the specific language governing permissions and |
||||
limitations under the License. |
@ -0,0 +1,15 @@ |
||||
# Civil Date and Time |
||||
|
||||
[![GoDoc](https://godoc.org/github.com/golang-sql/civil?status.svg)](https://godoc.org/github.com/golang-sql/civil) |
||||
|
||||
Civil provides Date, Time of Day, and DateTime data types. |
||||
|
||||
While there are many uses, using specific types when working |
||||
with databases make is conceptually eaiser to understand what value |
||||
is set in the remote system. |
||||
|
||||
## Source |
||||
|
||||
This civil package was extracted and forked from `cloud.google.com/go/civil`. |
||||
As such the license and contributing requirements remain the same as that |
||||
module. |
@ -0,0 +1,122 @@ |
||||
// Copyright 2019 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package acme |
||||
|
||||
import ( |
||||
"context" |
||||
"encoding/json" |
||||
"fmt" |
||||
"net/http" |
||||
) |
||||
|
||||
// DeactivateReg permanently disables an existing account associated with c.Key.
|
||||
// A deactivated account can no longer request certificate issuance or access
|
||||
// resources related to the account, such as orders or authorizations.
|
||||
//
|
||||
// It works only with RFC8555 compliant CAs.
|
||||
func (c *Client) DeactivateReg(ctx context.Context) error { |
||||
url := string(c.accountKID(ctx)) |
||||
if url == "" { |
||||
return ErrNoAccount |
||||
} |
||||
req := json.RawMessage(`{"status": "deactivated"}`) |
||||
res, err := c.post(ctx, nil, url, req, wantStatus(http.StatusOK)) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
res.Body.Close() |
||||
return nil |
||||
} |
||||
|
||||
// registerRFC is quivalent to c.Register but for RFC-compliant CAs.
|
||||
// It expects c.Discover to have already been called.
|
||||
// TODO: Implement externalAccountBinding.
|
||||
func (c *Client) registerRFC(ctx context.Context, acct *Account, prompt func(tosURL string) bool) (*Account, error) { |
||||
c.cacheMu.Lock() // guard c.kid access
|
||||
defer c.cacheMu.Unlock() |
||||
|
||||
req := struct { |
||||
TermsAgreed bool `json:"termsOfServiceAgreed,omitempty"` |
||||
Contact []string `json:"contact,omitempty"` |
||||
}{ |
||||
Contact: acct.Contact, |
||||
} |
||||
if c.dir.Terms != "" { |
||||
req.TermsAgreed = prompt(c.dir.Terms) |
||||
} |
||||
res, err := c.post(ctx, c.Key, c.dir.RegURL, req, wantStatus( |
||||
http.StatusOK, // account with this key already registered
|
||||
http.StatusCreated, // new account created
|
||||
)) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
defer res.Body.Close() |
||||
a, err := responseAccount(res) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// Cache Account URL even if we return an error to the caller.
|
||||
// It is by all means a valid and usable "kid" value for future requests.
|
||||
c.kid = keyID(a.URI) |
||||
if res.StatusCode == http.StatusOK { |
||||
return nil, ErrAccountAlreadyExists |
||||
} |
||||
return a, nil |
||||
} |
||||
|
||||
// updateGegRFC is equivalent to c.UpdateReg but for RFC-compliant CAs.
|
||||
// It expects c.Discover to have already been called.
|
||||
func (c *Client) updateRegRFC(ctx context.Context, a *Account) (*Account, error) { |
||||
url := string(c.accountKID(ctx)) |
||||
if url == "" { |
||||
return nil, ErrNoAccount |
||||
} |
||||
req := struct { |
||||
Contact []string `json:"contact,omitempty"` |
||||
}{ |
||||
Contact: a.Contact, |
||||
} |
||||
res, err := c.post(ctx, nil, url, req, wantStatus(http.StatusOK)) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
defer res.Body.Close() |
||||
return responseAccount(res) |
||||
} |
||||
|
||||
// getGegRFC is equivalent to c.GetReg but for RFC-compliant CAs.
|
||||
// It expects c.Discover to have already been called.
|
||||
func (c *Client) getRegRFC(ctx context.Context) (*Account, error) { |
||||
req := json.RawMessage(`{"onlyReturnExisting": true}`) |
||||
res, err := c.post(ctx, c.Key, c.dir.RegURL, req, wantStatus(http.StatusOK)) |
||||
if e, ok := err.(*Error); ok && e.ProblemType == "urn:ietf:params:acme:error:accountDoesNotExist" { |
||||
return nil, ErrNoAccount |
||||
} |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
defer res.Body.Close() |
||||
return responseAccount(res) |
||||
} |
||||
|
||||
func responseAccount(res *http.Response) (*Account, error) { |
||||
var v struct { |
||||
Status string |
||||
Contact []string |
||||
Orders string |
||||
} |
||||
if err := json.NewDecoder(res.Body).Decode(&v); err != nil { |
||||
return nil, fmt.Errorf("acme: invalid response: %v", err) |
||||
} |
||||
return &Account{ |
||||
URI: res.Header.Get("Location"), |
||||
Status: v.Status, |
||||
Contact: v.Contact, |
||||
OrdersURL: v.Orders, |
||||
}, nil |
||||
} |
Loading…
Reference in new issue