- update dorm

master
李光春 2 years ago
parent 542021f1ea
commit c2254684f5

@ -13,6 +13,7 @@ require (
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d
github.com/dgraph-io/ristretto v0.1.0
github.com/gin-gonic/gin v1.8.1
github.com/glebarez/sqlite v1.4.6
github.com/go-playground/locales v0.14.0
github.com/go-playground/universal-translator v0.18.0
github.com/go-playground/validator/v10 v10.11.0
@ -47,6 +48,8 @@ require (
gorm.io/datatypes v1.0.7
gorm.io/driver/mysql v1.3.5
gorm.io/driver/postgres v1.3.8
gorm.io/driver/sqlite v1.3.1
gorm.io/driver/sqlserver v1.3.1
gorm.io/gorm v1.23.8
xorm.io/xorm v1.3.1
)
@ -59,17 +62,22 @@ require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/denisenkom/go-mssqldb v0.12.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/glebarez/go-sqlite v1.17.3 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/goccy/go-json v0.9.8 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.12.1 // indirect
@ -86,11 +94,13 @@ require (
github.com/klauspost/compress v1.15.8 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mozillazg/go-httpheader v0.3.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/saracen/go7z-fixtures v0.0.0-20190623165746-aa6b8fba1d2f // indirect
github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f // indirect
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
@ -112,5 +122,9 @@ require (
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
modernc.org/libc v1.16.8 // indirect
modernc.org/mathutil v1.4.1 // indirect
modernc.org/memory v1.1.1 // indirect
modernc.org/sqlite v1.17.3 // indirect
xorm.io/builder v0.3.12 // indirect
)

@ -133,6 +133,10 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
github.com/glebarez/go-sqlite v1.17.3 h1:Rji9ROVSTTfjuWD6j5B+8DtkNvPILoUC3xRhkQzGxvk=
github.com/glebarez/go-sqlite v1.17.3/go.mod h1:Hg+PQuhUy98XCxWEJEaWob8x7lhJzhNYF1nZbUiRGIY=
github.com/glebarez/sqlite v1.4.6 h1:D5uxD2f6UJ82cHnVtO2TZ9pqsLyto3fpDKHIk2OsR8A=
github.com/glebarez/sqlite v1.4.6/go.mod h1:WYEtEFjhADPaPJqL/PGlbQQGINBA3eUAfDNbKFJf/zA=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgOZ7o=
@ -773,6 +777,7 @@ golang.org/x/sys v0.0.0-20210902050250-f475640dd07b/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211020174200-9d6173849985/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220405052023-b1e9470b6e64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e h1:NHvCuwuS43lGnYhten69ZWqi2QOj/CiDNcKbVqwVoew=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
@ -939,8 +944,11 @@ modernc.org/cc/v3 v3.35.10/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g
modernc.org/cc/v3 v3.35.15/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g=
modernc.org/cc/v3 v3.35.16/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g=
modernc.org/cc/v3 v3.35.17/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g=
modernc.org/cc/v3 v3.35.18 h1:rMZhRcWrba0y3nVmdiQ7kxAgOOSq2m2f2VzjHLgEs6U=
modernc.org/cc/v3 v3.35.18/go.mod h1:iPJg1pkwXqAV16SNgFBVYmggfMg6xhs+2oiO0vclK3g=
modernc.org/cc/v3 v3.36.0 h1:0kmRkTmqNidmu3c7BNDSdVHCxXCkWLmWmCIVX4LUboo=
modernc.org/cc/v3 v3.36.0/go.mod h1:NFUHyPn4ekoC/JHeZFfZurN6ixxawE1BnVonP/oahEI=
modernc.org/ccgo/v3 v3.0.0-20220428102840-41399a37e894/go.mod h1:eI31LL8EwEBKPpNpA4bU1/i+sKOwOrQy8D87zWUcRZc=
modernc.org/ccgo/v3 v3.0.0-20220430103911-bc99d88307be/go.mod h1:bwdAnOoaIt8Ax9YdWGjxWsdkPcZyRPHqrOvJxaKAKGw=
modernc.org/ccgo/v3 v3.9.5/go.mod h1:umuo2EP2oDSBnD3ckjaVUXMrmeAw8C8OSICVa0iFf60=
modernc.org/ccgo/v3 v3.10.0/go.mod h1:c0yBmkRFi7uW4J7fwx/JiijwOjeAeR2NoSaRVFPmjMw=
modernc.org/ccgo/v3 v3.11.0/go.mod h1:dGNposbDp9TOZ/1KBxghxtUp/bzErD0/0QW4hhSaBMI=
@ -975,10 +983,14 @@ modernc.org/ccgo/v3 v3.12.66/go.mod h1:jUuxlCFZTUZLMV08s7B1ekHX5+LIAurKTTaugUr/E
modernc.org/ccgo/v3 v3.12.67/go.mod h1:Bll3KwKvGROizP2Xj17GEGOTrlvB1XcVaBrC90ORO84=
modernc.org/ccgo/v3 v3.12.73/go.mod h1:hngkB+nUUqzOf3iqsM48Gf1FZhY599qzVg1iX+BT3cQ=
modernc.org/ccgo/v3 v3.12.81/go.mod h1:p2A1duHoBBg1mFtYvnhAnQyI6vL0uw5PGYLSIgF6rYY=
modernc.org/ccgo/v3 v3.12.82 h1:wudcnJyjLj1aQQCXF3IM9Gz2X6UNjw+afIghzdtn0v8=
modernc.org/ccgo/v3 v3.12.82/go.mod h1:ApbflUfa5BKadjHynCficldU1ghjen84tuM5jRynB7w=
modernc.org/ccgo/v3 v3.16.4/go.mod h1:tGtX0gE9Jn7hdZFeU88slbTh1UtCYKusWOoCJuvkWsQ=
modernc.org/ccgo/v3 v3.16.6 h1:3l18poV+iUemQ98O3X5OMr97LOqlzis+ytivU4NqGhA=
modernc.org/ccgo/v3 v3.16.6/go.mod h1:tGtX0gE9Jn7hdZFeU88slbTh1UtCYKusWOoCJuvkWsQ=
modernc.org/ccorpus v1.11.1/go.mod h1:2gEUTrWqdpH2pXsmTM1ZkjeSrUWDpjMu2T6m29L/ErQ=
modernc.org/ccorpus v1.11.6/go.mod h1:2gEUTrWqdpH2pXsmTM1ZkjeSrUWDpjMu2T6m29L/ErQ=
modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM=
modernc.org/libc v0.0.0-20220428101251-2d5f3daf273b/go.mod h1:p7Mg4+koNjc8jkqwcoFBJx7tXkpj00G77X7A72jXPXA=
modernc.org/libc v1.9.8/go.mod h1:U1eq8YWr/Kc1RWCMFUWEdkTg8OTcfLw2kY8EDwl039w=
modernc.org/libc v1.9.11/go.mod h1:NyF3tsA5ArIjJ83XB0JlqhjTabTCHm9aX4XMPHyQn0Q=
modernc.org/libc v1.11.0/go.mod h1:2lOfPmj7cz+g1MrPNmX65QCzVxgNq2C5o0jdLY2gAYg=
@ -1013,26 +1025,34 @@ modernc.org/libc v1.11.71/go.mod h1:DUOmMYe+IvKi9n6Mycyx3DbjfzSKrdr/0Vgt3j7P5gw=
modernc.org/libc v1.11.75/go.mod h1:dGRVugT6edz361wmD9gk6ax1AbDSe0x5vji0dGJiPT0=
modernc.org/libc v1.11.82/go.mod h1:NF+Ek1BOl2jeC7lw3a7Jj5PWyHPwWD4aq3wVKxqV1fI=
modernc.org/libc v1.11.86/go.mod h1:ePuYgoQLmvxdNT06RpGnaDKJmDNEkV7ZPKI2jnsvZoE=
modernc.org/libc v1.11.87 h1:PzIzOqtlzMDDcCzJ5cUP6h/Ku6Fa9iyflP2ccTY64aE=
modernc.org/libc v1.11.87/go.mod h1:Qvd5iXTeLhI5PS0XSyqMY99282y+3euapQFxM7jYnpY=
modernc.org/libc v1.16.0/go.mod h1:N4LD6DBE9cf+Dzf9buBlzVJndKr/iJHG97vGLHYnb5A=
modernc.org/libc v1.16.1/go.mod h1:JjJE0eu4yeK7tab2n4S1w8tlWd9MxXLRzheaRnAKymU=
modernc.org/libc v1.16.7/go.mod h1:hYIV5VZczAmGZAnG15Vdngn5HSF5cSkbvfz2B7GRuVU=
modernc.org/libc v1.16.8 h1:Ux98PaOMvolgoFX/YwusFOHBnanXdGRmWgI8ciI2z4o=
modernc.org/libc v1.16.8/go.mod h1:hYIV5VZczAmGZAnG15Vdngn5HSF5cSkbvfz2B7GRuVU=
modernc.org/mathutil v1.1.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/mathutil v1.2.2/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/mathutil v1.4.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/mathutil v1.4.1 h1:ij3fYGe8zBF4Vu+g0oT7mB06r8sqGWKuJu1yXeR4by8=
modernc.org/mathutil v1.4.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/memory v1.0.4/go.mod h1:nV2OApxradM3/OVbs2/0OsP6nPfakXpi50C7dcoHXlc=
modernc.org/memory v1.0.5 h1:XRch8trV7GgvTec2i7jc33YlUI0RKVDBvZ5eZ5m8y14=
modernc.org/memory v1.0.5/go.mod h1:B7OYswTRnfGg+4tDH1t1OeUNnsy2viGTdME4tzd+IjM=
modernc.org/memory v1.1.1 h1:bDOL0DIDLQv7bWhP3gMvIrnoFw+Eo6F7a2QK9HPDiFU=
modernc.org/memory v1.1.1/go.mod h1:/0wo5ibyrQiaoUoH7f9D8dnglAmILJ5/cxZlRECf+Nw=
modernc.org/opt v0.1.1 h1:/0RX92k9vwVeDXj+Xn23DKp2VJubL7k8qNffND6qn3A=
modernc.org/opt v0.1.1/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
modernc.org/sqlite v1.14.2 h1:ohsW2+e+Qe2To1W6GNezzKGwjXwSax6R+CrhRxVaFbE=
modernc.org/sqlite v1.14.2/go.mod h1:yqfn85u8wVOE6ub5UT8VI9JjhrwBUUCNyTACN0h6Sx8=
modernc.org/sqlite v1.17.3 h1:iE+coC5g17LtByDYDWKpR6m2Z9022YrSh3bumwOnIrI=
modernc.org/sqlite v1.17.3/go.mod h1:10hPVYar9C0kfXuTWGz8s0XtB8uAGymUy51ZzStYe3k=
modernc.org/strutil v1.1.1 h1:xv+J1BXY3Opl2ALrBwyfEikFAj8pmqcpnfmuwUwcozs=
modernc.org/strutil v1.1.1/go.mod h1:DE+MQQ/hjKBZS2zNInV5hhcipt5rLPWkmpbGeW5mmdw=
modernc.org/tcl v1.8.13/go.mod h1:V+q/Ef0IJaNUSECieLU4o+8IScapxnMyFV6i/7uQlAY=
modernc.org/tcl v1.13.1/go.mod h1:XOLfOwzhkljL4itZkK6T72ckMgvj0BDsnKNdZVUOecw=
modernc.org/token v1.0.0 h1:a0jaWiNMDhDUtqOj09wvjWWAqd3q7WpBulmL9H2egsk=
modernc.org/token v1.0.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
modernc.org/z v1.2.19/go.mod h1:+ZpP0pc4zz97eukOzW3xagV/lS82IpPN9NGG5pNF9vY=
modernc.org/z v1.5.1/go.mod h1:eWFB510QWW5Th9YGZT81s+LwvaAs3Q2yr4sP0rmLkv8=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=

@ -1,14 +1,9 @@
package dorm
import (
"errors"
"fmt"
"github.com/sirupsen/logrus"
"go.dtapp.net/library/utils/gotime"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"os"
"path"
"time"
@ -25,98 +20,6 @@ type GormClient struct {
config *ConfigGormClient // 配置
}
func NewGormMysqlClient(config *ConfigGormClient) (*GormClient, error) {
var err error
c := &GormClient{config: config}
// 判断路径
if c.config.LogUrl == "" {
logsUrl = "/logs/mysql"
}
if c.config.Log == true {
c.Db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{
Logger: logger.New(
writer{},
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound记录未找到错误
Colorful: false, // 禁用彩色打印
},
),
NowFunc: func() time.Time {
return gotime.Current().Now().Local()
},
})
} else {
c.Db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{})
}
if err != nil {
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
}
sqlDB, err := c.Db.DB()
if err != nil {
return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err))
}
sqlDB.SetMaxIdleConns(10) // 设置空闲连接池中连接的最大数量
sqlDB.SetMaxOpenConns(100) // 设置打开数据库连接的最大数量。
sqlDB.SetConnMaxLifetime(time.Second * 600) // 设置了连接可复用的最大时间。
return c, nil
}
func NewGormPostgresClient(config *ConfigGormClient) (*GormClient, error) {
c := &GormClient{}
c.config = config
// 判断路径
if c.config.LogUrl == "" {
logsUrl = "/logs/postgresql"
}
var err error
if c.config.Log == true {
c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{
Logger: logger.New(
writer{},
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound记录未找到错误
Colorful: false, // 禁用彩色打印
},
),
NowFunc: func() time.Time {
return gotime.Current().Now().Local()
},
})
} else {
c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{})
}
if err != nil {
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
}
sqlDB, err := c.Db.DB()
if err != nil {
return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err))
}
sqlDB.SetMaxIdleConns(10) // 设置空闲连接池中连接的最大数量
sqlDB.SetMaxOpenConns(100) // 设置打开数据库连接的最大数量。
sqlDB.SetConnMaxLifetime(time.Second * 600) // 设置了连接可复用的最大时间。
return c, nil
}
func (c *GormClient) GetDb() *gorm.DB {
return c.Db
}

@ -0,0 +1,56 @@
package dorm
import (
"errors"
"fmt"
"go.dtapp.net/library/utils/gotime"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"time"
)
func NewGormMysqlClient(config *ConfigGormClient) (*GormClient, error) {
var err error
c := &GormClient{config: config}
// 判断路径
if c.config.LogUrl == "" {
logsUrl = "/logs/mysql"
}
if c.config.Log == true {
c.Db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{
Logger: logger.New(
writer{},
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound记录未找到错误
Colorful: false, // 禁用彩色打印
},
),
NowFunc: func() time.Time {
return gotime.Current().Now().Local()
},
})
} else {
c.Db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{})
}
if err != nil {
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
}
sqlDB, err := c.Db.DB()
if err != nil {
return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err))
}
sqlDB.SetMaxIdleConns(10) // 设置空闲连接池中连接的最大数量
sqlDB.SetMaxOpenConns(100) // 设置打开数据库连接的最大数量。
sqlDB.SetConnMaxLifetime(time.Second * 600) // 设置了连接可复用的最大时间。
return c, nil
}

@ -0,0 +1,105 @@
package dorm
import (
"errors"
"fmt"
"go.dtapp.net/library/utils/gotime"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"time"
)
func NewGormPostgresClient(config *ConfigGormClient) (*GormClient, error) {
c := &GormClient{}
c.config = config
// 判断路径
if c.config.LogUrl == "" {
logsUrl = "/logs/postgresql"
}
var err error
if c.config.Log == true {
c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{
Logger: logger.New(
writer{},
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound记录未找到错误
Colorful: false, // 禁用彩色打印
},
),
NowFunc: func() time.Time {
return gotime.Current().Now().Local()
},
})
} else {
c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{})
}
if err != nil {
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
}
sqlDB, err := c.Db.DB()
if err != nil {
return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err))
}
sqlDB.SetMaxIdleConns(10) // 设置空闲连接池中连接的最大数量
sqlDB.SetMaxOpenConns(100) // 设置打开数据库连接的最大数量。
sqlDB.SetConnMaxLifetime(time.Second * 600) // 设置了连接可复用的最大时间。
return c, nil
}
func NewGormPostgresqlClient(config *ConfigGormClient) (*GormClient, error) {
c := &GormClient{}
c.config = config
// 判断路径
if c.config.LogUrl == "" {
logsUrl = "/logs/postgresql"
}
var err error
if c.config.Log == true {
c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{
Logger: logger.New(
writer{},
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound记录未找到错误
Colorful: false, // 禁用彩色打印
},
),
NowFunc: func() time.Time {
return gotime.Current().Now().Local()
},
})
} else {
c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{})
}
if err != nil {
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
}
sqlDB, err := c.Db.DB()
if err != nil {
return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err))
}
sqlDB.SetMaxIdleConns(10) // 设置空闲连接池中连接的最大数量
sqlDB.SetMaxOpenConns(100) // 设置打开数据库连接的最大数量。
sqlDB.SetConnMaxLifetime(time.Second * 600) // 设置了连接可复用的最大时间。
return c, nil
}

@ -0,0 +1,56 @@
package dorm
import (
"errors"
"fmt"
"github.com/glebarez/sqlite"
"go.dtapp.net/library/utils/gotime"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"time"
)
func NewGormSqliteClient(config *ConfigGormClient) (*GormClient, error) {
var err error
c := &GormClient{config: config}
// 判断路径
if c.config.LogUrl == "" {
logsUrl = "/logs/sqlite"
}
if c.config.Log == true {
c.Db, err = gorm.Open(sqlite.Open(c.config.Dns), &gorm.Config{
Logger: logger.New(
writer{},
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound记录未找到错误
Colorful: false, // 禁用彩色打印
},
),
NowFunc: func() time.Time {
return gotime.Current().Now().Local()
},
})
} else {
c.Db, err = gorm.Open(sqlite.Open(c.config.Dns), &gorm.Config{})
}
if err != nil {
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
}
sqlDB, err := c.Db.DB()
if err != nil {
return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err))
}
sqlDB.SetMaxIdleConns(10) // 设置空闲连接池中连接的最大数量
sqlDB.SetMaxOpenConns(100) // 设置打开数据库连接的最大数量。
sqlDB.SetConnMaxLifetime(time.Second * 600) // 设置了连接可复用的最大时间。
return c, nil
}

@ -0,0 +1,56 @@
package dorm
import (
"errors"
"fmt"
"go.dtapp.net/library/utils/gotime"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"time"
)
func NewGormSqliteCgoClient(config *ConfigGormClient) (*GormClient, error) {
var err error
c := &GormClient{config: config}
// 判断路径
if c.config.LogUrl == "" {
logsUrl = "/logs/sqlite"
}
if c.config.Log == true {
c.Db, err = gorm.Open(sqlite.Open(c.config.Dns), &gorm.Config{
Logger: logger.New(
writer{},
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound记录未找到错误
Colorful: false, // 禁用彩色打印
},
),
NowFunc: func() time.Time {
return gotime.Current().Now().Local()
},
})
} else {
c.Db, err = gorm.Open(sqlite.Open(c.config.Dns), &gorm.Config{})
}
if err != nil {
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
}
sqlDB, err := c.Db.DB()
if err != nil {
return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err))
}
sqlDB.SetMaxIdleConns(10) // 设置空闲连接池中连接的最大数量
sqlDB.SetMaxOpenConns(100) // 设置打开数据库连接的最大数量。
sqlDB.SetConnMaxLifetime(time.Second * 600) // 设置了连接可复用的最大时间。
return c, nil
}

@ -0,0 +1,56 @@
package dorm
import (
"errors"
"fmt"
"go.dtapp.net/library/utils/gotime"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"time"
)
func NewGormSqlServerClient(config *ConfigGormClient) (*GormClient, error) {
var err error
c := &GormClient{config: config}
// 判断路径
if c.config.LogUrl == "" {
logsUrl = "/logs/sqlserver"
}
if c.config.Log == true {
c.Db, err = gorm.Open(sqlserver.Open(c.config.Dns), &gorm.Config{
Logger: logger.New(
writer{},
logger.Config{
SlowThreshold: time.Second, // 慢 SQL 阈值
LogLevel: logger.Info, // 日志级别
IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound记录未找到错误
Colorful: false, // 禁用彩色打印
},
),
NowFunc: func() time.Time {
return gotime.Current().Now().Local()
},
})
} else {
c.Db, err = gorm.Open(sqlserver.Open(c.config.Dns), &gorm.Config{})
}
if err != nil {
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
}
sqlDB, err := c.Db.DB()
if err != nil {
return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err))
}
sqlDB.SetMaxIdleConns(10) // 设置空闲连接池中连接的最大数量
sqlDB.SetMaxOpenConns(100) // 设置打开数据库连接的最大数量。
sqlDB.SetConnMaxLifetime(time.Second * 600) // 设置了连接可复用的最大时间。
return c, nil
}

@ -0,0 +1,14 @@
/.idea
/.connstr
.vscode
.terraform
*.tfstate*
*.log
*.swp
*~
coverage.json
coverage.txt
coverage.xml
testresults.xml
.azureconnstr

@ -0,0 +1,10 @@
linters:
enable:
# basic go linters
- gofmt
- golint
- govet
# sql related linters
- rowserrcheck
- sqlclosecheck

@ -0,0 +1,27 @@
Copyright (c) 2012 The Go Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

@ -0,0 +1,348 @@
# A pure Go MSSQL driver for Go's database/sql package
[![Go Reference](https://pkg.go.dev/badge/github.com/denisenkom/go-mssqldb.svg)](https://pkg.go.dev/github.com/denisenkom/go-mssqldb)
[![Build status](https://ci.appveyor.com/api/projects/status/jrln8cs62wj9i0a2?svg=true)](https://ci.appveyor.com/project/denisenkom/go-mssqldb)
[![codecov](https://codecov.io/gh/denisenkom/go-mssqldb/branch/master/graph/badge.svg)](https://codecov.io/gh/denisenkom/go-mssqldb)
## Install
Requires Go 1.8 or above.
Install with `go get github.com/denisenkom/go-mssqldb` .
## Connection Parameters and DSN
The recommended connection string uses a URL format:
`sqlserver://username:password@host/instance?param1=value&param2=value`
Other supported formats are listed below.
### Common parameters
* `user id` - enter the SQL Server Authentication user id or the Windows Authentication user id in the DOMAIN\User format. On Windows, if user id is empty or missing Single-Sign-On is used. The user domain sensitive to the case which is defined in the connection string.
* `password`
* `database`
* `connection timeout` - in seconds (default is 0 for no timeout), set to 0 for no timeout. Recommended to set to 0 and use context to manage query and connection timeouts.
* `dial timeout` - in seconds (default is 15), set to 0 for no timeout
* `encrypt`
* `disable` - Data send between client and server is not encrypted.
* `false` - Data sent between client and server is not encrypted beyond the login packet. (Default)
* `true` - Data sent between client and server is encrypted.
* `app name` - The application name (default is go-mssqldb)
### Connection parameters for ODBC and ADO style connection strings
* `server` - host or host\instance (default localhost)
* `port` - used only when there is no instance in server (default 1433)
### Less common parameters
* `keepAlive` - in seconds; 0 to disable (default is 30)
* `failoverpartner` - host or host\instance (default is no partner).
* `failoverport` - used only when there is no instance in failoverpartner (default 1433)
* `packet size` - in bytes; 512 to 32767 (default is 4096)
* Encrypted connections have a maximum packet size of 16383 bytes
* Further information on usage: <https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-the-network-packet-size-server-configuration-option>
* `log` - logging flags (default 0/no logging, 63 for full logging)
* 1 log errors
* 2 log messages
* 4 log rows affected
* 8 trace sql statements
* 16 log statement parameters
* 32 log transaction begin/end
* `TrustServerCertificate`
* false - Server certificate is checked. Default is false if encypt is specified.
* true - Server certificate is not checked. Default is true if encrypt is not specified. If trust server certificate is true, driver accepts any certificate presented by the server and any host name in that certificate. In this mode, TLS is susceptible to man-in-the-middle attacks. This should be used only for testing.
* `certificate` - The file that contains the public key certificate of the CA that signed the SQL Server certificate. The specified certificate overrides the go platform specific CA certificates.
* `hostNameInCertificate` - Specifies the Common Name (CN) in the server certificate. Default value is the server host.
* `ServerSPN` - The kerberos SPN (Service Principal Name) for the server. Default is MSSQLSvc/host:port.
* `Workstation ID` - The workstation name (default is the host name)
* `ApplicationIntent` - Can be given the value `ReadOnly` to initiate a read-only connection to an Availability Group listener. The `database` must be specified when connecting with `Application Intent` set to `ReadOnly`.
### The connection string can be specified in one of three formats
1. URL: with `sqlserver` scheme. username and password appears before the host. Any instance appears as
the first segment in the path. All other options are query parameters. Examples:
* `sqlserver://username:password@host/instance?param1=value&param2=value`
* `sqlserver://username:password@host:port?param1=value&param2=value`
* `sqlserver://sa@localhost/SQLExpress?database=master&connection+timeout=30` // `SQLExpress instance.
* `sqlserver://sa:mypass@localhost?database=master&connection+timeout=30` // username=sa, password=mypass.
* `sqlserver://sa:mypass@localhost:1234?database=master&connection+timeout=30` // port 1234 on localhost.
* `sqlserver://sa:my%7Bpass@somehost?connection+timeout=30` // password is "my{pass"
A string of this format can be constructed using the `URL` type in the `net/url` package.
```go
query := url.Values{}
query.Add("app name", "MyAppName")
u := &url.URL{
Scheme: "sqlserver",
User: url.UserPassword(username, password),
Host: fmt.Sprintf("%s:%d", hostname, port),
// Path: instance, // if connecting to an instance instead of a port
RawQuery: query.Encode(),
}
db, err := sql.Open("sqlserver", u.String())
```
2. ADO: `key=value` pairs separated by `;`. Values may not contain `;`, leading and trailing whitespace is ignored.
Examples:
* `server=localhost\\SQLExpress;user id=sa;database=master;app name=MyAppName`
* `server=localhost;user id=sa;database=master;app name=MyAppName`
ADO strings support synonyms for database, app name, user id, and server
* server <= addr, address, network address, data source
* user id <= user, uid
* database <= initial catalog
* app name <= application name
3. ODBC: Prefix with `odbc`, `key=value` pairs separated by `;`. Allow `;` by wrapping
values in `{}`. Examples:
* `odbc:server=localhost\\SQLExpress;user id=sa;database=master;app name=MyAppName`
* `odbc:server=localhost;user id=sa;database=master;app name=MyAppName`
* `odbc:server=localhost;user id=sa;password={foo;bar}` // Value marked with `{}`, password is "foo;bar"
* `odbc:server=localhost;user id=sa;password={foo{bar}` // Value marked with `{}`, password is "foo{bar"
* `odbc:server=localhost;user id=sa;password={foobar }` // Value marked with `{}`, password is "foobar "
* `odbc:server=localhost;user id=sa;password=foo{bar` // Literal `{`, password is "foo{bar"
* `odbc:server=localhost;user id=sa;password=foo}bar` // Literal `}`, password is "foo}bar"
* `odbc:server=localhost;user id=sa;password={foo{bar}` // Literal `{`, password is "foo{bar"
* `odbc:server=localhost;user id=sa;password={foo}}bar}` // Escaped `} with`}}`, password is "foo}bar"
### Azure Active Directory authentication
Azure Active Directory authentication uses temporary authentication tokens to authenticate.
The `mssql` package does not provide an implementation to obtain tokens: instead, import the `azuread` package and use driver name `azuresql`. This driver uses [azidentity](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#section-readme) to acquire tokens using a variety of credential types.
The credential type is determined by the new `fedauth` connection string parameter.
* `fedauth=ActiveDirectoryServicePrincipal` or `fedauth=ActiveDirectoryApplication` - authenticates using an Azure Active Directory application client ID and client secret or certificate. Implemented using [ClientSecretCredential or CertificateCredential](https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/azidentity#authenticating-service-principals)
* `clientcertpath=<path to certificate file>;password=<certificate password>` or
* `password=<client secret>`
* `user id=<application id>[@tenantid]` Note the `@tenantid` component can be omitted if the server's tenant is the same as the application's tenant.
* `fedauth=ActiveDirectoryPassword` - authenticates using a user name and password.
* `user id=username@domain`
* `password=<password>`
* `applicationclientid=<application id>` - This guid identifies an Azure Active Directory enterprise application that the AAD admin has approved for accessing Azure SQL database resources in the tenant. This driver does not have an associated application id of its own.
* `fedauth=ActiveDirectoryDefault` - authenticates using a chained set of credentials. The chain is built from EnvironmentCredential -> ManagedIdentityCredential->AzureCLICredential. See [DefaultAzureCredential docs](https://github.com/Azure/azure-sdk-for-go/wiki/Set-up-Your-Environment-for-Authentication#configure-defaultazurecredential) for instructions on setting up your host environment to use it. Using this option allows you to have the same connection string in a service deployment as on your interactive development machine.
* `fedauth=ActiveDirectoryManagedIdentity` or `fedauth=ActiveDirectoryMSI` - authenticates using a system-assigned or user-assigned Azure Managed Identity.
* `user id=<identity id>` - optional id of user-assigned managed identity. If empty, system-assigned managed identity is used.
* `fedauth=ActiveDirectoryInteractive` - authenticates using credentials acquired from an external web browser. Only suitable for use with human interaction.
* `applicationclientid=<application id>` - This guid identifies an Azure Active Directory enterprise application that the AAD admin has approved for accessing Azure SQL database resources in the tenant. This driver does not have an associated application id of its own.
```go
import (
"database/sql"
"net/url"
// Import the Azure AD driver module (also imports the regular driver package)
"github.com/denisenkom/go-mssqldb/azuread"
)
func ConnectWithMSI() (*sql.DB, error) {
return sql.Open(azuread.DriverName, "sqlserver://azuresql.database.windows.net?database=yourdb&fedauth=ActiveDirectoryMSI")
}
```
## Executing Stored Procedures
To run a stored procedure, set the query text to the procedure name:
```go
var account = "abc"
_, err := db.ExecContext(ctx, "sp_RunMe",
sql.Named("ID", 123),
sql.Named("Account", sql.Out{Dest: &account}),
)
```
## Reading Output Parameters from a Stored Procedure with Resultset
To read output parameters from a stored procedure with resultset, make sure you read all the rows before reading the output parameters:
```go
sqltextcreate := `
CREATE PROCEDURE spwithoutputandrows
@bitparam BIT OUTPUT
AS BEGIN
SET @bitparam = 1
SELECT 'Row 1'
END
`
var bitout int64
rows, err := db.QueryContext(ctx, "spwithoutputandrows", sql.Named("bitparam", sql.Out{Dest: &bitout}))
var strrow string
for rows.Next() {
err = rows.Scan(&strrow)
}
fmt.Printf("bitparam is %d", bitout)
```
## Caveat for local temporary tables
Due to protocol limitations, temporary tables will only be allocated on the connection
as a result of executing a query with zero parameters. The following query
will, due to the use of a parameter, execute in its own session,
and `#mytemp` will be de-allocated right away:
```go
conn, err := pool.Conn(ctx)
defer conn.Close()
_, err := conn.ExecContext(ctx, "select @p1 as x into #mytemp", 1)
// at this point #mytemp is already dropped again as the session of the ExecContext is over
```
To work around this, always explicitly create the local temporary
table in a query without any parameters. As a special case, the driver
will then be able to execute the query directly on the
connection-scoped session. The following example works:
```go
conn, err := pool.Conn(ctx)
// Set us up so that temp table is always cleaned up, since conn.Close()
// merely returns conn to pool, rather than actually closing the connection.
defer func() {
_, _ = conn.ExecContext(ctx, "drop table #mytemp") // always clean up
conn.Close() // merely returns conn to pool
}()
// Since we not pass any parameters below, the query will execute on the scope of
// the connection and succeed in creating the table.
_, err := conn.ExecContext(ctx, "create table #mytemp ( x int )")
// #mytemp is now available even if you pass parameters
_, err := conn.ExecContext(ctx, "insert into #mytemp (x) values (@p1)", 1)
```
## Return Status
To get the procedure return status, pass into the parameters a
`*mssql.ReturnStatus`. For example:
```go
var rs mssql.ReturnStatus
_, err := db.ExecContext(ctx, "theproc", &rs)
log.Printf("status=%d", rs)
```
or
```go
var rs mssql.ReturnStatus
_, err := db.QueryContext(ctx, "theproc", &rs)
for rows.Next() {
err = rows.Scan(&val)
}
log.Printf("status=%d", rs)
```
Limitation: ReturnStatus cannot be retrieved using `QueryRow`.
## Parameters
The `sqlserver` driver uses normal MS SQL Server syntax and expects parameters in
the sql query to be in the form of either `@Name` or `@p1` to `@pN` (ordinal position).
```go
db.QueryContext(ctx, `select * from t where ID = @ID and Name = @p2;`, sql.Named("ID", 6), "Bob")
```
### Parameter Types
To pass specific types to the query parameters, say `varchar` or `date` types,
you must convert the types to the type before passing in. The following types
are supported:
* string -> nvarchar
* mssql.VarChar -> varchar
* time.Time -> datetimeoffset or datetime (TDS version dependent)
* mssql.DateTime1 -> datetime
* mssql.DateTimeOffset -> datetimeoffset
* "github.com/golang-sql/civil".Date -> date
* "github.com/golang-sql/civil".DateTime -> datetime2
* "github.com/golang-sql/civil".Time -> time
* mssql.TVP -> Table Value Parameter (TDS version dependent)
## Important Notes
* [LastInsertId](https://golang.org/pkg/database/sql/#Result.LastInsertId) should
not be used with this driver (or SQL Server) due to how the TDS protocol
works. Please use the [OUTPUT Clause](https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql)
or add a `select ID = convert(bigint, SCOPE_IDENTITY());` to the end of your
query (ref [SCOPE_IDENTITY](https://docs.microsoft.com/en-us/sql/t-sql/functions/scope-identity-transact-sql)).
This will ensure you are getting the correct ID and will prevent a network round trip.
* [NewConnector](https://godoc.org/github.com/denisenkom/go-mssqldb#NewConnector)
may be used with [OpenDB](https://golang.org/pkg/database/sql/#OpenDB).
* [Connector.SessionInitSQL](https://godoc.org/github.com/denisenkom/go-mssqldb#Connector.SessionInitSQL)
may be set to set any driver specific session settings after the session
has been reset. If empty the session will still be reset but use the database
defaults in Go1.10+.
## Features
* Can be used with SQL Server 2005 or newer
* Can be used with Microsoft Azure SQL Database
* Can be used on all go supported platforms (e.g. Linux, Mac OS X and Windows)
* Supports new date/time types: date, time, datetime2, datetimeoffset
* Supports string parameters longer than 8000 characters
* Supports encryption using SSL/TLS
* Supports SQL Server and Windows Authentication
* Supports Single-Sign-On on Windows
* Supports connections to AlwaysOn Availability Group listeners, including re-direction to read-only replicas.
* Supports query notifications
## Tests
`go test` is used for testing. A running instance of MSSQL server is required.
Environment variables are used to pass login information.
Example:
```bash
env SQLSERVER_DSN=sqlserver://user:pass@hostname/instance?database=test1 go test
```
`AZURESERVER_DSN` environment variable provides the connection string for Azure Active Directory-based authentication. If it's not set the AAD test will be skipped.
## Deprecated
These features still exist in the driver, but they are are deprecated.
### Query Parameter Token Replace (driver "mssql")
If you use the driver name "mssql" (rather then "sqlserver") the SQL text
will be loosly parsed and an attempt to extract identifiers using one of
* ?
* ?nnn
* :nnn
* $nnn
will be used. This is not recommended with SQL Server.
There is at least one existing `won't fix` issue with the query parsing.
Use the native "@Name" parameters instead with the "sqlserver" driver name.
## Known Issues
* SQL Server 2008 and 2008 R2 engine cannot handle login records when SSL encryption is not disabled.
To fix SQL Server 2008 R2 issue, install SQL Server 2008 R2 Service Pack 2.
To fix SQL Server 2008 issue, install Microsoft SQL Server 2008 Service Pack 3 and Cumulative update package 3 for SQL Server 2008 SP3.
More information: <http://support.microsoft.com/kb/2653857>

@ -0,0 +1,31 @@
// +build go1.10
package mssql
import (
"context"
"database/sql/driver"
"errors"
)
// NewAccessTokenConnector creates a new connector from a DSN and a token provider.
// The token provider func will be called when a new connection is requested and should return a valid access token.
// The returned connector may be used with sql.OpenDB.
func NewAccessTokenConnector(dsn string, tokenProvider func() (string, error)) (driver.Connector, error) {
if tokenProvider == nil {
return nil, errors.New("mssql: tokenProvider cannot be nil")
}
conn, err := NewConnector(dsn)
if err != nil {
return nil, err
}
conn.fedAuthRequired = true
conn.fedAuthLibrary = FedAuthLibrarySecurityToken
conn.securityTokenProvider = func(ctx context.Context) (string, error) {
return tokenProvider()
}
return conn, nil
}

@ -0,0 +1,69 @@
version: 1.0.{build}
image:
- Visual Studio 2015
clone_folder: c:\gopath\src\github.com\denisenkom\go-mssqldb
environment:
GOPATH: c:\gopath
HOST: localhost
SQLUSER: sa
SQLPASSWORD: Password12!
DATABASE: test
GOVERSION: 113
matrix:
- GOVERSION: 18
SQLINSTANCE: SQL2017
- GOVERSION: 19
SQLINSTANCE: SQL2017
- GOVERSION: 110
SQLINSTANCE: SQL2017
- GOVERSION: 111
SQLINSTANCE: SQL2017
- GOVERSION: 112
SQLINSTANCE: SQL2017
- SQLINSTANCE: SQL2017
- SQLINSTANCE: SQL2016
- SQLINSTANCE: SQL2014
- SQLINSTANCE: SQL2012SP1
- SQLINSTANCE: SQL2008R2SP2
# Go 1.14+ and SQL2019 are available on the Visual Studio 2019 image only
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
GOVERSION: 114
SQLINSTANCE: SQL2019
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
GOVERSION: 115
SQLINSTANCE: SQL2019
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
GOVERSION: 115
SQLINSTANCE: SQL2017
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
GOVERSION: 116
SQLINSTANCE: SQL2017
install:
- set GOROOT=c:\go%GOVERSION%
- set PATH=%GOPATH%\bin;%GOROOT%\bin;%PATH%
- go version
- go env
- go get -u github.com/golang-sql/civil
- go get -u github.com/golang-sql/sqlexp
build_script:
- go build
before_test:
# setup SQL Server
- ps: |
$instanceName = $env:SQLINSTANCE
Start-Service "MSSQL`$$instanceName"
Start-Service "SQLBrowser"
- sqlcmd -S "(local)\%SQLINSTANCE%" -Q "Use [master]; CREATE DATABASE test;"
- sqlcmd -S "(local)\%SQLINSTANCE%" -h -1 -Q "set nocount on; Select @@version"
- pip install codecov
test_script:
- go test -race -cpu 4 -coverprofile=coverage.txt -covermode=atomic
- codecov -f coverage.txt

@ -0,0 +1,271 @@
package mssql
import (
"encoding/binary"
"errors"
"io"
)
type packetType uint8
type header struct {
PacketType packetType
Status uint8
Size uint16
Spid uint16
PacketNo uint8
Pad uint8
}
// tdsBuffer reads and writes TDS packets of data to the transport.
// The write and read buffers are separate to make sending attn signals
// possible without locks. Currently attn signals are only sent during
// reads, not writes.
type tdsBuffer struct {
transport io.ReadWriteCloser
packetSize int
// Write fields.
wbuf []byte
wpos int
wPacketSeq byte
wPacketType packetType
// Read fields.
rbuf []byte
rpos int
rsize int
final bool
rPacketType packetType
// afterFirst is assigned to right after tdsBuffer is created and
// before the first use. It is executed after the first packet is
// written and then removed.
afterFirst func()
}
func newTdsBuffer(bufsize uint16, transport io.ReadWriteCloser) *tdsBuffer {
return &tdsBuffer{
packetSize: int(bufsize),
wbuf: make([]byte, bufsize),
rbuf: make([]byte, bufsize),
rpos: 8,
transport: transport,
}
}
func (rw *tdsBuffer) ResizeBuffer(packetSize int) {
rw.packetSize = packetSize
}
func (w *tdsBuffer) PackageSize() int {
return w.packetSize
}
func (w *tdsBuffer) flush() (err error) {
// Write packet size.
w.wbuf[0] = byte(w.wPacketType)
binary.BigEndian.PutUint16(w.wbuf[2:], uint16(w.wpos))
w.wbuf[6] = w.wPacketSeq
// Write packet into underlying transport.
if _, err = w.transport.Write(w.wbuf[:w.wpos]); err != nil {
return err
}
// It is possible to create a whole new buffer after a flush.
// Useful for debugging. Normally reuse the buffer.
// w.wbuf = make([]byte, 1<<16)
// Execute afterFirst hook if it is set.
if w.afterFirst != nil {
w.afterFirst()
w.afterFirst = nil
}
w.wpos = 8
w.wPacketSeq++
return nil
}
func (w *tdsBuffer) Write(p []byte) (total int, err error) {
for {
copied := copy(w.wbuf[w.wpos:w.packetSize], p)
w.wpos += copied
total += copied
if copied == len(p) {
return
}
if err = w.flush(); err != nil {
return
}
p = p[copied:]
}
}
func (w *tdsBuffer) WriteByte(b byte) error {
if int(w.wpos) == len(w.wbuf) || w.wpos == w.packetSize {
if err := w.flush(); err != nil {
return err
}
}
w.wbuf[w.wpos] = b
w.wpos += 1
return nil
}
func (w *tdsBuffer) BeginPacket(packetType packetType, resetSession bool) {
status := byte(0)
if resetSession {
switch packetType {
// Reset session can only be set on the following packet types.
case packSQLBatch, packRPCRequest, packTransMgrReq:
status = 0x8
}
}
w.wbuf[1] = status // Packet is incomplete. This byte is set again in FinishPacket.
w.wpos = 8
w.wPacketSeq = 1
w.wPacketType = packetType
}
func (w *tdsBuffer) FinishPacket() error {
w.wbuf[1] |= 1 // Mark this as the last packet in the message.
return w.flush()
}
var headerSize = binary.Size(header{})
func (r *tdsBuffer) readNextPacket() error {
buf := r.rbuf[:headerSize]
_, err := io.ReadFull(r.transport, buf)
if err != nil {
return err
}
h := header{
PacketType: packetType(buf[0]),
Status: buf[1],
Size: binary.BigEndian.Uint16(buf[2:4]),
Spid: binary.BigEndian.Uint16(buf[4:6]),
PacketNo: buf[6],
Pad: buf[7],
}
if int(h.Size) > r.packetSize {
return errors.New("invalid packet size, it is longer than buffer size")
}
if headerSize > int(h.Size) {
return errors.New("invalid packet size, it is shorter than header size")
}
_, err = io.ReadFull(r.transport, r.rbuf[headerSize:h.Size])
//s := base64.StdEncoding.EncodeToString(r.rbuf[headerSize:h.Size])
//fmt.Print(s)
if err != nil {
return err
}
r.rpos = headerSize
r.rsize = int(h.Size)
r.final = h.Status != 0
r.rPacketType = h.PacketType
return nil
}
func (r *tdsBuffer) BeginRead() (packetType, error) {
err := r.readNextPacket()
if err != nil {
return 0, err
}
return r.rPacketType, nil
}
func (r *tdsBuffer) ReadByte() (res byte, err error) {
if r.rpos == r.rsize {
if r.final {
return 0, io.EOF
}
err = r.readNextPacket()
if err != nil {
return 0, err
}
}
res = r.rbuf[r.rpos]
r.rpos++
return res, nil
}
func (r *tdsBuffer) byte() byte {
b, err := r.ReadByte()
if err != nil {
badStreamPanic(err)
}
return b
}
func (r *tdsBuffer) ReadFull(buf []byte) {
_, err := io.ReadFull(r, buf[:])
if err != nil {
badStreamPanic(err)
}
}
func (r *tdsBuffer) uint64() uint64 {
var buf [8]byte
r.ReadFull(buf[:])
return binary.LittleEndian.Uint64(buf[:])
}
func (r *tdsBuffer) int32() int32 {
return int32(r.uint32())
}
func (r *tdsBuffer) uint32() uint32 {
var buf [4]byte
r.ReadFull(buf[:])
return binary.LittleEndian.Uint32(buf[:])
}
func (r *tdsBuffer) uint16() uint16 {
var buf [2]byte
r.ReadFull(buf[:])
return binary.LittleEndian.Uint16(buf[:])
}
func (r *tdsBuffer) BVarChar() string {
return readBVarCharOrPanic(r)
}
func readBVarCharOrPanic(r io.Reader) string {
s, err := readBVarChar(r)
if err != nil {
badStreamPanic(err)
}
return s
}
func readUsVarCharOrPanic(r io.Reader) string {
s, err := readUsVarChar(r)
if err != nil {
badStreamPanic(err)
}
return s
}
func (r *tdsBuffer) UsVarChar() string {
return readUsVarCharOrPanic(r)
}
func (r *tdsBuffer) Read(buf []byte) (copied int, err error) {
copied = 0
err = nil
if r.rpos == r.rsize {
if r.final {
return 0, io.EOF
}
err = r.readNextPacket()
if err != nil {
return
}
}
copied = copy(buf, r.rbuf[r.rpos:r.rsize])
r.rpos += copied
return
}

@ -0,0 +1,598 @@
package mssql
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"math"
"reflect"
"strconv"
"strings"
"time"
"github.com/denisenkom/go-mssqldb/internal/decimal"
"github.com/denisenkom/go-mssqldb/msdsn"
)
type Bulk struct {
// ctx is used only for AddRow and Done methods.
// This could be removed if AddRow and Done accepted
// a ctx field as well, which is available with the
// database/sql call.
ctx context.Context
cn *Conn
metadata []columnStruct
bulkColumns []columnStruct
columnsName []string
tablename string
numRows int
headerSent bool
Options BulkOptions
Debug bool
}
type BulkOptions struct {
CheckConstraints bool
FireTriggers bool
KeepNulls bool
KilobytesPerBatch int
RowsPerBatch int
Order []string
Tablock bool
}
type DataValue interface{}
const (
sqlDateFormat = "2006-01-02"
sqlDateTimeFormat = "2006-01-02 15:04:05.999999999Z07:00"
sqlTimeFormat = "15:04:05.9999999"
)
func (cn *Conn) CreateBulk(table string, columns []string) (_ *Bulk) {
b := Bulk{ctx: context.Background(), cn: cn, tablename: table, headerSent: false, columnsName: columns}
b.Debug = false
return &b
}
func (cn *Conn) CreateBulkContext(ctx context.Context, table string, columns []string) (_ *Bulk) {
b := Bulk{ctx: ctx, cn: cn, tablename: table, headerSent: false, columnsName: columns}
b.Debug = false
return &b
}
func (b *Bulk) sendBulkCommand(ctx context.Context) (err error) {
//get table columns info
err = b.getMetadata(ctx)
if err != nil {
return err
}
//match the columns
for _, colname := range b.columnsName {
var bulkCol *columnStruct
for _, m := range b.metadata {
if m.ColName == colname {
bulkCol = &m
break
}
}
if bulkCol != nil {
if bulkCol.ti.TypeId == typeUdt {
//send udt as binary
bulkCol.ti.TypeId = typeBigVarBin
}
b.bulkColumns = append(b.bulkColumns, *bulkCol)
b.dlogf(ctx, "Adding column %s %s %#x", colname, bulkCol.ColName, bulkCol.ti.TypeId)
} else {
return fmt.Errorf("column %s does not exist in destination table %s", colname, b.tablename)
}
}
//create the bulk command
//columns definitions
var col_defs bytes.Buffer
for i, col := range b.bulkColumns {
if i != 0 {
col_defs.WriteString(", ")
}
col_defs.WriteString("[" + col.ColName + "] " + makeDecl(col.ti))
}
//options
var with_opts []string
if b.Options.CheckConstraints {
with_opts = append(with_opts, "CHECK_CONSTRAINTS")
}
if b.Options.FireTriggers {
with_opts = append(with_opts, "FIRE_TRIGGERS")
}
if b.Options.KeepNulls {
with_opts = append(with_opts, "KEEP_NULLS")
}
if b.Options.KilobytesPerBatch > 0 {
with_opts = append(with_opts, fmt.Sprintf("KILOBYTES_PER_BATCH = %d", b.Options.KilobytesPerBatch))
}
if b.Options.RowsPerBatch > 0 {
with_opts = append(with_opts, fmt.Sprintf("ROWS_PER_BATCH = %d", b.Options.RowsPerBatch))
}
if len(b.Options.Order) > 0 {
with_opts = append(with_opts, fmt.Sprintf("ORDER(%s)", strings.Join(b.Options.Order, ",")))
}
if b.Options.Tablock {
with_opts = append(with_opts, "TABLOCK")
}
var with_part string
if len(with_opts) > 0 {
with_part = fmt.Sprintf("WITH (%s)", strings.Join(with_opts, ","))
}
query := fmt.Sprintf("INSERT BULK %s (%s) %s", b.tablename, col_defs.String(), with_part)
stmt, err := b.cn.PrepareContext(ctx, query)
if err != nil {
return fmt.Errorf("Prepare failed: %s", err.Error())
}
b.dlogf(ctx, query)
_, err = stmt.(*Stmt).ExecContext(ctx, nil)
if err != nil {
return err
}
b.headerSent = true
var buf = b.cn.sess.buf
buf.BeginPacket(packBulkLoadBCP, false)
// Send the columns metadata.
columnMetadata := b.createColMetadata()
_, err = buf.Write(columnMetadata)
return
}
// AddRow immediately writes the row to the destination table.
// The arguments are the row values in the order they were specified.
func (b *Bulk) AddRow(row []interface{}) (err error) {
if !b.headerSent {
err = b.sendBulkCommand(b.ctx)
if err != nil {
return
}
}
if len(row) != len(b.bulkColumns) {
return fmt.Errorf("row does not have the same number of columns than the destination table %d %d",
len(row), len(b.bulkColumns))
}
bytes, err := b.makeRowData(row)
if err != nil {
return
}
_, err = b.cn.sess.buf.Write(bytes)
if err != nil {
return
}
b.numRows = b.numRows + 1
return
}
func (b *Bulk) makeRowData(row []interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
buf.WriteByte(byte(tokenRow))
var logcol bytes.Buffer
for i, col := range b.bulkColumns {
if b.Debug {
logcol.WriteString(fmt.Sprintf(" col[%d]='%v' ", i, row[i]))
}
param, err := b.makeParam(row[i], col)
if err != nil {
return nil, fmt.Errorf("bulkcopy: %s", err.Error())
}
if col.ti.Writer == nil {
return nil, fmt.Errorf("no writer for column: %s, TypeId: %#x",
col.ColName, col.ti.TypeId)
}
err = col.ti.Writer(buf, param.ti, param.buffer)
if err != nil {
return nil, fmt.Errorf("bulkcopy: %s", err.Error())
}
}
b.dlogf(b.ctx, "row[%d] %s", b.numRows, logcol.String())
return buf.Bytes(), nil
}
func (b *Bulk) Done() (rowcount int64, err error) {
if !b.headerSent {
//no rows had been sent
return 0, nil
}
var buf = b.cn.sess.buf
buf.WriteByte(byte(tokenDone))
binary.Write(buf, binary.LittleEndian, uint16(doneFinal))
binary.Write(buf, binary.LittleEndian, uint16(0)) // curcmd
if b.cn.sess.loginAck.TDSVersion >= verTDS72 {
binary.Write(buf, binary.LittleEndian, uint64(0)) //rowcount 0
} else {
binary.Write(buf, binary.LittleEndian, uint32(0)) //rowcount 0
}
buf.FinishPacket()
reader := startReading(b.cn.sess, b.ctx, outputs{})
err = reader.iterateResponse()
if err != nil {
return 0, b.cn.checkBadConn(b.ctx, err, false)
}
return reader.rowCount, nil
}
func (b *Bulk) createColMetadata() []byte {
buf := new(bytes.Buffer)
buf.WriteByte(byte(tokenColMetadata)) // token
binary.Write(buf, binary.LittleEndian, uint16(len(b.bulkColumns))) // column count
for i, col := range b.bulkColumns {
if b.cn.sess.loginAck.TDSVersion >= verTDS72 {
binary.Write(buf, binary.LittleEndian, uint32(col.UserType)) // usertype, always 0?
} else {
binary.Write(buf, binary.LittleEndian, uint16(col.UserType))
}
binary.Write(buf, binary.LittleEndian, uint16(col.Flags))
writeTypeInfo(buf, &b.bulkColumns[i].ti)
if col.ti.TypeId == typeNText ||
col.ti.TypeId == typeText ||
col.ti.TypeId == typeImage {
tablename_ucs2 := str2ucs2(b.tablename)
binary.Write(buf, binary.LittleEndian, uint16(len(tablename_ucs2)/2))
buf.Write(tablename_ucs2)
}
colname_ucs2 := str2ucs2(col.ColName)
buf.WriteByte(uint8(len(colname_ucs2) / 2))
buf.Write(colname_ucs2)
}
return buf.Bytes()
}
func (b *Bulk) getMetadata(ctx context.Context) (err error) {
stmt, err := b.cn.prepareContext(ctx, "SET FMTONLY ON")
if err != nil {
return
}
_, err = stmt.ExecContext(ctx, nil)
if err != nil {
return
}
// Get columns info.
stmt, err = b.cn.prepareContext(ctx, fmt.Sprintf("select * from %s SET FMTONLY OFF", b.tablename))
if err != nil {
return
}
rows, err := stmt.QueryContext(ctx, nil)
if err != nil {
return fmt.Errorf("get columns info failed: %v", err)
}
b.metadata = rows.(*Rows).cols
if b.Debug {
for _, col := range b.metadata {
b.dlogf(ctx, "col: %s typeId: %#x size: %d scale: %d prec: %d flags: %d lcid: %#x",
col.ColName, col.ti.TypeId, col.ti.Size, col.ti.Scale, col.ti.Prec,
col.Flags, col.ti.Collation.LcidAndFlags)
}
}
return rows.Close()
}
func (b *Bulk) makeParam(val DataValue, col columnStruct) (res param, err error) {
res.ti.Size = col.ti.Size
res.ti.TypeId = col.ti.TypeId
if val == nil {
res.ti.Size = 0
return
}
switch col.ti.TypeId {
case typeInt1, typeInt2, typeInt4, typeInt8, typeIntN:
var intvalue int64
switch val := val.(type) {
case int:
intvalue = int64(val)
case int32:
intvalue = int64(val)
case int64:
intvalue = val
case float32:
intvalue = int64(val)
case float64:
intvalue = int64(val)
default:
err = fmt.Errorf("mssql: invalid type for int column: %T", val)
return
}
res.buffer = make([]byte, res.ti.Size)
if col.ti.Size == 1 {
res.buffer[0] = byte(intvalue)
} else if col.ti.Size == 2 {
binary.LittleEndian.PutUint16(res.buffer, uint16(intvalue))
} else if col.ti.Size == 4 {
binary.LittleEndian.PutUint32(res.buffer, uint32(intvalue))
} else if col.ti.Size == 8 {
binary.LittleEndian.PutUint64(res.buffer, uint64(intvalue))
}
case typeFlt4, typeFlt8, typeFltN:
var floatvalue float64
switch val := val.(type) {
case float32:
floatvalue = float64(val)
case float64:
floatvalue = val
case int:
floatvalue = float64(val)
case int64:
floatvalue = float64(val)
default:
err = fmt.Errorf("mssql: invalid type for float column: %T %s", val, val)
return
}
if col.ti.Size == 4 {
res.buffer = make([]byte, 4)
binary.LittleEndian.PutUint32(res.buffer, math.Float32bits(float32(floatvalue)))
} else if col.ti.Size == 8 {
res.buffer = make([]byte, 8)
binary.LittleEndian.PutUint64(res.buffer, math.Float64bits(floatvalue))
}
case typeNVarChar, typeNText, typeNChar:
switch val := val.(type) {
case string:
res.buffer = str2ucs2(val)
case int64:
res.buffer = []byte(strconv.FormatInt(val, 10))
case []byte:
res.buffer = val
default:
err = fmt.Errorf("mssql: invalid type for nvarchar column: %T %s", val, val)
return
}
res.ti.Size = len(res.buffer)
case typeVarChar, typeBigVarChar, typeText, typeChar, typeBigChar:
switch val := val.(type) {
case string:
res.buffer = []byte(val)
case []byte:
res.buffer = val
case int64:
res.buffer = []byte(strconv.FormatInt(val, 10))
default:
err = fmt.Errorf("mssql: invalid type for varchar column: %T %s", val, val)
return
}
res.ti.Size = len(res.buffer)
case typeBit, typeBitN:
if reflect.TypeOf(val).Kind() != reflect.Bool {
err = fmt.Errorf("mssql: invalid type for bit column: %T %s", val, val)
return
}
res.ti.TypeId = typeBitN
res.ti.Size = 1
res.buffer = make([]byte, 1)
if val.(bool) {
res.buffer[0] = 1
}
case typeDateTime2N:
switch val := val.(type) {
case time.Time:
res.buffer = encodeDateTime2(val, int(col.ti.Scale))
res.ti.Size = len(res.buffer)
case string:
var t time.Time
if t, err = time.Parse(sqlDateTimeFormat, val); err != nil {
return res, fmt.Errorf("bulk: unable to convert string to date: %v", err)
}
res.buffer = encodeDateTime2(t, int(col.ti.Scale))
res.ti.Size = len(res.buffer)
default:
err = fmt.Errorf("mssql: invalid type for datetime2 column: %T %s", val, val)
return
}
case typeDateTimeOffsetN:
switch val := val.(type) {
case time.Time:
res.buffer = encodeDateTimeOffset(val, int(col.ti.Scale))
res.ti.Size = len(res.buffer)
case string:
var t time.Time
if t, err = time.Parse(sqlDateTimeFormat, val); err != nil {
return res, fmt.Errorf("bulk: unable to convert string to date: %v", err)
}
res.buffer = encodeDateTimeOffset(t, int(col.ti.Scale))
res.ti.Size = len(res.buffer)
default:
err = fmt.Errorf("mssql: invalid type for datetimeoffset column: %T %s", val, val)
return
}
case typeDateN:
switch val := val.(type) {
case time.Time:
res.buffer = encodeDate(val)
res.ti.Size = len(res.buffer)
case string:
var t time.Time
if t, err = time.ParseInLocation(sqlDateFormat, val, time.UTC); err != nil {
return res, fmt.Errorf("bulk: unable to convert string to date: %v", err)
}
res.buffer = encodeDate(t)
res.ti.Size = len(res.buffer)
default:
err = fmt.Errorf("mssql: invalid type for date column: %T %s", val, val)
return
}
case typeDateTime, typeDateTimeN, typeDateTim4:
var t time.Time
switch val := val.(type) {
case time.Time:
t = val
case string:
if t, err = time.Parse(sqlDateTimeFormat, val); err != nil {
return res, fmt.Errorf("bulk: unable to convert string to date: %v", err)
}
default:
err = fmt.Errorf("mssql: invalid type for datetime column: %T %s", val, val)
return
}
if col.ti.Size == 4 {
res.buffer = encodeDateTim4(t)
res.ti.Size = len(res.buffer)
} else if col.ti.Size == 8 {
res.buffer = encodeDateTime(t)
res.ti.Size = len(res.buffer)
} else {
err = fmt.Errorf("mssql: invalid size of column %d", col.ti.Size)
}
case typeTimeN:
var t time.Time
switch val := val.(type) {
case time.Time:
res.buffer = encodeTime(val.Hour(), val.Minute(), val.Second(), val.Nanosecond(), int(col.ti.Scale))
res.ti.Size = len(res.buffer)
case string:
if t, err = time.Parse(sqlTimeFormat, val); err != nil {
return res, fmt.Errorf("bulk: unable to convert string to time: %v", err)
}
res.buffer = encodeTime(t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), int(col.ti.Scale))
res.ti.Size = len(res.buffer)
default:
err = fmt.Errorf("mssql: invalid type for time column: %T %s", val, val)
return
}
// case typeMoney, typeMoney4, typeMoneyN:
case typeDecimal, typeDecimalN, typeNumeric, typeNumericN:
prec := col.ti.Prec
scale := col.ti.Scale
var dec decimal.Decimal
switch v := val.(type) {
case int:
dec = decimal.Int64ToDecimalScale(int64(v), 0)
case int8:
dec = decimal.Int64ToDecimalScale(int64(v), 0)
case int16:
dec = decimal.Int64ToDecimalScale(int64(v), 0)
case int32:
dec = decimal.Int64ToDecimalScale(int64(v), 0)
case int64:
dec = decimal.Int64ToDecimalScale(int64(v), 0)
case float32:
dec, err = decimal.Float64ToDecimalScale(float64(v), scale)
case float64:
dec, err = decimal.Float64ToDecimalScale(float64(v), scale)
case string:
dec, err = decimal.StringToDecimalScale(v, scale)
default:
return res, fmt.Errorf("unknown value for decimal: %T %#v", v, v)
}
if err != nil {
return res, err
}
dec.SetPrec(prec)
var length byte
switch {
case prec <= 9:
length = 4
case prec <= 19:
length = 8
case prec <= 28:
length = 12
default:
length = 16
}
buf := make([]byte, length+1)
// first byte length written by typeInfo.writer
res.ti.Size = int(length) + 1
// second byte sign
if !dec.IsPositive() {
buf[0] = 0
} else {
buf[0] = 1
}
ub := dec.UnscaledBytes()
l := len(ub)
if l > int(length) {
err = fmt.Errorf("decimal out of range: %s", dec)
return res, err
}
// reverse the bytes
for i, j := 1, l-1; j >= 0; i, j = i+1, j-1 {
buf[i] = ub[j]
}
res.buffer = buf
case typeBigVarBin, typeBigBinary:
switch val := val.(type) {
case []byte:
res.ti.Size = len(val)
res.buffer = val
default:
err = fmt.Errorf("mssql: invalid type for Binary column: %T %s", val, val)
return
}
case typeGuid:
switch val := val.(type) {
case []byte:
res.ti.Size = len(val)
res.buffer = val
default:
err = fmt.Errorf("mssql: invalid type for Guid column: %T %s", val, val)
return
}
default:
err = fmt.Errorf("mssql: type %x not implemented", col.ti.TypeId)
}
return
}
func (b *Bulk) dlogf(ctx context.Context, format string, v ...interface{}) {
if b.Debug {
b.cn.sess.logger.Log(ctx, msdsn.LogDebug, fmt.Sprintf(format, v...))
}
}

@ -0,0 +1,93 @@
package mssql
import (
"context"
"database/sql/driver"
"encoding/json"
"errors"
)
type copyin struct {
cn *Conn
bulkcopy *Bulk
closed bool
}
type serializableBulkConfig struct {
TableName string
ColumnsName []string
Options BulkOptions
}
func (d *Driver) OpenConnection(dsn string) (*Conn, error) {
return d.open(context.Background(), dsn)
}
func (c *Conn) prepareCopyIn(ctx context.Context, query string) (_ driver.Stmt, err error) {
config_json := query[11:]
bulkconfig := serializableBulkConfig{}
err = json.Unmarshal([]byte(config_json), &bulkconfig)
if err != nil {
return
}
bulkcopy := c.CreateBulkContext(ctx, bulkconfig.TableName, bulkconfig.ColumnsName)
bulkcopy.Options = bulkconfig.Options
ci := &copyin{
cn: c,
bulkcopy: bulkcopy,
}
return ci, nil
}
func CopyIn(table string, options BulkOptions, columns ...string) string {
bulkconfig := &serializableBulkConfig{TableName: table, Options: options, ColumnsName: columns}
config_json, err := json.Marshal(bulkconfig)
if err != nil {
panic(err)
}
stmt := "INSERTBULK " + string(config_json)
return stmt
}
func (ci *copyin) NumInput() int {
return -1
}
func (ci *copyin) Query(v []driver.Value) (r driver.Rows, err error) {
panic("should never be called")
}
func (ci *copyin) Exec(v []driver.Value) (r driver.Result, err error) {
if ci.closed {
return nil, errors.New("copyin query is closed")
}
if len(v) == 0 {
rowCount, err := ci.bulkcopy.Done()
ci.closed = true
return driver.RowsAffected(rowCount), err
}
t := make([]interface{}, len(v))
for i, val := range v {
t[i] = val
}
err = ci.bulkcopy.AddRow(t)
if err != nil {
return
}
return driver.RowsAffected(0), nil
}
func (ci *copyin) Close() (err error) {
return nil
}

@ -0,0 +1,306 @@
package mssql
import "errors"
// Copyright 2011 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.
// Type conversions for Scan.
// This file was imported from database.sql.convert for go 1.10.3 with minor modifications to get
// convertAssign function
// This function is used internally by sql to convert values during call to Scan, we need same
// logic to return values for OUTPUT parameters.
// TODO: sql library should instead expose function defaultCheckNamedValue to be callable by drivers
import (
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"strconv"
"time"
)
var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
// convertAssign copies to dest the value in src, converting it if possible.
// An error is returned if the copy would result in loss of information.
// dest should be a pointer type.
func convertAssign(dest, src interface{}) error {
// Common cases, without reflect.
switch s := src.(type) {
case string:
switch d := dest.(type) {
case *string:
if d == nil {
return errNilPtr
}
*d = s
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = []byte(s)
return nil
case *sql.RawBytes:
if d == nil {
return errNilPtr
}
*d = append((*d)[:0], s...)
return nil
}
case []byte:
switch d := dest.(type) {
case *string:
if d == nil {
return errNilPtr
}
*d = string(s)
return nil
case *interface{}:
if d == nil {
return errNilPtr
}
*d = cloneBytes(s)
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = cloneBytes(s)
return nil
case *sql.RawBytes:
if d == nil {
return errNilPtr
}
*d = s
return nil
}
case time.Time:
switch d := dest.(type) {
case *time.Time:
*d = s
return nil
case *string:
*d = s.Format(time.RFC3339Nano)
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = []byte(s.Format(time.RFC3339Nano))
return nil
case *sql.RawBytes:
if d == nil {
return errNilPtr
}
*d = s.AppendFormat((*d)[:0], time.RFC3339Nano)
return nil
}
case nil:
switch d := dest.(type) {
case *interface{}:
if d == nil {
return errNilPtr
}
*d = nil
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = nil
return nil
case *sql.RawBytes:
if d == nil {
return errNilPtr
}
*d = nil
return nil
}
}
var sv reflect.Value
switch d := dest.(type) {
case *string:
sv = reflect.ValueOf(src)
switch sv.Kind() {
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
*d = asString(src)
return nil
}
case *[]byte:
sv = reflect.ValueOf(src)
if b, ok := asBytes(nil, sv); ok {
*d = b
return nil
}
case *sql.RawBytes:
sv = reflect.ValueOf(src)
if b, ok := asBytes([]byte(*d)[:0], sv); ok {
*d = sql.RawBytes(b)
return nil
}
case *bool:
bv, err := driver.Bool.ConvertValue(src)
if err == nil {
*d = bv.(bool)
}
return err
case *interface{}:
*d = src
return nil
}
if scanner, ok := dest.(sql.Scanner); ok {
return scanner.Scan(src)
}
dpv := reflect.ValueOf(dest)
if dpv.Kind() != reflect.Ptr {
return errors.New("destination not a pointer")
}
if dpv.IsNil() {
return errNilPtr
}
if !sv.IsValid() {
sv = reflect.ValueOf(src)
}
dv := reflect.Indirect(dpv)
if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) {
switch b := src.(type) {
case []byte:
dv.Set(reflect.ValueOf(cloneBytes(b)))
default:
dv.Set(sv)
}
return nil
}
if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) {
dv.Set(sv.Convert(dv.Type()))
return nil
}
// The following conversions use a string value as an intermediate representation
// to convert between various numeric types.
//
// This also allows scanning into user defined types such as "type Int int64".
// For symmetry, also check for string destination types.
switch dv.Kind() {
case reflect.Ptr:
if src == nil {
dv.Set(reflect.Zero(dv.Type()))
return nil
} else {
dv.Set(reflect.New(dv.Type().Elem()))
return convertAssign(dv.Interface(), src)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s := asString(src)
i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
if err != nil {
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
}
dv.SetInt(i64)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
s := asString(src)
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
if err != nil {
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
}
dv.SetUint(u64)
return nil
case reflect.Float32, reflect.Float64:
s := asString(src)
f64, err := strconv.ParseFloat(s, dv.Type().Bits())
if err != nil {
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
}
dv.SetFloat(f64)
return nil
case reflect.String:
switch v := src.(type) {
case string:
dv.SetString(v)
return nil
case []byte:
dv.SetString(string(v))
return nil
}
}
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest)
}
func strconvErr(err error) error {
if ne, ok := err.(*strconv.NumError); ok {
return ne.Err
}
return err
}
func cloneBytes(b []byte) []byte {
if b == nil {
return nil
} else {
c := make([]byte, len(b))
copy(c, b)
return c
}
}
func asString(src interface{}) string {
switch v := src.(type) {
case string:
return v
case []byte:
return string(v)
}
rv := reflect.ValueOf(src)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(rv.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(rv.Uint(), 10)
case reflect.Float64:
return strconv.FormatFloat(rv.Float(), 'g', -1, 64)
case reflect.Float32:
return strconv.FormatFloat(rv.Float(), 'g', -1, 32)
case reflect.Bool:
return strconv.FormatBool(rv.Bool())
}
return fmt.Sprintf("%v", src)
}
func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) {
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.AppendInt(buf, rv.Int(), 10), true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.AppendUint(buf, rv.Uint(), 10), true
case reflect.Float32:
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true
case reflect.Float64:
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true
case reflect.Bool:
return strconv.AppendBool(buf, rv.Bool()), true
case reflect.String:
s := rv.String()
return append(buf, s...), true
}
return
}

@ -0,0 +1,14 @@
// package mssql implements the TDS protocol used to connect to MS SQL Server (sqlserver)
// database servers.
//
// This package registers the driver:
// sqlserver: uses native "@" parameter placeholder names and does no pre-processing.
//
// If the ordinal position is used for query parameters, identifiers will be named
// "@p1", "@p2", ... "@pN".
//
// Please refer to the README for the format of the DSN. There are multiple DSN
// formats accepted: ADO style, ODBC style, and URL style. The following is an
// example of a URL style DSN:
// sqlserver://sa:mypass@localhost:1234?database=master&connection+timeout=30
package mssql

@ -0,0 +1,117 @@
package mssql
import (
"database/sql/driver"
"fmt"
)
// Error represents an SQL Server error. This
// type includes methods for reading the contents
// of the struct, which allows calling programs
// to check for specific error conditions without
// having to import this package directly.
type Error struct {
Number int32
State uint8
Class uint8
Message string
ServerName string
ProcName string
LineNo int32
// All lists all errors that were received from first to last.
// This includes the last one, which is described in the other members.
All []Error
}
func (e Error) Error() string {
return "mssql: " + e.Message
}
// SQLErrorNumber returns the SQL Server error number.
func (e Error) SQLErrorNumber() int32 {
return e.Number
}
func (e Error) SQLErrorState() uint8 {
return e.State
}
func (e Error) SQLErrorClass() uint8 {
return e.Class
}
func (e Error) SQLErrorMessage() string {
return e.Message
}
func (e Error) SQLErrorServerName() string {
return e.ServerName
}
func (e Error) SQLErrorProcName() string {
return e.ProcName
}
func (e Error) SQLErrorLineNo() int32 {
return e.LineNo
}
type StreamError struct {
InnerError error
}
func (e StreamError) Error() string {
return "Invalid TDS stream: " + e.InnerError.Error()
}
func badStreamPanic(err error) {
panic(StreamError{InnerError: err})
}
func badStreamPanicf(format string, v ...interface{}) {
panic(fmt.Errorf(format, v...))
}
// ServerError is returned when the server got a fatal error
// that aborts the process and severs the connection.
//
// To get the errors returned before the process was aborted,
// unwrap this error or call errors.As with a pointer to an
// mssql.Error variable.
type ServerError struct {
sqlError Error
}
func (e ServerError) Error() string {
return "SQL Server had internal error"
}
func (e ServerError) Unwrap() error {
return e.sqlError
}
// RetryableError is returned when an error was caused by a bad
// connection at the start of a query and can be safely retried
// using database/sql's automatic retry logic.
//
// In many cases database/sql's retry logic will transparently
// handle this error, the retried call will return successfully,
// and you won't even see this error. However, you may see this
// error if the retry logic cannot successfully handle the error.
// In that case you can get the underlying error by calling this
// error's UnWrap function.
type RetryableError struct {
err error
}
func (r RetryableError) Error() string {
return r.err.Error()
}
func (r RetryableError) Unwrap() error {
return r.err
}
func (r RetryableError) Is(err error) bool {
return err == driver.ErrBadConn
}

@ -0,0 +1,78 @@
package mssql
import (
"context"
"errors"
"github.com/denisenkom/go-mssqldb/msdsn"
)
// Federated authentication library affects the login data structure and message sequence.
const (
// FedAuthLibraryLiveIDCompactToken specifies the Microsoft Live ID Compact Token authentication scheme
FedAuthLibraryLiveIDCompactToken = 0x00
// FedAuthLibrarySecurityToken specifies a token-based authentication where the token is available
// without additional information provided during the login sequence.
FedAuthLibrarySecurityToken = 0x01
// FedAuthLibraryADAL specifies a token-based authentication where a token is obtained during the
// login sequence using the server SPN and STS URL provided by the server during login.
FedAuthLibraryADAL = 0x02
// FedAuthLibraryReserved is used to indicate that no federated authentication scheme applies.
FedAuthLibraryReserved = 0x7F
)
// Federated authentication ADAL workflow affects the mechanism used to authenticate.
const (
// FedAuthADALWorkflowPassword uses a username/password to obtain a token from Active Directory
FedAuthADALWorkflowPassword = 0x01
// fedAuthADALWorkflowPassword uses the Windows identity to obtain a token from Active Directory
FedAuthADALWorkflowIntegrated = 0x02
// FedAuthADALWorkflowMSI uses the managed identity service to obtain a token
FedAuthADALWorkflowMSI = 0x03
)
// newSecurityTokenConnector creates a new connector from a Config and a token provider.
// When invoked, token provider implementations should contact the security token
// service specified and obtain the appropriate token, or return an error
// to indicate why a token is not available.
// The returned connector may be used with sql.OpenDB.
func NewSecurityTokenConnector(config msdsn.Config, tokenProvider func(ctx context.Context) (string, error)) (*Connector, error) {
if tokenProvider == nil {
return nil, errors.New("mssql: tokenProvider cannot be nil")
}
conn := NewConnectorConfig(config)
conn.fedAuthRequired = true
conn.fedAuthLibrary = FedAuthLibrarySecurityToken
conn.securityTokenProvider = tokenProvider
return conn, nil
}
// newADALTokenConnector creates a new connector from a Config and a Active Directory token provider.
// Token provider implementations are called during federated
// authentication login sequences where the server provides a service
// principal name and security token service endpoint that should be used
// to obtain the token. Implementations should contact the security token
// service specified and obtain the appropriate token, or return an error
// to indicate why a token is not available.
//
// The returned connector may be used with sql.OpenDB.
func NewActiveDirectoryTokenConnector(config msdsn.Config, adalWorkflow byte, tokenProvider func(ctx context.Context, serverSPN, stsURL string) (string, error)) (*Connector, error) {
if tokenProvider == nil {
return nil, errors.New("mssql: tokenProvider cannot be nil")
}
conn := NewConnectorConfig(config)
conn.fedAuthRequired = true
conn.fedAuthLibrary = FedAuthLibraryADAL
conn.fedAuthADALWorkflow = adalWorkflow
conn.adalTokenProvider = tokenProvider
return conn, nil
}

@ -0,0 +1,113 @@
package cp
type charsetMap struct {
sb [256]rune // single byte runes, -1 for a double byte character lead byte
db map[int]rune // double byte runes
}
func collation2charset(col Collation) *charsetMap {
// http://msdn.microsoft.com/en-us/library/ms144250.aspx
// http://msdn.microsoft.com/en-us/library/ms144250(v=sql.105).aspx
switch col.SortId {
case 30, 31, 32, 33, 34:
return cp437
case 40, 41, 42, 44, 49, 55, 56, 57, 58, 59, 60, 61:
return cp850
case 50, 51, 52, 53, 54, 71, 72, 73, 74, 75:
return cp1252
case 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96:
return cp1250
case 104, 105, 106, 107, 108:
return cp1251
case 112, 113, 114, 121, 124:
return cp1253
case 128, 129, 130:
return cp1254
case 136, 137, 138:
return cp1255
case 144, 145, 146:
return cp1256
case 152, 153, 154, 155, 156, 157, 158, 159, 160:
return cp1257
case 183, 184, 185, 186:
return cp1252
case 192, 193:
return cp932
case 194, 195:
return cp949
case 196, 197:
return cp950
case 198, 199:
return cp936
case 200:
return cp932
case 201:
return cp949
case 202:
return cp950
case 203:
return cp936
case 204, 205, 206:
return cp874
case 210, 211, 212, 213, 214, 215, 216, 217:
return cp1252
}
// http://technet.microsoft.com/en-us/library/aa176553(v=sql.80).aspx
switch col.getLcid() {
case 0x001e, 0x041e:
return cp874
case 0x0411, 0x10411:
return cp932
case 0x0804, 0x1004, 0x20804:
return cp936
case 0x0012, 0x0412:
return cp949
case 0x0404, 0x1404, 0x0c04, 0x7c04, 0x30404:
return cp950
case 0x041c, 0x041a, 0x0405, 0x040e, 0x104e, 0x0415, 0x0418, 0x041b, 0x0424, 0x1040e:
return cp1250
case 0x0423, 0x0402, 0x042f, 0x0419, 0x081a, 0x0c1a, 0x0422, 0x043f, 0x0444, 0x082c:
return cp1251
case 0x0408:
return cp1253
case 0x041f, 0x042c, 0x0443:
return cp1254
case 0x040d:
return cp1255
case 0x0401, 0x0801, 0xc01, 0x1001, 0x1401, 0x1801, 0x1c01, 0x2001, 0x2401, 0x2801, 0x2c01, 0x3001, 0x3401, 0x3801, 0x3c01, 0x4001, 0x0429, 0x0420:
return cp1256
case 0x0425, 0x0426, 0x0427, 0x0827:
return cp1257
case 0x042a:
return cp1258
case 0x0439, 0x045a, 0x0465:
return nil
}
return cp1252
}
func CharsetToUTF8(col Collation, s []byte) string {
cm := collation2charset(col)
if cm == nil {
return string(s)
}
buf := make([]rune, 0, len(s))
for i := 0; i < len(s); i++ {
ch := cm.sb[s[i]]
if ch == -1 {
if i+1 == len(s) {
ch = 0xfffd
} else {
n := int(s[i+1]) + (int(s[i]) << 8)
i++
var ok bool
ch, ok = cm.db[n]
if !ok {
ch = 0xfffd
}
}
}
buf = append(buf, ch)
}
return string(buf)
}

@ -0,0 +1,20 @@
package cp
// http://msdn.microsoft.com/en-us/library/dd340437.aspx
type Collation struct {
LcidAndFlags uint32
SortId uint8
}
func (c Collation) getLcid() uint32 {
return c.LcidAndFlags & 0x000fffff
}
func (c Collation) getFlags() uint32 {
return (c.LcidAndFlags & 0x0ff00000) >> 20
}
func (c Collation) getVersion() uint32 {
return (c.LcidAndFlags & 0xf0000000) >> 28
}

@ -0,0 +1,262 @@
package cp
var cp1250 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000A, //LINE FEED
0x000B, //VERTICAL TABULATION
0x000C, //FORM FEED
0x000D, //CARRIAGE RETURN
0x000E, //SHIFT OUT
0x000F, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001A, //SUBSTITUTE
0x001B, //ESCAPE
0x001C, //FILE SEPARATOR
0x001D, //GROUP SEPARATOR
0x001E, //RECORD SEPARATOR
0x001F, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002A, //ASTERISK
0x002B, //PLUS SIGN
0x002C, //COMMA
0x002D, //HYPHEN-MINUS
0x002E, //FULL STOP
0x002F, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003A, //COLON
0x003B, //SEMICOLON
0x003C, //LESS-THAN SIGN
0x003D, //EQUALS SIGN
0x003E, //GREATER-THAN SIGN
0x003F, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004A, //LATIN CAPITAL LETTER J
0x004B, //LATIN CAPITAL LETTER K
0x004C, //LATIN CAPITAL LETTER L
0x004D, //LATIN CAPITAL LETTER M
0x004E, //LATIN CAPITAL LETTER N
0x004F, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005A, //LATIN CAPITAL LETTER Z
0x005B, //LEFT SQUARE BRACKET
0x005C, //REVERSE SOLIDUS
0x005D, //RIGHT SQUARE BRACKET
0x005E, //CIRCUMFLEX ACCENT
0x005F, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006A, //LATIN SMALL LETTER J
0x006B, //LATIN SMALL LETTER K
0x006C, //LATIN SMALL LETTER L
0x006D, //LATIN SMALL LETTER M
0x006E, //LATIN SMALL LETTER N
0x006F, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007A, //LATIN SMALL LETTER Z
0x007B, //LEFT CURLY BRACKET
0x007C, //VERTICAL LINE
0x007D, //RIGHT CURLY BRACKET
0x007E, //TILDE
0x007F, //DELETE
0x20AC, //EURO SIGN
0xFFFD, //UNDEFINED
0x201A, //SINGLE LOW-9 QUOTATION MARK
0xFFFD, //UNDEFINED
0x201E, //DOUBLE LOW-9 QUOTATION MARK
0x2026, //HORIZONTAL ELLIPSIS
0x2020, //DAGGER
0x2021, //DOUBLE DAGGER
0xFFFD, //UNDEFINED
0x2030, //PER MILLE SIGN
0x0160, //LATIN CAPITAL LETTER S WITH CARON
0x2039, //SINGLE LEFT-POINTING ANGLE QUOTATION MARK
0x015A, //LATIN CAPITAL LETTER S WITH ACUTE
0x0164, //LATIN CAPITAL LETTER T WITH CARON
0x017D, //LATIN CAPITAL LETTER Z WITH CARON
0x0179, //LATIN CAPITAL LETTER Z WITH ACUTE
0xFFFD, //UNDEFINED
0x2018, //LEFT SINGLE QUOTATION MARK
0x2019, //RIGHT SINGLE QUOTATION MARK
0x201C, //LEFT DOUBLE QUOTATION MARK
0x201D, //RIGHT DOUBLE QUOTATION MARK
0x2022, //BULLET
0x2013, //EN DASH
0x2014, //EM DASH
0xFFFD, //UNDEFINED
0x2122, //TRADE MARK SIGN
0x0161, //LATIN SMALL LETTER S WITH CARON
0x203A, //SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
0x015B, //LATIN SMALL LETTER S WITH ACUTE
0x0165, //LATIN SMALL LETTER T WITH CARON
0x017E, //LATIN SMALL LETTER Z WITH CARON
0x017A, //LATIN SMALL LETTER Z WITH ACUTE
0x00A0, //NO-BREAK SPACE
0x02C7, //CARON
0x02D8, //BREVE
0x0141, //LATIN CAPITAL LETTER L WITH STROKE
0x00A4, //CURRENCY SIGN
0x0104, //LATIN CAPITAL LETTER A WITH OGONEK
0x00A6, //BROKEN BAR
0x00A7, //SECTION SIGN
0x00A8, //DIAERESIS
0x00A9, //COPYRIGHT SIGN
0x015E, //LATIN CAPITAL LETTER S WITH CEDILLA
0x00AB, //LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00AC, //NOT SIGN
0x00AD, //SOFT HYPHEN
0x00AE, //REGISTERED SIGN
0x017B, //LATIN CAPITAL LETTER Z WITH DOT ABOVE
0x00B0, //DEGREE SIGN
0x00B1, //PLUS-MINUS SIGN
0x02DB, //OGONEK
0x0142, //LATIN SMALL LETTER L WITH STROKE
0x00B4, //ACUTE ACCENT
0x00B5, //MICRO SIGN
0x00B6, //PILCROW SIGN
0x00B7, //MIDDLE DOT
0x00B8, //CEDILLA
0x0105, //LATIN SMALL LETTER A WITH OGONEK
0x015F, //LATIN SMALL LETTER S WITH CEDILLA
0x00BB, //RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x013D, //LATIN CAPITAL LETTER L WITH CARON
0x02DD, //DOUBLE ACUTE ACCENT
0x013E, //LATIN SMALL LETTER L WITH CARON
0x017C, //LATIN SMALL LETTER Z WITH DOT ABOVE
0x0154, //LATIN CAPITAL LETTER R WITH ACUTE
0x00C1, //LATIN CAPITAL LETTER A WITH ACUTE
0x00C2, //LATIN CAPITAL LETTER A WITH CIRCUMFLEX
0x0102, //LATIN CAPITAL LETTER A WITH BREVE
0x00C4, //LATIN CAPITAL LETTER A WITH DIAERESIS
0x0139, //LATIN CAPITAL LETTER L WITH ACUTE
0x0106, //LATIN CAPITAL LETTER C WITH ACUTE
0x00C7, //LATIN CAPITAL LETTER C WITH CEDILLA
0x010C, //LATIN CAPITAL LETTER C WITH CARON
0x00C9, //LATIN CAPITAL LETTER E WITH ACUTE
0x0118, //LATIN CAPITAL LETTER E WITH OGONEK
0x00CB, //LATIN CAPITAL LETTER E WITH DIAERESIS
0x011A, //LATIN CAPITAL LETTER E WITH CARON
0x00CD, //LATIN CAPITAL LETTER I WITH ACUTE
0x00CE, //LATIN CAPITAL LETTER I WITH CIRCUMFLEX
0x010E, //LATIN CAPITAL LETTER D WITH CARON
0x0110, //LATIN CAPITAL LETTER D WITH STROKE
0x0143, //LATIN CAPITAL LETTER N WITH ACUTE
0x0147, //LATIN CAPITAL LETTER N WITH CARON
0x00D3, //LATIN CAPITAL LETTER O WITH ACUTE
0x00D4, //LATIN CAPITAL LETTER O WITH CIRCUMFLEX
0x0150, //LATIN CAPITAL LETTER O WITH DOUBLE ACUTE
0x00D6, //LATIN CAPITAL LETTER O WITH DIAERESIS
0x00D7, //MULTIPLICATION SIGN
0x0158, //LATIN CAPITAL LETTER R WITH CARON
0x016E, //LATIN CAPITAL LETTER U WITH RING ABOVE
0x00DA, //LATIN CAPITAL LETTER U WITH ACUTE
0x0170, //LATIN CAPITAL LETTER U WITH DOUBLE ACUTE
0x00DC, //LATIN CAPITAL LETTER U WITH DIAERESIS
0x00DD, //LATIN CAPITAL LETTER Y WITH ACUTE
0x0162, //LATIN CAPITAL LETTER T WITH CEDILLA
0x00DF, //LATIN SMALL LETTER SHARP S
0x0155, //LATIN SMALL LETTER R WITH ACUTE
0x00E1, //LATIN SMALL LETTER A WITH ACUTE
0x00E2, //LATIN SMALL LETTER A WITH CIRCUMFLEX
0x0103, //LATIN SMALL LETTER A WITH BREVE
0x00E4, //LATIN SMALL LETTER A WITH DIAERESIS
0x013A, //LATIN SMALL LETTER L WITH ACUTE
0x0107, //LATIN SMALL LETTER C WITH ACUTE
0x00E7, //LATIN SMALL LETTER C WITH CEDILLA
0x010D, //LATIN SMALL LETTER C WITH CARON
0x00E9, //LATIN SMALL LETTER E WITH ACUTE
0x0119, //LATIN SMALL LETTER E WITH OGONEK
0x00EB, //LATIN SMALL LETTER E WITH DIAERESIS
0x011B, //LATIN SMALL LETTER E WITH CARON
0x00ED, //LATIN SMALL LETTER I WITH ACUTE
0x00EE, //LATIN SMALL LETTER I WITH CIRCUMFLEX
0x010F, //LATIN SMALL LETTER D WITH CARON
0x0111, //LATIN SMALL LETTER D WITH STROKE
0x0144, //LATIN SMALL LETTER N WITH ACUTE
0x0148, //LATIN SMALL LETTER N WITH CARON
0x00F3, //LATIN SMALL LETTER O WITH ACUTE
0x00F4, //LATIN SMALL LETTER O WITH CIRCUMFLEX
0x0151, //LATIN SMALL LETTER O WITH DOUBLE ACUTE
0x00F6, //LATIN SMALL LETTER O WITH DIAERESIS
0x00F7, //DIVISION SIGN
0x0159, //LATIN SMALL LETTER R WITH CARON
0x016F, //LATIN SMALL LETTER U WITH RING ABOVE
0x00FA, //LATIN SMALL LETTER U WITH ACUTE
0x0171, //LATIN SMALL LETTER U WITH DOUBLE ACUTE
0x00FC, //LATIN SMALL LETTER U WITH DIAERESIS
0x00FD, //LATIN SMALL LETTER Y WITH ACUTE
0x0163, //LATIN SMALL LETTER T WITH CEDILLA
0x02D9, //DOT ABOVE
},
}

@ -0,0 +1,262 @@
package cp
var cp1251 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000A, //LINE FEED
0x000B, //VERTICAL TABULATION
0x000C, //FORM FEED
0x000D, //CARRIAGE RETURN
0x000E, //SHIFT OUT
0x000F, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001A, //SUBSTITUTE
0x001B, //ESCAPE
0x001C, //FILE SEPARATOR
0x001D, //GROUP SEPARATOR
0x001E, //RECORD SEPARATOR
0x001F, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002A, //ASTERISK
0x002B, //PLUS SIGN
0x002C, //COMMA
0x002D, //HYPHEN-MINUS
0x002E, //FULL STOP
0x002F, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003A, //COLON
0x003B, //SEMICOLON
0x003C, //LESS-THAN SIGN
0x003D, //EQUALS SIGN
0x003E, //GREATER-THAN SIGN
0x003F, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004A, //LATIN CAPITAL LETTER J
0x004B, //LATIN CAPITAL LETTER K
0x004C, //LATIN CAPITAL LETTER L
0x004D, //LATIN CAPITAL LETTER M
0x004E, //LATIN CAPITAL LETTER N
0x004F, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005A, //LATIN CAPITAL LETTER Z
0x005B, //LEFT SQUARE BRACKET
0x005C, //REVERSE SOLIDUS
0x005D, //RIGHT SQUARE BRACKET
0x005E, //CIRCUMFLEX ACCENT
0x005F, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006A, //LATIN SMALL LETTER J
0x006B, //LATIN SMALL LETTER K
0x006C, //LATIN SMALL LETTER L
0x006D, //LATIN SMALL LETTER M
0x006E, //LATIN SMALL LETTER N
0x006F, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007A, //LATIN SMALL LETTER Z
0x007B, //LEFT CURLY BRACKET
0x007C, //VERTICAL LINE
0x007D, //RIGHT CURLY BRACKET
0x007E, //TILDE
0x007F, //DELETE
0x0402, //CYRILLIC CAPITAL LETTER DJE
0x0403, //CYRILLIC CAPITAL LETTER GJE
0x201A, //SINGLE LOW-9 QUOTATION MARK
0x0453, //CYRILLIC SMALL LETTER GJE
0x201E, //DOUBLE LOW-9 QUOTATION MARK
0x2026, //HORIZONTAL ELLIPSIS
0x2020, //DAGGER
0x2021, //DOUBLE DAGGER
0x20AC, //EURO SIGN
0x2030, //PER MILLE SIGN
0x0409, //CYRILLIC CAPITAL LETTER LJE
0x2039, //SINGLE LEFT-POINTING ANGLE QUOTATION MARK
0x040A, //CYRILLIC CAPITAL LETTER NJE
0x040C, //CYRILLIC CAPITAL LETTER KJE
0x040B, //CYRILLIC CAPITAL LETTER TSHE
0x040F, //CYRILLIC CAPITAL LETTER DZHE
0x0452, //CYRILLIC SMALL LETTER DJE
0x2018, //LEFT SINGLE QUOTATION MARK
0x2019, //RIGHT SINGLE QUOTATION MARK
0x201C, //LEFT DOUBLE QUOTATION MARK
0x201D, //RIGHT DOUBLE QUOTATION MARK
0x2022, //BULLET
0x2013, //EN DASH
0x2014, //EM DASH
0xFFFD, //UNDEFINED
0x2122, //TRADE MARK SIGN
0x0459, //CYRILLIC SMALL LETTER LJE
0x203A, //SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
0x045A, //CYRILLIC SMALL LETTER NJE
0x045C, //CYRILLIC SMALL LETTER KJE
0x045B, //CYRILLIC SMALL LETTER TSHE
0x045F, //CYRILLIC SMALL LETTER DZHE
0x00A0, //NO-BREAK SPACE
0x040E, //CYRILLIC CAPITAL LETTER SHORT U
0x045E, //CYRILLIC SMALL LETTER SHORT U
0x0408, //CYRILLIC CAPITAL LETTER JE
0x00A4, //CURRENCY SIGN
0x0490, //CYRILLIC CAPITAL LETTER GHE WITH UPTURN
0x00A6, //BROKEN BAR
0x00A7, //SECTION SIGN
0x0401, //CYRILLIC CAPITAL LETTER IO
0x00A9, //COPYRIGHT SIGN
0x0404, //CYRILLIC CAPITAL LETTER UKRAINIAN IE
0x00AB, //LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00AC, //NOT SIGN
0x00AD, //SOFT HYPHEN
0x00AE, //REGISTERED SIGN
0x0407, //CYRILLIC CAPITAL LETTER YI
0x00B0, //DEGREE SIGN
0x00B1, //PLUS-MINUS SIGN
0x0406, //CYRILLIC CAPITAL LETTER BYELORUSSIAN-UKRAINIAN I
0x0456, //CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I
0x0491, //CYRILLIC SMALL LETTER GHE WITH UPTURN
0x00B5, //MICRO SIGN
0x00B6, //PILCROW SIGN
0x00B7, //MIDDLE DOT
0x0451, //CYRILLIC SMALL LETTER IO
0x2116, //NUMERO SIGN
0x0454, //CYRILLIC SMALL LETTER UKRAINIAN IE
0x00BB, //RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x0458, //CYRILLIC SMALL LETTER JE
0x0405, //CYRILLIC CAPITAL LETTER DZE
0x0455, //CYRILLIC SMALL LETTER DZE
0x0457, //CYRILLIC SMALL LETTER YI
0x0410, //CYRILLIC CAPITAL LETTER A
0x0411, //CYRILLIC CAPITAL LETTER BE
0x0412, //CYRILLIC CAPITAL LETTER VE
0x0413, //CYRILLIC CAPITAL LETTER GHE
0x0414, //CYRILLIC CAPITAL LETTER DE
0x0415, //CYRILLIC CAPITAL LETTER IE
0x0416, //CYRILLIC CAPITAL LETTER ZHE
0x0417, //CYRILLIC CAPITAL LETTER ZE
0x0418, //CYRILLIC CAPITAL LETTER I
0x0419, //CYRILLIC CAPITAL LETTER SHORT I
0x041A, //CYRILLIC CAPITAL LETTER KA
0x041B, //CYRILLIC CAPITAL LETTER EL
0x041C, //CYRILLIC CAPITAL LETTER EM
0x041D, //CYRILLIC CAPITAL LETTER EN
0x041E, //CYRILLIC CAPITAL LETTER O
0x041F, //CYRILLIC CAPITAL LETTER PE
0x0420, //CYRILLIC CAPITAL LETTER ER
0x0421, //CYRILLIC CAPITAL LETTER ES
0x0422, //CYRILLIC CAPITAL LETTER TE
0x0423, //CYRILLIC CAPITAL LETTER U
0x0424, //CYRILLIC CAPITAL LETTER EF
0x0425, //CYRILLIC CAPITAL LETTER HA
0x0426, //CYRILLIC CAPITAL LETTER TSE
0x0427, //CYRILLIC CAPITAL LETTER CHE
0x0428, //CYRILLIC CAPITAL LETTER SHA
0x0429, //CYRILLIC CAPITAL LETTER SHCHA
0x042A, //CYRILLIC CAPITAL LETTER HARD SIGN
0x042B, //CYRILLIC CAPITAL LETTER YERU
0x042C, //CYRILLIC CAPITAL LETTER SOFT SIGN
0x042D, //CYRILLIC CAPITAL LETTER E
0x042E, //CYRILLIC CAPITAL LETTER YU
0x042F, //CYRILLIC CAPITAL LETTER YA
0x0430, //CYRILLIC SMALL LETTER A
0x0431, //CYRILLIC SMALL LETTER BE
0x0432, //CYRILLIC SMALL LETTER VE
0x0433, //CYRILLIC SMALL LETTER GHE
0x0434, //CYRILLIC SMALL LETTER DE
0x0435, //CYRILLIC SMALL LETTER IE
0x0436, //CYRILLIC SMALL LETTER ZHE
0x0437, //CYRILLIC SMALL LETTER ZE
0x0438, //CYRILLIC SMALL LETTER I
0x0439, //CYRILLIC SMALL LETTER SHORT I
0x043A, //CYRILLIC SMALL LETTER KA
0x043B, //CYRILLIC SMALL LETTER EL
0x043C, //CYRILLIC SMALL LETTER EM
0x043D, //CYRILLIC SMALL LETTER EN
0x043E, //CYRILLIC SMALL LETTER O
0x043F, //CYRILLIC SMALL LETTER PE
0x0440, //CYRILLIC SMALL LETTER ER
0x0441, //CYRILLIC SMALL LETTER ES
0x0442, //CYRILLIC SMALL LETTER TE
0x0443, //CYRILLIC SMALL LETTER U
0x0444, //CYRILLIC SMALL LETTER EF
0x0445, //CYRILLIC SMALL LETTER HA
0x0446, //CYRILLIC SMALL LETTER TSE
0x0447, //CYRILLIC SMALL LETTER CHE
0x0448, //CYRILLIC SMALL LETTER SHA
0x0449, //CYRILLIC SMALL LETTER SHCHA
0x044A, //CYRILLIC SMALL LETTER HARD SIGN
0x044B, //CYRILLIC SMALL LETTER YERU
0x044C, //CYRILLIC SMALL LETTER SOFT SIGN
0x044D, //CYRILLIC SMALL LETTER E
0x044E, //CYRILLIC SMALL LETTER YU
0x044F, //CYRILLIC SMALL LETTER YA
},
}

@ -0,0 +1,262 @@
package cp
var cp1252 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000A, //LINE FEED
0x000B, //VERTICAL TABULATION
0x000C, //FORM FEED
0x000D, //CARRIAGE RETURN
0x000E, //SHIFT OUT
0x000F, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001A, //SUBSTITUTE
0x001B, //ESCAPE
0x001C, //FILE SEPARATOR
0x001D, //GROUP SEPARATOR
0x001E, //RECORD SEPARATOR
0x001F, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002A, //ASTERISK
0x002B, //PLUS SIGN
0x002C, //COMMA
0x002D, //HYPHEN-MINUS
0x002E, //FULL STOP
0x002F, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003A, //COLON
0x003B, //SEMICOLON
0x003C, //LESS-THAN SIGN
0x003D, //EQUALS SIGN
0x003E, //GREATER-THAN SIGN
0x003F, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004A, //LATIN CAPITAL LETTER J
0x004B, //LATIN CAPITAL LETTER K
0x004C, //LATIN CAPITAL LETTER L
0x004D, //LATIN CAPITAL LETTER M
0x004E, //LATIN CAPITAL LETTER N
0x004F, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005A, //LATIN CAPITAL LETTER Z
0x005B, //LEFT SQUARE BRACKET
0x005C, //REVERSE SOLIDUS
0x005D, //RIGHT SQUARE BRACKET
0x005E, //CIRCUMFLEX ACCENT
0x005F, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006A, //LATIN SMALL LETTER J
0x006B, //LATIN SMALL LETTER K
0x006C, //LATIN SMALL LETTER L
0x006D, //LATIN SMALL LETTER M
0x006E, //LATIN SMALL LETTER N
0x006F, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007A, //LATIN SMALL LETTER Z
0x007B, //LEFT CURLY BRACKET
0x007C, //VERTICAL LINE
0x007D, //RIGHT CURLY BRACKET
0x007E, //TILDE
0x007F, //DELETE
0x20AC, //EURO SIGN
0xFFFD, //UNDEFINED
0x201A, //SINGLE LOW-9 QUOTATION MARK
0x0192, //LATIN SMALL LETTER F WITH HOOK
0x201E, //DOUBLE LOW-9 QUOTATION MARK
0x2026, //HORIZONTAL ELLIPSIS
0x2020, //DAGGER
0x2021, //DOUBLE DAGGER
0x02C6, //MODIFIER LETTER CIRCUMFLEX ACCENT
0x2030, //PER MILLE SIGN
0x0160, //LATIN CAPITAL LETTER S WITH CARON
0x2039, //SINGLE LEFT-POINTING ANGLE QUOTATION MARK
0x0152, //LATIN CAPITAL LIGATURE OE
0xFFFD, //UNDEFINED
0x017D, //LATIN CAPITAL LETTER Z WITH CARON
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x2018, //LEFT SINGLE QUOTATION MARK
0x2019, //RIGHT SINGLE QUOTATION MARK
0x201C, //LEFT DOUBLE QUOTATION MARK
0x201D, //RIGHT DOUBLE QUOTATION MARK
0x2022, //BULLET
0x2013, //EN DASH
0x2014, //EM DASH
0x02DC, //SMALL TILDE
0x2122, //TRADE MARK SIGN
0x0161, //LATIN SMALL LETTER S WITH CARON
0x203A, //SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
0x0153, //LATIN SMALL LIGATURE OE
0xFFFD, //UNDEFINED
0x017E, //LATIN SMALL LETTER Z WITH CARON
0x0178, //LATIN CAPITAL LETTER Y WITH DIAERESIS
0x00A0, //NO-BREAK SPACE
0x00A1, //INVERTED EXCLAMATION MARK
0x00A2, //CENT SIGN
0x00A3, //POUND SIGN
0x00A4, //CURRENCY SIGN
0x00A5, //YEN SIGN
0x00A6, //BROKEN BAR
0x00A7, //SECTION SIGN
0x00A8, //DIAERESIS
0x00A9, //COPYRIGHT SIGN
0x00AA, //FEMININE ORDINAL INDICATOR
0x00AB, //LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00AC, //NOT SIGN
0x00AD, //SOFT HYPHEN
0x00AE, //REGISTERED SIGN
0x00AF, //MACRON
0x00B0, //DEGREE SIGN
0x00B1, //PLUS-MINUS SIGN
0x00B2, //SUPERSCRIPT TWO
0x00B3, //SUPERSCRIPT THREE
0x00B4, //ACUTE ACCENT
0x00B5, //MICRO SIGN
0x00B6, //PILCROW SIGN
0x00B7, //MIDDLE DOT
0x00B8, //CEDILLA
0x00B9, //SUPERSCRIPT ONE
0x00BA, //MASCULINE ORDINAL INDICATOR
0x00BB, //RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00BC, //VULGAR FRACTION ONE QUARTER
0x00BD, //VULGAR FRACTION ONE HALF
0x00BE, //VULGAR FRACTION THREE QUARTERS
0x00BF, //INVERTED QUESTION MARK
0x00C0, //LATIN CAPITAL LETTER A WITH GRAVE
0x00C1, //LATIN CAPITAL LETTER A WITH ACUTE
0x00C2, //LATIN CAPITAL LETTER A WITH CIRCUMFLEX
0x00C3, //LATIN CAPITAL LETTER A WITH TILDE
0x00C4, //LATIN CAPITAL LETTER A WITH DIAERESIS
0x00C5, //LATIN CAPITAL LETTER A WITH RING ABOVE
0x00C6, //LATIN CAPITAL LETTER AE
0x00C7, //LATIN CAPITAL LETTER C WITH CEDILLA
0x00C8, //LATIN CAPITAL LETTER E WITH GRAVE
0x00C9, //LATIN CAPITAL LETTER E WITH ACUTE
0x00CA, //LATIN CAPITAL LETTER E WITH CIRCUMFLEX
0x00CB, //LATIN CAPITAL LETTER E WITH DIAERESIS
0x00CC, //LATIN CAPITAL LETTER I WITH GRAVE
0x00CD, //LATIN CAPITAL LETTER I WITH ACUTE
0x00CE, //LATIN CAPITAL LETTER I WITH CIRCUMFLEX
0x00CF, //LATIN CAPITAL LETTER I WITH DIAERESIS
0x00D0, //LATIN CAPITAL LETTER ETH
0x00D1, //LATIN CAPITAL LETTER N WITH TILDE
0x00D2, //LATIN CAPITAL LETTER O WITH GRAVE
0x00D3, //LATIN CAPITAL LETTER O WITH ACUTE
0x00D4, //LATIN CAPITAL LETTER O WITH CIRCUMFLEX
0x00D5, //LATIN CAPITAL LETTER O WITH TILDE
0x00D6, //LATIN CAPITAL LETTER O WITH DIAERESIS
0x00D7, //MULTIPLICATION SIGN
0x00D8, //LATIN CAPITAL LETTER O WITH STROKE
0x00D9, //LATIN CAPITAL LETTER U WITH GRAVE
0x00DA, //LATIN CAPITAL LETTER U WITH ACUTE
0x00DB, //LATIN CAPITAL LETTER U WITH CIRCUMFLEX
0x00DC, //LATIN CAPITAL LETTER U WITH DIAERESIS
0x00DD, //LATIN CAPITAL LETTER Y WITH ACUTE
0x00DE, //LATIN CAPITAL LETTER THORN
0x00DF, //LATIN SMALL LETTER SHARP S
0x00E0, //LATIN SMALL LETTER A WITH GRAVE
0x00E1, //LATIN SMALL LETTER A WITH ACUTE
0x00E2, //LATIN SMALL LETTER A WITH CIRCUMFLEX
0x00E3, //LATIN SMALL LETTER A WITH TILDE
0x00E4, //LATIN SMALL LETTER A WITH DIAERESIS
0x00E5, //LATIN SMALL LETTER A WITH RING ABOVE
0x00E6, //LATIN SMALL LETTER AE
0x00E7, //LATIN SMALL LETTER C WITH CEDILLA
0x00E8, //LATIN SMALL LETTER E WITH GRAVE
0x00E9, //LATIN SMALL LETTER E WITH ACUTE
0x00EA, //LATIN SMALL LETTER E WITH CIRCUMFLEX
0x00EB, //LATIN SMALL LETTER E WITH DIAERESIS
0x00EC, //LATIN SMALL LETTER I WITH GRAVE
0x00ED, //LATIN SMALL LETTER I WITH ACUTE
0x00EE, //LATIN SMALL LETTER I WITH CIRCUMFLEX
0x00EF, //LATIN SMALL LETTER I WITH DIAERESIS
0x00F0, //LATIN SMALL LETTER ETH
0x00F1, //LATIN SMALL LETTER N WITH TILDE
0x00F2, //LATIN SMALL LETTER O WITH GRAVE
0x00F3, //LATIN SMALL LETTER O WITH ACUTE
0x00F4, //LATIN SMALL LETTER O WITH CIRCUMFLEX
0x00F5, //LATIN SMALL LETTER O WITH TILDE
0x00F6, //LATIN SMALL LETTER O WITH DIAERESIS
0x00F7, //DIVISION SIGN
0x00F8, //LATIN SMALL LETTER O WITH STROKE
0x00F9, //LATIN SMALL LETTER U WITH GRAVE
0x00FA, //LATIN SMALL LETTER U WITH ACUTE
0x00FB, //LATIN SMALL LETTER U WITH CIRCUMFLEX
0x00FC, //LATIN SMALL LETTER U WITH DIAERESIS
0x00FD, //LATIN SMALL LETTER Y WITH ACUTE
0x00FE, //LATIN SMALL LETTER THORN
0x00FF, //LATIN SMALL LETTER Y WITH DIAERESIS
},
}

@ -0,0 +1,262 @@
package cp
var cp1253 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000A, //LINE FEED
0x000B, //VERTICAL TABULATION
0x000C, //FORM FEED
0x000D, //CARRIAGE RETURN
0x000E, //SHIFT OUT
0x000F, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001A, //SUBSTITUTE
0x001B, //ESCAPE
0x001C, //FILE SEPARATOR
0x001D, //GROUP SEPARATOR
0x001E, //RECORD SEPARATOR
0x001F, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002A, //ASTERISK
0x002B, //PLUS SIGN
0x002C, //COMMA
0x002D, //HYPHEN-MINUS
0x002E, //FULL STOP
0x002F, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003A, //COLON
0x003B, //SEMICOLON
0x003C, //LESS-THAN SIGN
0x003D, //EQUALS SIGN
0x003E, //GREATER-THAN SIGN
0x003F, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004A, //LATIN CAPITAL LETTER J
0x004B, //LATIN CAPITAL LETTER K
0x004C, //LATIN CAPITAL LETTER L
0x004D, //LATIN CAPITAL LETTER M
0x004E, //LATIN CAPITAL LETTER N
0x004F, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005A, //LATIN CAPITAL LETTER Z
0x005B, //LEFT SQUARE BRACKET
0x005C, //REVERSE SOLIDUS
0x005D, //RIGHT SQUARE BRACKET
0x005E, //CIRCUMFLEX ACCENT
0x005F, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006A, //LATIN SMALL LETTER J
0x006B, //LATIN SMALL LETTER K
0x006C, //LATIN SMALL LETTER L
0x006D, //LATIN SMALL LETTER M
0x006E, //LATIN SMALL LETTER N
0x006F, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007A, //LATIN SMALL LETTER Z
0x007B, //LEFT CURLY BRACKET
0x007C, //VERTICAL LINE
0x007D, //RIGHT CURLY BRACKET
0x007E, //TILDE
0x007F, //DELETE
0x20AC, //EURO SIGN
0xFFFD, //UNDEFINED
0x201A, //SINGLE LOW-9 QUOTATION MARK
0x0192, //LATIN SMALL LETTER F WITH HOOK
0x201E, //DOUBLE LOW-9 QUOTATION MARK
0x2026, //HORIZONTAL ELLIPSIS
0x2020, //DAGGER
0x2021, //DOUBLE DAGGER
0xFFFD, //UNDEFINED
0x2030, //PER MILLE SIGN
0xFFFD, //UNDEFINED
0x2039, //SINGLE LEFT-POINTING ANGLE QUOTATION MARK
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x2018, //LEFT SINGLE QUOTATION MARK
0x2019, //RIGHT SINGLE QUOTATION MARK
0x201C, //LEFT DOUBLE QUOTATION MARK
0x201D, //RIGHT DOUBLE QUOTATION MARK
0x2022, //BULLET
0x2013, //EN DASH
0x2014, //EM DASH
0xFFFD, //UNDEFINED
0x2122, //TRADE MARK SIGN
0xFFFD, //UNDEFINED
0x203A, //SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x00A0, //NO-BREAK SPACE
0x0385, //GREEK DIALYTIKA TONOS
0x0386, //GREEK CAPITAL LETTER ALPHA WITH TONOS
0x00A3, //POUND SIGN
0x00A4, //CURRENCY SIGN
0x00A5, //YEN SIGN
0x00A6, //BROKEN BAR
0x00A7, //SECTION SIGN
0x00A8, //DIAERESIS
0x00A9, //COPYRIGHT SIGN
0xFFFD, //UNDEFINED
0x00AB, //LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00AC, //NOT SIGN
0x00AD, //SOFT HYPHEN
0x00AE, //REGISTERED SIGN
0x2015, //HORIZONTAL BAR
0x00B0, //DEGREE SIGN
0x00B1, //PLUS-MINUS SIGN
0x00B2, //SUPERSCRIPT TWO
0x00B3, //SUPERSCRIPT THREE
0x0384, //GREEK TONOS
0x00B5, //MICRO SIGN
0x00B6, //PILCROW SIGN
0x00B7, //MIDDLE DOT
0x0388, //GREEK CAPITAL LETTER EPSILON WITH TONOS
0x0389, //GREEK CAPITAL LETTER ETA WITH TONOS
0x038A, //GREEK CAPITAL LETTER IOTA WITH TONOS
0x00BB, //RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x038C, //GREEK CAPITAL LETTER OMICRON WITH TONOS
0x00BD, //VULGAR FRACTION ONE HALF
0x038E, //GREEK CAPITAL LETTER UPSILON WITH TONOS
0x038F, //GREEK CAPITAL LETTER OMEGA WITH TONOS
0x0390, //GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
0x0391, //GREEK CAPITAL LETTER ALPHA
0x0392, //GREEK CAPITAL LETTER BETA
0x0393, //GREEK CAPITAL LETTER GAMMA
0x0394, //GREEK CAPITAL LETTER DELTA
0x0395, //GREEK CAPITAL LETTER EPSILON
0x0396, //GREEK CAPITAL LETTER ZETA
0x0397, //GREEK CAPITAL LETTER ETA
0x0398, //GREEK CAPITAL LETTER THETA
0x0399, //GREEK CAPITAL LETTER IOTA
0x039A, //GREEK CAPITAL LETTER KAPPA
0x039B, //GREEK CAPITAL LETTER LAMDA
0x039C, //GREEK CAPITAL LETTER MU
0x039D, //GREEK CAPITAL LETTER NU
0x039E, //GREEK CAPITAL LETTER XI
0x039F, //GREEK CAPITAL LETTER OMICRON
0x03A0, //GREEK CAPITAL LETTER PI
0x03A1, //GREEK CAPITAL LETTER RHO
0xFFFD, //UNDEFINED
0x03A3, //GREEK CAPITAL LETTER SIGMA
0x03A4, //GREEK CAPITAL LETTER TAU
0x03A5, //GREEK CAPITAL LETTER UPSILON
0x03A6, //GREEK CAPITAL LETTER PHI
0x03A7, //GREEK CAPITAL LETTER CHI
0x03A8, //GREEK CAPITAL LETTER PSI
0x03A9, //GREEK CAPITAL LETTER OMEGA
0x03AA, //GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
0x03AB, //GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA
0x03AC, //GREEK SMALL LETTER ALPHA WITH TONOS
0x03AD, //GREEK SMALL LETTER EPSILON WITH TONOS
0x03AE, //GREEK SMALL LETTER ETA WITH TONOS
0x03AF, //GREEK SMALL LETTER IOTA WITH TONOS
0x03B0, //GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
0x03B1, //GREEK SMALL LETTER ALPHA
0x03B2, //GREEK SMALL LETTER BETA
0x03B3, //GREEK SMALL LETTER GAMMA
0x03B4, //GREEK SMALL LETTER DELTA
0x03B5, //GREEK SMALL LETTER EPSILON
0x03B6, //GREEK SMALL LETTER ZETA
0x03B7, //GREEK SMALL LETTER ETA
0x03B8, //GREEK SMALL LETTER THETA
0x03B9, //GREEK SMALL LETTER IOTA
0x03BA, //GREEK SMALL LETTER KAPPA
0x03BB, //GREEK SMALL LETTER LAMDA
0x03BC, //GREEK SMALL LETTER MU
0x03BD, //GREEK SMALL LETTER NU
0x03BE, //GREEK SMALL LETTER XI
0x03BF, //GREEK SMALL LETTER OMICRON
0x03C0, //GREEK SMALL LETTER PI
0x03C1, //GREEK SMALL LETTER RHO
0x03C2, //GREEK SMALL LETTER FINAL SIGMA
0x03C3, //GREEK SMALL LETTER SIGMA
0x03C4, //GREEK SMALL LETTER TAU
0x03C5, //GREEK SMALL LETTER UPSILON
0x03C6, //GREEK SMALL LETTER PHI
0x03C7, //GREEK SMALL LETTER CHI
0x03C8, //GREEK SMALL LETTER PSI
0x03C9, //GREEK SMALL LETTER OMEGA
0x03CA, //GREEK SMALL LETTER IOTA WITH DIALYTIKA
0x03CB, //GREEK SMALL LETTER UPSILON WITH DIALYTIKA
0x03CC, //GREEK SMALL LETTER OMICRON WITH TONOS
0x03CD, //GREEK SMALL LETTER UPSILON WITH TONOS
0x03CE, //GREEK SMALL LETTER OMEGA WITH TONOS
0xFFFD, //UNDEFINED
},
}

@ -0,0 +1,262 @@
package cp
var cp1254 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000A, //LINE FEED
0x000B, //VERTICAL TABULATION
0x000C, //FORM FEED
0x000D, //CARRIAGE RETURN
0x000E, //SHIFT OUT
0x000F, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001A, //SUBSTITUTE
0x001B, //ESCAPE
0x001C, //FILE SEPARATOR
0x001D, //GROUP SEPARATOR
0x001E, //RECORD SEPARATOR
0x001F, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002A, //ASTERISK
0x002B, //PLUS SIGN
0x002C, //COMMA
0x002D, //HYPHEN-MINUS
0x002E, //FULL STOP
0x002F, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003A, //COLON
0x003B, //SEMICOLON
0x003C, //LESS-THAN SIGN
0x003D, //EQUALS SIGN
0x003E, //GREATER-THAN SIGN
0x003F, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004A, //LATIN CAPITAL LETTER J
0x004B, //LATIN CAPITAL LETTER K
0x004C, //LATIN CAPITAL LETTER L
0x004D, //LATIN CAPITAL LETTER M
0x004E, //LATIN CAPITAL LETTER N
0x004F, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005A, //LATIN CAPITAL LETTER Z
0x005B, //LEFT SQUARE BRACKET
0x005C, //REVERSE SOLIDUS
0x005D, //RIGHT SQUARE BRACKET
0x005E, //CIRCUMFLEX ACCENT
0x005F, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006A, //LATIN SMALL LETTER J
0x006B, //LATIN SMALL LETTER K
0x006C, //LATIN SMALL LETTER L
0x006D, //LATIN SMALL LETTER M
0x006E, //LATIN SMALL LETTER N
0x006F, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007A, //LATIN SMALL LETTER Z
0x007B, //LEFT CURLY BRACKET
0x007C, //VERTICAL LINE
0x007D, //RIGHT CURLY BRACKET
0x007E, //TILDE
0x007F, //DELETE
0x20AC, //EURO SIGN
0xFFFD, //UNDEFINED
0x201A, //SINGLE LOW-9 QUOTATION MARK
0x0192, //LATIN SMALL LETTER F WITH HOOK
0x201E, //DOUBLE LOW-9 QUOTATION MARK
0x2026, //HORIZONTAL ELLIPSIS
0x2020, //DAGGER
0x2021, //DOUBLE DAGGER
0x02C6, //MODIFIER LETTER CIRCUMFLEX ACCENT
0x2030, //PER MILLE SIGN
0x0160, //LATIN CAPITAL LETTER S WITH CARON
0x2039, //SINGLE LEFT-POINTING ANGLE QUOTATION MARK
0x0152, //LATIN CAPITAL LIGATURE OE
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x2018, //LEFT SINGLE QUOTATION MARK
0x2019, //RIGHT SINGLE QUOTATION MARK
0x201C, //LEFT DOUBLE QUOTATION MARK
0x201D, //RIGHT DOUBLE QUOTATION MARK
0x2022, //BULLET
0x2013, //EN DASH
0x2014, //EM DASH
0x02DC, //SMALL TILDE
0x2122, //TRADE MARK SIGN
0x0161, //LATIN SMALL LETTER S WITH CARON
0x203A, //SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
0x0153, //LATIN SMALL LIGATURE OE
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x0178, //LATIN CAPITAL LETTER Y WITH DIAERESIS
0x00A0, //NO-BREAK SPACE
0x00A1, //INVERTED EXCLAMATION MARK
0x00A2, //CENT SIGN
0x00A3, //POUND SIGN
0x00A4, //CURRENCY SIGN
0x00A5, //YEN SIGN
0x00A6, //BROKEN BAR
0x00A7, //SECTION SIGN
0x00A8, //DIAERESIS
0x00A9, //COPYRIGHT SIGN
0x00AA, //FEMININE ORDINAL INDICATOR
0x00AB, //LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00AC, //NOT SIGN
0x00AD, //SOFT HYPHEN
0x00AE, //REGISTERED SIGN
0x00AF, //MACRON
0x00B0, //DEGREE SIGN
0x00B1, //PLUS-MINUS SIGN
0x00B2, //SUPERSCRIPT TWO
0x00B3, //SUPERSCRIPT THREE
0x00B4, //ACUTE ACCENT
0x00B5, //MICRO SIGN
0x00B6, //PILCROW SIGN
0x00B7, //MIDDLE DOT
0x00B8, //CEDILLA
0x00B9, //SUPERSCRIPT ONE
0x00BA, //MASCULINE ORDINAL INDICATOR
0x00BB, //RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00BC, //VULGAR FRACTION ONE QUARTER
0x00BD, //VULGAR FRACTION ONE HALF
0x00BE, //VULGAR FRACTION THREE QUARTERS
0x00BF, //INVERTED QUESTION MARK
0x00C0, //LATIN CAPITAL LETTER A WITH GRAVE
0x00C1, //LATIN CAPITAL LETTER A WITH ACUTE
0x00C2, //LATIN CAPITAL LETTER A WITH CIRCUMFLEX
0x00C3, //LATIN CAPITAL LETTER A WITH TILDE
0x00C4, //LATIN CAPITAL LETTER A WITH DIAERESIS
0x00C5, //LATIN CAPITAL LETTER A WITH RING ABOVE
0x00C6, //LATIN CAPITAL LETTER AE
0x00C7, //LATIN CAPITAL LETTER C WITH CEDILLA
0x00C8, //LATIN CAPITAL LETTER E WITH GRAVE
0x00C9, //LATIN CAPITAL LETTER E WITH ACUTE
0x00CA, //LATIN CAPITAL LETTER E WITH CIRCUMFLEX
0x00CB, //LATIN CAPITAL LETTER E WITH DIAERESIS
0x00CC, //LATIN CAPITAL LETTER I WITH GRAVE
0x00CD, //LATIN CAPITAL LETTER I WITH ACUTE
0x00CE, //LATIN CAPITAL LETTER I WITH CIRCUMFLEX
0x00CF, //LATIN CAPITAL LETTER I WITH DIAERESIS
0x011E, //LATIN CAPITAL LETTER G WITH BREVE
0x00D1, //LATIN CAPITAL LETTER N WITH TILDE
0x00D2, //LATIN CAPITAL LETTER O WITH GRAVE
0x00D3, //LATIN CAPITAL LETTER O WITH ACUTE
0x00D4, //LATIN CAPITAL LETTER O WITH CIRCUMFLEX
0x00D5, //LATIN CAPITAL LETTER O WITH TILDE
0x00D6, //LATIN CAPITAL LETTER O WITH DIAERESIS
0x00D7, //MULTIPLICATION SIGN
0x00D8, //LATIN CAPITAL LETTER O WITH STROKE
0x00D9, //LATIN CAPITAL LETTER U WITH GRAVE
0x00DA, //LATIN CAPITAL LETTER U WITH ACUTE
0x00DB, //LATIN CAPITAL LETTER U WITH CIRCUMFLEX
0x00DC, //LATIN CAPITAL LETTER U WITH DIAERESIS
0x0130, //LATIN CAPITAL LETTER I WITH DOT ABOVE
0x015E, //LATIN CAPITAL LETTER S WITH CEDILLA
0x00DF, //LATIN SMALL LETTER SHARP S
0x00E0, //LATIN SMALL LETTER A WITH GRAVE
0x00E1, //LATIN SMALL LETTER A WITH ACUTE
0x00E2, //LATIN SMALL LETTER A WITH CIRCUMFLEX
0x00E3, //LATIN SMALL LETTER A WITH TILDE
0x00E4, //LATIN SMALL LETTER A WITH DIAERESIS
0x00E5, //LATIN SMALL LETTER A WITH RING ABOVE
0x00E6, //LATIN SMALL LETTER AE
0x00E7, //LATIN SMALL LETTER C WITH CEDILLA
0x00E8, //LATIN SMALL LETTER E WITH GRAVE
0x00E9, //LATIN SMALL LETTER E WITH ACUTE
0x00EA, //LATIN SMALL LETTER E WITH CIRCUMFLEX
0x00EB, //LATIN SMALL LETTER E WITH DIAERESIS
0x00EC, //LATIN SMALL LETTER I WITH GRAVE
0x00ED, //LATIN SMALL LETTER I WITH ACUTE
0x00EE, //LATIN SMALL LETTER I WITH CIRCUMFLEX
0x00EF, //LATIN SMALL LETTER I WITH DIAERESIS
0x011F, //LATIN SMALL LETTER G WITH BREVE
0x00F1, //LATIN SMALL LETTER N WITH TILDE
0x00F2, //LATIN SMALL LETTER O WITH GRAVE
0x00F3, //LATIN SMALL LETTER O WITH ACUTE
0x00F4, //LATIN SMALL LETTER O WITH CIRCUMFLEX
0x00F5, //LATIN SMALL LETTER O WITH TILDE
0x00F6, //LATIN SMALL LETTER O WITH DIAERESIS
0x00F7, //DIVISION SIGN
0x00F8, //LATIN SMALL LETTER O WITH STROKE
0x00F9, //LATIN SMALL LETTER U WITH GRAVE
0x00FA, //LATIN SMALL LETTER U WITH ACUTE
0x00FB, //LATIN SMALL LETTER U WITH CIRCUMFLEX
0x00FC, //LATIN SMALL LETTER U WITH DIAERESIS
0x0131, //LATIN SMALL LETTER DOTLESS I
0x015F, //LATIN SMALL LETTER S WITH CEDILLA
0x00FF, //LATIN SMALL LETTER Y WITH DIAERESIS
},
}

@ -0,0 +1,262 @@
package cp
var cp1255 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000A, //LINE FEED
0x000B, //VERTICAL TABULATION
0x000C, //FORM FEED
0x000D, //CARRIAGE RETURN
0x000E, //SHIFT OUT
0x000F, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001A, //SUBSTITUTE
0x001B, //ESCAPE
0x001C, //FILE SEPARATOR
0x001D, //GROUP SEPARATOR
0x001E, //RECORD SEPARATOR
0x001F, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002A, //ASTERISK
0x002B, //PLUS SIGN
0x002C, //COMMA
0x002D, //HYPHEN-MINUS
0x002E, //FULL STOP
0x002F, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003A, //COLON
0x003B, //SEMICOLON
0x003C, //LESS-THAN SIGN
0x003D, //EQUALS SIGN
0x003E, //GREATER-THAN SIGN
0x003F, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004A, //LATIN CAPITAL LETTER J
0x004B, //LATIN CAPITAL LETTER K
0x004C, //LATIN CAPITAL LETTER L
0x004D, //LATIN CAPITAL LETTER M
0x004E, //LATIN CAPITAL LETTER N
0x004F, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005A, //LATIN CAPITAL LETTER Z
0x005B, //LEFT SQUARE BRACKET
0x005C, //REVERSE SOLIDUS
0x005D, //RIGHT SQUARE BRACKET
0x005E, //CIRCUMFLEX ACCENT
0x005F, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006A, //LATIN SMALL LETTER J
0x006B, //LATIN SMALL LETTER K
0x006C, //LATIN SMALL LETTER L
0x006D, //LATIN SMALL LETTER M
0x006E, //LATIN SMALL LETTER N
0x006F, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007A, //LATIN SMALL LETTER Z
0x007B, //LEFT CURLY BRACKET
0x007C, //VERTICAL LINE
0x007D, //RIGHT CURLY BRACKET
0x007E, //TILDE
0x007F, //DELETE
0x20AC, //EURO SIGN
0xFFFD, //UNDEFINED
0x201A, //SINGLE LOW-9 QUOTATION MARK
0x0192, //LATIN SMALL LETTER F WITH HOOK
0x201E, //DOUBLE LOW-9 QUOTATION MARK
0x2026, //HORIZONTAL ELLIPSIS
0x2020, //DAGGER
0x2021, //DOUBLE DAGGER
0x02C6, //MODIFIER LETTER CIRCUMFLEX ACCENT
0x2030, //PER MILLE SIGN
0xFFFD, //UNDEFINED
0x2039, //SINGLE LEFT-POINTING ANGLE QUOTATION MARK
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x2018, //LEFT SINGLE QUOTATION MARK
0x2019, //RIGHT SINGLE QUOTATION MARK
0x201C, //LEFT DOUBLE QUOTATION MARK
0x201D, //RIGHT DOUBLE QUOTATION MARK
0x2022, //BULLET
0x2013, //EN DASH
0x2014, //EM DASH
0x02DC, //SMALL TILDE
0x2122, //TRADE MARK SIGN
0xFFFD, //UNDEFINED
0x203A, //SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x00A0, //NO-BREAK SPACE
0x00A1, //INVERTED EXCLAMATION MARK
0x00A2, //CENT SIGN
0x00A3, //POUND SIGN
0x20AA, //NEW SHEQEL SIGN
0x00A5, //YEN SIGN
0x00A6, //BROKEN BAR
0x00A7, //SECTION SIGN
0x00A8, //DIAERESIS
0x00A9, //COPYRIGHT SIGN
0x00D7, //MULTIPLICATION SIGN
0x00AB, //LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00AC, //NOT SIGN
0x00AD, //SOFT HYPHEN
0x00AE, //REGISTERED SIGN
0x00AF, //MACRON
0x00B0, //DEGREE SIGN
0x00B1, //PLUS-MINUS SIGN
0x00B2, //SUPERSCRIPT TWO
0x00B3, //SUPERSCRIPT THREE
0x00B4, //ACUTE ACCENT
0x00B5, //MICRO SIGN
0x00B6, //PILCROW SIGN
0x00B7, //MIDDLE DOT
0x00B8, //CEDILLA
0x00B9, //SUPERSCRIPT ONE
0x00F7, //DIVISION SIGN
0x00BB, //RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00BC, //VULGAR FRACTION ONE QUARTER
0x00BD, //VULGAR FRACTION ONE HALF
0x00BE, //VULGAR FRACTION THREE QUARTERS
0x00BF, //INVERTED QUESTION MARK
0x05B0, //HEBREW POINT SHEVA
0x05B1, //HEBREW POINT HATAF SEGOL
0x05B2, //HEBREW POINT HATAF PATAH
0x05B3, //HEBREW POINT HATAF QAMATS
0x05B4, //HEBREW POINT HIRIQ
0x05B5, //HEBREW POINT TSERE
0x05B6, //HEBREW POINT SEGOL
0x05B7, //HEBREW POINT PATAH
0x05B8, //HEBREW POINT QAMATS
0x05B9, //HEBREW POINT HOLAM
0xFFFD, //UNDEFINED
0x05BB, //HEBREW POINT QUBUTS
0x05BC, //HEBREW POINT DAGESH OR MAPIQ
0x05BD, //HEBREW POINT METEG
0x05BE, //HEBREW PUNCTUATION MAQAF
0x05BF, //HEBREW POINT RAFE
0x05C0, //HEBREW PUNCTUATION PASEQ
0x05C1, //HEBREW POINT SHIN DOT
0x05C2, //HEBREW POINT SIN DOT
0x05C3, //HEBREW PUNCTUATION SOF PASUQ
0x05F0, //HEBREW LIGATURE YIDDISH DOUBLE VAV
0x05F1, //HEBREW LIGATURE YIDDISH VAV YOD
0x05F2, //HEBREW LIGATURE YIDDISH DOUBLE YOD
0x05F3, //HEBREW PUNCTUATION GERESH
0x05F4, //HEBREW PUNCTUATION GERSHAYIM
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x05D0, //HEBREW LETTER ALEF
0x05D1, //HEBREW LETTER BET
0x05D2, //HEBREW LETTER GIMEL
0x05D3, //HEBREW LETTER DALET
0x05D4, //HEBREW LETTER HE
0x05D5, //HEBREW LETTER VAV
0x05D6, //HEBREW LETTER ZAYIN
0x05D7, //HEBREW LETTER HET
0x05D8, //HEBREW LETTER TET
0x05D9, //HEBREW LETTER YOD
0x05DA, //HEBREW LETTER FINAL KAF
0x05DB, //HEBREW LETTER KAF
0x05DC, //HEBREW LETTER LAMED
0x05DD, //HEBREW LETTER FINAL MEM
0x05DE, //HEBREW LETTER MEM
0x05DF, //HEBREW LETTER FINAL NUN
0x05E0, //HEBREW LETTER NUN
0x05E1, //HEBREW LETTER SAMEKH
0x05E2, //HEBREW LETTER AYIN
0x05E3, //HEBREW LETTER FINAL PE
0x05E4, //HEBREW LETTER PE
0x05E5, //HEBREW LETTER FINAL TSADI
0x05E6, //HEBREW LETTER TSADI
0x05E7, //HEBREW LETTER QOF
0x05E8, //HEBREW LETTER RESH
0x05E9, //HEBREW LETTER SHIN
0x05EA, //HEBREW LETTER TAV
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x200E, //LEFT-TO-RIGHT MARK
0x200F, //RIGHT-TO-LEFT MARK
0xFFFD, //UNDEFINED
},
}

@ -0,0 +1,262 @@
package cp
var cp1256 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000A, //LINE FEED
0x000B, //VERTICAL TABULATION
0x000C, //FORM FEED
0x000D, //CARRIAGE RETURN
0x000E, //SHIFT OUT
0x000F, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001A, //SUBSTITUTE
0x001B, //ESCAPE
0x001C, //FILE SEPARATOR
0x001D, //GROUP SEPARATOR
0x001E, //RECORD SEPARATOR
0x001F, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002A, //ASTERISK
0x002B, //PLUS SIGN
0x002C, //COMMA
0x002D, //HYPHEN-MINUS
0x002E, //FULL STOP
0x002F, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003A, //COLON
0x003B, //SEMICOLON
0x003C, //LESS-THAN SIGN
0x003D, //EQUALS SIGN
0x003E, //GREATER-THAN SIGN
0x003F, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004A, //LATIN CAPITAL LETTER J
0x004B, //LATIN CAPITAL LETTER K
0x004C, //LATIN CAPITAL LETTER L
0x004D, //LATIN CAPITAL LETTER M
0x004E, //LATIN CAPITAL LETTER N
0x004F, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005A, //LATIN CAPITAL LETTER Z
0x005B, //LEFT SQUARE BRACKET
0x005C, //REVERSE SOLIDUS
0x005D, //RIGHT SQUARE BRACKET
0x005E, //CIRCUMFLEX ACCENT
0x005F, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006A, //LATIN SMALL LETTER J
0x006B, //LATIN SMALL LETTER K
0x006C, //LATIN SMALL LETTER L
0x006D, //LATIN SMALL LETTER M
0x006E, //LATIN SMALL LETTER N
0x006F, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007A, //LATIN SMALL LETTER Z
0x007B, //LEFT CURLY BRACKET
0x007C, //VERTICAL LINE
0x007D, //RIGHT CURLY BRACKET
0x007E, //TILDE
0x007F, //DELETE
0x20AC, //EURO SIGN
0x067E, //ARABIC LETTER PEH
0x201A, //SINGLE LOW-9 QUOTATION MARK
0x0192, //LATIN SMALL LETTER F WITH HOOK
0x201E, //DOUBLE LOW-9 QUOTATION MARK
0x2026, //HORIZONTAL ELLIPSIS
0x2020, //DAGGER
0x2021, //DOUBLE DAGGER
0x02C6, //MODIFIER LETTER CIRCUMFLEX ACCENT
0x2030, //PER MILLE SIGN
0x0679, //ARABIC LETTER TTEH
0x2039, //SINGLE LEFT-POINTING ANGLE QUOTATION MARK
0x0152, //LATIN CAPITAL LIGATURE OE
0x0686, //ARABIC LETTER TCHEH
0x0698, //ARABIC LETTER JEH
0x0688, //ARABIC LETTER DDAL
0x06AF, //ARABIC LETTER GAF
0x2018, //LEFT SINGLE QUOTATION MARK
0x2019, //RIGHT SINGLE QUOTATION MARK
0x201C, //LEFT DOUBLE QUOTATION MARK
0x201D, //RIGHT DOUBLE QUOTATION MARK
0x2022, //BULLET
0x2013, //EN DASH
0x2014, //EM DASH
0x06A9, //ARABIC LETTER KEHEH
0x2122, //TRADE MARK SIGN
0x0691, //ARABIC LETTER RREH
0x203A, //SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
0x0153, //LATIN SMALL LIGATURE OE
0x200C, //ZERO WIDTH NON-JOINER
0x200D, //ZERO WIDTH JOINER
0x06BA, //ARABIC LETTER NOON GHUNNA
0x00A0, //NO-BREAK SPACE
0x060C, //ARABIC COMMA
0x00A2, //CENT SIGN
0x00A3, //POUND SIGN
0x00A4, //CURRENCY SIGN
0x00A5, //YEN SIGN
0x00A6, //BROKEN BAR
0x00A7, //SECTION SIGN
0x00A8, //DIAERESIS
0x00A9, //COPYRIGHT SIGN
0x06BE, //ARABIC LETTER HEH DOACHASHMEE
0x00AB, //LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00AC, //NOT SIGN
0x00AD, //SOFT HYPHEN
0x00AE, //REGISTERED SIGN
0x00AF, //MACRON
0x00B0, //DEGREE SIGN
0x00B1, //PLUS-MINUS SIGN
0x00B2, //SUPERSCRIPT TWO
0x00B3, //SUPERSCRIPT THREE
0x00B4, //ACUTE ACCENT
0x00B5, //MICRO SIGN
0x00B6, //PILCROW SIGN
0x00B7, //MIDDLE DOT
0x00B8, //CEDILLA
0x00B9, //SUPERSCRIPT ONE
0x061B, //ARABIC SEMICOLON
0x00BB, //RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00BC, //VULGAR FRACTION ONE QUARTER
0x00BD, //VULGAR FRACTION ONE HALF
0x00BE, //VULGAR FRACTION THREE QUARTERS
0x061F, //ARABIC QUESTION MARK
0x06C1, //ARABIC LETTER HEH GOAL
0x0621, //ARABIC LETTER HAMZA
0x0622, //ARABIC LETTER ALEF WITH MADDA ABOVE
0x0623, //ARABIC LETTER ALEF WITH HAMZA ABOVE
0x0624, //ARABIC LETTER WAW WITH HAMZA ABOVE
0x0625, //ARABIC LETTER ALEF WITH HAMZA BELOW
0x0626, //ARABIC LETTER YEH WITH HAMZA ABOVE
0x0627, //ARABIC LETTER ALEF
0x0628, //ARABIC LETTER BEH
0x0629, //ARABIC LETTER TEH MARBUTA
0x062A, //ARABIC LETTER TEH
0x062B, //ARABIC LETTER THEH
0x062C, //ARABIC LETTER JEEM
0x062D, //ARABIC LETTER HAH
0x062E, //ARABIC LETTER KHAH
0x062F, //ARABIC LETTER DAL
0x0630, //ARABIC LETTER THAL
0x0631, //ARABIC LETTER REH
0x0632, //ARABIC LETTER ZAIN
0x0633, //ARABIC LETTER SEEN
0x0634, //ARABIC LETTER SHEEN
0x0635, //ARABIC LETTER SAD
0x0636, //ARABIC LETTER DAD
0x00D7, //MULTIPLICATION SIGN
0x0637, //ARABIC LETTER TAH
0x0638, //ARABIC LETTER ZAH
0x0639, //ARABIC LETTER AIN
0x063A, //ARABIC LETTER GHAIN
0x0640, //ARABIC TATWEEL
0x0641, //ARABIC LETTER FEH
0x0642, //ARABIC LETTER QAF
0x0643, //ARABIC LETTER KAF
0x00E0, //LATIN SMALL LETTER A WITH GRAVE
0x0644, //ARABIC LETTER LAM
0x00E2, //LATIN SMALL LETTER A WITH CIRCUMFLEX
0x0645, //ARABIC LETTER MEEM
0x0646, //ARABIC LETTER NOON
0x0647, //ARABIC LETTER HEH
0x0648, //ARABIC LETTER WAW
0x00E7, //LATIN SMALL LETTER C WITH CEDILLA
0x00E8, //LATIN SMALL LETTER E WITH GRAVE
0x00E9, //LATIN SMALL LETTER E WITH ACUTE
0x00EA, //LATIN SMALL LETTER E WITH CIRCUMFLEX
0x00EB, //LATIN SMALL LETTER E WITH DIAERESIS
0x0649, //ARABIC LETTER ALEF MAKSURA
0x064A, //ARABIC LETTER YEH
0x00EE, //LATIN SMALL LETTER I WITH CIRCUMFLEX
0x00EF, //LATIN SMALL LETTER I WITH DIAERESIS
0x064B, //ARABIC FATHATAN
0x064C, //ARABIC DAMMATAN
0x064D, //ARABIC KASRATAN
0x064E, //ARABIC FATHA
0x00F4, //LATIN SMALL LETTER O WITH CIRCUMFLEX
0x064F, //ARABIC DAMMA
0x0650, //ARABIC KASRA
0x00F7, //DIVISION SIGN
0x0651, //ARABIC SHADDA
0x00F9, //LATIN SMALL LETTER U WITH GRAVE
0x0652, //ARABIC SUKUN
0x00FB, //LATIN SMALL LETTER U WITH CIRCUMFLEX
0x00FC, //LATIN SMALL LETTER U WITH DIAERESIS
0x200E, //LEFT-TO-RIGHT MARK
0x200F, //RIGHT-TO-LEFT MARK
0x06D2, //ARABIC LETTER YEH BARREE
},
}

@ -0,0 +1,262 @@
package cp
var cp1257 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000A, //LINE FEED
0x000B, //VERTICAL TABULATION
0x000C, //FORM FEED
0x000D, //CARRIAGE RETURN
0x000E, //SHIFT OUT
0x000F, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001A, //SUBSTITUTE
0x001B, //ESCAPE
0x001C, //FILE SEPARATOR
0x001D, //GROUP SEPARATOR
0x001E, //RECORD SEPARATOR
0x001F, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002A, //ASTERISK
0x002B, //PLUS SIGN
0x002C, //COMMA
0x002D, //HYPHEN-MINUS
0x002E, //FULL STOP
0x002F, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003A, //COLON
0x003B, //SEMICOLON
0x003C, //LESS-THAN SIGN
0x003D, //EQUALS SIGN
0x003E, //GREATER-THAN SIGN
0x003F, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004A, //LATIN CAPITAL LETTER J
0x004B, //LATIN CAPITAL LETTER K
0x004C, //LATIN CAPITAL LETTER L
0x004D, //LATIN CAPITAL LETTER M
0x004E, //LATIN CAPITAL LETTER N
0x004F, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005A, //LATIN CAPITAL LETTER Z
0x005B, //LEFT SQUARE BRACKET
0x005C, //REVERSE SOLIDUS
0x005D, //RIGHT SQUARE BRACKET
0x005E, //CIRCUMFLEX ACCENT
0x005F, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006A, //LATIN SMALL LETTER J
0x006B, //LATIN SMALL LETTER K
0x006C, //LATIN SMALL LETTER L
0x006D, //LATIN SMALL LETTER M
0x006E, //LATIN SMALL LETTER N
0x006F, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007A, //LATIN SMALL LETTER Z
0x007B, //LEFT CURLY BRACKET
0x007C, //VERTICAL LINE
0x007D, //RIGHT CURLY BRACKET
0x007E, //TILDE
0x007F, //DELETE
0x20AC, //EURO SIGN
0xFFFD, //UNDEFINED
0x201A, //SINGLE LOW-9 QUOTATION MARK
0xFFFD, //UNDEFINED
0x201E, //DOUBLE LOW-9 QUOTATION MARK
0x2026, //HORIZONTAL ELLIPSIS
0x2020, //DAGGER
0x2021, //DOUBLE DAGGER
0xFFFD, //UNDEFINED
0x2030, //PER MILLE SIGN
0xFFFD, //UNDEFINED
0x2039, //SINGLE LEFT-POINTING ANGLE QUOTATION MARK
0xFFFD, //UNDEFINED
0x00A8, //DIAERESIS
0x02C7, //CARON
0x00B8, //CEDILLA
0xFFFD, //UNDEFINED
0x2018, //LEFT SINGLE QUOTATION MARK
0x2019, //RIGHT SINGLE QUOTATION MARK
0x201C, //LEFT DOUBLE QUOTATION MARK
0x201D, //RIGHT DOUBLE QUOTATION MARK
0x2022, //BULLET
0x2013, //EN DASH
0x2014, //EM DASH
0xFFFD, //UNDEFINED
0x2122, //TRADE MARK SIGN
0xFFFD, //UNDEFINED
0x203A, //SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
0xFFFD, //UNDEFINED
0x00AF, //MACRON
0x02DB, //OGONEK
0xFFFD, //UNDEFINED
0x00A0, //NO-BREAK SPACE
0xFFFD, //UNDEFINED
0x00A2, //CENT SIGN
0x00A3, //POUND SIGN
0x00A4, //CURRENCY SIGN
0xFFFD, //UNDEFINED
0x00A6, //BROKEN BAR
0x00A7, //SECTION SIGN
0x00D8, //LATIN CAPITAL LETTER O WITH STROKE
0x00A9, //COPYRIGHT SIGN
0x0156, //LATIN CAPITAL LETTER R WITH CEDILLA
0x00AB, //LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00AC, //NOT SIGN
0x00AD, //SOFT HYPHEN
0x00AE, //REGISTERED SIGN
0x00C6, //LATIN CAPITAL LETTER AE
0x00B0, //DEGREE SIGN
0x00B1, //PLUS-MINUS SIGN
0x00B2, //SUPERSCRIPT TWO
0x00B3, //SUPERSCRIPT THREE
0x00B4, //ACUTE ACCENT
0x00B5, //MICRO SIGN
0x00B6, //PILCROW SIGN
0x00B7, //MIDDLE DOT
0x00F8, //LATIN SMALL LETTER O WITH STROKE
0x00B9, //SUPERSCRIPT ONE
0x0157, //LATIN SMALL LETTER R WITH CEDILLA
0x00BB, //RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00BC, //VULGAR FRACTION ONE QUARTER
0x00BD, //VULGAR FRACTION ONE HALF
0x00BE, //VULGAR FRACTION THREE QUARTERS
0x00E6, //LATIN SMALL LETTER AE
0x0104, //LATIN CAPITAL LETTER A WITH OGONEK
0x012E, //LATIN CAPITAL LETTER I WITH OGONEK
0x0100, //LATIN CAPITAL LETTER A WITH MACRON
0x0106, //LATIN CAPITAL LETTER C WITH ACUTE
0x00C4, //LATIN CAPITAL LETTER A WITH DIAERESIS
0x00C5, //LATIN CAPITAL LETTER A WITH RING ABOVE
0x0118, //LATIN CAPITAL LETTER E WITH OGONEK
0x0112, //LATIN CAPITAL LETTER E WITH MACRON
0x010C, //LATIN CAPITAL LETTER C WITH CARON
0x00C9, //LATIN CAPITAL LETTER E WITH ACUTE
0x0179, //LATIN CAPITAL LETTER Z WITH ACUTE
0x0116, //LATIN CAPITAL LETTER E WITH DOT ABOVE
0x0122, //LATIN CAPITAL LETTER G WITH CEDILLA
0x0136, //LATIN CAPITAL LETTER K WITH CEDILLA
0x012A, //LATIN CAPITAL LETTER I WITH MACRON
0x013B, //LATIN CAPITAL LETTER L WITH CEDILLA
0x0160, //LATIN CAPITAL LETTER S WITH CARON
0x0143, //LATIN CAPITAL LETTER N WITH ACUTE
0x0145, //LATIN CAPITAL LETTER N WITH CEDILLA
0x00D3, //LATIN CAPITAL LETTER O WITH ACUTE
0x014C, //LATIN CAPITAL LETTER O WITH MACRON
0x00D5, //LATIN CAPITAL LETTER O WITH TILDE
0x00D6, //LATIN CAPITAL LETTER O WITH DIAERESIS
0x00D7, //MULTIPLICATION SIGN
0x0172, //LATIN CAPITAL LETTER U WITH OGONEK
0x0141, //LATIN CAPITAL LETTER L WITH STROKE
0x015A, //LATIN CAPITAL LETTER S WITH ACUTE
0x016A, //LATIN CAPITAL LETTER U WITH MACRON
0x00DC, //LATIN CAPITAL LETTER U WITH DIAERESIS
0x017B, //LATIN CAPITAL LETTER Z WITH DOT ABOVE
0x017D, //LATIN CAPITAL LETTER Z WITH CARON
0x00DF, //LATIN SMALL LETTER SHARP S
0x0105, //LATIN SMALL LETTER A WITH OGONEK
0x012F, //LATIN SMALL LETTER I WITH OGONEK
0x0101, //LATIN SMALL LETTER A WITH MACRON
0x0107, //LATIN SMALL LETTER C WITH ACUTE
0x00E4, //LATIN SMALL LETTER A WITH DIAERESIS
0x00E5, //LATIN SMALL LETTER A WITH RING ABOVE
0x0119, //LATIN SMALL LETTER E WITH OGONEK
0x0113, //LATIN SMALL LETTER E WITH MACRON
0x010D, //LATIN SMALL LETTER C WITH CARON
0x00E9, //LATIN SMALL LETTER E WITH ACUTE
0x017A, //LATIN SMALL LETTER Z WITH ACUTE
0x0117, //LATIN SMALL LETTER E WITH DOT ABOVE
0x0123, //LATIN SMALL LETTER G WITH CEDILLA
0x0137, //LATIN SMALL LETTER K WITH CEDILLA
0x012B, //LATIN SMALL LETTER I WITH MACRON
0x013C, //LATIN SMALL LETTER L WITH CEDILLA
0x0161, //LATIN SMALL LETTER S WITH CARON
0x0144, //LATIN SMALL LETTER N WITH ACUTE
0x0146, //LATIN SMALL LETTER N WITH CEDILLA
0x00F3, //LATIN SMALL LETTER O WITH ACUTE
0x014D, //LATIN SMALL LETTER O WITH MACRON
0x00F5, //LATIN SMALL LETTER O WITH TILDE
0x00F6, //LATIN SMALL LETTER O WITH DIAERESIS
0x00F7, //DIVISION SIGN
0x0173, //LATIN SMALL LETTER U WITH OGONEK
0x0142, //LATIN SMALL LETTER L WITH STROKE
0x015B, //LATIN SMALL LETTER S WITH ACUTE
0x016B, //LATIN SMALL LETTER U WITH MACRON
0x00FC, //LATIN SMALL LETTER U WITH DIAERESIS
0x017C, //LATIN SMALL LETTER Z WITH DOT ABOVE
0x017E, //LATIN SMALL LETTER Z WITH CARON
0x02D9, //DOT ABOVE
},
}

@ -0,0 +1,262 @@
package cp
var cp1258 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000A, //LINE FEED
0x000B, //VERTICAL TABULATION
0x000C, //FORM FEED
0x000D, //CARRIAGE RETURN
0x000E, //SHIFT OUT
0x000F, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001A, //SUBSTITUTE
0x001B, //ESCAPE
0x001C, //FILE SEPARATOR
0x001D, //GROUP SEPARATOR
0x001E, //RECORD SEPARATOR
0x001F, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002A, //ASTERISK
0x002B, //PLUS SIGN
0x002C, //COMMA
0x002D, //HYPHEN-MINUS
0x002E, //FULL STOP
0x002F, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003A, //COLON
0x003B, //SEMICOLON
0x003C, //LESS-THAN SIGN
0x003D, //EQUALS SIGN
0x003E, //GREATER-THAN SIGN
0x003F, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004A, //LATIN CAPITAL LETTER J
0x004B, //LATIN CAPITAL LETTER K
0x004C, //LATIN CAPITAL LETTER L
0x004D, //LATIN CAPITAL LETTER M
0x004E, //LATIN CAPITAL LETTER N
0x004F, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005A, //LATIN CAPITAL LETTER Z
0x005B, //LEFT SQUARE BRACKET
0x005C, //REVERSE SOLIDUS
0x005D, //RIGHT SQUARE BRACKET
0x005E, //CIRCUMFLEX ACCENT
0x005F, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006A, //LATIN SMALL LETTER J
0x006B, //LATIN SMALL LETTER K
0x006C, //LATIN SMALL LETTER L
0x006D, //LATIN SMALL LETTER M
0x006E, //LATIN SMALL LETTER N
0x006F, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007A, //LATIN SMALL LETTER Z
0x007B, //LEFT CURLY BRACKET
0x007C, //VERTICAL LINE
0x007D, //RIGHT CURLY BRACKET
0x007E, //TILDE
0x007F, //DELETE
0x20AC, //EURO SIGN
0xFFFD, //UNDEFINED
0x201A, //SINGLE LOW-9 QUOTATION MARK
0x0192, //LATIN SMALL LETTER F WITH HOOK
0x201E, //DOUBLE LOW-9 QUOTATION MARK
0x2026, //HORIZONTAL ELLIPSIS
0x2020, //DAGGER
0x2021, //DOUBLE DAGGER
0x02C6, //MODIFIER LETTER CIRCUMFLEX ACCENT
0x2030, //PER MILLE SIGN
0xFFFD, //UNDEFINED
0x2039, //SINGLE LEFT-POINTING ANGLE QUOTATION MARK
0x0152, //LATIN CAPITAL LIGATURE OE
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x2018, //LEFT SINGLE QUOTATION MARK
0x2019, //RIGHT SINGLE QUOTATION MARK
0x201C, //LEFT DOUBLE QUOTATION MARK
0x201D, //RIGHT DOUBLE QUOTATION MARK
0x2022, //BULLET
0x2013, //EN DASH
0x2014, //EM DASH
0x02DC, //SMALL TILDE
0x2122, //TRADE MARK SIGN
0xFFFD, //UNDEFINED
0x203A, //SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
0x0153, //LATIN SMALL LIGATURE OE
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x0178, //LATIN CAPITAL LETTER Y WITH DIAERESIS
0x00A0, //NO-BREAK SPACE
0x00A1, //INVERTED EXCLAMATION MARK
0x00A2, //CENT SIGN
0x00A3, //POUND SIGN
0x00A4, //CURRENCY SIGN
0x00A5, //YEN SIGN
0x00A6, //BROKEN BAR
0x00A7, //SECTION SIGN
0x00A8, //DIAERESIS
0x00A9, //COPYRIGHT SIGN
0x00AA, //FEMININE ORDINAL INDICATOR
0x00AB, //LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00AC, //NOT SIGN
0x00AD, //SOFT HYPHEN
0x00AE, //REGISTERED SIGN
0x00AF, //MACRON
0x00B0, //DEGREE SIGN
0x00B1, //PLUS-MINUS SIGN
0x00B2, //SUPERSCRIPT TWO
0x00B3, //SUPERSCRIPT THREE
0x00B4, //ACUTE ACCENT
0x00B5, //MICRO SIGN
0x00B6, //PILCROW SIGN
0x00B7, //MIDDLE DOT
0x00B8, //CEDILLA
0x00B9, //SUPERSCRIPT ONE
0x00BA, //MASCULINE ORDINAL INDICATOR
0x00BB, //RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00BC, //VULGAR FRACTION ONE QUARTER
0x00BD, //VULGAR FRACTION ONE HALF
0x00BE, //VULGAR FRACTION THREE QUARTERS
0x00BF, //INVERTED QUESTION MARK
0x00C0, //LATIN CAPITAL LETTER A WITH GRAVE
0x00C1, //LATIN CAPITAL LETTER A WITH ACUTE
0x00C2, //LATIN CAPITAL LETTER A WITH CIRCUMFLEX
0x0102, //LATIN CAPITAL LETTER A WITH BREVE
0x00C4, //LATIN CAPITAL LETTER A WITH DIAERESIS
0x00C5, //LATIN CAPITAL LETTER A WITH RING ABOVE
0x00C6, //LATIN CAPITAL LETTER AE
0x00C7, //LATIN CAPITAL LETTER C WITH CEDILLA
0x00C8, //LATIN CAPITAL LETTER E WITH GRAVE
0x00C9, //LATIN CAPITAL LETTER E WITH ACUTE
0x00CA, //LATIN CAPITAL LETTER E WITH CIRCUMFLEX
0x00CB, //LATIN CAPITAL LETTER E WITH DIAERESIS
0x0300, //COMBINING GRAVE ACCENT
0x00CD, //LATIN CAPITAL LETTER I WITH ACUTE
0x00CE, //LATIN CAPITAL LETTER I WITH CIRCUMFLEX
0x00CF, //LATIN CAPITAL LETTER I WITH DIAERESIS
0x0110, //LATIN CAPITAL LETTER D WITH STROKE
0x00D1, //LATIN CAPITAL LETTER N WITH TILDE
0x0309, //COMBINING HOOK ABOVE
0x00D3, //LATIN CAPITAL LETTER O WITH ACUTE
0x00D4, //LATIN CAPITAL LETTER O WITH CIRCUMFLEX
0x01A0, //LATIN CAPITAL LETTER O WITH HORN
0x00D6, //LATIN CAPITAL LETTER O WITH DIAERESIS
0x00D7, //MULTIPLICATION SIGN
0x00D8, //LATIN CAPITAL LETTER O WITH STROKE
0x00D9, //LATIN CAPITAL LETTER U WITH GRAVE
0x00DA, //LATIN CAPITAL LETTER U WITH ACUTE
0x00DB, //LATIN CAPITAL LETTER U WITH CIRCUMFLEX
0x00DC, //LATIN CAPITAL LETTER U WITH DIAERESIS
0x01AF, //LATIN CAPITAL LETTER U WITH HORN
0x0303, //COMBINING TILDE
0x00DF, //LATIN SMALL LETTER SHARP S
0x00E0, //LATIN SMALL LETTER A WITH GRAVE
0x00E1, //LATIN SMALL LETTER A WITH ACUTE
0x00E2, //LATIN SMALL LETTER A WITH CIRCUMFLEX
0x0103, //LATIN SMALL LETTER A WITH BREVE
0x00E4, //LATIN SMALL LETTER A WITH DIAERESIS
0x00E5, //LATIN SMALL LETTER A WITH RING ABOVE
0x00E6, //LATIN SMALL LETTER AE
0x00E7, //LATIN SMALL LETTER C WITH CEDILLA
0x00E8, //LATIN SMALL LETTER E WITH GRAVE
0x00E9, //LATIN SMALL LETTER E WITH ACUTE
0x00EA, //LATIN SMALL LETTER E WITH CIRCUMFLEX
0x00EB, //LATIN SMALL LETTER E WITH DIAERESIS
0x0301, //COMBINING ACUTE ACCENT
0x00ED, //LATIN SMALL LETTER I WITH ACUTE
0x00EE, //LATIN SMALL LETTER I WITH CIRCUMFLEX
0x00EF, //LATIN SMALL LETTER I WITH DIAERESIS
0x0111, //LATIN SMALL LETTER D WITH STROKE
0x00F1, //LATIN SMALL LETTER N WITH TILDE
0x0323, //COMBINING DOT BELOW
0x00F3, //LATIN SMALL LETTER O WITH ACUTE
0x00F4, //LATIN SMALL LETTER O WITH CIRCUMFLEX
0x01A1, //LATIN SMALL LETTER O WITH HORN
0x00F6, //LATIN SMALL LETTER O WITH DIAERESIS
0x00F7, //DIVISION SIGN
0x00F8, //LATIN SMALL LETTER O WITH STROKE
0x00F9, //LATIN SMALL LETTER U WITH GRAVE
0x00FA, //LATIN SMALL LETTER U WITH ACUTE
0x00FB, //LATIN SMALL LETTER U WITH CIRCUMFLEX
0x00FC, //LATIN SMALL LETTER U WITH DIAERESIS
0x01B0, //LATIN SMALL LETTER U WITH HORN
0x20AB, //DONG SIGN
0x00FF, //LATIN SMALL LETTER Y WITH DIAERESIS
},
}

@ -0,0 +1,262 @@
package cp
var cp437 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000a, //LINE FEED
0x000b, //VERTICAL TABULATION
0x000c, //FORM FEED
0x000d, //CARRIAGE RETURN
0x000e, //SHIFT OUT
0x000f, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001a, //SUBSTITUTE
0x001b, //ESCAPE
0x001c, //FILE SEPARATOR
0x001d, //GROUP SEPARATOR
0x001e, //RECORD SEPARATOR
0x001f, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002a, //ASTERISK
0x002b, //PLUS SIGN
0x002c, //COMMA
0x002d, //HYPHEN-MINUS
0x002e, //FULL STOP
0x002f, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003a, //COLON
0x003b, //SEMICOLON
0x003c, //LESS-THAN SIGN
0x003d, //EQUALS SIGN
0x003e, //GREATER-THAN SIGN
0x003f, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004a, //LATIN CAPITAL LETTER J
0x004b, //LATIN CAPITAL LETTER K
0x004c, //LATIN CAPITAL LETTER L
0x004d, //LATIN CAPITAL LETTER M
0x004e, //LATIN CAPITAL LETTER N
0x004f, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005a, //LATIN CAPITAL LETTER Z
0x005b, //LEFT SQUARE BRACKET
0x005c, //REVERSE SOLIDUS
0x005d, //RIGHT SQUARE BRACKET
0x005e, //CIRCUMFLEX ACCENT
0x005f, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006a, //LATIN SMALL LETTER J
0x006b, //LATIN SMALL LETTER K
0x006c, //LATIN SMALL LETTER L
0x006d, //LATIN SMALL LETTER M
0x006e, //LATIN SMALL LETTER N
0x006f, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007a, //LATIN SMALL LETTER Z
0x007b, //LEFT CURLY BRACKET
0x007c, //VERTICAL LINE
0x007d, //RIGHT CURLY BRACKET
0x007e, //TILDE
0x007f, //DELETE
0x00c7, //LATIN CAPITAL LETTER C WITH CEDILLA
0x00fc, //LATIN SMALL LETTER U WITH DIAERESIS
0x00e9, //LATIN SMALL LETTER E WITH ACUTE
0x00e2, //LATIN SMALL LETTER A WITH CIRCUMFLEX
0x00e4, //LATIN SMALL LETTER A WITH DIAERESIS
0x00e0, //LATIN SMALL LETTER A WITH GRAVE
0x00e5, //LATIN SMALL LETTER A WITH RING ABOVE
0x00e7, //LATIN SMALL LETTER C WITH CEDILLA
0x00ea, //LATIN SMALL LETTER E WITH CIRCUMFLEX
0x00eb, //LATIN SMALL LETTER E WITH DIAERESIS
0x00e8, //LATIN SMALL LETTER E WITH GRAVE
0x00ef, //LATIN SMALL LETTER I WITH DIAERESIS
0x00ee, //LATIN SMALL LETTER I WITH CIRCUMFLEX
0x00ec, //LATIN SMALL LETTER I WITH GRAVE
0x00c4, //LATIN CAPITAL LETTER A WITH DIAERESIS
0x00c5, //LATIN CAPITAL LETTER A WITH RING ABOVE
0x00c9, //LATIN CAPITAL LETTER E WITH ACUTE
0x00e6, //LATIN SMALL LIGATURE AE
0x00c6, //LATIN CAPITAL LIGATURE AE
0x00f4, //LATIN SMALL LETTER O WITH CIRCUMFLEX
0x00f6, //LATIN SMALL LETTER O WITH DIAERESIS
0x00f2, //LATIN SMALL LETTER O WITH GRAVE
0x00fb, //LATIN SMALL LETTER U WITH CIRCUMFLEX
0x00f9, //LATIN SMALL LETTER U WITH GRAVE
0x00ff, //LATIN SMALL LETTER Y WITH DIAERESIS
0x00d6, //LATIN CAPITAL LETTER O WITH DIAERESIS
0x00dc, //LATIN CAPITAL LETTER U WITH DIAERESIS
0x00a2, //CENT SIGN
0x00a3, //POUND SIGN
0x00a5, //YEN SIGN
0x20a7, //PESETA SIGN
0x0192, //LATIN SMALL LETTER F WITH HOOK
0x00e1, //LATIN SMALL LETTER A WITH ACUTE
0x00ed, //LATIN SMALL LETTER I WITH ACUTE
0x00f3, //LATIN SMALL LETTER O WITH ACUTE
0x00fa, //LATIN SMALL LETTER U WITH ACUTE
0x00f1, //LATIN SMALL LETTER N WITH TILDE
0x00d1, //LATIN CAPITAL LETTER N WITH TILDE
0x00aa, //FEMININE ORDINAL INDICATOR
0x00ba, //MASCULINE ORDINAL INDICATOR
0x00bf, //INVERTED QUESTION MARK
0x2310, //REVERSED NOT SIGN
0x00ac, //NOT SIGN
0x00bd, //VULGAR FRACTION ONE HALF
0x00bc, //VULGAR FRACTION ONE QUARTER
0x00a1, //INVERTED EXCLAMATION MARK
0x00ab, //LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00bb, //RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x2591, //LIGHT SHADE
0x2592, //MEDIUM SHADE
0x2593, //DARK SHADE
0x2502, //BOX DRAWINGS LIGHT VERTICAL
0x2524, //BOX DRAWINGS LIGHT VERTICAL AND LEFT
0x2561, //BOX DRAWINGS VERTICAL SINGLE AND LEFT DOUBLE
0x2562, //BOX DRAWINGS VERTICAL DOUBLE AND LEFT SINGLE
0x2556, //BOX DRAWINGS DOWN DOUBLE AND LEFT SINGLE
0x2555, //BOX DRAWINGS DOWN SINGLE AND LEFT DOUBLE
0x2563, //BOX DRAWINGS DOUBLE VERTICAL AND LEFT
0x2551, //BOX DRAWINGS DOUBLE VERTICAL
0x2557, //BOX DRAWINGS DOUBLE DOWN AND LEFT
0x255d, //BOX DRAWINGS DOUBLE UP AND LEFT
0x255c, //BOX DRAWINGS UP DOUBLE AND LEFT SINGLE
0x255b, //BOX DRAWINGS UP SINGLE AND LEFT DOUBLE
0x2510, //BOX DRAWINGS LIGHT DOWN AND LEFT
0x2514, //BOX DRAWINGS LIGHT UP AND RIGHT
0x2534, //BOX DRAWINGS LIGHT UP AND HORIZONTAL
0x252c, //BOX DRAWINGS LIGHT DOWN AND HORIZONTAL
0x251c, //BOX DRAWINGS LIGHT VERTICAL AND RIGHT
0x2500, //BOX DRAWINGS LIGHT HORIZONTAL
0x253c, //BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL
0x255e, //BOX DRAWINGS VERTICAL SINGLE AND RIGHT DOUBLE
0x255f, //BOX DRAWINGS VERTICAL DOUBLE AND RIGHT SINGLE
0x255a, //BOX DRAWINGS DOUBLE UP AND RIGHT
0x2554, //BOX DRAWINGS DOUBLE DOWN AND RIGHT
0x2569, //BOX DRAWINGS DOUBLE UP AND HORIZONTAL
0x2566, //BOX DRAWINGS DOUBLE DOWN AND HORIZONTAL
0x2560, //BOX DRAWINGS DOUBLE VERTICAL AND RIGHT
0x2550, //BOX DRAWINGS DOUBLE HORIZONTAL
0x256c, //BOX DRAWINGS DOUBLE VERTICAL AND HORIZONTAL
0x2567, //BOX DRAWINGS UP SINGLE AND HORIZONTAL DOUBLE
0x2568, //BOX DRAWINGS UP DOUBLE AND HORIZONTAL SINGLE
0x2564, //BOX DRAWINGS DOWN SINGLE AND HORIZONTAL DOUBLE
0x2565, //BOX DRAWINGS DOWN DOUBLE AND HORIZONTAL SINGLE
0x2559, //BOX DRAWINGS UP DOUBLE AND RIGHT SINGLE
0x2558, //BOX DRAWINGS UP SINGLE AND RIGHT DOUBLE
0x2552, //BOX DRAWINGS DOWN SINGLE AND RIGHT DOUBLE
0x2553, //BOX DRAWINGS DOWN DOUBLE AND RIGHT SINGLE
0x256b, //BOX DRAWINGS VERTICAL DOUBLE AND HORIZONTAL SINGLE
0x256a, //BOX DRAWINGS VERTICAL SINGLE AND HORIZONTAL DOUBLE
0x2518, //BOX DRAWINGS LIGHT UP AND LEFT
0x250c, //BOX DRAWINGS LIGHT DOWN AND RIGHT
0x2588, //FULL BLOCK
0x2584, //LOWER HALF BLOCK
0x258c, //LEFT HALF BLOCK
0x2590, //RIGHT HALF BLOCK
0x2580, //UPPER HALF BLOCK
0x03b1, //GREEK SMALL LETTER ALPHA
0x00df, //LATIN SMALL LETTER SHARP S
0x0393, //GREEK CAPITAL LETTER GAMMA
0x03c0, //GREEK SMALL LETTER PI
0x03a3, //GREEK CAPITAL LETTER SIGMA
0x03c3, //GREEK SMALL LETTER SIGMA
0x00b5, //MICRO SIGN
0x03c4, //GREEK SMALL LETTER TAU
0x03a6, //GREEK CAPITAL LETTER PHI
0x0398, //GREEK CAPITAL LETTER THETA
0x03a9, //GREEK CAPITAL LETTER OMEGA
0x03b4, //GREEK SMALL LETTER DELTA
0x221e, //INFINITY
0x03c6, //GREEK SMALL LETTER PHI
0x03b5, //GREEK SMALL LETTER EPSILON
0x2229, //INTERSECTION
0x2261, //IDENTICAL TO
0x00b1, //PLUS-MINUS SIGN
0x2265, //GREATER-THAN OR EQUAL TO
0x2264, //LESS-THAN OR EQUAL TO
0x2320, //TOP HALF INTEGRAL
0x2321, //BOTTOM HALF INTEGRAL
0x00f7, //DIVISION SIGN
0x2248, //ALMOST EQUAL TO
0x00b0, //DEGREE SIGN
0x2219, //BULLET OPERATOR
0x00b7, //MIDDLE DOT
0x221a, //SQUARE ROOT
0x207f, //SUPERSCRIPT LATIN SMALL LETTER N
0x00b2, //SUPERSCRIPT TWO
0x25a0, //BLACK SQUARE
0x00a0, //NO-BREAK SPACE
},
}

@ -0,0 +1,262 @@
package cp
var cp850 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000a, //LINE FEED
0x000b, //VERTICAL TABULATION
0x000c, //FORM FEED
0x000d, //CARRIAGE RETURN
0x000e, //SHIFT OUT
0x000f, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001a, //SUBSTITUTE
0x001b, //ESCAPE
0x001c, //FILE SEPARATOR
0x001d, //GROUP SEPARATOR
0x001e, //RECORD SEPARATOR
0x001f, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002a, //ASTERISK
0x002b, //PLUS SIGN
0x002c, //COMMA
0x002d, //HYPHEN-MINUS
0x002e, //FULL STOP
0x002f, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003a, //COLON
0x003b, //SEMICOLON
0x003c, //LESS-THAN SIGN
0x003d, //EQUALS SIGN
0x003e, //GREATER-THAN SIGN
0x003f, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004a, //LATIN CAPITAL LETTER J
0x004b, //LATIN CAPITAL LETTER K
0x004c, //LATIN CAPITAL LETTER L
0x004d, //LATIN CAPITAL LETTER M
0x004e, //LATIN CAPITAL LETTER N
0x004f, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005a, //LATIN CAPITAL LETTER Z
0x005b, //LEFT SQUARE BRACKET
0x005c, //REVERSE SOLIDUS
0x005d, //RIGHT SQUARE BRACKET
0x005e, //CIRCUMFLEX ACCENT
0x005f, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006a, //LATIN SMALL LETTER J
0x006b, //LATIN SMALL LETTER K
0x006c, //LATIN SMALL LETTER L
0x006d, //LATIN SMALL LETTER M
0x006e, //LATIN SMALL LETTER N
0x006f, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007a, //LATIN SMALL LETTER Z
0x007b, //LEFT CURLY BRACKET
0x007c, //VERTICAL LINE
0x007d, //RIGHT CURLY BRACKET
0x007e, //TILDE
0x007f, //DELETE
0x00c7, //LATIN CAPITAL LETTER C WITH CEDILLA
0x00fc, //LATIN SMALL LETTER U WITH DIAERESIS
0x00e9, //LATIN SMALL LETTER E WITH ACUTE
0x00e2, //LATIN SMALL LETTER A WITH CIRCUMFLEX
0x00e4, //LATIN SMALL LETTER A WITH DIAERESIS
0x00e0, //LATIN SMALL LETTER A WITH GRAVE
0x00e5, //LATIN SMALL LETTER A WITH RING ABOVE
0x00e7, //LATIN SMALL LETTER C WITH CEDILLA
0x00ea, //LATIN SMALL LETTER E WITH CIRCUMFLEX
0x00eb, //LATIN SMALL LETTER E WITH DIAERESIS
0x00e8, //LATIN SMALL LETTER E WITH GRAVE
0x00ef, //LATIN SMALL LETTER I WITH DIAERESIS
0x00ee, //LATIN SMALL LETTER I WITH CIRCUMFLEX
0x00ec, //LATIN SMALL LETTER I WITH GRAVE
0x00c4, //LATIN CAPITAL LETTER A WITH DIAERESIS
0x00c5, //LATIN CAPITAL LETTER A WITH RING ABOVE
0x00c9, //LATIN CAPITAL LETTER E WITH ACUTE
0x00e6, //LATIN SMALL LIGATURE AE
0x00c6, //LATIN CAPITAL LIGATURE AE
0x00f4, //LATIN SMALL LETTER O WITH CIRCUMFLEX
0x00f6, //LATIN SMALL LETTER O WITH DIAERESIS
0x00f2, //LATIN SMALL LETTER O WITH GRAVE
0x00fb, //LATIN SMALL LETTER U WITH CIRCUMFLEX
0x00f9, //LATIN SMALL LETTER U WITH GRAVE
0x00ff, //LATIN SMALL LETTER Y WITH DIAERESIS
0x00d6, //LATIN CAPITAL LETTER O WITH DIAERESIS
0x00dc, //LATIN CAPITAL LETTER U WITH DIAERESIS
0x00f8, //LATIN SMALL LETTER O WITH STROKE
0x00a3, //POUND SIGN
0x00d8, //LATIN CAPITAL LETTER O WITH STROKE
0x00d7, //MULTIPLICATION SIGN
0x0192, //LATIN SMALL LETTER F WITH HOOK
0x00e1, //LATIN SMALL LETTER A WITH ACUTE
0x00ed, //LATIN SMALL LETTER I WITH ACUTE
0x00f3, //LATIN SMALL LETTER O WITH ACUTE
0x00fa, //LATIN SMALL LETTER U WITH ACUTE
0x00f1, //LATIN SMALL LETTER N WITH TILDE
0x00d1, //LATIN CAPITAL LETTER N WITH TILDE
0x00aa, //FEMININE ORDINAL INDICATOR
0x00ba, //MASCULINE ORDINAL INDICATOR
0x00bf, //INVERTED QUESTION MARK
0x00ae, //REGISTERED SIGN
0x00ac, //NOT SIGN
0x00bd, //VULGAR FRACTION ONE HALF
0x00bc, //VULGAR FRACTION ONE QUARTER
0x00a1, //INVERTED EXCLAMATION MARK
0x00ab, //LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
0x00bb, //RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
0x2591, //LIGHT SHADE
0x2592, //MEDIUM SHADE
0x2593, //DARK SHADE
0x2502, //BOX DRAWINGS LIGHT VERTICAL
0x2524, //BOX DRAWINGS LIGHT VERTICAL AND LEFT
0x00c1, //LATIN CAPITAL LETTER A WITH ACUTE
0x00c2, //LATIN CAPITAL LETTER A WITH CIRCUMFLEX
0x00c0, //LATIN CAPITAL LETTER A WITH GRAVE
0x00a9, //COPYRIGHT SIGN
0x2563, //BOX DRAWINGS DOUBLE VERTICAL AND LEFT
0x2551, //BOX DRAWINGS DOUBLE VERTICAL
0x2557, //BOX DRAWINGS DOUBLE DOWN AND LEFT
0x255d, //BOX DRAWINGS DOUBLE UP AND LEFT
0x00a2, //CENT SIGN
0x00a5, //YEN SIGN
0x2510, //BOX DRAWINGS LIGHT DOWN AND LEFT
0x2514, //BOX DRAWINGS LIGHT UP AND RIGHT
0x2534, //BOX DRAWINGS LIGHT UP AND HORIZONTAL
0x252c, //BOX DRAWINGS LIGHT DOWN AND HORIZONTAL
0x251c, //BOX DRAWINGS LIGHT VERTICAL AND RIGHT
0x2500, //BOX DRAWINGS LIGHT HORIZONTAL
0x253c, //BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL
0x00e3, //LATIN SMALL LETTER A WITH TILDE
0x00c3, //LATIN CAPITAL LETTER A WITH TILDE
0x255a, //BOX DRAWINGS DOUBLE UP AND RIGHT
0x2554, //BOX DRAWINGS DOUBLE DOWN AND RIGHT
0x2569, //BOX DRAWINGS DOUBLE UP AND HORIZONTAL
0x2566, //BOX DRAWINGS DOUBLE DOWN AND HORIZONTAL
0x2560, //BOX DRAWINGS DOUBLE VERTICAL AND RIGHT
0x2550, //BOX DRAWINGS DOUBLE HORIZONTAL
0x256c, //BOX DRAWINGS DOUBLE VERTICAL AND HORIZONTAL
0x00a4, //CURRENCY SIGN
0x00f0, //LATIN SMALL LETTER ETH
0x00d0, //LATIN CAPITAL LETTER ETH
0x00ca, //LATIN CAPITAL LETTER E WITH CIRCUMFLEX
0x00cb, //LATIN CAPITAL LETTER E WITH DIAERESIS
0x00c8, //LATIN CAPITAL LETTER E WITH GRAVE
0x0131, //LATIN SMALL LETTER DOTLESS I
0x00cd, //LATIN CAPITAL LETTER I WITH ACUTE
0x00ce, //LATIN CAPITAL LETTER I WITH CIRCUMFLEX
0x00cf, //LATIN CAPITAL LETTER I WITH DIAERESIS
0x2518, //BOX DRAWINGS LIGHT UP AND LEFT
0x250c, //BOX DRAWINGS LIGHT DOWN AND RIGHT
0x2588, //FULL BLOCK
0x2584, //LOWER HALF BLOCK
0x00a6, //BROKEN BAR
0x00cc, //LATIN CAPITAL LETTER I WITH GRAVE
0x2580, //UPPER HALF BLOCK
0x00d3, //LATIN CAPITAL LETTER O WITH ACUTE
0x00df, //LATIN SMALL LETTER SHARP S
0x00d4, //LATIN CAPITAL LETTER O WITH CIRCUMFLEX
0x00d2, //LATIN CAPITAL LETTER O WITH GRAVE
0x00f5, //LATIN SMALL LETTER O WITH TILDE
0x00d5, //LATIN CAPITAL LETTER O WITH TILDE
0x00b5, //MICRO SIGN
0x00fe, //LATIN SMALL LETTER THORN
0x00de, //LATIN CAPITAL LETTER THORN
0x00da, //LATIN CAPITAL LETTER U WITH ACUTE
0x00db, //LATIN CAPITAL LETTER U WITH CIRCUMFLEX
0x00d9, //LATIN CAPITAL LETTER U WITH GRAVE
0x00fd, //LATIN SMALL LETTER Y WITH ACUTE
0x00dd, //LATIN CAPITAL LETTER Y WITH ACUTE
0x00af, //MACRON
0x00b4, //ACUTE ACCENT
0x00ad, //SOFT HYPHEN
0x00b1, //PLUS-MINUS SIGN
0x2017, //DOUBLE LOW LINE
0x00be, //VULGAR FRACTION THREE QUARTERS
0x00b6, //PILCROW SIGN
0x00a7, //SECTION SIGN
0x00f7, //DIVISION SIGN
0x00b8, //CEDILLA
0x00b0, //DEGREE SIGN
0x00a8, //DIAERESIS
0x00b7, //MIDDLE DOT
0x00b9, //SUPERSCRIPT ONE
0x00b3, //SUPERSCRIPT THREE
0x00b2, //SUPERSCRIPT TWO
0x25a0, //BLACK SQUARE
0x00a0, //NO-BREAK SPACE
},
}

@ -0,0 +1,262 @@
package cp
var cp874 *charsetMap = &charsetMap{
sb: [256]rune{
0x0000, //NULL
0x0001, //START OF HEADING
0x0002, //START OF TEXT
0x0003, //END OF TEXT
0x0004, //END OF TRANSMISSION
0x0005, //ENQUIRY
0x0006, //ACKNOWLEDGE
0x0007, //BELL
0x0008, //BACKSPACE
0x0009, //HORIZONTAL TABULATION
0x000A, //LINE FEED
0x000B, //VERTICAL TABULATION
0x000C, //FORM FEED
0x000D, //CARRIAGE RETURN
0x000E, //SHIFT OUT
0x000F, //SHIFT IN
0x0010, //DATA LINK ESCAPE
0x0011, //DEVICE CONTROL ONE
0x0012, //DEVICE CONTROL TWO
0x0013, //DEVICE CONTROL THREE
0x0014, //DEVICE CONTROL FOUR
0x0015, //NEGATIVE ACKNOWLEDGE
0x0016, //SYNCHRONOUS IDLE
0x0017, //END OF TRANSMISSION BLOCK
0x0018, //CANCEL
0x0019, //END OF MEDIUM
0x001A, //SUBSTITUTE
0x001B, //ESCAPE
0x001C, //FILE SEPARATOR
0x001D, //GROUP SEPARATOR
0x001E, //RECORD SEPARATOR
0x001F, //UNIT SEPARATOR
0x0020, //SPACE
0x0021, //EXCLAMATION MARK
0x0022, //QUOTATION MARK
0x0023, //NUMBER SIGN
0x0024, //DOLLAR SIGN
0x0025, //PERCENT SIGN
0x0026, //AMPERSAND
0x0027, //APOSTROPHE
0x0028, //LEFT PARENTHESIS
0x0029, //RIGHT PARENTHESIS
0x002A, //ASTERISK
0x002B, //PLUS SIGN
0x002C, //COMMA
0x002D, //HYPHEN-MINUS
0x002E, //FULL STOP
0x002F, //SOLIDUS
0x0030, //DIGIT ZERO
0x0031, //DIGIT ONE
0x0032, //DIGIT TWO
0x0033, //DIGIT THREE
0x0034, //DIGIT FOUR
0x0035, //DIGIT FIVE
0x0036, //DIGIT SIX
0x0037, //DIGIT SEVEN
0x0038, //DIGIT EIGHT
0x0039, //DIGIT NINE
0x003A, //COLON
0x003B, //SEMICOLON
0x003C, //LESS-THAN SIGN
0x003D, //EQUALS SIGN
0x003E, //GREATER-THAN SIGN
0x003F, //QUESTION MARK
0x0040, //COMMERCIAL AT
0x0041, //LATIN CAPITAL LETTER A
0x0042, //LATIN CAPITAL LETTER B
0x0043, //LATIN CAPITAL LETTER C
0x0044, //LATIN CAPITAL LETTER D
0x0045, //LATIN CAPITAL LETTER E
0x0046, //LATIN CAPITAL LETTER F
0x0047, //LATIN CAPITAL LETTER G
0x0048, //LATIN CAPITAL LETTER H
0x0049, //LATIN CAPITAL LETTER I
0x004A, //LATIN CAPITAL LETTER J
0x004B, //LATIN CAPITAL LETTER K
0x004C, //LATIN CAPITAL LETTER L
0x004D, //LATIN CAPITAL LETTER M
0x004E, //LATIN CAPITAL LETTER N
0x004F, //LATIN CAPITAL LETTER O
0x0050, //LATIN CAPITAL LETTER P
0x0051, //LATIN CAPITAL LETTER Q
0x0052, //LATIN CAPITAL LETTER R
0x0053, //LATIN CAPITAL LETTER S
0x0054, //LATIN CAPITAL LETTER T
0x0055, //LATIN CAPITAL LETTER U
0x0056, //LATIN CAPITAL LETTER V
0x0057, //LATIN CAPITAL LETTER W
0x0058, //LATIN CAPITAL LETTER X
0x0059, //LATIN CAPITAL LETTER Y
0x005A, //LATIN CAPITAL LETTER Z
0x005B, //LEFT SQUARE BRACKET
0x005C, //REVERSE SOLIDUS
0x005D, //RIGHT SQUARE BRACKET
0x005E, //CIRCUMFLEX ACCENT
0x005F, //LOW LINE
0x0060, //GRAVE ACCENT
0x0061, //LATIN SMALL LETTER A
0x0062, //LATIN SMALL LETTER B
0x0063, //LATIN SMALL LETTER C
0x0064, //LATIN SMALL LETTER D
0x0065, //LATIN SMALL LETTER E
0x0066, //LATIN SMALL LETTER F
0x0067, //LATIN SMALL LETTER G
0x0068, //LATIN SMALL LETTER H
0x0069, //LATIN SMALL LETTER I
0x006A, //LATIN SMALL LETTER J
0x006B, //LATIN SMALL LETTER K
0x006C, //LATIN SMALL LETTER L
0x006D, //LATIN SMALL LETTER M
0x006E, //LATIN SMALL LETTER N
0x006F, //LATIN SMALL LETTER O
0x0070, //LATIN SMALL LETTER P
0x0071, //LATIN SMALL LETTER Q
0x0072, //LATIN SMALL LETTER R
0x0073, //LATIN SMALL LETTER S
0x0074, //LATIN SMALL LETTER T
0x0075, //LATIN SMALL LETTER U
0x0076, //LATIN SMALL LETTER V
0x0077, //LATIN SMALL LETTER W
0x0078, //LATIN SMALL LETTER X
0x0079, //LATIN SMALL LETTER Y
0x007A, //LATIN SMALL LETTER Z
0x007B, //LEFT CURLY BRACKET
0x007C, //VERTICAL LINE
0x007D, //RIGHT CURLY BRACKET
0x007E, //TILDE
0x007F, //DELETE
0x20AC, //EURO SIGN
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x2026, //HORIZONTAL ELLIPSIS
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x2018, //LEFT SINGLE QUOTATION MARK
0x2019, //RIGHT SINGLE QUOTATION MARK
0x201C, //LEFT DOUBLE QUOTATION MARK
0x201D, //RIGHT DOUBLE QUOTATION MARK
0x2022, //BULLET
0x2013, //EN DASH
0x2014, //EM DASH
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x00A0, //NO-BREAK SPACE
0x0E01, //THAI CHARACTER KO KAI
0x0E02, //THAI CHARACTER KHO KHAI
0x0E03, //THAI CHARACTER KHO KHUAT
0x0E04, //THAI CHARACTER KHO KHWAI
0x0E05, //THAI CHARACTER KHO KHON
0x0E06, //THAI CHARACTER KHO RAKHANG
0x0E07, //THAI CHARACTER NGO NGU
0x0E08, //THAI CHARACTER CHO CHAN
0x0E09, //THAI CHARACTER CHO CHING
0x0E0A, //THAI CHARACTER CHO CHANG
0x0E0B, //THAI CHARACTER SO SO
0x0E0C, //THAI CHARACTER CHO CHOE
0x0E0D, //THAI CHARACTER YO YING
0x0E0E, //THAI CHARACTER DO CHADA
0x0E0F, //THAI CHARACTER TO PATAK
0x0E10, //THAI CHARACTER THO THAN
0x0E11, //THAI CHARACTER THO NANGMONTHO
0x0E12, //THAI CHARACTER THO PHUTHAO
0x0E13, //THAI CHARACTER NO NEN
0x0E14, //THAI CHARACTER DO DEK
0x0E15, //THAI CHARACTER TO TAO
0x0E16, //THAI CHARACTER THO THUNG
0x0E17, //THAI CHARACTER THO THAHAN
0x0E18, //THAI CHARACTER THO THONG
0x0E19, //THAI CHARACTER NO NU
0x0E1A, //THAI CHARACTER BO BAIMAI
0x0E1B, //THAI CHARACTER PO PLA
0x0E1C, //THAI CHARACTER PHO PHUNG
0x0E1D, //THAI CHARACTER FO FA
0x0E1E, //THAI CHARACTER PHO PHAN
0x0E1F, //THAI CHARACTER FO FAN
0x0E20, //THAI CHARACTER PHO SAMPHAO
0x0E21, //THAI CHARACTER MO MA
0x0E22, //THAI CHARACTER YO YAK
0x0E23, //THAI CHARACTER RO RUA
0x0E24, //THAI CHARACTER RU
0x0E25, //THAI CHARACTER LO LING
0x0E26, //THAI CHARACTER LU
0x0E27, //THAI CHARACTER WO WAEN
0x0E28, //THAI CHARACTER SO SALA
0x0E29, //THAI CHARACTER SO RUSI
0x0E2A, //THAI CHARACTER SO SUA
0x0E2B, //THAI CHARACTER HO HIP
0x0E2C, //THAI CHARACTER LO CHULA
0x0E2D, //THAI CHARACTER O ANG
0x0E2E, //THAI CHARACTER HO NOKHUK
0x0E2F, //THAI CHARACTER PAIYANNOI
0x0E30, //THAI CHARACTER SARA A
0x0E31, //THAI CHARACTER MAI HAN-AKAT
0x0E32, //THAI CHARACTER SARA AA
0x0E33, //THAI CHARACTER SARA AM
0x0E34, //THAI CHARACTER SARA I
0x0E35, //THAI CHARACTER SARA II
0x0E36, //THAI CHARACTER SARA UE
0x0E37, //THAI CHARACTER SARA UEE
0x0E38, //THAI CHARACTER SARA U
0x0E39, //THAI CHARACTER SARA UU
0x0E3A, //THAI CHARACTER PHINTHU
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0x0E3F, //THAI CURRENCY SYMBOL BAHT
0x0E40, //THAI CHARACTER SARA E
0x0E41, //THAI CHARACTER SARA AE
0x0E42, //THAI CHARACTER SARA O
0x0E43, //THAI CHARACTER SARA AI MAIMUAN
0x0E44, //THAI CHARACTER SARA AI MAIMALAI
0x0E45, //THAI CHARACTER LAKKHANGYAO
0x0E46, //THAI CHARACTER MAIYAMOK
0x0E47, //THAI CHARACTER MAITAIKHU
0x0E48, //THAI CHARACTER MAI EK
0x0E49, //THAI CHARACTER MAI THO
0x0E4A, //THAI CHARACTER MAI TRI
0x0E4B, //THAI CHARACTER MAI CHATTAWA
0x0E4C, //THAI CHARACTER THANTHAKHAT
0x0E4D, //THAI CHARACTER NIKHAHIT
0x0E4E, //THAI CHARACTER YAMAKKAN
0x0E4F, //THAI CHARACTER FONGMAN
0x0E50, //THAI DIGIT ZERO
0x0E51, //THAI DIGIT ONE
0x0E52, //THAI DIGIT TWO
0x0E53, //THAI DIGIT THREE
0x0E54, //THAI DIGIT FOUR
0x0E55, //THAI DIGIT FIVE
0x0E56, //THAI DIGIT SIX
0x0E57, //THAI DIGIT SEVEN
0x0E58, //THAI DIGIT EIGHT
0x0E59, //THAI DIGIT NINE
0x0E5A, //THAI CHARACTER ANGKHANKHU
0x0E5B, //THAI CHARACTER KHOMUT
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
0xFFFD, //UNDEFINED
},
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -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,265 @@
// Package querytext is the old query parser and parameter substitute process.
// Do not use on new code.
//
// This package is not subject to any API compatibility guarantee.
package querytext
import (
"bytes"
"io"
"strconv"
)
type parser struct {
r *bytes.Reader
w bytes.Buffer
paramCount int
paramMax int
// using map as a set
namedParams map[string]bool
}
func (p *parser) next() (rune, bool) {
ch, _, err := p.r.ReadRune()
if err != nil {
if err != io.EOF {
panic(err)
}
return 0, false
}
return ch, true
}
func (p *parser) unread() {
err := p.r.UnreadRune()
if err != nil {
panic(err)
}
}
func (p *parser) write(ch rune) {
p.w.WriteRune(ch)
}
type stateFunc func(*parser) stateFunc
// ParseParams rewrites the query from using "?" placeholders
// to using "@pN" parameter names that SQL Server will accept.
//
// This function and package is not subject to any API compatibility guarantee.
func ParseParams(query string) (string, int) {
p := &parser{
r: bytes.NewReader([]byte(query)),
namedParams: map[string]bool{},
}
state := parseNormal
for state != nil {
state = state(p)
}
return p.w.String(), p.paramMax + len(p.namedParams)
}
func parseNormal(p *parser) stateFunc {
for {
ch, ok := p.next()
if !ok {
return nil
}
if ch == '?' {
return parseOrdinalParameter
} else if ch == '$' || ch == ':' {
ch2, ok := p.next()
if !ok {
p.write(ch)
return nil
}
p.unread()
if ch2 >= '0' && ch2 <= '9' {
return parseOrdinalParameter
} else if 'a' <= ch2 && ch2 <= 'z' || 'A' <= ch2 && ch2 <= 'Z' {
return parseNamedParameter
}
}
p.write(ch)
switch ch {
case '\'':
return parseQuote
case '"':
return parseDoubleQuote
case '[':
return parseBracket
case '-':
return parseLineComment
case '/':
return parseComment
}
}
}
func parseOrdinalParameter(p *parser) stateFunc {
var paramN int
var ok bool
for {
var ch rune
ch, ok = p.next()
if ok && ch >= '0' && ch <= '9' {
paramN = paramN*10 + int(ch-'0')
} else {
break
}
}
if ok {
p.unread()
}
if paramN == 0 {
p.paramCount++
paramN = p.paramCount
}
if paramN > p.paramMax {
p.paramMax = paramN
}
p.w.WriteString("@p")
p.w.WriteString(strconv.Itoa(paramN))
if !ok {
return nil
}
return parseNormal
}
func parseNamedParameter(p *parser) stateFunc {
var paramName string
var ok bool
for {
var ch rune
ch, ok = p.next()
if ok && (ch >= '0' && ch <= '9' || 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z') {
paramName = paramName + string(ch)
} else {
break
}
}
if ok {
p.unread()
}
p.namedParams[paramName] = true
p.w.WriteString("@")
p.w.WriteString(paramName)
if !ok {
return nil
}
return parseNormal
}
func parseQuote(p *parser) stateFunc {
for {
ch, ok := p.next()
if !ok {
return nil
}
p.write(ch)
if ch == '\'' {
return parseNormal
}
}
}
func parseDoubleQuote(p *parser) stateFunc {
for {
ch, ok := p.next()
if !ok {
return nil
}
p.write(ch)
if ch == '"' {
return parseNormal
}
}
}
func parseBracket(p *parser) stateFunc {
for {
ch, ok := p.next()
if !ok {
return nil
}
p.write(ch)
if ch == ']' {
ch, ok = p.next()
if !ok {
return nil
}
if ch != ']' {
p.unread()
return parseNormal
}
p.write(ch)
}
}
}
func parseLineComment(p *parser) stateFunc {
ch, ok := p.next()
if !ok {
return nil
}
if ch != '-' {
p.unread()
return parseNormal
}
p.write(ch)
for {
ch, ok = p.next()
if !ok {
return nil
}
p.write(ch)
if ch == '\n' {
return parseNormal
}
}
}
func parseComment(p *parser) stateFunc {
var nested int
ch, ok := p.next()
if !ok {
return nil
}
if ch != '*' {
p.unread()
return parseNormal
}
p.write(ch)
for {
ch, ok = p.next()
if !ok {
return nil
}
p.write(ch)
for ch == '*' {
ch, ok = p.next()
if !ok {
return nil
}
p.write(ch)
if ch == '/' {
if nested == 0 {
return parseNormal
} else {
nested--
}
}
}
for ch == '/' {
ch, ok = p.next()
if !ok {
return nil
}
p.write(ch)
if ch == '*' {
nested++
}
}
}
}

@ -0,0 +1,71 @@
package mssql
import (
"context"
"github.com/denisenkom/go-mssqldb/msdsn"
)
const (
logErrors = uint64(msdsn.LogErrors)
logMessages = uint64(msdsn.LogMessages)
logRows = uint64(msdsn.LogRows)
logSQL = uint64(msdsn.LogSQL)
logParams = uint64(msdsn.LogParams)
logTransaction = uint64(msdsn.LogTransaction)
logDebug = uint64(msdsn.LogDebug)
logRetries = uint64(msdsn.LogRetries)
)
// Logger is an interface you can implement to have the go-msqldb
// driver automatically log detailed information on your behalf
type Logger interface {
Printf(format string, v ...interface{})
Println(v ...interface{})
}
// ContextLogger is an interface that provides more information
// than Logger and lets you log messages yourself. This gives you
// more information to log (e.g. trace IDs in the context), more
// control over the logging activity (e.g. log it, trace it, or
// log and trace it, depending on the class of message), and lets
// you log in exactly the format you want.
type ContextLogger interface {
Log(ctx context.Context, category msdsn.Log, msg string)
}
// optionalLogger implements the ContextLogger interface with
// a default "do nothing" behavior that can be overridden by an
// optional ContextLogger supplied by the user.
type optionalLogger struct {
logger ContextLogger
}
// Log does nothing unless the user has specified an optional
// ContextLogger to override the "do nothing" default behavior.
func (o optionalLogger) Log(ctx context.Context, category msdsn.Log, msg string) {
if nil != o.logger {
o.logger.Log(ctx, category, msg)
}
}
// loggerAdapter converts Logger interfaces into ContextLogger
// interfaces. It provides backwards compatibility.
type loggerAdapter struct {
logger Logger
}
// Log passes the message to the underlying Logger interface's
// Println function, emulating the orignal Logger behavior.
func (la loggerAdapter) Log(_ context.Context, category msdsn.Log, msg string) {
// Add prefix for certain categories
switch category {
case msdsn.LogErrors:
msg = "ERROR: " + msg
case msdsn.LogRetries:
msg = "RETRY: " + msg
}
la.logger.Println(msg)
}

@ -0,0 +1,610 @@
package msdsn
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"strconv"
"strings"
"time"
"unicode"
)
type (
Encryption int
Log uint64
)
const (
EncryptionOff = 0
EncryptionRequired = 1
EncryptionDisabled = 3
)
const (
LogErrors Log = 1
LogMessages Log = 2
LogRows Log = 4
LogSQL Log = 8
LogParams Log = 16
LogTransaction Log = 32
LogDebug Log = 64
LogRetries Log = 128
)
type Config struct {
Port uint64
Host string
Instance string
Database string
User string
Password string
Encryption Encryption
TLSConfig *tls.Config
FailOverPartner string
FailOverPort uint64
// If true the TLSConfig servername should use the routed server.
HostInCertificateProvided bool
// Read Only intent for application database.
// NOTE: This does not make queries to most databases read-only.
ReadOnlyIntent bool
LogFlags Log
ServerSPN string
Workstation string
AppName string
// If true disables database/sql's automatic retry of queries
// that start on bad connections.
DisableRetry bool
// Do not use the following.
DialTimeout time.Duration // DialTimeout defaults to 15s. Set negative to disable.
ConnTimeout time.Duration // Use context for timeouts.
KeepAlive time.Duration // Leave at default.
PacketSize uint16
}
func SetupTLS(certificate string, insecureSkipVerify bool, hostInCertificate string) (*tls.Config, error) {
config := tls.Config{
ServerName: hostInCertificate,
InsecureSkipVerify: insecureSkipVerify,
// fix for https://github.com/denisenkom/go-mssqldb/issues/166
// Go implementation of TLS payload size heuristic algorithm splits single TDS package to multiple TCP segments,
// while SQL Server seems to expect one TCP segment per encrypted TDS package.
// Setting DynamicRecordSizingDisabled to true disables that algorithm and uses 16384 bytes per TLS package
DynamicRecordSizingDisabled: true,
}
if len(certificate) == 0 {
return &config, nil
}
pem, err := ioutil.ReadFile(certificate)
if err != nil {
return nil, fmt.Errorf("cannot read certificate %q: %w", certificate, err)
}
if strings.Contains(config.ServerName, ":") && !insecureSkipVerify {
err := setupTLSCommonName(&config, pem)
if err != skipSetup {
return &config, err
}
}
certs := x509.NewCertPool()
certs.AppendCertsFromPEM(pem)
config.RootCAs = certs
return &config, nil
}
var skipSetup = errors.New("skip setting up TLS")
func Parse(dsn string) (Config, map[string]string, error) {
p := Config{}
var params map[string]string
if strings.HasPrefix(dsn, "odbc:") {
parameters, err := splitConnectionStringOdbc(dsn[len("odbc:"):])
if err != nil {
return p, params, err
}
params = parameters
} else if strings.HasPrefix(dsn, "sqlserver://") {
parameters, err := splitConnectionStringURL(dsn)
if err != nil {
return p, params, err
}
params = parameters
} else {
params = splitConnectionString(dsn)
}
strlog, ok := params["log"]
if ok {
flags, err := strconv.ParseUint(strlog, 10, 64)
if err != nil {
return p, params, fmt.Errorf("invalid log parameter '%s': %s", strlog, err.Error())
}
p.LogFlags = Log(flags)
}
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 = 0
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, params, 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\
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, params, 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, params, fmt.Errorf(f, strconntimeout, err.Error())
}
p.ConnTimeout = time.Duration(timeout) * time.Second
}
p.DialTimeout = 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, params, fmt.Errorf(f, strdialtimeout, err.Error())
}
p.DialTimeout = 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, params, fmt.Errorf(f, keepAlive, err.Error())
}
p.KeepAlive = time.Duration(timeout) * time.Second
}
var (
trustServerCert = false
certificate = ""
hostInCertificate = ""
)
encrypt, ok := params["encrypt"]
if ok {
if strings.EqualFold(encrypt, "DISABLE") {
p.Encryption = EncryptionDisabled
} else {
e, err := strconv.ParseBool(encrypt)
if err != nil {
f := "invalid encrypt '%s': %s"
return p, params, fmt.Errorf(f, encrypt, err.Error())
}
if e {
p.Encryption = EncryptionRequired
}
}
} else {
trustServerCert = true
}
trust, ok := params["trustservercertificate"]
if ok {
var err error
trustServerCert, err = strconv.ParseBool(trust)
if err != nil {
f := "invalid trust server certificate '%s': %s"
return p, params, fmt.Errorf(f, trust, err.Error())
}
}
certificate = params["certificate"]
hostInCertificate, ok = params["hostnameincertificate"]
if ok {
p.HostInCertificateProvided = true
} else {
hostInCertificate = p.Host
p.HostInCertificateProvided = false
}
if p.Encryption != EncryptionDisabled {
var err error
p.TLSConfig, err = SetupTLS(certificate, trustServerCert, hostInCertificate)
if err != nil {
return p, params, fmt.Errorf("failed to setup TLS: %w", err)
}
}
serverSPN, ok := params["serverspn"]
if ok {
p.ServerSPN = serverSPN
} else {
p.ServerSPN = generateSpn(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" {
if p.Database == "" {
return p, params, fmt.Errorf("database must be specified when ApplicationIntent is ReadOnly")
}
p.ReadOnlyIntent = true
}
}
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 failover port '%v': %v"
return p, params, fmt.Errorf(f, failOverPort, err.Error())
}
}
disableRetry, ok := params["disableretry"]
if ok {
var err error
p.DisableRetry, err = strconv.ParseBool(disableRetry)
if err != nil {
f := "invalid disableRetry '%s': %s"
return p, params, fmt.Errorf(f, disableRetry, err.Error())
}
} else {
p.DisableRetry = disableRetryDefault
}
return p, params, nil
}
// convert connectionParams to url style connection string
// used mostly for testing
func (p Config) URL() *url.URL {
q := url.Values{}
if p.Database != "" {
q.Add("database", p.Database)
}
if p.LogFlags != 0 {
q.Add("log", strconv.FormatUint(uint64(p.LogFlags), 10))
}
host := p.Host
if p.Port > 0 {
host = fmt.Sprintf("%s:%d", p.Host, p.Port)
}
q.Add("disableRetry", fmt.Sprintf("%t", p.DisableRetry))
res := url.URL{
Scheme: "sqlserver",
Host: host,
User: url.UserPassword(p.User, p.Password),
}
if p.Instance != "" {
res.Path = p.Instance
}
if len(q) > 0 {
res.RawQuery = q.Encode()
}
return &res
}
var adoSynonyms = map[string]string{
"application name": "app name",
"data source": "server",
"address": "server",
"network address": "server",
"addr": "server",
"user": "user id",
"uid": "user id",
"initial catalog": "database",
}
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])
}
synonym, hasSynonym := adoSynonyms[name]
if hasSynonym {
name = synonym
}
// "server" in ADO can include a protocol and a port.
// We only support tcp protocol
if name == "server" {
value = strings.TrimPrefix(value, "tcp:")
serverParts := strings.Split(value, ",")
if len(serverParts) == 2 && len(serverParts[1]) > 0 {
value = serverParts[0]
res["port"] = serverParts[1]
}
}
res[name] = value
}
return res
}
// Splits a URL of the form sqlserver://username:password@host/instance?param1=value&param2=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))
}
func generateSpn(host string, port uint64) string {
return fmt.Sprintf("MSSQLSvc/%s:%d", host, port)
}

@ -0,0 +1,35 @@
//go:build go1.15
// +build go1.15
package msdsn
import (
"crypto/tls"
"crypto/x509"
"fmt"
)
func setupTLSCommonName(config *tls.Config, pem []byte) error {
// fix for https://github.com/denisenkom/go-mssqldb/issues/704
// A SSL/TLS certificate Common Name (CN) containing the ":" character
// (which is a non-standard character) will cause normal verification to fail.
// Since the VerifyConnection callback runs after normal certificate
// verification, confirm that SetupTLS() has been called
// with "insecureSkipVerify=false", then InsecureSkipVerify must be set to true
// for this VerifyConnection callback to accomplish certificate verification.
config.InsecureSkipVerify = true
config.VerifyConnection = func(cs tls.ConnectionState) error {
commonName := cs.PeerCertificates[0].Subject.CommonName
if commonName != cs.ServerName {
return fmt.Errorf("invalid certificate name %q, expected %q", commonName, cs.ServerName)
}
opts := x509.VerifyOptions{
Roots: nil,
Intermediates: x509.NewCertPool(),
}
opts.Intermediates.AppendCertsFromPEM(pem)
_, err := cs.PeerCertificates[0].Verify(opts)
return err
}
return nil
}

@ -0,0 +1,12 @@
//go:build !go1.15
// +build !go1.15
package msdsn
import "crypto/tls"
func setupTLSCommonName(config *tls.Config, pem []byte) error {
// Prior to Go 1.15, the TLS allowed ":" when checking the hostname.
// See https://golang.org/issue/40748 for details.
return skipSetup
}

@ -0,0 +1,9 @@
// +build go1.18
package msdsn
// disableRetryDefault is false for Go versions 1.18 and higher. This matches
// the behavior requested in issue #586. A query that fails at the start due to
// a bad connection is automatically retried. An error is returned only if the
// query fails all of its retries.
const disableRetryDefault bool = false

@ -0,0 +1,9 @@
// +build !go1.18
package msdsn
// disableRetryDefault is true for versions of Go less than 1.18. This matches
// the behavior requested in issue #275. A query that fails at the start due to
// a bad connection is not retried. Instead, the detailed error is immediately
// returned to the caller.
const disableRetryDefault bool = true

File diff suppressed because it is too large Load Diff

@ -0,0 +1,52 @@
// +build go1.10
package mssql
import (
"context"
"database/sql/driver"
"errors"
)
var _ driver.Connector = &Connector{}
var _ driver.SessionResetter = &Conn{}
func (c *Conn) ResetSession(ctx context.Context) error {
if !c.connectionGood {
return driver.ErrBadConn
}
c.resetSession = true
if c.connector == nil || len(c.connector.SessionInitSQL) == 0 {
return nil
}
s, err := c.prepareContext(ctx, c.connector.SessionInitSQL)
if err != nil {
return driver.ErrBadConn
}
_, err = s.exec(ctx, nil)
if err != nil {
return driver.ErrBadConn
}
return nil
}
// Connect to the server and return a TDS connection.
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
conn, err := c.driver.connect(ctx, c, c.params)
if err == nil {
err = conn.ResetSession(ctx)
}
return conn, err
}
// Driver underlying the Connector.
func (c *Connector) Driver() driver.Driver {
return c.driver
}
func (r *Result) LastInsertId() (int64, error) {
return -1, errors.New("LastInsertId is not supported. Please use the OUTPUT clause or add `select ID = convert(bigint, SCOPE_IDENTITY())` to the end of your query")
}

@ -0,0 +1,31 @@
// +build !go1.10
package mssql
import (
"database/sql/driver"
"errors"
)
func (r *Result) LastInsertId() (int64, error) {
s, err := r.c.Prepare("select cast(@@identity as bigint)")
if err != nil {
return 0, err
}
defer s.Close()
rows, err := s.Query(nil)
if err != nil {
return 0, err
}
defer rows.Close()
dest := make([]driver.Value, 1)
err = rows.Next(dest)
if err != nil {
return 0, err
}
if dest[0] == nil {
return -1, errors.New("There is no generated identity value")
}
lastInsertId := dest[0].(int64)
return lastInsertId, nil
}

@ -0,0 +1,14 @@
// +build go1.18
package mssql
// newRetryableError returns an error that allows the database/sql package
// to automatically retry the failed query. Versions of Go 1.18 and higher
// use errors.Is to determine whether or not a failed query can be retried.
// Therefore, we wrap the underlying error in a RetryableError that both
// implements errors.Is for automatic retry and maintains the error details.
func newRetryableError(err error) error {
return RetryableError{
err: err,
}
}

@ -0,0 +1,17 @@
// +build !go1.18
package mssql
import (
"database/sql/driver"
)
// newRetryableError returns an error that allows the database/sql package
// to automatically retry the failed query. Versions of Go lower than 1.18
// compare directly to the sentinel error driver.ErrBadConn to determine
// whether or not a failed query can be retried. Therefore, we replace the
// actual error with driver.ErrBadConn, enabling retry but losing the error
// details.
func newRetryableError(err error) error {
return driver.ErrBadConn
}

@ -0,0 +1,207 @@
// +build go1.9
package mssql
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"reflect"
"time"
"github.com/golang-sql/sqlexp"
// "github.com/cockroachdb/apd"
"github.com/golang-sql/civil"
)
// Type alias provided for compatibility.
type MssqlDriver = Driver // Deprecated: users should transition to the new name when possible.
type MssqlBulk = Bulk // Deprecated: users should transition to the new name when possible.
type MssqlBulkOptions = BulkOptions // Deprecated: users should transition to the new name when possible.
type MssqlConn = Conn // Deprecated: users should transition to the new name when possible.
type MssqlResult = Result // Deprecated: users should transition to the new name when possible.
type MssqlRows = Rows // Deprecated: users should transition to the new name when possible.
type MssqlStmt = Stmt // Deprecated: users should transition to the new name when possible.
var _ driver.NamedValueChecker = &Conn{}
// VarChar parameter types.
type VarChar string
type NVarCharMax string
type VarCharMax string
// DateTime1 encodes parameters to original DateTime SQL types.
type DateTime1 time.Time
// DateTimeOffset encodes parameters to DateTimeOffset, preserving the UTC offset.
type DateTimeOffset time.Time
func convertInputParameter(val interface{}) (interface{}, error) {
switch v := val.(type) {
case VarChar:
return val, nil
case NVarCharMax:
return val, nil
case VarCharMax:
return val, nil
case DateTime1:
return val, nil
case DateTimeOffset:
return val, nil
case civil.Date:
return val, nil
case civil.DateTime:
return val, nil
case civil.Time:
return val, nil
// case *apd.Decimal:
// return nil
default:
return driver.DefaultParameterConverter.ConvertValue(v)
}
}
func (c *Conn) CheckNamedValue(nv *driver.NamedValue) error {
switch v := nv.Value.(type) {
case sql.Out:
if c.outs.params == nil {
c.outs.params = make(map[string]interface{})
}
c.outs.params[nv.Name] = v.Dest
if v.Dest == nil {
return errors.New("destination is a nil pointer")
}
dest_info := reflect.ValueOf(v.Dest)
if dest_info.Kind() != reflect.Ptr {
return errors.New("destination not a pointer")
}
if dest_info.IsNil() {
return errors.New("destination is a nil pointer")
}
pointed_value := reflect.Indirect(dest_info)
// don't allow pointer to a pointer, only pointer to a value can be handled
// correctly
if pointed_value.Kind() == reflect.Ptr {
return errors.New("destination is a pointer to a pointer")
}
// Unwrap the Out value and check the inner value.
val := pointed_value.Interface()
if val == nil {
return errors.New("MSSQL does not allow NULL value without type for OUTPUT parameters")
}
conv, err := convertInputParameter(val)
if err != nil {
return err
}
if conv == nil {
// if we replace with nil we would lose type information
nv.Value = sql.Out{Dest: val}
} else {
nv.Value = sql.Out{Dest: conv}
}
return nil
case *ReturnStatus:
*v = 0 // By default the return value should be zero.
c.outs.returnStatus = v
return driver.ErrRemoveArgument
case TVP:
return nil
case *sqlexp.ReturnMessage:
sqlexp.ReturnMessageInit(v)
c.outs.msgq = v
return driver.ErrRemoveArgument
default:
var err error
nv.Value, err = convertInputParameter(nv.Value)
return err
}
}
func (s *Stmt) makeParamExtra(val driver.Value) (res param, err error) {
switch val := val.(type) {
case VarChar:
res.ti.TypeId = typeBigVarChar
res.buffer = []byte(val)
res.ti.Size = len(res.buffer)
case VarCharMax:
res.ti.TypeId = typeBigVarChar
res.buffer = []byte(val)
res.ti.Size = 0 // currently zero forces varchar(max)
case NVarCharMax:
res.ti.TypeId = typeNVarChar
res.buffer = str2ucs2(string(val))
res.ti.Size = 0 // currently zero forces nvarchar(max)
case DateTime1:
t := time.Time(val)
res.ti.TypeId = typeDateTimeN
res.buffer = encodeDateTime(t)
res.ti.Size = len(res.buffer)
case DateTimeOffset:
res.ti.TypeId = typeDateTimeOffsetN
res.ti.Scale = 7
res.buffer = encodeDateTimeOffset(time.Time(val), int(res.ti.Scale))
res.ti.Size = len(res.buffer)
case civil.Date:
res.ti.TypeId = typeDateN
res.buffer = encodeDate(val.In(time.UTC))
res.ti.Size = len(res.buffer)
case civil.DateTime:
res.ti.TypeId = typeDateTime2N
res.ti.Scale = 7
res.buffer = encodeDateTime2(val.In(time.UTC), int(res.ti.Scale))
res.ti.Size = len(res.buffer)
case civil.Time:
res.ti.TypeId = typeTimeN
res.ti.Scale = 7
res.buffer = encodeTime(val.Hour, val.Minute, val.Second, val.Nanosecond, int(res.ti.Scale))
res.ti.Size = len(res.buffer)
case sql.Out:
res, err = s.makeParam(val.Dest)
res.Flags = fByRevValue
case TVP:
err = val.check()
if err != nil {
return
}
schema, name, errGetName := getSchemeAndName(val.TypeName)
if errGetName != nil {
return
}
res.ti.UdtInfo.TypeName = name
res.ti.UdtInfo.SchemaName = schema
res.ti.TypeId = typeTvp
columnStr, tvpFieldIndexes, errCalTypes := val.columnTypes()
if errCalTypes != nil {
err = errCalTypes
return
}
res.buffer, err = val.encode(schema, name, columnStr, tvpFieldIndexes)
if err != nil {
return
}
res.ti.Size = len(res.buffer)
default:
err = fmt.Errorf("mssql: unknown type for %T", val)
}
return
}
func scanIntoOut(name string, fromServer, scanInto interface{}) error {
return convertAssign(scanInto, fromServer)
}
func isOutputValue(val driver.Value) bool {
_, out := val.(sql.Out)
return out
}

@ -0,0 +1,20 @@
// +build !go1.9
package mssql
import (
"database/sql/driver"
"fmt"
)
func (s *Stmt) makeParamExtra(val driver.Value) (param, error) {
return param{}, fmt.Errorf("mssql: unknown type for %T", val)
}
func scanIntoOut(name string, fromServer, scanInto interface{}) error {
return fmt.Errorf("mssql: unsupported OUTPUT type, use a newer Go version")
}
func isOutputValue(val driver.Value) bool {
return false
}

@ -0,0 +1,168 @@
package mssql
import (
"fmt"
"net"
"time"
)
type timeoutConn struct {
c net.Conn
timeout time.Duration
}
func newTimeoutConn(conn net.Conn, timeout time.Duration) *timeoutConn {
return &timeoutConn{
c: conn,
timeout: timeout,
}
}
func (c *timeoutConn) Read(b []byte) (n int, err error) {
if c.timeout > 0 {
err = c.c.SetDeadline(time.Now().Add(c.timeout))
if err != nil {
return
}
}
return c.c.Read(b)
}
func (c *timeoutConn) Write(b []byte) (n int, err error) {
if c.timeout > 0 {
err = c.c.SetDeadline(time.Now().Add(c.timeout))
if err != nil {
return
}
}
return c.c.Write(b)
}
func (c timeoutConn) Close() error {
return c.c.Close()
}
func (c timeoutConn) LocalAddr() net.Addr {
return c.c.LocalAddr()
}
func (c timeoutConn) RemoteAddr() net.Addr {
return c.c.RemoteAddr()
}
func (c timeoutConn) SetDeadline(t time.Time) error {
return c.c.SetDeadline(t)
}
func (c timeoutConn) SetReadDeadline(t time.Time) error {
return c.c.SetReadDeadline(t)
}
func (c timeoutConn) SetWriteDeadline(t time.Time) error {
return c.c.SetWriteDeadline(t)
}
// this connection is used during TLS Handshake
// TDS protocol requires TLS handshake messages to be sent inside TDS packets
type tlsHandshakeConn struct {
buf *tdsBuffer
packetPending bool
continueRead bool
}
func (c *tlsHandshakeConn) Read(b []byte) (n int, err error) {
if c.packetPending {
c.packetPending = false
err = c.buf.FinishPacket()
if err != nil {
err = fmt.Errorf("cannot send handshake packet: %s", err.Error())
return
}
c.continueRead = false
}
if !c.continueRead {
var packet packetType
packet, err = c.buf.BeginRead()
if err != nil {
err = fmt.Errorf("cannot read handshake packet: %s", err.Error())
return
}
if packet != packPrelogin {
err = fmt.Errorf("unexpected packet %d, expecting prelogin", packet)
return
}
c.continueRead = true
}
return c.buf.Read(b)
}
func (c *tlsHandshakeConn) Write(b []byte) (n int, err error) {
if !c.packetPending {
c.buf.BeginPacket(packPrelogin, false)
c.packetPending = true
}
return c.buf.Write(b)
}
func (c *tlsHandshakeConn) Close() error {
return c.buf.transport.Close()
}
func (c *tlsHandshakeConn) LocalAddr() net.Addr {
return nil
}
func (c *tlsHandshakeConn) RemoteAddr() net.Addr {
return nil
}
func (c *tlsHandshakeConn) SetDeadline(_ time.Time) error {
return nil
}
func (c *tlsHandshakeConn) SetReadDeadline(_ time.Time) error {
return nil
}
func (c *tlsHandshakeConn) SetWriteDeadline(_ time.Time) error {
return nil
}
// this connection just delegates all methods to it's wrapped connection
// it also allows switching underlying connection on the fly
// it is needed because tls.Conn does not allow switching underlying connection
type passthroughConn struct {
c net.Conn
}
func (c passthroughConn) Read(b []byte) (n int, err error) {
return c.c.Read(b)
}
func (c passthroughConn) Write(b []byte) (n int, err error) {
return c.c.Write(b)
}
func (c passthroughConn) Close() error {
return c.c.Close()
}
func (c passthroughConn) LocalAddr() net.Addr {
return c.c.LocalAddr()
}
func (c passthroughConn) RemoteAddr() net.Addr {
return c.c.RemoteAddr()
}
func (c passthroughConn) SetDeadline(t time.Time) error {
return c.c.SetDeadline(t)
}
func (c passthroughConn) SetReadDeadline(t time.Time) error {
return c.c.SetReadDeadline(t)
}
func (c passthroughConn) SetWriteDeadline(t time.Time) error {
return c.c.SetWriteDeadline(t)
}

@ -0,0 +1,393 @@
// +build !windows
package mssql
import (
"crypto/des"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"strings"
"time"
"unicode/utf16"
//lint:ignore SA1019 MD4 is used by legacy NTLM
"golang.org/x/crypto/md4"
)
const (
_NEGOTIATE_MESSAGE = 1
_CHALLENGE_MESSAGE = 2
_AUTHENTICATE_MESSAGE = 3
)
const (
_NEGOTIATE_UNICODE = 0x00000001
_NEGOTIATE_OEM = 0x00000002
_NEGOTIATE_TARGET = 0x00000004
_NEGOTIATE_SIGN = 0x00000010
_NEGOTIATE_SEAL = 0x00000020
_NEGOTIATE_DATAGRAM = 0x00000040
_NEGOTIATE_LMKEY = 0x00000080
_NEGOTIATE_NTLM = 0x00000200
_NEGOTIATE_ANONYMOUS = 0x00000800
_NEGOTIATE_OEM_DOMAIN_SUPPLIED = 0x00001000
_NEGOTIATE_OEM_WORKSTATION_SUPPLIED = 0x00002000
_NEGOTIATE_ALWAYS_SIGN = 0x00008000
_NEGOTIATE_TARGET_TYPE_DOMAIN = 0x00010000
_NEGOTIATE_TARGET_TYPE_SERVER = 0x00020000
_NEGOTIATE_EXTENDED_SESSIONSECURITY = 0x00080000
_NEGOTIATE_IDENTIFY = 0x00100000
_REQUEST_NON_NT_SESSION_KEY = 0x00400000
_NEGOTIATE_TARGET_INFO = 0x00800000
_NEGOTIATE_VERSION = 0x02000000
_NEGOTIATE_128 = 0x20000000
_NEGOTIATE_KEY_EXCH = 0x40000000
_NEGOTIATE_56 = 0x80000000
)
const _NEGOTIATE_FLAGS = _NEGOTIATE_UNICODE |
_NEGOTIATE_NTLM |
_NEGOTIATE_OEM_DOMAIN_SUPPLIED |
_NEGOTIATE_OEM_WORKSTATION_SUPPLIED |
_NEGOTIATE_ALWAYS_SIGN |
_NEGOTIATE_EXTENDED_SESSIONSECURITY
type ntlmAuth struct {
Domain string
UserName string
Password string
Workstation string
}
func getAuth(user, password, service, workstation string) (auth, bool) {
if !strings.ContainsRune(user, '\\') {
return nil, false
}
domain_user := strings.SplitN(user, "\\", 2)
return &ntlmAuth{
Domain: domain_user[0],
UserName: domain_user[1],
Password: password,
Workstation: workstation,
}, true
}
func utf16le(val string) []byte {
var v []byte
for _, r := range val {
if utf16.IsSurrogate(r) {
r1, r2 := utf16.EncodeRune(r)
v = append(v, byte(r1), byte(r1>>8))
v = append(v, byte(r2), byte(r2>>8))
} else {
v = append(v, byte(r), byte(r>>8))
}
}
return v
}
func (auth *ntlmAuth) InitialBytes() ([]byte, error) {
domain_len := len(auth.Domain)
workstation_len := len(auth.Workstation)
msg := make([]byte, 40+domain_len+workstation_len)
copy(msg, []byte("NTLMSSP\x00"))
binary.LittleEndian.PutUint32(msg[8:], _NEGOTIATE_MESSAGE)
binary.LittleEndian.PutUint32(msg[12:], _NEGOTIATE_FLAGS)
// Domain Name Fields
binary.LittleEndian.PutUint16(msg[16:], uint16(domain_len))
binary.LittleEndian.PutUint16(msg[18:], uint16(domain_len))
binary.LittleEndian.PutUint32(msg[20:], 40)
// Workstation Fields
binary.LittleEndian.PutUint16(msg[24:], uint16(workstation_len))
binary.LittleEndian.PutUint16(msg[26:], uint16(workstation_len))
binary.LittleEndian.PutUint32(msg[28:], uint32(40+domain_len))
// Version
binary.LittleEndian.PutUint32(msg[32:], 0)
binary.LittleEndian.PutUint32(msg[36:], 0)
// Payload
copy(msg[40:], auth.Domain)
copy(msg[40+domain_len:], auth.Workstation)
return msg, nil
}
var errorNTLM = errors.New("NTLM protocol error")
func createDesKey(bytes, material []byte) {
material[0] = bytes[0]
material[1] = (byte)(bytes[0]<<7 | (bytes[1]&0xff)>>1)
material[2] = (byte)(bytes[1]<<6 | (bytes[2]&0xff)>>2)
material[3] = (byte)(bytes[2]<<5 | (bytes[3]&0xff)>>3)
material[4] = (byte)(bytes[3]<<4 | (bytes[4]&0xff)>>4)
material[5] = (byte)(bytes[4]<<3 | (bytes[5]&0xff)>>5)
material[6] = (byte)(bytes[5]<<2 | (bytes[6]&0xff)>>6)
material[7] = (byte)(bytes[6] << 1)
}
func encryptDes(key []byte, cleartext []byte, ciphertext []byte) {
var desKey [8]byte
createDesKey(key, desKey[:])
cipher, err := des.NewCipher(desKey[:])
if err != nil {
panic(err)
}
cipher.Encrypt(ciphertext, cleartext)
}
func response(challenge [8]byte, hash [21]byte) (ret [24]byte) {
encryptDes(hash[:7], challenge[:], ret[:8])
encryptDes(hash[7:14], challenge[:], ret[8:16])
encryptDes(hash[14:], challenge[:], ret[16:])
return
}
func lmHash(password string) (hash [21]byte) {
var lmpass [14]byte
copy(lmpass[:14], []byte(strings.ToUpper(password)))
magic := []byte("KGS!@#$%")
encryptDes(lmpass[:7], magic, hash[:8])
encryptDes(lmpass[7:], magic, hash[8:])
return
}
func lmResponse(challenge [8]byte, password string) [24]byte {
hash := lmHash(password)
return response(challenge, hash)
}
func ntlmHash(password string) (hash [21]byte) {
h := md4.New()
h.Write(utf16le(password))
h.Sum(hash[:0])
return
}
func ntResponse(challenge [8]byte, password string) [24]byte {
hash := ntlmHash(password)
return response(challenge, hash)
}
func clientChallenge() (nonce [8]byte) {
_, err := rand.Read(nonce[:])
if err != nil {
panic(err)
}
return
}
func ntlmSessionResponse(clientNonce [8]byte, serverChallenge [8]byte, password string) [24]byte {
var sessionHash [16]byte
h := md5.New()
h.Write(serverChallenge[:])
h.Write(clientNonce[:])
h.Sum(sessionHash[:0])
var hash [8]byte
copy(hash[:], sessionHash[:8])
passwordHash := ntlmHash(password)
return response(hash, passwordHash)
}
func ntlmHashNoPadding(val string) []byte {
hash := make([]byte, 16)
h := md4.New()
h.Write(utf16le(val))
h.Sum(hash[:0])
return hash
}
func hmacMD5(passwordHash, data []byte) []byte {
hmacEntity := hmac.New(md5.New, passwordHash)
hmacEntity.Write(data)
return hmacEntity.Sum(nil)
}
func getNTLMv2AndLMv2ResponsePayloads(userDomain, username, password string, challenge, nonce [8]byte, targetInfoFields []byte, timestamp time.Time) (ntlmV2Payload, lmV2Payload []byte) {
// NTLMv2 response payload: http://davenport.sourceforge.net/ntlm.html#theNtlmv2Response
ntlmHash := ntlmHashNoPadding(password)
usernameAndTargetBytes := utf16le(strings.ToUpper(username) + userDomain)
ntlmV2Hash := hmacMD5(ntlmHash, usernameAndTargetBytes)
targetInfoLength := len(targetInfoFields)
blob := make([]byte, 32+targetInfoLength)
binary.BigEndian.PutUint32(blob[:4], 0x01010000)
binary.BigEndian.PutUint32(blob[4:8], 0x00000000)
binary.BigEndian.PutUint64(blob[8:16], uint64(timestamp.UnixNano()))
copy(blob[16:24], nonce[:])
binary.BigEndian.PutUint32(blob[24:28], 0x00000000)
copy(blob[28:], targetInfoFields)
binary.BigEndian.PutUint32(blob[28+targetInfoLength:], 0x00000000)
challengeLength := len(challenge)
blobLength := len(blob)
challengeAndBlob := make([]byte, challengeLength+blobLength)
copy(challengeAndBlob[:challengeLength], challenge[:])
copy(challengeAndBlob[challengeLength:], blob)
hashedChallenge := hmacMD5(ntlmV2Hash, challengeAndBlob)
ntlmV2Payload = append(hashedChallenge, blob...)
// LMv2 response payload: http://davenport.sourceforge.net/ntlm.html#theLmv2Response
ntlmV2hash := hmacMD5(ntlmHash, usernameAndTargetBytes)
challengeAndNonce := make([]byte, 16)
copy(challengeAndNonce[:8], challenge[:])
copy(challengeAndNonce[8:], nonce[:])
hashedChallenge = hmacMD5(ntlmV2hash, challengeAndNonce)
lmV2Payload = append(hashedChallenge, nonce[:]...)
return
}
func negotiateExtendedSessionSecurity(flags uint32, message []byte, challenge [8]byte, username, password, userDom string) (lm, nt []byte, err error) {
nonce := clientChallenge()
// Official specification: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/b38c36ed-2804-4868-a9ff-8dd3182128e4
// Unofficial walk through referenced by https://www.freetds.org/userguide/domains.htm: http://davenport.sourceforge.net/ntlm.html
if (flags & _NEGOTIATE_TARGET_INFO) != 0 {
targetInfoFields, err := getNTLMv2TargetInfoFields(message)
if err != nil {
return lm, nt, err
}
nt, lm = getNTLMv2AndLMv2ResponsePayloads(userDom, username, password, challenge, nonce, targetInfoFields, time.Now())
return lm, nt, nil
}
var lm_bytes [24]byte
copy(lm_bytes[:8], nonce[:])
lm = lm_bytes[:]
nt_bytes := ntlmSessionResponse(nonce, challenge, password)
nt = nt_bytes[:]
return lm, nt, nil
}
func getNTLMv2TargetInfoFields(type2Message []byte) (info []byte, err error) {
type2MessageError := "mssql: while parsing NTLMv2 type 2 message, length %d too small for offset %d"
type2MessageLength := len(type2Message)
if type2MessageLength < 20 {
return nil, fmt.Errorf(type2MessageError, type2MessageLength, 20)
}
targetNameAllocated := binary.LittleEndian.Uint16(type2Message[14:16])
targetNameOffset := binary.LittleEndian.Uint32(type2Message[16:20])
endOfOffset := int(targetNameOffset + uint32(targetNameAllocated))
if type2MessageLength < endOfOffset {
return nil, fmt.Errorf(type2MessageError, type2MessageLength, endOfOffset)
}
targetInformationAllocated := binary.LittleEndian.Uint16(type2Message[42:44])
targetInformationDataOffset := binary.LittleEndian.Uint32(type2Message[44:48])
endOfOffset = int(targetInformationDataOffset + uint32(targetInformationAllocated))
if type2MessageLength < endOfOffset {
return nil, fmt.Errorf(type2MessageError, type2MessageLength, endOfOffset)
}
targetInformationBytes := make([]byte, targetInformationAllocated)
copy(targetInformationBytes, type2Message[targetInformationDataOffset:targetInformationDataOffset+uint32(targetInformationAllocated)])
return targetInformationBytes, nil
}
func buildNTLMResponsePayload(lm, nt []byte, flags uint32, domain, workstation, username string) ([]byte, error) {
lm_len := len(lm)
nt_len := len(nt)
domain16 := utf16le(domain)
domain_len := len(domain16)
user16 := utf16le(username)
user_len := len(user16)
workstation16 := utf16le(workstation)
workstation_len := len(workstation16)
msg := make([]byte, 88+lm_len+nt_len+domain_len+user_len+workstation_len)
copy(msg, []byte("NTLMSSP\x00"))
binary.LittleEndian.PutUint32(msg[8:], _AUTHENTICATE_MESSAGE)
// Lm Challenge Response Fields
binary.LittleEndian.PutUint16(msg[12:], uint16(lm_len))
binary.LittleEndian.PutUint16(msg[14:], uint16(lm_len))
binary.LittleEndian.PutUint32(msg[16:], 88)
// Nt Challenge Response Fields
binary.LittleEndian.PutUint16(msg[20:], uint16(nt_len))
binary.LittleEndian.PutUint16(msg[22:], uint16(nt_len))
binary.LittleEndian.PutUint32(msg[24:], uint32(88+lm_len))
// Domain Name Fields
binary.LittleEndian.PutUint16(msg[28:], uint16(domain_len))
binary.LittleEndian.PutUint16(msg[30:], uint16(domain_len))
binary.LittleEndian.PutUint32(msg[32:], uint32(88+lm_len+nt_len))
// User Name Fields
binary.LittleEndian.PutUint16(msg[36:], uint16(user_len))
binary.LittleEndian.PutUint16(msg[38:], uint16(user_len))
binary.LittleEndian.PutUint32(msg[40:], uint32(88+lm_len+nt_len+domain_len))
// Workstation Fields
binary.LittleEndian.PutUint16(msg[44:], uint16(workstation_len))
binary.LittleEndian.PutUint16(msg[46:], uint16(workstation_len))
binary.LittleEndian.PutUint32(msg[48:], uint32(88+lm_len+nt_len+domain_len+user_len))
// Encrypted Random Session Key Fields
binary.LittleEndian.PutUint16(msg[52:], 0)
binary.LittleEndian.PutUint16(msg[54:], 0)
binary.LittleEndian.PutUint32(msg[56:], uint32(88+lm_len+nt_len+domain_len+user_len+workstation_len))
// Negotiate Flags
binary.LittleEndian.PutUint32(msg[60:], flags)
// Version
binary.LittleEndian.PutUint32(msg[64:], 0)
binary.LittleEndian.PutUint32(msg[68:], 0)
// MIC
binary.LittleEndian.PutUint32(msg[72:], 0)
binary.LittleEndian.PutUint32(msg[76:], 0)
binary.LittleEndian.PutUint32(msg[88:], 0)
binary.LittleEndian.PutUint32(msg[84:], 0)
// Payload
copy(msg[88:], lm)
copy(msg[88+lm_len:], nt)
copy(msg[88+lm_len+nt_len:], domain16)
copy(msg[88+lm_len+nt_len+domain_len:], user16)
copy(msg[88+lm_len+nt_len+domain_len+user_len:], workstation16)
return msg, nil
}
func (auth *ntlmAuth) NextBytes(bytes []byte) ([]byte, error) {
signature := string(bytes[0:8])
if signature != "NTLMSSP\x00" {
return nil, errorNTLM
}
messageTypeIndicator := binary.LittleEndian.Uint32(bytes[8:12])
if messageTypeIndicator != _CHALLENGE_MESSAGE {
return nil, errorNTLM
}
var challenge [8]byte
copy(challenge[:], bytes[24:32])
flags := binary.LittleEndian.Uint32(bytes[20:24])
if (flags & _NEGOTIATE_EXTENDED_SESSIONSECURITY) != 0 {
lm, nt, err := negotiateExtendedSessionSecurity(flags, bytes, challenge, auth.UserName, auth.Password, auth.Domain)
if err != nil {
return nil, err
}
return buildNTLMResponsePayload(lm, nt, flags, auth.Domain, auth.Workstation, auth.UserName)
}
lm_bytes := lmResponse(challenge, auth.Password)
lm := lm_bytes[:]
nt_bytes := ntResponse(challenge, auth.Password)
nt := nt_bytes[:]
return buildNTLMResponsePayload(lm, nt, flags, auth.Domain, auth.Workstation, auth.UserName)
}
func (auth *ntlmAuth) Free() {
}

@ -0,0 +1,83 @@
package mssql
import (
"encoding/binary"
)
type procId struct {
id uint16
name string
}
// parameter flags
const (
fByRevValue = 1
fDefaultValue = 2
)
type param struct {
Name string
Flags uint8
ti typeInfo
buffer []byte
}
var (
sp_Cursor = procId{1, ""}
sp_CursorOpen = procId{2, ""}
sp_CursorPrepare = procId{3, ""}
sp_CursorExecute = procId{4, ""}
sp_CursorPrepExec = procId{5, ""}
sp_CursorUnprepare = procId{6, ""}
sp_CursorFetch = procId{7, ""}
sp_CursorOption = procId{8, ""}
sp_CursorClose = procId{9, ""}
sp_ExecuteSql = procId{10, ""}
sp_Prepare = procId{11, ""}
sp_PrepExec = procId{13, ""}
sp_PrepExecRpc = procId{14, ""}
sp_Unprepare = procId{15, ""}
)
// http://msdn.microsoft.com/en-us/library/dd357576.aspx
func sendRpc(buf *tdsBuffer, headers []headerStruct, proc procId, flags uint16, params []param, resetSession bool) (err error) {
buf.BeginPacket(packRPCRequest, resetSession)
writeAllHeaders(buf, headers)
if len(proc.name) == 0 {
var idswitch uint16 = 0xffff
err = binary.Write(buf, binary.LittleEndian, &idswitch)
if err != nil {
return
}
err = binary.Write(buf, binary.LittleEndian, &proc.id)
if err != nil {
return
}
} else {
err = writeUsVarChar(buf, proc.name)
if err != nil {
return
}
}
err = binary.Write(buf, binary.LittleEndian, &flags)
if err != nil {
return
}
for _, param := range params {
if err = writeBVarChar(buf, param.Name); err != nil {
return
}
if err = binary.Write(buf, binary.LittleEndian, param.Flags); err != nil {
return
}
err = writeTypeInfo(buf, &param.ti)
if err != nil {
return
}
err = param.ti.Writer(buf, param.ti, param.buffer)
if err != nil {
return
}
}
return buf.FinishPacket()
}

@ -0,0 +1,266 @@
package mssql
import (
"fmt"
"strings"
"syscall"
"unsafe"
)
var (
secur32_dll = syscall.NewLazyDLL("secur32.dll")
initSecurityInterface = secur32_dll.NewProc("InitSecurityInterfaceW")
sec_fn *SecurityFunctionTable
)
func init() {
ptr, _, _ := initSecurityInterface.Call()
sec_fn = (*SecurityFunctionTable)(unsafe.Pointer(ptr))
}
const (
SEC_E_OK = 0
SECPKG_CRED_OUTBOUND = 2
SEC_WINNT_AUTH_IDENTITY_UNICODE = 2
ISC_REQ_DELEGATE = 0x00000001
ISC_REQ_REPLAY_DETECT = 0x00000004
ISC_REQ_SEQUENCE_DETECT = 0x00000008
ISC_REQ_CONFIDENTIALITY = 0x00000010
ISC_REQ_CONNECTION = 0x00000800
SECURITY_NETWORK_DREP = 0
SEC_I_CONTINUE_NEEDED = 0x00090312
SEC_I_COMPLETE_NEEDED = 0x00090313
SEC_I_COMPLETE_AND_CONTINUE = 0x00090314
SECBUFFER_VERSION = 0
SECBUFFER_TOKEN = 2
NTLMBUF_LEN = 12000
)
const ISC_REQ = ISC_REQ_CONFIDENTIALITY |
ISC_REQ_REPLAY_DETECT |
ISC_REQ_SEQUENCE_DETECT |
ISC_REQ_CONNECTION |
ISC_REQ_DELEGATE
type SecurityFunctionTable struct {
dwVersion uint32
EnumerateSecurityPackages uintptr
QueryCredentialsAttributes uintptr
AcquireCredentialsHandle uintptr
FreeCredentialsHandle uintptr
Reserved2 uintptr
InitializeSecurityContext uintptr
AcceptSecurityContext uintptr
CompleteAuthToken uintptr
DeleteSecurityContext uintptr
ApplyControlToken uintptr
QueryContextAttributes uintptr
ImpersonateSecurityContext uintptr
RevertSecurityContext uintptr
MakeSignature uintptr
VerifySignature uintptr
FreeContextBuffer uintptr
QuerySecurityPackageInfo uintptr
Reserved3 uintptr
Reserved4 uintptr
Reserved5 uintptr
Reserved6 uintptr
Reserved7 uintptr
Reserved8 uintptr
QuerySecurityContextToken uintptr
EncryptMessage uintptr
DecryptMessage uintptr
}
type SEC_WINNT_AUTH_IDENTITY struct {
User *uint16
UserLength uint32
Domain *uint16
DomainLength uint32
Password *uint16
PasswordLength uint32
Flags uint32
}
type TimeStamp struct {
LowPart uint32
HighPart int32
}
type SecHandle struct {
dwLower uintptr
dwUpper uintptr
}
type SecBuffer struct {
cbBuffer uint32
BufferType uint32
pvBuffer *byte
}
type SecBufferDesc struct {
ulVersion uint32
cBuffers uint32
pBuffers *SecBuffer
}
type SSPIAuth struct {
Domain string
UserName string
Password string
Service string
cred SecHandle
ctxt SecHandle
}
func getAuth(user, password, service, workstation string) (auth, bool) {
if user == "" {
return &SSPIAuth{Service: service}, true
}
if !strings.ContainsRune(user, '\\') {
return nil, false
}
domain_user := strings.SplitN(user, "\\", 2)
return &SSPIAuth{
Domain: domain_user[0],
UserName: domain_user[1],
Password: password,
Service: service,
}, true
}
func (auth *SSPIAuth) InitialBytes() ([]byte, error) {
var identity *SEC_WINNT_AUTH_IDENTITY
if auth.UserName != "" {
identity = &SEC_WINNT_AUTH_IDENTITY{
Flags: SEC_WINNT_AUTH_IDENTITY_UNICODE,
Password: syscall.StringToUTF16Ptr(auth.Password),
PasswordLength: uint32(len(auth.Password)),
Domain: syscall.StringToUTF16Ptr(auth.Domain),
DomainLength: uint32(len(auth.Domain)),
User: syscall.StringToUTF16Ptr(auth.UserName),
UserLength: uint32(len(auth.UserName)),
}
}
var ts TimeStamp
sec_ok, _, _ := syscall.Syscall9(sec_fn.AcquireCredentialsHandle,
9,
0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Negotiate"))),
SECPKG_CRED_OUTBOUND,
0,
uintptr(unsafe.Pointer(identity)),
0,
0,
uintptr(unsafe.Pointer(&auth.cred)),
uintptr(unsafe.Pointer(&ts)))
if sec_ok != SEC_E_OK {
return nil, fmt.Errorf("AcquireCredentialsHandle failed %x", sec_ok)
}
var buf SecBuffer
var desc SecBufferDesc
desc.ulVersion = SECBUFFER_VERSION
desc.cBuffers = 1
desc.pBuffers = &buf
outbuf := make([]byte, NTLMBUF_LEN)
buf.cbBuffer = NTLMBUF_LEN
buf.BufferType = SECBUFFER_TOKEN
buf.pvBuffer = &outbuf[0]
var attrs uint32
sec_ok, _, _ = syscall.Syscall12(sec_fn.InitializeSecurityContext,
12,
uintptr(unsafe.Pointer(&auth.cred)),
0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(auth.Service))),
ISC_REQ,
0,
SECURITY_NETWORK_DREP,
0,
0,
uintptr(unsafe.Pointer(&auth.ctxt)),
uintptr(unsafe.Pointer(&desc)),
uintptr(unsafe.Pointer(&attrs)),
uintptr(unsafe.Pointer(&ts)))
if sec_ok == SEC_I_COMPLETE_AND_CONTINUE ||
sec_ok == SEC_I_COMPLETE_NEEDED {
syscall.Syscall6(sec_fn.CompleteAuthToken,
2,
uintptr(unsafe.Pointer(&auth.ctxt)),
uintptr(unsafe.Pointer(&desc)),
0, 0, 0, 0)
} else if sec_ok != SEC_E_OK &&
sec_ok != SEC_I_CONTINUE_NEEDED {
syscall.Syscall6(sec_fn.FreeCredentialsHandle,
1,
uintptr(unsafe.Pointer(&auth.cred)),
0, 0, 0, 0, 0)
return nil, fmt.Errorf("InitialBytes InitializeSecurityContext failed %x", sec_ok)
}
return outbuf[:buf.cbBuffer], nil
}
func (auth *SSPIAuth) NextBytes(bytes []byte) ([]byte, error) {
var in_buf, out_buf SecBuffer
var in_desc, out_desc SecBufferDesc
in_desc.ulVersion = SECBUFFER_VERSION
in_desc.cBuffers = 1
in_desc.pBuffers = &in_buf
out_desc.ulVersion = SECBUFFER_VERSION
out_desc.cBuffers = 1
out_desc.pBuffers = &out_buf
in_buf.BufferType = SECBUFFER_TOKEN
in_buf.pvBuffer = &bytes[0]
in_buf.cbBuffer = uint32(len(bytes))
outbuf := make([]byte, NTLMBUF_LEN)
out_buf.BufferType = SECBUFFER_TOKEN
out_buf.pvBuffer = &outbuf[0]
out_buf.cbBuffer = NTLMBUF_LEN
var attrs uint32
var ts TimeStamp
sec_ok, _, _ := syscall.Syscall12(sec_fn.InitializeSecurityContext,
12,
uintptr(unsafe.Pointer(&auth.cred)),
uintptr(unsafe.Pointer(&auth.ctxt)),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(auth.Service))),
ISC_REQ,
0,
SECURITY_NETWORK_DREP,
uintptr(unsafe.Pointer(&in_desc)),
0,
uintptr(unsafe.Pointer(&auth.ctxt)),
uintptr(unsafe.Pointer(&out_desc)),
uintptr(unsafe.Pointer(&attrs)),
uintptr(unsafe.Pointer(&ts)))
if sec_ok == SEC_I_COMPLETE_AND_CONTINUE ||
sec_ok == SEC_I_COMPLETE_NEEDED {
syscall.Syscall6(sec_fn.CompleteAuthToken,
2,
uintptr(unsafe.Pointer(&auth.ctxt)),
uintptr(unsafe.Pointer(&out_desc)),
0, 0, 0, 0)
} else if sec_ok != SEC_E_OK &&
sec_ok != SEC_I_CONTINUE_NEEDED {
return nil, fmt.Errorf("NextBytes InitializeSecurityContext failed %x", sec_ok)
}
return outbuf[:out_buf.cbBuffer], nil
}
func (auth *SSPIAuth) Free() {
syscall.Syscall6(sec_fn.DeleteSecurityContext,
1,
uintptr(unsafe.Pointer(&auth.ctxt)),
0, 0, 0, 0, 0)
syscall.Syscall6(sec_fn.FreeCredentialsHandle,
1,
uintptr(unsafe.Pointer(&auth.cred)),
0, 0, 0, 0, 0)
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,954 @@
package mssql
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"strconv"
"github.com/denisenkom/go-mssqldb/msdsn"
"github.com/golang-sql/sqlexp"
)
//go:generate go run golang.org/x/tools/cmd/stringer -type token
type token byte
// token ids
const (
tokenReturnStatus token = 121 // 0x79
tokenColMetadata token = 129 // 0x81
tokenOrder token = 169 // 0xA9
tokenError token = 170 // 0xAA
tokenInfo token = 171 // 0xAB
tokenReturnValue token = 0xAC
tokenLoginAck token = 173 // 0xad
tokenFeatureExtAck token = 174 // 0xae
tokenRow token = 209 // 0xd1
tokenNbcRow token = 210 // 0xd2
tokenEnvChange token = 227 // 0xE3
tokenSSPI token = 237 // 0xED
tokenFedAuthInfo token = 238 // 0xEE
tokenDone token = 253 // 0xFD
tokenDoneProc token = 254
tokenDoneInProc token = 255
)
// done flags
// https://msdn.microsoft.com/en-us/library/dd340421.aspx
const (
doneFinal = 0
doneMore = 1
doneError = 2
doneInxact = 4
doneCount = 0x10
doneAttn = 0x20
doneSrvError = 0x100
)
// ENVCHANGE types
// http://msdn.microsoft.com/en-us/library/dd303449.aspx
const (
envTypDatabase = 1
envTypLanguage = 2
envTypCharset = 3
envTypPacketSize = 4
envSortId = 5
envSortFlags = 6
envSqlCollation = 7
envTypBeginTran = 8
envTypCommitTran = 9
envTypRollbackTran = 10
envEnlistDTC = 11
envDefectTran = 12
envDatabaseMirrorPartner = 13
envPromoteTran = 15
envTranMgrAddr = 16
envTranEnded = 17
envResetConnAck = 18
envStartedInstanceName = 19
envRouting = 20
)
const (
fedAuthInfoSTSURL = 0x01
fedAuthInfoSPN = 0x02
)
// COLMETADATA flags
// https://msdn.microsoft.com/en-us/library/dd357363.aspx
const (
colFlagNullable = 1
// TODO implement more flags
)
// interface for all tokens
type tokenStruct interface{}
type orderStruct struct {
ColIds []uint16
}
type doneStruct struct {
Status uint16
CurCmd uint16
RowCount uint64
errors []Error
}
func (d doneStruct) isError() bool {
return d.Status&doneError != 0 || len(d.errors) > 0
}
func (d doneStruct) getError() Error {
n := len(d.errors)
if n == 0 {
return Error{Message: "Request failed but didn't provide reason"}
}
err := d.errors[n-1]
// should this return the most severe error?
err.All = make([]Error, n)
copy(err.All, d.errors)
return err
}
type doneInProcStruct doneStruct
// ENVCHANGE stream
// http://msdn.microsoft.com/en-us/library/dd303449.aspx
func processEnvChg(ctx context.Context, sess *tdsSession) {
size := sess.buf.uint16()
r := &io.LimitedReader{R: sess.buf, N: int64(size)}
for {
var err error
var envtype uint8
err = binary.Read(r, binary.LittleEndian, &envtype)
if err == io.EOF {
return
}
if err != nil {
badStreamPanic(err)
}
switch envtype {
case envTypDatabase:
sess.database, err = readBVarChar(r)
if err != nil {
badStreamPanic(err)
}
_, err = readBVarChar(r)
if err != nil {
badStreamPanic(err)
}
case envTypLanguage:
// currently ignored
// new value
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
// old value
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envTypCharset:
// currently ignored
// new value
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
// old value
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envTypPacketSize:
packetsize, err := readBVarChar(r)
if err != nil {
badStreamPanic(err)
}
_, err = readBVarChar(r)
if err != nil {
badStreamPanic(err)
}
packetsizei, err := strconv.Atoi(packetsize)
if err != nil {
badStreamPanicf("Invalid Packet size value returned from server (%s): %s", packetsize, err.Error())
}
sess.buf.ResizeBuffer(packetsizei)
case envSortId:
// currently ignored
// new value
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
// old value, should be 0
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envSortFlags:
// currently ignored
// new value
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
// old value, should be 0
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envSqlCollation:
// currently ignored
var collationSize uint8
err = binary.Read(r, binary.LittleEndian, &collationSize)
if err != nil {
badStreamPanic(err)
}
// SQL Collation data should contain 5 bytes in length
if collationSize != 5 {
badStreamPanicf("Invalid SQL Collation size value returned from server: %d", collationSize)
}
// 4 bytes, contains: LCID ColFlags Version
var info uint32
err = binary.Read(r, binary.LittleEndian, &info)
if err != nil {
badStreamPanic(err)
}
// 1 byte, contains: sortID
var sortID uint8
err = binary.Read(r, binary.LittleEndian, &sortID)
if err != nil {
badStreamPanic(err)
}
// old value, should be 0
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envTypBeginTran:
tranid, err := readBVarByte(r)
if len(tranid) != 8 {
badStreamPanicf("invalid size of transaction identifier: %d", len(tranid))
}
sess.tranid = binary.LittleEndian.Uint64(tranid)
if err != nil {
badStreamPanic(err)
}
if sess.logFlags&logTransaction != 0 {
sess.logger.Log(ctx, msdsn.LogTransaction, fmt.Sprintf("BEGIN TRANSACTION %x", sess.tranid))
}
_, err = readBVarByte(r)
if err != nil {
badStreamPanic(err)
}
case envTypCommitTran, envTypRollbackTran:
_, err = readBVarByte(r)
if err != nil {
badStreamPanic(err)
}
_, err = readBVarByte(r)
if err != nil {
badStreamPanic(err)
}
if sess.logFlags&logTransaction != 0 {
if envtype == envTypCommitTran {
sess.logger.Log(ctx, msdsn.LogTransaction, fmt.Sprintf("COMMIT TRANSACTION %x", sess.tranid))
} else {
sess.logger.Log(ctx, msdsn.LogTransaction, fmt.Sprintf("ROLLBACK TRANSACTION %x", sess.tranid))
}
}
sess.tranid = 0
case envEnlistDTC:
// currently ignored
// new value, should be 0
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
// old value
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envDefectTran:
// currently ignored
// new value
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
// old value, should be 0
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envDatabaseMirrorPartner:
sess.partner, err = readBVarChar(r)
if err != nil {
badStreamPanic(err)
}
_, err = readBVarChar(r)
if err != nil {
badStreamPanic(err)
}
case envPromoteTran:
// currently ignored
// old value, should be 0
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
// dtc token
// spec says it should be L_VARBYTE, so this code might be wrong
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envTranMgrAddr:
// currently ignored
// old value, should be 0
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
// XACT_MANAGER_ADDRESS = B_VARBYTE
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envTranEnded:
// currently ignored
// old value, B_VARBYTE
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
// should be 0
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envResetConnAck:
// currently ignored
// old value, should be 0
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
// should be 0
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envStartedInstanceName:
// currently ignored
// old value, should be 0
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
// instance name
if _, err = readBVarChar(r); err != nil {
badStreamPanic(err)
}
case envRouting:
// RoutingData message is:
// ValueLength USHORT
// Protocol (TCP = 0) BYTE
// ProtocolProperty (new port) USHORT
// AlternateServer US_VARCHAR
_, err := readUshort(r)
if err != nil {
badStreamPanic(err)
}
protocol, err := readByte(r)
if err != nil || protocol != 0 {
badStreamPanic(err)
}
newPort, err := readUshort(r)
if err != nil {
badStreamPanic(err)
}
newServer, err := readUsVarChar(r)
if err != nil {
badStreamPanic(err)
}
// consume the OLDVALUE = %x00 %x00
_, err = readUshort(r)
if err != nil {
badStreamPanic(err)
}
sess.routedServer = newServer
sess.routedPort = newPort
default:
// ignore rest of records because we don't know how to skip those
if sess.logFlags&logDebug != 0 {
sess.logger.Log(ctx, msdsn.LogDebug, fmt.Sprintf("WARN: Unknown ENVCHANGE record detected with type id = %d", envtype))
}
return
}
}
}
// http://msdn.microsoft.com/en-us/library/dd358180.aspx
func parseReturnStatus(r *tdsBuffer) ReturnStatus {
return ReturnStatus(r.int32())
}
func parseOrder(r *tdsBuffer) (res orderStruct) {
len := int(r.uint16())
res.ColIds = make([]uint16, len/2)
for i := 0; i < len/2; i++ {
res.ColIds[i] = r.uint16()
}
return res
}
// https://msdn.microsoft.com/en-us/library/dd340421.aspx
func parseDone(r *tdsBuffer) (res doneStruct) {
res.Status = r.uint16()
res.CurCmd = r.uint16()
res.RowCount = r.uint64()
return res
}
// https://msdn.microsoft.com/en-us/library/dd340553.aspx
func parseDoneInProc(r *tdsBuffer) (res doneInProcStruct) {
res.Status = r.uint16()
res.CurCmd = r.uint16()
res.RowCount = r.uint64()
return res
}
type sspiMsg []byte
func parseSSPIMsg(r *tdsBuffer) sspiMsg {
size := r.uint16()
buf := make([]byte, size)
r.ReadFull(buf)
return sspiMsg(buf)
}
type fedAuthInfoStruct struct {
STSURL string
ServerSPN string
}
type fedAuthInfoOpt struct {
fedAuthInfoID byte
dataLength, dataOffset uint32
}
func parseFedAuthInfo(r *tdsBuffer) fedAuthInfoStruct {
size := r.uint32()
var STSURL, SPN string
var err error
// Each fedAuthInfoOpt is one byte to indicate the info ID,
// then a four byte offset and a four byte length.
count := r.uint32()
offset := uint32(4)
opts := make([]fedAuthInfoOpt, count)
for i := uint32(0); i < count; i++ {
fedAuthInfoID := r.byte()
dataLength := r.uint32()
dataOffset := r.uint32()
offset += 1 + 4 + 4
opts[i] = fedAuthInfoOpt{
fedAuthInfoID: fedAuthInfoID,
dataLength: dataLength,
dataOffset: dataOffset,
}
}
data := make([]byte, size-offset)
r.ReadFull(data)
for i := uint32(0); i < count; i++ {
if opts[i].dataOffset < offset {
badStreamPanicf("Fed auth info opt stated data offset %d is before data begins in packet at %d",
opts[i].dataOffset, offset)
// returns via panic
}
if opts[i].dataOffset+opts[i].dataLength > size {
badStreamPanicf("Fed auth info opt stated data length %d added to stated offset exceeds size of packet %d",
opts[i].dataOffset+opts[i].dataLength, size)
// returns via panic
}
optData := data[opts[i].dataOffset-offset : opts[i].dataOffset-offset+opts[i].dataLength]
switch opts[i].fedAuthInfoID {
case fedAuthInfoSTSURL:
STSURL, err = ucs22str(optData)
case fedAuthInfoSPN:
SPN, err = ucs22str(optData)
default:
err = fmt.Errorf("unexpected fed auth info opt ID %d", int(opts[i].fedAuthInfoID))
}
if err != nil {
badStreamPanic(err)
}
}
return fedAuthInfoStruct{
STSURL: STSURL,
ServerSPN: SPN,
}
}
type loginAckStruct struct {
Interface uint8
TDSVersion uint32
ProgName string
ProgVer uint32
}
func parseLoginAck(r *tdsBuffer) loginAckStruct {
size := r.uint16()
buf := make([]byte, size)
r.ReadFull(buf)
var res loginAckStruct
res.Interface = buf[0]
res.TDSVersion = binary.BigEndian.Uint32(buf[1:])
prognamelen := buf[1+4]
var err error
if res.ProgName, err = ucs22str(buf[1+4+1 : 1+4+1+prognamelen*2]); err != nil {
badStreamPanic(err)
}
res.ProgVer = binary.BigEndian.Uint32(buf[size-4:])
return res
}
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-tds/2eb82f8e-11f0-46dc-b42d-27302fa4701a
type fedAuthAckStruct struct {
Nonce []byte
Signature []byte
}
func parseFeatureExtAck(r *tdsBuffer) map[byte]interface{} {
ack := map[byte]interface{}{}
for feature := r.byte(); feature != featExtTERMINATOR; feature = r.byte() {
length := r.uint32()
switch feature {
case featExtFEDAUTH:
// In theory we need to know the federated authentication library to
// know how to parse, but the alternatives provide compatible structures.
fedAuthAck := fedAuthAckStruct{}
if length >= 32 {
fedAuthAck.Nonce = make([]byte, 32)
r.ReadFull(fedAuthAck.Nonce)
length -= 32
}
if length >= 32 {
fedAuthAck.Signature = make([]byte, 32)
r.ReadFull(fedAuthAck.Signature)
length -= 32
}
ack[feature] = fedAuthAck
}
// Skip unprocessed bytes
if length > 0 {
io.CopyN(ioutil.Discard, r, int64(length))
}
}
return ack
}
// http://msdn.microsoft.com/en-us/library/dd357363.aspx
func parseColMetadata72(r *tdsBuffer) (columns []columnStruct) {
count := r.uint16()
if count == 0xffff {
// no metadata is sent
return nil
}
columns = make([]columnStruct, count)
for i := range columns {
column := &columns[i]
column.UserType = r.uint32()
column.Flags = r.uint16()
// parsing TYPE_INFO structure
column.ti = readTypeInfo(r)
column.ColName = r.BVarChar()
}
return columns
}
// http://msdn.microsoft.com/en-us/library/dd357254.aspx
func parseRow(r *tdsBuffer, columns []columnStruct, row []interface{}) {
for i, column := range columns {
row[i] = column.ti.Reader(&column.ti, r)
}
}
// http://msdn.microsoft.com/en-us/library/dd304783.aspx
func parseNbcRow(r *tdsBuffer, columns []columnStruct, row []interface{}) {
bitlen := (len(columns) + 7) / 8
pres := make([]byte, bitlen)
r.ReadFull(pres)
for i, col := range columns {
if pres[i/8]&(1<<(uint(i)%8)) != 0 {
row[i] = nil
continue
}
row[i] = col.ti.Reader(&col.ti, r)
}
}
// http://msdn.microsoft.com/en-us/library/dd304156.aspx
func parseError72(r *tdsBuffer) (res Error) {
length := r.uint16()
_ = length // ignore length
res.Number = r.int32()
res.State = r.byte()
res.Class = r.byte()
res.Message = r.UsVarChar()
res.ServerName = r.BVarChar()
res.ProcName = r.BVarChar()
res.LineNo = r.int32()
return
}
// http://msdn.microsoft.com/en-us/library/dd304156.aspx
func parseInfo(r *tdsBuffer) (res Error) {
length := r.uint16()
_ = length // ignore length
res.Number = r.int32()
res.State = r.byte()
res.Class = r.byte()
res.Message = r.UsVarChar()
res.ServerName = r.BVarChar()
res.ProcName = r.BVarChar()
res.LineNo = r.int32()
return
}
// https://msdn.microsoft.com/en-us/library/dd303881.aspx
func parseReturnValue(r *tdsBuffer) (nv namedValue) {
/*
ParamOrdinal
ParamName
Status
UserType
Flags
TypeInfo
CryptoMetadata
Value
*/
r.uint16()
nv.Name = r.BVarChar()
r.byte()
r.uint32() // UserType (uint16 prior to 7.2)
r.uint16()
ti := readTypeInfo(r)
nv.Value = ti.Reader(&ti, r)
return
}
func processSingleResponse(ctx context.Context, sess *tdsSession, ch chan tokenStruct, outs outputs) {
firstResult := true
defer func() {
if err := recover(); err != nil {
if sess.logFlags&logErrors != 0 {
sess.logger.Log(ctx, msdsn.LogErrors, fmt.Sprintf("Intercepted panic %v", err))
}
ch <- err
}
close(ch)
}()
packet_type, err := sess.buf.BeginRead()
if err != nil {
if sess.logFlags&logErrors != 0 {
sess.logger.Log(ctx, msdsn.LogErrors, fmt.Sprintf("BeginRead failed %v", err))
}
ch <- err
return
}
if packet_type != packReply {
badStreamPanic(fmt.Errorf("unexpected packet type in reply: got %v, expected %v", packet_type, packReply))
}
var columns []columnStruct
errs := make([]Error, 0, 5)
for tokens := 0; ; tokens += 1 {
token := token(sess.buf.byte())
if sess.logFlags&logDebug != 0 {
sess.logger.Log(ctx, msdsn.LogDebug, fmt.Sprintf("got token %v", token))
}
switch token {
case tokenSSPI:
ch <- parseSSPIMsg(sess.buf)
return
case tokenFedAuthInfo:
ch <- parseFedAuthInfo(sess.buf)
return
case tokenReturnStatus:
returnStatus := parseReturnStatus(sess.buf)
ch <- returnStatus
case tokenLoginAck:
loginAck := parseLoginAck(sess.buf)
ch <- loginAck
case tokenFeatureExtAck:
featureExtAck := parseFeatureExtAck(sess.buf)
ch <- featureExtAck
case tokenOrder:
order := parseOrder(sess.buf)
ch <- order
case tokenDoneInProc:
done := parseDoneInProc(sess.buf)
ch <- done
if sess.logFlags&logRows != 0 && done.Status&doneCount != 0 {
sess.logger.Log(ctx, msdsn.LogRows, fmt.Sprintf("(%d rows affected)", done.RowCount))
if outs.msgq != nil {
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgRowsAffected{Count: int64(done.RowCount)})
}
}
if done.Status&doneMore == 0 {
if outs.msgq != nil {
// For now we ignore ctx->Done errors that ReturnMessageEnqueue might return
// It's not clear how to handle them correctly here, and data/sql seems
// to set Rows.Err correctly when ctx expires already
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgNextResultSet{})
}
return
}
case tokenDone, tokenDoneProc:
done := parseDone(sess.buf)
done.errors = errs
if outs.msgq != nil {
errs = make([]Error, 0, 5)
}
if sess.logFlags&logDebug != 0 {
sess.logger.Log(ctx, msdsn.LogDebug, fmt.Sprintf("got DONE or DONEPROC status=%d", done.Status))
}
if done.Status&doneSrvError != 0 {
ch <- ServerError{done.getError()}
if outs.msgq != nil {
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgNextResultSet{})
}
return
}
if sess.logFlags&logRows != 0 && done.Status&doneCount != 0 {
sess.logger.Log(ctx, msdsn.LogRows, fmt.Sprintf("(%d row(s) affected)", done.RowCount))
}
ch <- done
if done.Status&doneCount != 0 {
if outs.msgq != nil {
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgRowsAffected{Count: int64(done.RowCount)})
}
}
if done.Status&doneMore == 0 {
if outs.msgq != nil {
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgNextResultSet{})
}
return
}
case tokenColMetadata:
columns = parseColMetadata72(sess.buf)
ch <- columns
if outs.msgq != nil {
if !firstResult {
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgNextResultSet{})
}
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgNext{})
}
firstResult = false
case tokenRow:
row := make([]interface{}, len(columns))
parseRow(sess.buf, columns, row)
ch <- row
case tokenNbcRow:
row := make([]interface{}, len(columns))
parseNbcRow(sess.buf, columns, row)
ch <- row
case tokenEnvChange:
processEnvChg(ctx, sess)
case tokenError:
err := parseError72(sess.buf)
if sess.logFlags&logDebug != 0 {
sess.logger.Log(ctx, msdsn.LogDebug, fmt.Sprintf("got ERROR %d %s", err.Number, err.Message))
}
errs = append(errs, err)
if sess.logFlags&logErrors != 0 {
sess.logger.Log(ctx, msdsn.LogErrors, err.Message)
}
if outs.msgq != nil {
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgError{Error: err})
}
case tokenInfo:
info := parseInfo(sess.buf)
if sess.logFlags&logDebug != 0 {
sess.logger.Log(ctx, msdsn.LogDebug, fmt.Sprintf("got INFO %d %s", info.Number, info.Message))
}
if sess.logFlags&logMessages != 0 {
sess.logger.Log(ctx, msdsn.LogMessages, info.Message)
}
if outs.msgq != nil {
_ = sqlexp.ReturnMessageEnqueue(ctx, outs.msgq, sqlexp.MsgNotice{Message: info.Message})
}
case tokenReturnValue:
nv := parseReturnValue(sess.buf)
if len(nv.Name) > 0 {
name := nv.Name[1:] // Remove the leading "@".
if ov, has := outs.params[name]; has {
err = scanIntoOut(name, nv.Value, ov)
if err != nil {
fmt.Println("scan error", err)
ch <- err
}
}
}
default:
badStreamPanic(fmt.Errorf("unknown token type returned: %v", token))
}
}
}
type tokenProcessor struct {
tokChan chan tokenStruct
ctx context.Context
sess *tdsSession
outs outputs
lastRow []interface{}
rowCount int64
firstError error
// whether to skip sending attention when ctx is done
noAttn bool
}
func startReading(sess *tdsSession, ctx context.Context, outs outputs) *tokenProcessor {
tokChan := make(chan tokenStruct, 5)
go processSingleResponse(ctx, sess, tokChan, outs)
return &tokenProcessor{
tokChan: tokChan,
ctx: ctx,
sess: sess,
outs: outs,
}
}
func (t *tokenProcessor) iterateResponse() error {
for {
tok, err := t.nextToken()
if err == nil {
if tok == nil {
return t.firstError
} else {
switch token := tok.(type) {
case []columnStruct:
t.sess.columns = token
case []interface{}:
t.lastRow = token
case doneInProcStruct:
if token.Status&doneCount != 0 {
t.rowCount += int64(token.RowCount)
}
case doneStruct:
if token.Status&doneCount != 0 {
t.rowCount += int64(token.RowCount)
}
if token.isError() && t.firstError == nil {
t.firstError = token.getError()
}
case ReturnStatus:
if t.outs.returnStatus != nil {
*t.outs.returnStatus = token
}
/*case error:
if resultError == nil {
resultError = token
}*/
}
}
} else {
return err
}
}
}
func (t tokenProcessor) nextToken() (tokenStruct, error) {
// we do this separate non-blocking check on token channel to
// prioritize it over cancellation channel
select {
case tok, more := <-t.tokChan:
err, more := tok.(error)
if more {
// this is an error and not a token
return nil, err
} else {
return tok, nil
}
default:
// there are no tokens on the channel, will need to wait
}
select {
case tok, more := <-t.tokChan:
if more {
err, ok := tok.(error)
if ok {
// this is an error and not a token
return nil, err
} else {
return tok, nil
}
} else {
// completed reading response
return nil, nil
}
case <-t.ctx.Done():
// It seems the Message function on t.outs.msgq doesn't get the Done if it comes here instead
if t.outs.msgq != nil {
_ = sqlexp.ReturnMessageEnqueue(t.ctx, t.outs.msgq, sqlexp.MsgNextResultSet{})
}
if t.noAttn {
return nil, t.ctx.Err()
}
if err := sendAttention(t.sess.buf); err != nil {
// unable to send attention, current connection is bad
// notify caller and close channel
return nil, err
}
// now the server should send cancellation confirmation
// it is possible that we already received full response
// just before we sent cancellation request
// in this case current response would not contain confirmation
// and we would need to read one more response
// first lets finish reading current response and look
// for confirmation in it
if readCancelConfirmation(t.tokChan) {
// we got confirmation in current response
return nil, t.ctx.Err()
}
// we did not get cancellation confirmation in the current response
// read one more response, it must be there
t.tokChan = make(chan tokenStruct, 5)
go processSingleResponse(t.ctx, t.sess, t.tokChan, t.outs)
if readCancelConfirmation(t.tokChan) {
return nil, t.ctx.Err()
}
// we did not get cancellation confirmation, something is not
// right, this connection is not usable anymore
return nil, errors.New("did not get cancellation confirmation from the server")
}
}
func readCancelConfirmation(tokChan chan tokenStruct) bool {
for tok := range tokChan {
switch tok := tok.(type) {
default:
// just skip token
case doneStruct:
if tok.Status&doneAttn != 0 {
// got cancellation confirmation, exit
return true
}
}
}
return false
}

@ -0,0 +1,47 @@
// Code generated by "stringer -type token"; DO NOT EDIT.
package mssql
import "strconv"
const (
_token_name_0 = "tokenReturnStatus"
_token_name_1 = "tokenColMetadata"
_token_name_2 = "tokenOrdertokenErrortokenInfotokenReturnValuetokenLoginAcktokenFeatureExtAck"
_token_name_3 = "tokenRowtokenNbcRow"
_token_name_4 = "tokenEnvChange"
_token_name_5 = "tokenSSPItokenFedAuthInfo"
_token_name_6 = "tokenDonetokenDoneProctokenDoneInProc"
)
var (
_token_index_2 = [...]uint8{0, 10, 20, 29, 45, 58, 76}
_token_index_3 = [...]uint8{0, 8, 19}
_token_index_5 = [...]uint8{0, 9, 25}
_token_index_6 = [...]uint8{0, 9, 22, 37}
)
func (i token) String() string {
switch {
case i == 121:
return _token_name_0
case i == 129:
return _token_name_1
case 169 <= i && i <= 174:
i -= 169
return _token_name_2[_token_index_2[i]:_token_index_2[i+1]]
case 209 <= i && i <= 210:
i -= 209
return _token_name_3[_token_index_3[i]:_token_index_3[i+1]]
case i == 227:
return _token_name_4
case 237 <= i && i <= 238:
i -= 237
return _token_name_5[_token_index_5[i]:_token_index_5[i+1]]
case 253 <= i && i <= 255:
i -= 253
return _token_name_6[_token_index_6[i]:_token_index_6[i+1]]
default:
return "token(" + strconv.FormatInt(int64(i), 10) + ")"
}
}

@ -0,0 +1,110 @@
package mssql
// Transaction Manager requests
// http://msdn.microsoft.com/en-us/library/dd339887.aspx
import (
"encoding/binary"
)
const (
tmGetDtcAddr = 0
tmPropagateXact = 1
tmBeginXact = 5
tmPromoteXact = 6
tmCommitXact = 7
tmRollbackXact = 8
tmSaveXact = 9
)
type isoLevel uint8
const (
isolationUseCurrent isoLevel = 0
isolationReadUncommited isoLevel = 1
isolationReadCommited isoLevel = 2
isolationRepeatableRead isoLevel = 3
isolationSerializable isoLevel = 4
isolationSnapshot isoLevel = 5
)
func sendBeginXact(buf *tdsBuffer, headers []headerStruct, isolation isoLevel, name string, resetSession bool) (err error) {
buf.BeginPacket(packTransMgrReq, resetSession)
writeAllHeaders(buf, headers)
var rqtype uint16 = tmBeginXact
err = binary.Write(buf, binary.LittleEndian, &rqtype)
if err != nil {
return
}
err = binary.Write(buf, binary.LittleEndian, &isolation)
if err != nil {
return
}
err = writeBVarChar(buf, name)
if err != nil {
return
}
return buf.FinishPacket()
}
const (
fBeginXact = 1
)
func sendCommitXact(buf *tdsBuffer, headers []headerStruct, name string, flags uint8, isolation uint8, newname string, resetSession bool) error {
buf.BeginPacket(packTransMgrReq, resetSession)
writeAllHeaders(buf, headers)
var rqtype uint16 = tmCommitXact
err := binary.Write(buf, binary.LittleEndian, &rqtype)
if err != nil {
return err
}
err = writeBVarChar(buf, name)
if err != nil {
return err
}
err = binary.Write(buf, binary.LittleEndian, &flags)
if err != nil {
return err
}
if flags&fBeginXact != 0 {
err = binary.Write(buf, binary.LittleEndian, &isolation)
if err != nil {
return err
}
err = writeBVarChar(buf, name)
if err != nil {
return err
}
}
return buf.FinishPacket()
}
func sendRollbackXact(buf *tdsBuffer, headers []headerStruct, name string, flags uint8, isolation uint8, newname string, resetSession bool) error {
buf.BeginPacket(packTransMgrReq, resetSession)
writeAllHeaders(buf, headers)
var rqtype uint16 = tmRollbackXact
err := binary.Write(buf, binary.LittleEndian, &rqtype)
if err != nil {
return err
}
err = writeBVarChar(buf, name)
if err != nil {
return err
}
err = binary.Write(buf, binary.LittleEndian, &flags)
if err != nil {
return err
}
if flags&fBeginXact != 0 {
err = binary.Write(buf, binary.LittleEndian, &isolation)
if err != nil {
return err
}
err = writeBVarChar(buf, name)
if err != nil {
return err
}
}
return buf.FinishPacket()
}

@ -0,0 +1,293 @@
// +build go1.9
package mssql
import (
"bytes"
"database/sql"
"encoding/binary"
"errors"
"fmt"
"reflect"
"strings"
"time"
)
const (
jsonTag = "json"
tvpTag = "tvp"
skipTagValue = "-"
sqlSeparator = "."
)
var (
ErrorEmptyTVPTypeName = errors.New("TypeName must not be empty")
ErrorTypeSlice = errors.New("TVP must be slice type")
ErrorTypeSliceIsEmpty = errors.New("TVP mustn't be null value")
ErrorSkip = errors.New("all fields mustn't skip")
ErrorObjectName = errors.New("wrong tvp name")
ErrorWrongTyping = errors.New("the number of elements in columnStr and tvpFieldIndexes do not align")
)
//TVP is driver type, which allows supporting Table Valued Parameters (TVP) in SQL Server
type TVP struct {
//TypeName mustn't be default value
TypeName string
//Value must be the slice, mustn't be nil
Value interface{}
}
func (tvp TVP) check() error {
if len(tvp.TypeName) == 0 {
return ErrorEmptyTVPTypeName
}
if !isProc(tvp.TypeName) {
return ErrorEmptyTVPTypeName
}
if sepCount := getCountSQLSeparators(tvp.TypeName); sepCount > 1 {
return ErrorObjectName
}
valueOf := reflect.ValueOf(tvp.Value)
if valueOf.Kind() != reflect.Slice {
return ErrorTypeSlice
}
if valueOf.IsNil() {
return ErrorTypeSliceIsEmpty
}
if reflect.TypeOf(tvp.Value).Elem().Kind() != reflect.Struct {
return ErrorTypeSlice
}
return nil
}
func (tvp TVP) encode(schema, name string, columnStr []columnStruct, tvpFieldIndexes []int) ([]byte, error) {
if len(columnStr) != len(tvpFieldIndexes) {
return nil, ErrorWrongTyping
}
preparedBuffer := make([]byte, 0, 20+(10*len(columnStr)))
buf := bytes.NewBuffer(preparedBuffer)
err := writeBVarChar(buf, "")
if err != nil {
return nil, err
}
writeBVarChar(buf, schema)
writeBVarChar(buf, name)
binary.Write(buf, binary.LittleEndian, uint16(len(columnStr)))
for i, column := range columnStr {
binary.Write(buf, binary.LittleEndian, uint32(column.UserType))
binary.Write(buf, binary.LittleEndian, uint16(column.Flags))
writeTypeInfo(buf, &columnStr[i].ti)
writeBVarChar(buf, "")
}
// The returned error is always nil
buf.WriteByte(_TVP_END_TOKEN)
conn := new(Conn)
conn.sess = new(tdsSession)
conn.sess.loginAck = loginAckStruct{TDSVersion: verTDS73}
stmt := &Stmt{
c: conn,
}
val := reflect.ValueOf(tvp.Value)
for i := 0; i < val.Len(); i++ {
refStr := reflect.ValueOf(val.Index(i).Interface())
buf.WriteByte(_TVP_ROW_TOKEN)
for columnStrIdx, fieldIdx := range tvpFieldIndexes {
field := refStr.Field(fieldIdx)
tvpVal := field.Interface()
if tvp.verifyStandardTypeOnNull(buf, tvpVal) {
continue
}
valOf := reflect.ValueOf(tvpVal)
elemKind := field.Kind()
if elemKind == reflect.Ptr && valOf.IsNil() {
switch tvpVal.(type) {
case *bool, *time.Time, *int8, *int16, *int32, *int64, *float32, *float64, *int,
*uint8, *uint16, *uint32, *uint64, *uint:
binary.Write(buf, binary.LittleEndian, uint8(0))
continue
default:
binary.Write(buf, binary.LittleEndian, uint64(_PLP_NULL))
continue
}
}
if elemKind == reflect.Slice && valOf.IsNil() {
binary.Write(buf, binary.LittleEndian, uint64(_PLP_NULL))
continue
}
cval, err := convertInputParameter(tvpVal)
if err != nil {
return nil, fmt.Errorf("failed to convert tvp parameter row col: %s", err)
}
param, err := stmt.makeParam(cval)
if err != nil {
return nil, fmt.Errorf("failed to make tvp parameter row col: %s", err)
}
columnStr[columnStrIdx].ti.Writer(buf, param.ti, param.buffer)
}
}
buf.WriteByte(_TVP_END_TOKEN)
return buf.Bytes(), nil
}
func (tvp TVP) columnTypes() ([]columnStruct, []int, error) {
val := reflect.ValueOf(tvp.Value)
var firstRow interface{}
if val.Len() != 0 {
firstRow = val.Index(0).Interface()
} else {
firstRow = reflect.New(reflect.TypeOf(tvp.Value).Elem()).Elem().Interface()
}
tvpRow := reflect.TypeOf(firstRow)
columnCount := tvpRow.NumField()
defaultValues := make([]interface{}, 0, columnCount)
tvpFieldIndexes := make([]int, 0, columnCount)
for i := 0; i < columnCount; i++ {
field := tvpRow.Field(i)
tvpTagValue, isTvpTag := field.Tag.Lookup(tvpTag)
jsonTagValue, isJsonTag := field.Tag.Lookup(jsonTag)
if IsSkipField(tvpTagValue, isTvpTag, jsonTagValue, isJsonTag) {
continue
}
tvpFieldIndexes = append(tvpFieldIndexes, i)
if field.Type.Kind() == reflect.Ptr {
v := reflect.New(field.Type.Elem())
defaultValues = append(defaultValues, v.Interface())
continue
}
defaultValues = append(defaultValues, tvp.createZeroType(reflect.Zero(field.Type).Interface()))
}
if columnCount-len(tvpFieldIndexes) == columnCount {
return nil, nil, ErrorSkip
}
conn := new(Conn)
conn.sess = new(tdsSession)
conn.sess.loginAck = loginAckStruct{TDSVersion: verTDS73}
stmt := &Stmt{
c: conn,
}
columnConfiguration := make([]columnStruct, 0, columnCount)
for index, val := range defaultValues {
cval, err := convertInputParameter(val)
if err != nil {
return nil, nil, fmt.Errorf("failed to convert tvp parameter row %d col %d: %s", index, val, err)
}
param, err := stmt.makeParam(cval)
if err != nil {
return nil, nil, err
}
column := columnStruct{
ti: param.ti,
}
switch param.ti.TypeId {
case typeNVarChar, typeBigVarBin:
column.ti.Size = 0
}
columnConfiguration = append(columnConfiguration, column)
}
return columnConfiguration, tvpFieldIndexes, nil
}
func IsSkipField(tvpTagValue string, isTvpValue bool, jsonTagValue string, isJsonTagValue bool) bool {
if !isTvpValue && !isJsonTagValue {
return false
} else if isTvpValue && tvpTagValue != skipTagValue {
return false
} else if !isTvpValue && isJsonTagValue && jsonTagValue != skipTagValue {
return false
}
return true
}
func getSchemeAndName(tvpName string) (string, string, error) {
if len(tvpName) == 0 {
return "", "", ErrorEmptyTVPTypeName
}
splitVal := strings.Split(tvpName, ".")
if len(splitVal) > 2 {
return "", "", ErrorObjectName
}
const (
openSquareBrackets = "["
closeSquareBrackets = "]"
)
if len(splitVal) == 2 {
res := make([]string, 2)
for key, value := range splitVal {
tmp := strings.Replace(value, openSquareBrackets, "", -1)
tmp = strings.Replace(tmp, closeSquareBrackets, "", -1)
res[key] = tmp
}
return res[0], res[1], nil
}
tmp := strings.Replace(splitVal[0], openSquareBrackets, "", -1)
tmp = strings.Replace(tmp, closeSquareBrackets, "", -1)
return "", tmp, nil
}
func getCountSQLSeparators(str string) int {
return strings.Count(str, sqlSeparator)
}
// verify types https://golang.org/pkg/database/sql/
func (tvp TVP) createZeroType(fieldVal interface{}) interface{} {
const (
defaultBool = false
defaultFloat64 = float64(0)
defaultInt64 = int64(0)
defaultString = ""
)
switch fieldVal.(type) {
case sql.NullBool:
return defaultBool
case sql.NullFloat64:
return defaultFloat64
case sql.NullInt64:
return defaultInt64
case sql.NullString:
return defaultString
}
return fieldVal
}
// verify types https://golang.org/pkg/database/sql/
func (tvp TVP) verifyStandardTypeOnNull(buf *bytes.Buffer, tvpVal interface{}) bool {
const (
defaultNull = uint8(0)
)
switch val := tvpVal.(type) {
case sql.NullBool:
if !val.Valid {
binary.Write(buf, binary.LittleEndian, defaultNull)
return true
}
case sql.NullFloat64:
if !val.Valid {
binary.Write(buf, binary.LittleEndian, defaultNull)
return true
}
case sql.NullInt64:
if !val.Valid {
binary.Write(buf, binary.LittleEndian, defaultNull)
return true
}
case sql.NullString:
if !val.Valid {
binary.Write(buf, binary.LittleEndian, uint64(_PLP_NULL))
return true
}
}
return false
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,80 @@
package mssql
import (
"database/sql/driver"
"encoding/hex"
"errors"
"fmt"
)
type UniqueIdentifier [16]byte
func (u *UniqueIdentifier) Scan(v interface{}) error {
reverse := func(b []byte) {
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
}
switch vt := v.(type) {
case []byte:
if len(vt) != 16 {
return errors.New("mssql: invalid UniqueIdentifier length")
}
var raw UniqueIdentifier
copy(raw[:], vt)
reverse(raw[0:4])
reverse(raw[4:6])
reverse(raw[6:8])
*u = raw
return nil
case string:
if len(vt) != 36 {
return errors.New("mssql: invalid UniqueIdentifier string length")
}
b := []byte(vt)
for i, c := range b {
switch c {
case '-':
b = append(b[:i], b[i+1:]...)
}
}
_, err := hex.Decode(u[:], []byte(b))
return err
default:
return fmt.Errorf("mssql: cannot convert %T to UniqueIdentifier", v)
}
}
func (u UniqueIdentifier) Value() (driver.Value, error) {
reverse := func(b []byte) {
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
}
raw := make([]byte, len(u))
copy(raw, u[:])
reverse(raw[0:4])
reverse(raw[4:6])
reverse(raw[6:8])
return raw, nil
}
func (u UniqueIdentifier) String() string {
return fmt.Sprintf("%X-%X-%X-%X-%X", u[0:4], u[4:6], u[6:8], u[8:10], u[10:])
}
// MarshalText converts Uniqueidentifier to bytes corresponding to the stringified hexadecimal representation of the Uniqueidentifier
// e.g., "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA" -> [65 65 65 65 65 65 65 65 45 65 65 65 65 45 65 65 65 65 45 65 65 65 65 65 65 65 65 65 65 65 65]
func (u UniqueIdentifier) MarshalText() []byte {
return []byte(u.String())
}

@ -0,0 +1,20 @@
# This file lists authors for copyright purposes. This file is distinct from
# the CONTRIBUTORS files. See the latter for an explanation.
#
# Names should be added to this file as:
# Name or Organization <email address>
#
# The email address is not required for organizations.
#
# Please keep the list sorted.
Artyom Pervukhin <github@artyom.dev>
Dan Peterson <danp@danp.net>
Davsk Ltd Co <skinner.david@gmail.com>
Jaap Aarts <jaap.aarts1@gmail.com>
Jan Mercl <0xjnml@gmail.com>
Logan Snow <logansnow@protonmail.com>
Michael Hoffmann <mhoffm@posteo.de>
Ross Light <ross@zombiezen.com>
Steffen Butzer <steffen(dot)butzer@outlook.com>
Saed SayedAhmed <saadmtsa@gmail.com>

@ -0,0 +1,22 @@
# This file lists people who contributed code to this repository. The AUTHORS
# file lists the copyright holders; this file lists people.
#
# Names should be added to this file like so:
# Name <email address>
#
# Please keep the list sorted.
Alexander Menzhinsky <amenzhinsky@gmail.com>
Artyom Pervukhin <github@artyom.dev>
Dan Peterson <danp@danp.net>
David Skinner <skinner.david@gmail.com>
Gleb Sakhnov <gleb.sakhnov@gmail.com>
Jaap Aarts <jaap.aarts1@gmail.com>
Jan Mercl <0xjnml@gmail.com>
Logan Snow <logansnow@protonmail.com>
Matthew Gabeler-Lee <fastcat@gmail.com>
Michael Hoffmann <mhoffm@posteo.de>
Ross Light <ross@zombiezen.com>
Steffen Butzer <steffen(dot)butzer@outlook.com>
Yaacov Akiba Slama <ya@slamail.org>
Saed SayedAhmed <saadmtsa@gmail.com>

@ -0,0 +1,26 @@
Copyright (c) 2017 The Sqlite Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

@ -0,0 +1,45 @@
[![Tests](https://github.com/glebarez/go-sqlite/actions/workflows/tests.yml/badge.svg)](https://github.com/glebarez/go-sqlite/actions/workflows/tests.yml)
![badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/glebarez/0fd7561eb29baf31d5362ffee1ae1702/raw/badge-sqlite-version-with-date.json)
# go-sqlite
This is a pure-Go SQLite driver for Golang's native [database/sql](https://pkg.go.dev/database/sql) package.
The driver has [Go-based implementation of SQLite](https://gitlab.com/cznic/sqlite) embedded in itself (so, you don't need to install SQLite separately)
# Usage
## Example
```go
package main
import (
"database/sql"
"log"
_ "github.com/glebarez/go-sqlite"
)
func main() {
// connect
db, err := sql.Open("sqlite", ":memory:")
if err != nil {
log.Fatal(err)
}
// get SQLite version
_ := db.QueryRow("select sqlite_version()")
}
```
## Connection string examples
- in-memory SQLite: ```":memory:"```
- on-disk SQLite: ```"path/to/some.db"```
- Foreign-key constraint activation: ```":memory:?_pragma=foreign_keys(1)"```
## Settings PRAGMAs in connection string
Any SQLIte pragma can be preset for a Database connection using ```_pragma``` query parameter. Examples:
- [journal mode](https://www.sqlite.org/pragma.html#pragma_journal_mode): ```path/to/some.db?_pragma=journal_mode(WAL)```
- [busy timeout](https://www.sqlite.org/pragma.html#pragma_busy_timeout): ```:memory:?_pragma=busy_timeout(5000)```
Multiple PRAGMAs can be specified, e.g.:<br>
```path/to/some.db?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)```

@ -0,0 +1,25 @@
SQLite Is Public Domain
All of the code and documentation in SQLite has been dedicated to the public
domain by the authors. All code authors, and representatives of the companies
they work for, have signed affidavits dedicating their contributions to the
public domain and originals of those signed affidavits are stored in a firesafe
at the main offices of Hwaci. Anyone is free to copy, modify, publish, use,
compile, sell, or distribute the original SQLite code, either in source code
form or as a compiled binary, for any purpose, commercial or non-commercial,
and by any means.
The previous paragraph applies to the deliverable code and documentation in
SQLite - those parts of the SQLite library that you actually bundle and ship
with a larger application. Some scripts used as part of the build process (for
example the "configure" scripts generated by autoconf) might fall under other
open-source licenses. Nothing from these build scripts ever reaches the final
deliverable SQLite library, however, and so the licenses associated with those
scripts should not be a factor in assessing your rights to copy and use the
SQLite library.
All of the deliverable code in SQLite has been written from scratch. No code
has been taken from other projects or from the open internet. Every line of
code can be traced back to its original author, and all of those authors have
public domain dedications on file. So the SQLite code base is clean and is
uncontaminated with licensed code from other projects.

@ -0,0 +1,23 @@
// Copyright 2019 The Sqlite 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 sqlite // import "modernc.org/sqlite"
import (
"sync"
"unsafe"
"modernc.org/libc"
"modernc.org/libc/sys/types"
)
type mutex struct {
sync.Mutex
}
func mutexAlloc(tls *libc.TLS) uintptr {
return libc.Xcalloc(tls, 1, types.Size_t(unsafe.Sizeof(mutex{})))
}
func mutexFree(tls *libc.TLS, m uintptr) { libc.Xfree(tls, m) }

@ -0,0 +1,10 @@
// Copyright 2021 The Sqlite Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !freebsd
// +build !freebsd
package sqlite // import "modernc.org/sqlite"
func setMaxOpenFiles(n int) error { return nil }

@ -0,0 +1,16 @@
// Copyright 2021 The Sqlite 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 sqlite // import "modernc.org/sqlite"
import (
"golang.org/x/sys/unix"
)
func setMaxOpenFiles(n int64) error {
var rLimit unix.Rlimit
rLimit.Max = n
rLimit.Cur = n
return unix.Setrlimit(unix.RLIMIT_NOFILE, &rLimit)
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,49 @@
// Copyright 2017 The Sqlite Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.8
// +build go1.8
package sqlite // import "modernc.org/sqlite"
import (
"context"
"database/sql/driver"
)
// Ping implements driver.Pinger
func (c *conn) Ping(ctx context.Context) error {
_, err := c.ExecContext(ctx, "select 1", nil)
return err
}
// BeginTx implements driver.ConnBeginTx
func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return c.begin(ctx, opts)
}
// PrepareContext implements driver.ConnPrepareContext
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
return c.prepare(ctx, query)
}
// ExecContext implements driver.ExecerContext
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
return c.exec(ctx, query, args)
}
// QueryContext implements driver.QueryerContext
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
return c.query(ctx, query, args)
}
// ExecContext implements driver.StmtExecContext
func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
return s.exec(ctx, args)
}
// QueryContext implements driver.StmtQueryContext
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
return s.query(ctx, args)
}

@ -0,0 +1 @@
vendor/

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2013-NOW Jinzhu <wosmvp@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

@ -0,0 +1,58 @@
![badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/glebarez/fb4d23f63d866b3e1e58b26d2f5ed01f/raw/badge-gorm-tests.json)
![badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/glebarez/fb4d23f63d866b3e1e58b26d2f5ed01f/raw/badge-sqlite-version.json)
# Pure-Go SQLite driver for GORM
Pure-go (without cgo) implementation of SQLite driver for [GORM](https://gorm.io/)<br><br>
This driver has SQLite embedded, you don't need to install one separately.
# Usage
```go
import (
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
db, err := gorm.Open(sqlite.Open("sqlite.db"), &gorm.Config{})
```
### In-memory DB example
```go
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
```
### Foreign-key constraint activation
Foreign-key constraint is disabled by default in SQLite. To activate it, use connection URL parameter:
```go
db, err := gorm.Open(sqlite.Open(":memory:?_pragma=foreign_keys(1)"), &gorm.Config{})
```
More info: [https://www.sqlite.org/foreignkeys.html](https://www.sqlite.org/foreignkeys.html)
# FAQ
## How is this better than standard GORM SQLite driver?
The [standard GORM driver for SQLite](https://github.com/go-gorm/sqlite) has one major drawback: it is based on a [Go-bindings of SQLite C-source](https://github.com/mattn/go-sqlite3) (this is called [cgo](https://go.dev/blog/cgo)). This fact imposes following restrictions on Go developers:
- to build and run your code, you will need a C compiler installed on a machine
- SQLite has many features that need to be enabled at compile time (e.g. [json support](https://www.sqlite.org/json1.html)). If you plan to use those, you will have to include proper build tags for every ```go``` command to work properly (```go run```, ```go test```, etc.).
- Because of C-compiler requirement, you can't build your Go code inside tiny stripped containers like (golang-alpine)
- Building on GCP is not possible because Google Cloud Platform does not allow gcc to be executed.
**Instead**, this driver is based on pure-Go implementation of SQLite (https://gitlab.com/cznic/sqlite), which is basically an original SQLite C-source AST, translated into Go! So, you may be sure you're using the original SQLite implementation under the hood.
## Is this tested good ?
Yes, The CI pipeline of this driver employs [whole test base](https://github.com/go-gorm/gorm/tree/master/tests) of GORM, which includes more than **12k** tests (see badge on the page-top). Testing is run against latest major releases of Go:
- 1.16
- 1.17
- 1.18
In following environments:
- Linux
- Windows
- MacOS
## Is it fast?
Well, it's slower than CGo implementation, but not terribly. See the [bechmark of underlying pure-Go driver vs CGo implementation](https://github.com/glebarez/go-sqlite/tree/master/benchmark).
## Included features
- JSON1 (https://www.sqlite.org/json1.html)
- Math functions (https://www.sqlite.org/lang_mathfunc.html)

@ -0,0 +1,231 @@
package sqlite
import (
"database/sql"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"gorm.io/gorm/migrator"
)
var (
sqliteSeparator = "`|\"|'|\t"
indexRegexp = regexp.MustCompile(fmt.Sprintf("CREATE(?: UNIQUE)? INDEX [%v]?[\\w\\d-]+[%v]? ON (.*)$", sqliteSeparator, sqliteSeparator))
tableRegexp = regexp.MustCompile(fmt.Sprintf("(?i)(CREATE TABLE [%v]?[\\w\\d-]+[%v]?)(?: \\((.*)\\))?", sqliteSeparator, sqliteSeparator))
separatorRegexp = regexp.MustCompile(fmt.Sprintf("[%v]", sqliteSeparator))
columnsRegexp = regexp.MustCompile(fmt.Sprintf("\\([%v]?([\\w\\d]+)[%v]?(?:,[%v]?([\\w\\d]+)[%v]){0,}\\)", sqliteSeparator, sqliteSeparator, sqliteSeparator, sqliteSeparator))
columnRegexp = regexp.MustCompile(fmt.Sprintf("^[%v]?([\\w\\d]+)[%v]?\\s+([\\w\\(\\)\\d]+)(.*)$", sqliteSeparator, sqliteSeparator))
defaultValueRegexp = regexp.MustCompile("(?i) DEFAULT \\(?(.+)?\\)?( |COLLATE|GENERATED|$)")
regRealDataType = regexp.MustCompile(`[^\d](\d+)[^\d]?`)
)
type ddl struct {
head string
fields []string
columns []migrator.ColumnType
}
func parseDDL(strs ...string) (*ddl, error) {
var result ddl
for _, str := range strs {
if sections := tableRegexp.FindStringSubmatch(str); len(sections) > 0 {
var (
ddlBody = sections[2]
ddlBodyRunes = []rune(ddlBody)
bracketLevel int
quote rune
buf string
)
ddlBodyRunesLen := len(ddlBodyRunes)
result.head = sections[1]
for idx := 0; idx < ddlBodyRunesLen; idx++ {
var (
next rune = 0
c = ddlBodyRunes[idx]
)
if idx+1 < ddlBodyRunesLen {
next = ddlBodyRunes[idx+1]
}
if sc := string(c); separatorRegexp.MatchString(sc) {
if c == next {
buf += sc // Skip escaped quote
idx++
} else if quote > 0 {
quote = 0
} else {
quote = c
}
} else if quote == 0 {
if c == '(' {
bracketLevel++
} else if c == ')' {
bracketLevel--
} else if bracketLevel == 0 {
if c == ',' {
result.fields = append(result.fields, strings.TrimSpace(buf))
buf = ""
continue
}
}
}
if bracketLevel < 0 {
return nil, errors.New("invalid DDL, unbalanced brackets")
}
buf += string(c)
}
if bracketLevel != 0 {
return nil, errors.New("invalid DDL, unbalanced brackets")
}
if buf != "" {
result.fields = append(result.fields, strings.TrimSpace(buf))
}
for _, f := range result.fields {
fUpper := strings.ToUpper(f)
if strings.HasPrefix(fUpper, "CHECK") ||
strings.HasPrefix(fUpper, "CONSTRAINT") {
continue
}
if strings.HasPrefix(fUpper, "PRIMARY KEY") {
matches := columnsRegexp.FindStringSubmatch(f)
if len(matches) > 1 {
for _, name := range matches[1:] {
for idx, column := range result.columns {
if column.NameValue.String == name {
column.PrimaryKeyValue = sql.NullBool{Bool: true, Valid: true}
result.columns[idx] = column
break
}
}
}
}
} else if matches := columnRegexp.FindStringSubmatch(f); len(matches) > 0 {
columnType := migrator.ColumnType{
NameValue: sql.NullString{String: matches[1], Valid: true},
DataTypeValue: sql.NullString{String: matches[2], Valid: true},
ColumnTypeValue: sql.NullString{String: matches[2], Valid: true},
PrimaryKeyValue: sql.NullBool{Valid: true},
UniqueValue: sql.NullBool{Valid: true},
NullableValue: sql.NullBool{Valid: true},
DefaultValueValue: sql.NullString{Valid: false},
}
matchUpper := strings.ToUpper(matches[3])
if strings.Contains(matchUpper, " NOT NULL") {
columnType.NullableValue = sql.NullBool{Bool: false, Valid: true}
} else if strings.Contains(matchUpper, " NULL") {
columnType.NullableValue = sql.NullBool{Bool: true, Valid: true}
}
if strings.Contains(matchUpper, " UNIQUE") {
columnType.UniqueValue = sql.NullBool{Bool: true, Valid: true}
}
if strings.Contains(matchUpper, " PRIMARY") {
columnType.PrimaryKeyValue = sql.NullBool{Bool: true, Valid: true}
}
if defaultMatches := defaultValueRegexp.FindStringSubmatch(matches[3]); len(defaultMatches) > 1 {
columnType.DefaultValueValue = sql.NullString{String: strings.Trim(defaultMatches[1], `"`), Valid: true}
}
// data type length
matches := regRealDataType.FindAllStringSubmatch(columnType.DataTypeValue.String, -1)
if len(matches) == 1 && len(matches[0]) == 2 {
size, _ := strconv.Atoi(matches[0][1])
columnType.LengthValue = sql.NullInt64{Valid: true, Int64: int64(size)}
columnType.DataTypeValue.String = strings.TrimSuffix(columnType.DataTypeValue.String, matches[0][0])
}
result.columns = append(result.columns, columnType)
}
}
} else if matches := indexRegexp.FindStringSubmatch(str); len(matches) > 0 {
if columns := columnsRegexp.FindStringSubmatch(matches[1]); len(columns) == 1 {
for idx, c := range result.columns {
if c.NameValue.String == columns[0] {
c.UniqueValue = sql.NullBool{Bool: true, Valid: true}
result.columns[idx] = c
}
}
}
} else {
return nil, errors.New("invalid DDL")
}
}
return &result, nil
}
func (d *ddl) compile() string {
if len(d.fields) == 0 {
return d.head
}
return fmt.Sprintf("%s (%s)", d.head, strings.Join(d.fields, ","))
}
func (d *ddl) addConstraint(name string, sql string) {
reg := regexp.MustCompile("^CONSTRAINT [\"`]?" + regexp.QuoteMeta(name) + "[\"` ]")
for i := 0; i < len(d.fields); i++ {
if reg.MatchString(d.fields[i]) {
d.fields[i] = sql
return
}
}
d.fields = append(d.fields, sql)
}
func (d *ddl) removeConstraint(name string) bool {
reg := regexp.MustCompile("^CONSTRAINT [\"`]?" + regexp.QuoteMeta(name) + "[\"` ]")
for i := 0; i < len(d.fields); i++ {
if reg.MatchString(d.fields[i]) {
d.fields = append(d.fields[:i], d.fields[i+1:]...)
return true
}
}
return false
}
func (d *ddl) hasConstraint(name string) bool {
reg := regexp.MustCompile("^CONSTRAINT [\"`]?" + regexp.QuoteMeta(name) + "[\"` ]")
for _, f := range d.fields {
if reg.MatchString(f) {
return true
}
}
return false
}
func (d *ddl) getColumns() []string {
res := []string{}
for _, f := range d.fields {
fUpper := strings.ToUpper(f)
if strings.HasPrefix(fUpper, "PRIMARY KEY") ||
strings.HasPrefix(fUpper, "CHECK") ||
strings.HasPrefix(fUpper, "CONSTRAINT") {
continue
}
reg := regexp.MustCompile("^[\"`']?([\\w\\d]+)[\"`']?")
match := reg.FindStringSubmatch(f)
if match != nil {
res = append(res, "`"+match[1]+"`")
}
}
return res
}

@ -0,0 +1,7 @@
package sqlite
import "errors"
var (
ErrConstraintsNotImplemented = errors.New("constraints not implemented on sqlite, consider using DisableForeignKeyConstraintWhenMigrating, more details https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft#all-new-migrator")
)

@ -0,0 +1,423 @@
package sqlite
import (
"database/sql"
"fmt"
"regexp"
"strings"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
)
type Migrator struct {
migrator.Migrator
}
func (m *Migrator) RunWithoutForeignKey(fc func() error) error {
var enabled int
m.DB.Raw("PRAGMA foreign_keys").Scan(&enabled)
if enabled == 1 {
m.DB.Exec("PRAGMA foreign_keys = OFF")
defer m.DB.Exec("PRAGMA foreign_keys = ON")
}
return fc()
}
func (m Migrator) HasTable(value interface{}) bool {
var count int
m.Migrator.RunWithValue(value, func(stmt *gorm.Statement) error {
return m.DB.Raw("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?", stmt.Table).Row().Scan(&count)
})
return count > 0
}
func (m Migrator) DropTable(values ...interface{}) error {
return m.RunWithoutForeignKey(func() error {
values = m.ReorderModels(values, false)
tx := m.DB.Session(&gorm.Session{})
for i := len(values) - 1; i >= 0; i-- {
if err := m.RunWithValue(values[i], func(stmt *gorm.Statement) error {
return tx.Exec("DROP TABLE IF EXISTS ?", clause.Table{Name: stmt.Table}).Error
}); err != nil {
return err
}
}
return nil
})
}
func (m Migrator) GetTables() (tableList []string, err error) {
return tableList, m.DB.Raw("SELECT name FROM sqlite_master where type=?", "table").Scan(&tableList).Error
}
func (m Migrator) HasColumn(value interface{}, name string) bool {
var count int
m.Migrator.RunWithValue(value, func(stmt *gorm.Statement) error {
if stmt.Schema != nil {
if field := stmt.Schema.LookUpField(name); field != nil {
name = field.DBName
}
}
if name != "" {
m.DB.Raw(
"SELECT count(*) FROM sqlite_master WHERE type = ? AND tbl_name = ? AND (sql LIKE ? OR sql LIKE ? OR sql LIKE ? OR sql LIKE ? OR sql LIKE ?)",
"table", stmt.Table, `%"`+name+`" %`, `%`+name+` %`, "%`"+name+"`%", "%["+name+"]%", "%\t"+name+"\t%",
).Row().Scan(&count)
}
return nil
})
return count > 0
}
func (m Migrator) AlterColumn(value interface{}, name string) error {
return m.RunWithoutForeignKey(func() error {
return m.recreateTable(value, nil, func(rawDDL string, stmt *gorm.Statement) (sql string, sqlArgs []interface{}, err error) {
if field := stmt.Schema.LookUpField(name); field != nil {
// lookup field from table definition, ddl might looks like `'name' int,` or `'name' int)`
reg, err := regexp.Compile("(`|'|\"| )" + field.DBName + "(`|'|\"| ) .*?(,|\\)\\s*$)")
if err != nil {
return "", nil, err
}
createSQL := reg.ReplaceAllString(rawDDL, fmt.Sprintf("`%v` ?$3", field.DBName))
if createSQL == rawDDL {
return "", nil, fmt.Errorf("failed to look up field %v from DDL %v", field.DBName, rawDDL)
}
return createSQL, []interface{}{m.FullDataTypeOf(field)}, nil
}
return "", nil, fmt.Errorf("failed to alter field with name %v", name)
})
})
}
// ColumnTypes return columnTypes []gorm.ColumnType and execErr error
func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
columnTypes := make([]gorm.ColumnType, 0)
execErr := m.RunWithValue(value, func(stmt *gorm.Statement) (err error) {
var (
sqls []string
sqlDDL *ddl
)
if err := m.DB.Raw("SELECT sql FROM sqlite_master WHERE type IN ? AND tbl_name = ? AND sql IS NOT NULL order by type = ? desc", []string{"table", "index"}, stmt.Table, "table").Scan(&sqls).Error; err != nil {
return err
}
if sqlDDL, err = parseDDL(sqls...); err != nil {
return err
}
rows, err := m.DB.Session(&gorm.Session{}).Table(stmt.Table).Limit(1).Rows()
if err != nil {
return err
}
defer func() {
err = rows.Close()
}()
var rawColumnTypes []*sql.ColumnType
rawColumnTypes, err = rows.ColumnTypes()
if err != nil {
return err
}
for _, c := range rawColumnTypes {
columnType := migrator.ColumnType{SQLColumnType: c}
for _, column := range sqlDDL.columns {
if column.NameValue.String == c.Name() {
column.SQLColumnType = c
columnType = column
break
}
}
columnTypes = append(columnTypes, columnType)
}
return err
})
return columnTypes, execErr
}
func (m Migrator) DropColumn(value interface{}, name string) error {
return m.recreateTable(value, nil, func(rawDDL string, stmt *gorm.Statement) (sql string, sqlArgs []interface{}, err error) {
if field := stmt.Schema.LookUpField(name); field != nil {
name = field.DBName
}
reg, err := regexp.Compile("(`|'|\"| |\\[)" + name + "(`|'|\"| |\\]) .*?,")
if err != nil {
return "", nil, err
}
createSQL := reg.ReplaceAllString(rawDDL, "")
return createSQL, nil, nil
})
}
func (m Migrator) CreateConstraint(value interface{}, name string) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
constraint, chk, table := m.GuessConstraintAndTable(stmt, name)
return m.recreateTable(value, &table,
func(rawDDL string, stmt *gorm.Statement) (sql string, sqlArgs []interface{}, err error) {
var (
constraintName string
constraintSql string
constraintValues []interface{}
)
if constraint != nil {
constraintName = constraint.Name
constraintSql, constraintValues = buildConstraint(constraint)
} else if chk != nil {
constraintName = chk.Name
constraintSql = "CONSTRAINT ? CHECK (?)"
constraintValues = []interface{}{clause.Column{Name: chk.Name}, clause.Expr{SQL: chk.Constraint}}
} else {
return "", nil, nil
}
createDDL, err := parseDDL(rawDDL)
if err != nil {
return "", nil, err
}
createDDL.addConstraint(constraintName, constraintSql)
createSQL := createDDL.compile()
return createSQL, constraintValues, nil
})
})
}
func (m Migrator) DropConstraint(value interface{}, name string) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
constraint, chk, table := m.GuessConstraintAndTable(stmt, name)
if constraint != nil {
name = constraint.Name
} else if chk != nil {
name = chk.Name
}
return m.recreateTable(value, &table,
func(rawDDL string, stmt *gorm.Statement) (sql string, sqlArgs []interface{}, err error) {
createDDL, err := parseDDL(rawDDL)
if err != nil {
return "", nil, err
}
createDDL.removeConstraint(name)
createSQL := createDDL.compile()
return createSQL, nil, nil
})
})
}
func (m Migrator) HasConstraint(value interface{}, name string) bool {
var count int64
m.RunWithValue(value, func(stmt *gorm.Statement) error {
constraint, chk, table := m.GuessConstraintAndTable(stmt, name)
if constraint != nil {
name = constraint.Name
} else if chk != nil {
name = chk.Name
}
m.DB.Raw(
"SELECT count(*) FROM sqlite_master WHERE type = ? AND tbl_name = ? AND (sql LIKE ? OR sql LIKE ? OR sql LIKE ? OR sql LIKE ? OR sql LIKE ?)",
"table", table, `%CONSTRAINT "`+name+`" %`, `%CONSTRAINT `+name+` %`, "%CONSTRAINT `"+name+"`%", "%CONSTRAINT ["+name+"]%", "%CONSTRAINT \t"+name+"\t%",
).Row().Scan(&count)
return nil
})
return count > 0
}
func (m Migrator) CurrentDatabase() (name string) {
var null interface{}
m.DB.Raw("PRAGMA database_list").Row().Scan(&null, &name, &null)
return
}
func (m Migrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{}) {
for _, opt := range opts {
str := stmt.Quote(opt.DBName)
if opt.Expression != "" {
str = opt.Expression
}
if opt.Collate != "" {
str += " COLLATE " + opt.Collate
}
if opt.Sort != "" {
str += " " + opt.Sort
}
results = append(results, clause.Expr{SQL: str})
}
return
}
func (m Migrator) CreateIndex(value interface{}, name string) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
if idx := stmt.Schema.LookIndex(name); idx != nil {
opts := m.BuildIndexOptions(idx.Fields, stmt)
values := []interface{}{clause.Column{Name: idx.Name}, clause.Table{Name: stmt.Table}, opts}
createIndexSQL := "CREATE "
if idx.Class != "" {
createIndexSQL += idx.Class + " "
}
createIndexSQL += "INDEX ?"
if idx.Type != "" {
createIndexSQL += " USING " + idx.Type
}
createIndexSQL += " ON ??"
if idx.Where != "" {
createIndexSQL += " WHERE " + idx.Where
}
return m.DB.Exec(createIndexSQL, values...).Error
}
return fmt.Errorf("failed to create index with name %v", name)
})
}
func (m Migrator) HasIndex(value interface{}, name string) bool {
var count int
m.RunWithValue(value, func(stmt *gorm.Statement) error {
if idx := stmt.Schema.LookIndex(name); idx != nil {
name = idx.Name
}
if name != "" {
m.DB.Raw(
"SELECT count(*) FROM sqlite_master WHERE type = ? AND tbl_name = ? AND name = ?", "index", stmt.Table, name,
).Row().Scan(&count)
}
return nil
})
return count > 0
}
func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
var sql string
m.DB.Raw("SELECT sql FROM sqlite_master WHERE type = ? AND tbl_name = ? AND name = ?", "index", stmt.Table, oldName).Row().Scan(&sql)
if sql != "" {
return m.DB.Exec(strings.Replace(sql, oldName, newName, 1)).Error
}
return fmt.Errorf("failed to find index with name %v", oldName)
})
}
func (m Migrator) DropIndex(value interface{}, name string) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
if idx := stmt.Schema.LookIndex(name); idx != nil {
name = idx.Name
}
return m.DB.Exec("DROP INDEX ?", clause.Column{Name: name}).Error
})
}
func buildConstraint(constraint *schema.Constraint) (sql string, results []interface{}) {
sql = "CONSTRAINT ? FOREIGN KEY ? REFERENCES ??"
if constraint.OnDelete != "" {
sql += " ON DELETE " + constraint.OnDelete
}
if constraint.OnUpdate != "" {
sql += " ON UPDATE " + constraint.OnUpdate
}
var foreignKeys, references []interface{}
for _, field := range constraint.ForeignKeys {
foreignKeys = append(foreignKeys, clause.Column{Name: field.DBName})
}
for _, field := range constraint.References {
references = append(references, clause.Column{Name: field.DBName})
}
results = append(results, clause.Table{Name: constraint.Name}, foreignKeys, clause.Table{Name: constraint.ReferenceSchema.Table}, references)
return
}
func (m Migrator) getRawDDL(table string) (string, error) {
var createSQL string
m.DB.Raw("SELECT sql FROM sqlite_master WHERE type = ? AND tbl_name = ? AND name = ?", "table", table, table).Row().Scan(&createSQL)
if m.DB.Error != nil {
return "", m.DB.Error
}
return createSQL, nil
}
func (m Migrator) recreateTable(value interface{}, tablePtr *string,
getCreateSQL func(rawDDL string, stmt *gorm.Statement) (sql string, sqlArgs []interface{}, err error)) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
table := stmt.Table
if tablePtr != nil {
table = *tablePtr
}
rawDDL, err := m.getRawDDL(table)
if err != nil {
return err
}
newTableName := table + "__temp"
createSQL, sqlArgs, err := getCreateSQL(rawDDL, stmt)
if err != nil {
return err
}
if createSQL == "" {
return nil
}
tableReg, err := regexp.Compile(" ('|`|\"| )" + table + "('|`|\"| ) ")
if err != nil {
return err
}
createSQL = tableReg.ReplaceAllString(createSQL, fmt.Sprintf(" `%v` ", newTableName))
createDDL, err := parseDDL(createSQL)
if err != nil {
return err
}
columns := createDDL.getColumns()
return m.DB.Transaction(func(tx *gorm.DB) error {
if err := tx.Exec(createSQL, sqlArgs...).Error; err != nil {
return err
}
queries := []string{
fmt.Sprintf("INSERT INTO `%v`(%v) SELECT %v FROM `%v`", newTableName, strings.Join(columns, ","), strings.Join(columns, ","), table),
fmt.Sprintf("DROP TABLE `%v`", table),
fmt.Sprintf("ALTER TABLE `%v` RENAME TO `%v`", newTableName, table),
}
for _, query := range queries {
if err := tx.Exec(query).Error; err != nil {
return err
}
}
return nil
})
})
}

@ -0,0 +1,221 @@
package sqlite
import (
"context"
"database/sql"
"strconv"
"strings"
"gorm.io/gorm/callbacks"
_ "github.com/glebarez/go-sqlite"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
)
// DriverName is the default driver name for SQLite.
const DriverName = "sqlite"
type Dialector struct {
DriverName string
DSN string
Conn gorm.ConnPool
}
func Open(dsn string) gorm.Dialector {
return &Dialector{DSN: dsn}
}
func (dialector Dialector) Name() string {
return "sqlite"
}
func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
if dialector.DriverName == "" {
dialector.DriverName = DriverName
}
if dialector.Conn != nil {
db.ConnPool = dialector.Conn
} else {
conn, err := sql.Open(dialector.DriverName, dialector.DSN)
if err != nil {
return err
}
db.ConnPool = conn
}
var version string
if err := db.ConnPool.QueryRowContext(context.Background(), "select sqlite_version()").Scan(&version); err != nil {
return err
}
// https://www.sqlite.org/releaselog/3_35_0.html
if compareVersion(version, "3.35.0") >= 0 {
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{
CreateClauses: []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"},
UpdateClauses: []string{"UPDATE", "SET", "WHERE", "RETURNING"},
DeleteClauses: []string{"DELETE", "FROM", "WHERE", "RETURNING"},
LastInsertIDReversed: true,
})
} else {
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{
LastInsertIDReversed: true,
})
}
for k, v := range dialector.ClauseBuilders() {
db.ClauseBuilders[k] = v
}
return
}
func (dialector Dialector) ClauseBuilders() map[string]clause.ClauseBuilder {
return map[string]clause.ClauseBuilder{
"INSERT": func(c clause.Clause, builder clause.Builder) {
if insert, ok := c.Expression.(clause.Insert); ok {
if stmt, ok := builder.(*gorm.Statement); ok {
stmt.WriteString("INSERT ")
if insert.Modifier != "" {
stmt.WriteString(insert.Modifier)
stmt.WriteByte(' ')
}
stmt.WriteString("INTO ")
if insert.Table.Name == "" {
stmt.WriteQuoted(stmt.Table)
} else {
stmt.WriteQuoted(insert.Table)
}
return
}
}
c.Build(builder)
},
"LIMIT": func(c clause.Clause, builder clause.Builder) {
if limit, ok := c.Expression.(clause.Limit); ok {
if limit.Limit > 0 || limit.Offset > 0 {
if limit.Limit <= 0 {
limit.Limit = -1
}
builder.WriteString("LIMIT ")
builder.WriteString(strconv.Itoa(limit.Limit))
}
if limit.Offset > 0 {
builder.WriteString(" OFFSET ")
builder.WriteString(strconv.Itoa(limit.Offset))
}
}
},
"FOR": func(c clause.Clause, builder clause.Builder) {
if _, ok := c.Expression.(clause.Locking); ok {
// SQLite3 does not support row-level locking.
return
}
c.Build(builder)
},
}
}
func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression {
if field.AutoIncrement {
return clause.Expr{SQL: "NULL"}
}
// doesn't work, will raise error
return clause.Expr{SQL: "DEFAULT"}
}
func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
return Migrator{migrator.Migrator{Config: migrator.Config{
DB: db,
Dialector: dialector,
CreateIndexAfterCreateTable: true,
}}}
}
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
writer.WriteByte('?')
}
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
writer.WriteByte('`')
if strings.Contains(str, ".") {
for idx, str := range strings.Split(str, ".") {
if idx > 0 {
writer.WriteString(".`")
}
writer.WriteString(str)
writer.WriteByte('`')
}
} else {
writer.WriteString(str)
writer.WriteByte('`')
}
}
func (dialector Dialector) Explain(sql string, vars ...interface{}) string {
return logger.ExplainSQL(sql, nil, `"`, vars...)
}
func (dialector Dialector) DataTypeOf(field *schema.Field) string {
switch field.DataType {
case schema.Bool:
return "numeric"
case schema.Int, schema.Uint:
if field.AutoIncrement && !field.PrimaryKey {
// https://www.sqlite.org/autoinc.html
return "integer PRIMARY KEY AUTOINCREMENT"
} else {
return "integer"
}
case schema.Float:
return "real"
case schema.String:
return "text"
case schema.Time:
return "datetime"
case schema.Bytes:
return "blob"
}
return string(field.DataType)
}
func (dialectopr Dialector) SavePoint(tx *gorm.DB, name string) error {
tx.Exec("SAVEPOINT " + name)
return nil
}
func (dialectopr Dialector) RollbackTo(tx *gorm.DB, name string) error {
tx.Exec("ROLLBACK TO SAVEPOINT " + name)
return nil
}
func compareVersion(version1, version2 string) int {
n, m := len(version1), len(version2)
i, j := 0, 0
for i < n || j < m {
x := 0
for ; i < n && version1[i] != '.'; i++ {
x = x*10 + int(version1[i]-'0')
}
i++
y := 0
for ; j < m && version2[j] != '.'; j++ {
y = y*10 + int(version2[j]-'0')
}
j++
if x > y {
return 1
}
if x < y {
return -1
}
}
return 0
}

@ -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,292 @@
// Copyright 2016 Google LLC
//
// 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.
// Package civil implements types for civil time, a time-zone-independent
// representation of time that follows the rules of the proleptic
// Gregorian calendar with exactly 24-hour days, 60-minute hours, and 60-second
// minutes.
//
// Because they lack location information, these types do not represent unique
// moments or intervals of time. Use time.Time for that purpose.
package civil
import (
"fmt"
"time"
)
// A Date represents a date (year, month, day).
//
// This type does not include location information, and therefore does not
// describe a unique 24-hour timespan.
type Date struct {
Year int // Year (e.g., 2014).
Month time.Month // Month of the year (January = 1, ...).
Day int // Day of the month, starting at 1.
}
// DateOf returns the Date in which a time occurs in that time's location.
func DateOf(t time.Time) Date {
var d Date
d.Year, d.Month, d.Day = t.Date()
return d
}
// ParseDate parses a string in RFC3339 full-date format and returns the date value it represents.
func ParseDate(s string) (Date, error) {
t, err := time.Parse("2006-01-02", s)
if err != nil {
return Date{}, err
}
return DateOf(t), nil
}
// String returns the date in RFC3339 full-date format.
func (d Date) String() string {
return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day)
}
// IsValid reports whether the date is valid.
func (d Date) IsValid() bool {
return DateOf(d.In(time.UTC)) == d
}
// In returns the time corresponding to time 00:00:00 of the date in the location.
//
// In is always consistent with time.Date, even when time.Date returns a time
// on a different day. For example, if loc is America/Indiana/Vincennes, then both
// time.Date(1955, time.May, 1, 0, 0, 0, 0, loc)
// and
// civil.Date{Year: 1955, Month: time.May, Day: 1}.In(loc)
// return 23:00:00 on April 30, 1955.
//
// In panics if loc is nil.
func (d Date) In(loc *time.Location) time.Time {
return time.Date(d.Year, d.Month, d.Day, 0, 0, 0, 0, loc)
}
// AddDays returns the date that is n days in the future.
// n can also be negative to go into the past.
func (d Date) AddDays(n int) Date {
return DateOf(d.In(time.UTC).AddDate(0, 0, n))
}
// DaysSince returns the signed number of days between the date and s, not including the end day.
// This is the inverse operation to AddDays.
func (d Date) DaysSince(s Date) (days int) {
// We convert to Unix time so we do not have to worry about leap seconds:
// Unix time increases by exactly 86400 seconds per day.
deltaUnix := d.In(time.UTC).Unix() - s.In(time.UTC).Unix()
return int(deltaUnix / 86400)
}
// Before reports whether d1 occurs before d2.
func (d1 Date) Before(d2 Date) bool {
if d1.Year != d2.Year {
return d1.Year < d2.Year
}
if d1.Month != d2.Month {
return d1.Month < d2.Month
}
return d1.Day < d2.Day
}
// After reports whether d1 occurs after d2.
func (d1 Date) After(d2 Date) bool {
return d2.Before(d1)
}
// IsZero reports whether date fields are set to their default value.
func (d Date) IsZero() bool {
return (d.Year == 0) && (int(d.Month) == 0) && (d.Day == 0)
}
// MarshalText implements the encoding.TextMarshaler interface.
// The output is the result of d.String().
func (d Date) MarshalText() ([]byte, error) {
return []byte(d.String()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// The date is expected to be a string in a format accepted by ParseDate.
func (d *Date) UnmarshalText(data []byte) error {
var err error
*d, err = ParseDate(string(data))
return err
}
// A Time represents a time with nanosecond precision.
//
// This type does not include location information, and therefore does not
// describe a unique moment in time.
//
// This type exists to represent the TIME type in storage-based APIs like BigQuery.
// Most operations on Times are unlikely to be meaningful. Prefer the DateTime type.
type Time struct {
Hour int // The hour of the day in 24-hour format; range [0-23]
Minute int // The minute of the hour; range [0-59]
Second int // The second of the minute; range [0-59]
Nanosecond int // The nanosecond of the second; range [0-999999999]
}
// TimeOf returns the Time representing the time of day in which a time occurs
// in that time's location. It ignores the date.
func TimeOf(t time.Time) Time {
var tm Time
tm.Hour, tm.Minute, tm.Second = t.Clock()
tm.Nanosecond = t.Nanosecond()
return tm
}
// ParseTime parses a string and returns the time value it represents.
// ParseTime accepts an extended form of the RFC3339 partial-time format. After
// the HH:MM:SS part of the string, an optional fractional part may appear,
// consisting of a decimal point followed by one to nine decimal digits.
// (RFC3339 admits only one digit after the decimal point).
func ParseTime(s string) (Time, error) {
t, err := time.Parse("15:04:05.999999999", s)
if err != nil {
return Time{}, err
}
return TimeOf(t), nil
}
// String returns the date in the format described in ParseTime. If Nanoseconds
// is zero, no fractional part will be generated. Otherwise, the result will
// end with a fractional part consisting of a decimal point and nine digits.
func (t Time) String() string {
s := fmt.Sprintf("%02d:%02d:%02d", t.Hour, t.Minute, t.Second)
if t.Nanosecond == 0 {
return s
}
return s + fmt.Sprintf(".%09d", t.Nanosecond)
}
// IsValid reports whether the time is valid.
func (t Time) IsValid() bool {
// Construct a non-zero time.
tm := time.Date(2, 2, 2, t.Hour, t.Minute, t.Second, t.Nanosecond, time.UTC)
return TimeOf(tm) == t
}
// IsZero reports whether time fields are set to their default value.
func (t Time) IsZero() bool {
return (t.Hour == 0) && (t.Minute == 0) && (t.Second == 0) && (t.Nanosecond == 0)
}
// MarshalText implements the encoding.TextMarshaler interface.
// The output is the result of t.String().
func (t Time) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// The time is expected to be a string in a format accepted by ParseTime.
func (t *Time) UnmarshalText(data []byte) error {
var err error
*t, err = ParseTime(string(data))
return err
}
// A DateTime represents a date and time.
//
// This type does not include location information, and therefore does not
// describe a unique moment in time.
type DateTime struct {
Date Date
Time Time
}
// Note: We deliberately do not embed Date into DateTime, to avoid promoting AddDays and Sub.
// DateTimeOf returns the DateTime in which a time occurs in that time's location.
func DateTimeOf(t time.Time) DateTime {
return DateTime{
Date: DateOf(t),
Time: TimeOf(t),
}
}
// ParseDateTime parses a string and returns the DateTime it represents.
// ParseDateTime accepts a variant of the RFC3339 date-time format that omits
// the time offset but includes an optional fractional time, as described in
// ParseTime. Informally, the accepted format is
// YYYY-MM-DDTHH:MM:SS[.FFFFFFFFF]
// where the 'T' may be a lower-case 't'.
func ParseDateTime(s string) (DateTime, error) {
t, err := time.Parse("2006-01-02T15:04:05.999999999", s)
if err != nil {
t, err = time.Parse("2006-01-02t15:04:05.999999999", s)
if err != nil {
return DateTime{}, err
}
}
return DateTimeOf(t), nil
}
// String returns the date in the format described in ParseDate.
func (dt DateTime) String() string {
return dt.Date.String() + "T" + dt.Time.String()
}
// IsValid reports whether the datetime is valid.
func (dt DateTime) IsValid() bool {
return dt.Date.IsValid() && dt.Time.IsValid()
}
// In returns the time corresponding to the DateTime in the given location.
//
// If the time is missing or ambigous at the location, In returns the same
// result as time.Date. For example, if loc is America/Indiana/Vincennes, then
// both
// time.Date(1955, time.May, 1, 0, 30, 0, 0, loc)
// and
// civil.DateTime{
// civil.Date{Year: 1955, Month: time.May, Day: 1}},
// civil.Time{Minute: 30}}.In(loc)
// return 23:30:00 on April 30, 1955.
//
// In panics if loc is nil.
func (dt DateTime) In(loc *time.Location) time.Time {
return time.Date(dt.Date.Year, dt.Date.Month, dt.Date.Day, dt.Time.Hour, dt.Time.Minute, dt.Time.Second, dt.Time.Nanosecond, loc)
}
// Before reports whether dt1 occurs before dt2.
func (dt1 DateTime) Before(dt2 DateTime) bool {
return dt1.In(time.UTC).Before(dt2.In(time.UTC))
}
// After reports whether dt1 occurs after dt2.
func (dt1 DateTime) After(dt2 DateTime) bool {
return dt2.Before(dt1)
}
// IsZero reports whether datetime fields are set to their default value.
func (dt DateTime) IsZero() bool {
return dt.Date.IsZero() && dt.Time.IsZero()
}
// MarshalText implements the encoding.TextMarshaler interface.
// The output is the result of dt.String().
func (dt DateTime) MarshalText() ([]byte, error) {
return []byte(dt.String()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// The datetime is expected to be a string in a format accepted by ParseDateTime
func (dt *DateTime) UnmarshalText(data []byte) error {
var err error
*dt, err = ParseDateTime(string(data))
return err
}

@ -0,0 +1,27 @@
Copyright (c) 2017 The Go Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

@ -0,0 +1,22 @@
Additional IP Rights Grant (Patents)
"This implementation" means the copyrightable works distributed by
Google as part of the Go project.
Google 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,
transfer and otherwise run, modify and propagate the contents of this
implementation of Go, where such license applies only to those patent
claims, both currently owned or controlled by Google and acquired in
the future, licensable by Google that are necessarily infringed by this
implementation of Go. This grant does not include claims that would be
infringed only as a consequence of further modification of this
implementation. If you or your agent or exclusive licensee institute or
order or agree to the institution of patent litigation against any
entity (including a cross-claim or counterclaim in a lawsuit) alleging
that this implementation of Go or any code incorporated within this
implementation of Go constitutes direct or contributory patent
infringement, or inducement of patent infringement, then any patent
rights granted to you under this License for this implementation of Go
shall terminate as of the date such litigation is filed.

@ -0,0 +1,5 @@
# golang-sql exp
https://godoc.org/github.com/golang-sql/sqlexp
All contributions must have a valid golang CLA.

@ -0,0 +1,8 @@
// Copyright 2017 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 sqlexp provides interfaces and functions that may be adopted into
// the database/sql package in the future. All features may change or be removed
// in the future.
package sqlexp // imports github.com/golang-sql/sqlexp

@ -0,0 +1,79 @@
package sqlexp
import (
"context"
)
// RawMessage is returned from RowsMessage.
type RawMessage interface{}
// ReturnMessage may be passed into a Query argument.
//
// Drivers must implement driver.NamedValueChecker,
// call ReturnMessageInit on it, save it internally,
// and return driver.ErrOmitArgument to prevent
// this from appearing in the query arguments.
//
// Queries that recieve this message should also not return
// SQL errors from the Query method, but wait to return
// it in a Message.
type ReturnMessage struct {
queue chan RawMessage
}
// Message is called by clients after Query to dequeue messages.
func (m *ReturnMessage) Message(ctx context.Context) RawMessage {
select {
case <-ctx.Done():
return MsgNextResultSet{}
case raw := <-m.queue:
return raw
}
}
// ReturnMessageEnqueue is called by the driver to enqueue the driver.
// Drivers should not call this until after it returns from Query.
func ReturnMessageEnqueue(ctx context.Context, m *ReturnMessage, raw RawMessage) error {
select {
case <-ctx.Done():
return ctx.Err()
case m.queue <- raw:
return nil
}
}
// ReturnMessageInit is called by database/sql setup the ReturnMessage internals.
func ReturnMessageInit(m *ReturnMessage) {
m.queue = make(chan RawMessage, 15)
}
type (
// MsgNextResultSet must be checked for. When received, NextResultSet
// should be called and if false the message loop should be exited.
MsgNextResultSet struct{}
// MsgNext indicates the result set ready to be scanned.
// This message will often be followed with:
//
// for rows.Next() {
// rows.Scan(&v)
// }
MsgNext struct{}
// MsgRowsAffected returns the number of rows affected.
// Not all operations that affect rows return results, thus this message
// may be received multiple times.
MsgRowsAffected struct{ Count int64 }
// MsgLastInsertID returns the value of last inserted row. For many
// database systems and tables this will return int64. Some databases
// may return a string or GUID equivalent.
MsgLastInsertID struct{ Value interface{} }
// MsgNotice is raised from the SQL text and is only informational.
MsgNotice struct{ Message string }
// MsgError returns SQL errors from the database system (not transport
// or other system level errors).
MsgError struct{ Error error }
)

@ -0,0 +1,73 @@
// Copyright 2017 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 sqlexp
import (
"context"
"database/sql/driver"
"fmt"
"strings"
)
type mssql struct{}
var (
_ DriverNamer = mssql{}
_ DriverQuoter = mssql{}
_ DriverSavepointer = mssql{}
)
func (mssql) Open(string) (driver.Conn, error) {
panic("not implemented")
}
func (mssql) Namer(ctx context.Context) (Namer, error) {
return sqlServerNamer{}, nil
}
func (mssql) Quoter(ctx context.Context) (Quoter, error) {
return sqlServerQuoter{}, nil
}
func (mssql) Savepointer() (Savepointer, error) {
return sqlServerSavepointer{}, nil
}
type sqlServerNamer struct{}
func (sqlServerNamer) Name() string {
return "sqlserver"
}
func (sqlServerNamer) Dialect() string {
return DialectTSQL
}
type sqlServerQuoter struct{}
func (sqlServerQuoter) ID(name string) string {
return "[" + strings.Replace(name, "]", "]]", -1) + "]"
}
func (sqlServerQuoter) Value(v interface{}) string {
switch v := v.(type) {
default:
panic("unsupported value")
case string:
return "'" + strings.Replace(v, "'", "''", -1) + "'"
}
}
type sqlServerSavepointer struct{}
func (sqlServerSavepointer) Release(name string) string {
return ""
}
func (sqlServerSavepointer) Create(name string) string {
return fmt.Sprintf("save tran %s;", name)
}
func (sqlServerSavepointer) Rollback(name string) string {
return fmt.Sprintf("rollback tran %s;", name)
}

@ -0,0 +1,59 @@
// Copyright 2017 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 sqlexp
import (
"context"
"database/sql/driver"
"errors"
"reflect"
)
const (
DialectPostgres = "postgres"
DialectTSQL = "tsql"
DialectMySQL = "mysql"
DialectSQLite = "sqlite"
DialectOracle = "oracle"
)
// Namer returns the name of the database and the SQL dialect it
// uses.
type Namer interface {
// Name of the database management system.
//
// Examples:
// "posgresql-9.6"
// "sqlserver-10.54.32"
// "cockroachdb-1.0"
Name() string
// Dialect of SQL used in the database.
Dialect() string
}
// DriverNamer may be implemented on the driver.Driver interface.
// It may need to request information from the server to return
// the correct information.
type DriverNamer interface {
Namer(ctx context.Context) (Namer, error)
}
// NamerFromDriver returns the DriverNamer from the DB if
// it is implemented.
func NamerFromDriver(d driver.Driver, ctx context.Context) (Namer, error) {
if q, is := d.(DriverNamer); is {
return q.Namer(ctx)
}
dv := reflect.ValueOf(d)
d, found := internalDrivers[dv.Type().String()]
if found {
if q, is := d.(DriverNamer); is {
return q.Namer(ctx)
}
}
return nil, errors.New("namer not found")
}

@ -0,0 +1,67 @@
// Copyright 2017 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 sqlexp
import (
"context"
"database/sql/driver"
"fmt"
)
type postgresql struct{}
var (
_ DriverNamer = postgresql{}
_ DriverQuoter = postgresql{}
_ DriverSavepointer = postgresql{}
)
func (postgresql) Open(string) (driver.Conn, error) {
panic("not implemented")
}
func (postgresql) Namer(ctx context.Context) (Namer, error) {
return pgNamer{}, nil
}
func (postgresql) Quoter(ctx context.Context) (Quoter, error) {
panic("not implemented")
}
func (postgresql) Savepointer() (Savepointer, error) {
return pgSavepointer{}, nil
}
type pgNamer struct{}
func (pgNamer) Name() string {
return "postgresql"
}
func (pgNamer) Dialect() string {
return DialectPostgres
}
type pgQuoter struct{}
func (pgQuoter) ID(name string) string {
return ""
}
func (pgQuoter) Value(v interface{}) string {
return ""
}
type pgSavepointer struct{}
func (pgSavepointer) Release(name string) string {
return fmt.Sprintf("release savepoint %s;", name)
}
func (pgSavepointer) Create(name string) string {
return fmt.Sprintf("savepoint %s;", name)
}
func (pgSavepointer) Rollback(name string) string {
return fmt.Sprintf("rollback to savepoint %s;", name)
}

@ -0,0 +1,22 @@
// Copyright 2017 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 sqlexp
import (
"context"
"database/sql"
)
// Querier is the common interface to execute queries on a DB, Tx, or Conn.
type Querier interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
var (
_ Querier = &sql.DB{}
_ Querier = &sql.Tx{}
)

@ -0,0 +1,57 @@
// Copyright 2017 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 sqlexp
import (
"context"
"database/sql/driver"
"errors"
"reflect"
)
// BUG(kardianos): Both the Quoter and Namer may need to access the server.
// Quoter returns safe and valid SQL strings to use when building a SQL text.
type Quoter interface {
// ID quotes identifiers such as schema, table, or column names.
// ID does not operate on multipart identifiers such as "public.Table",
// it only operates on single identifiers such as "public" and "Table".
ID(name string) string
// Value quotes database values such as string or []byte types as strings
// that are suitable and safe to embed in SQL text. The returned value
// of a string will include all surrounding quotes.
//
// If a value type is not supported it must panic.
Value(v interface{}) string
}
// DriverQuoter returns a Quoter interface and is suitable for extending
// the driver.Driver type.
//
// The driver may need to hit the database to determine how it is configured to
// ensure the correct escaping rules are used.
type DriverQuoter interface {
Quoter(ctx context.Context) (Quoter, error)
}
// QuoterFromDriver takes a database driver, often obtained through a sql.DB.Driver
// call or from using it directly to get the quoter interface.
//
// Currently MssqlDriver is hard-coded to also return a valided Quoter.
func QuoterFromDriver(d driver.Driver, ctx context.Context) (Quoter, error) {
if q, is := d.(DriverQuoter); is {
return q.Quoter(ctx)
}
dv := reflect.ValueOf(d)
d, found := internalDrivers[dv.Type().String()]
if found {
if q, is := d.(DriverQuoter); is {
return q.Quoter(ctx)
}
}
return nil, errors.New("quoter interface not found")
}

@ -0,0 +1,15 @@
// Copyright 2017 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 sqlexp
import (
"database/sql/driver"
)
var internalDrivers = map[string]driver.Driver{
"*mssql.MssqlDriver": mssql{},
"*pq.Driver": postgresql{},
"*stdlib.Driver": postgresql{},
}

@ -0,0 +1,37 @@
// Copyright 2017 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 sqlexp
import (
"database/sql/driver"
"errors"
"reflect"
)
type Savepointer interface {
Release(name string) string
Create(name string) string
Rollback(name string) string
}
type DriverSavepointer interface {
Savepointer() (Savepointer, error)
}
// SavepointFromDriver
func SavepointFromDriver(d driver.Driver) (Savepointer, error) {
if q, is := d.(DriverSavepointer); is {
return q.Savepointer()
}
dv := reflect.ValueOf(d)
d, found := internalDrivers[dv.Type().String()]
if found {
if q, is := d.(DriverSavepointer); is {
return q.Savepointer()
}
}
return nil, errors.New("savepointer interface not found")
}

@ -0,0 +1,9 @@
language: go
go:
- 1.4.3
- 1.5.3
- tip
script:
- go test -v ./...

@ -0,0 +1,10 @@
# How to contribute
We definitely welcome patches and contribution to this project!
### Legal requirements
In order to protect both you and ourselves, you will need to sign the
[Contributor License Agreement](https://cla.developers.google.com/clas).
You may have already signed it for other Google projects.

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save