-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtype_varchar.go
More file actions
108 lines (91 loc) 路 2.33 KB
/
Copy pathtype_varchar.go
File metadata and controls
108 lines (91 loc) 路 2.33 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package parco
import (
"bytes"
"encoding/binary"
"io"
)
func Blob(header IntType) Type[[]byte] {
return varType[[]byte]{
header: header,
sizer: SizerFunc[[]byte](func(x []byte) int { return len(x) }),
pool: SinglePool,
parser: ParseBlob,
compiler: CompileBlob,
}
}
func NewVarcharType(header IntType) Type[string] {
return varType[string]{
header: header,
sizer: SizerFunc[string](func(x string) int { return len(x) }),
pool: SinglePool,
parser: ParseStringFactory(),
compiler: CompileStringWriter,
}
}
func String(header IntType) Type[string] {
return varType[string]{
header: header,
sizer: SizerFunc[string](func(x string) int { return len(x) }),
pool: SinglePool,
parser: ParseStringFactory(),
compiler: CompileStringWriter,
}
}
func SmallVarchar() Type[string] {
return NewVarcharType(UInt8Header())
}
func Varchar() Type[string] {
return NewVarcharType(UInt16HeaderLE())
}
func VarcharOrder(order binary.ByteOrder) Type[string] {
return NewVarcharType(UInt16Header(order))
}
func Text(order binary.ByteOrder) Type[string] {
return NewVarcharType(UInt32Header(order))
}
func LongText(order binary.ByteOrder) Type[string] {
return NewVarcharType(UInt64Header(order))
}
func ParseStringFactory() ParserFunc[string] {
return func(data []byte) (string, error) {
return ParseString(data)
}
}
func ParseString(data []byte) (res string, err error) {
return string(data), nil
}
func CompileStringWriter(x string, w io.Writer) (err error) {
var written int
data := String2Bytes(x)
written, err = w.Write(data)
if written != len(x) {
err = ErrCannotWrite
}
return
}
func CompileString(x string, box []byte) (err error) {
bites := String2Bytes(x)
if copy(box, bites) != len(bites) {
return ErrCannotWrite
}
return
}
func CompileStringFactory() CompilerFunc[string] {
return func(s string, box []byte) error {
return CompileString(s, box)
}
}
// ParseBlob copies the input: the incoming slice aliases a pooled buffer
// that is recycled right after the parse, so returning it as-is would hand
// the caller bytes that mutate on the next parse.
func ParseBlob(data []byte) ([]byte, error) {
return bytes.Clone(data), nil
}
func CompileBlob(x []byte, w io.Writer) (err error) {
var written int
written, err = w.Write(x)
if written != len(x) {
err = ErrCannotWrite
}
return
}