/
utf16.go
78 lines (66 loc) · 1.75 KB
/
utf16.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package odbc
import (
"database/sql/driver"
"encoding/binary"
"github.com/ninthclowd/unixodbc/internal/api"
"reflect"
"unicode/utf16"
"unsafe"
)
func init() {
registerColumnFactoryForType(newUTF16Column,
api.SQL_WCHAR,
api.SQL_WVARCHAR,
api.SQL_WLONGVARCHAR,
)
}
func newUTF16Column(info *columnInfo, hnd handle) Column {
return &columnUTF16{hnd, info}
}
type columnUTF16 struct {
handle
*columnInfo
}
func (c *columnUTF16) VariableLength() (length int64, ok bool) {
return int64(c.columnSize), true
}
func (c *columnUTF16) ScanType() reflect.Type {
return reflect.TypeOf((*string)(nil))
}
func (c *columnUTF16) Decimal() (precision int64, scale int64, ok bool) {
return
}
func (c *columnUTF16) Value() (driver.Value, error) {
utfLength := c.columnSize * 2
value := make([]byte, utfLength+1)
var valueLength api.SQLLEN
if _, err := c.result(c.api().SQLGetData(api.SQLHSTMT(c.hnd()), c.columnNumber, api.SQL_C_WCHAR, api.SQLPOINTER(&value[0]), api.SQLLEN(len(value)), &valueLength)); err != nil {
return nil, err
}
if valueLength == api.SQL_NULL_DATA {
return nil, nil
}
str := utf16String(value[:valueLength])
return str, nil
}
func (s *Statement) bindUTF16(index int, src string) error {
nts := make([]rune, len(src)+1)
for i, r := range src {
nts[i] = r
}
val := utf16.Encode(nts)
sz := unsafe.Sizeof(val)
_, err := s.result(s.api().SQLBindParameter((api.SQLHSTMT)(s.hnd()), api.SQLUSMALLINT(index+1), api.SQL_PARAM_INPUT,
api.SQL_C_WCHAR, api.SQLSMALLINT(api.SQL_WVARCHAR),
api.SQLULEN(sz), 0,
api.SQLPOINTER(&val[0]),
0, nil))
return err
}
func utf16String(b []byte) string {
utf := make([]uint16, len(b)/2)
for i := 0; i < len(b); i += 2 {
utf[i/2] = binary.LittleEndian.Uint16(b[i:])
}
return string(utf16.Decode(utf))
}