@@ -10,6 +10,94 @@ import (
1010 "github.com/stretchr/testify/require"
1111)
1212
13+ func TestEncodeBatchRowFixedChar (t * testing.T ) {
14+ for _ , tc := range []struct {
15+ name string
16+ charset int
17+ length int
18+ value []byte
19+ want []byte
20+ wantErr bool
21+ }{
22+ {"utf8" , 4 , 12 , []byte ("abc" ), []byte ("abc " ), false },
23+ {"ascii with padding" , 2 , 5 , []byte ("abc" ), []byte ("abc " ), false },
24+ {"full text with padding" , 2 , 5 , []byte ("abcde" ), []byte ("abcde " ), false },
25+ {"full aligned text" , 2 , 4 , []byte ("abcd" ), []byte ("abcd" ), false },
26+ {"text across padding blocks" , 2 , 2049 , []byte ("abc" ), append ([]byte ("abc" ), bytes .Repeat ([]byte {' ' }, 2049 )... ), false },
27+ {"empty text" , 2 , 5 , []byte {}, []byte (" " ), false },
28+ {"null" , 4 , 12 , nil , nil , false },
29+ {"octets" , fbproto .CS_BINARY , 5 , []byte {1 , 0 , 2 }, []byte {1 , 0 , 2 , 0 , 0 , 0 , 0 , 0 }, false },
30+ {"empty octets" , fbproto .CS_BINARY , 5 , []byte {}, make ([]byte , 8 ), false },
31+ {"text too long" , 4 , 2 , []byte ("abc" ), nil , true },
32+ {"octets too long" , fbproto .CS_BINARY , 2 , []byte {1 , 2 , 3 }, nil , true },
33+ } {
34+ t .Run (tc .name , func (t * testing.T ) {
35+ sd := & StatementDescription {StatementInfo : & StatementInfo {
36+ ParametersDescriptors : []fbproto.FieldDescriptor {
37+ {Type : fbproto .SQL_TYPE_TEXT , Subtype : tc .charset , Length : tc .length },
38+ {Type : fbproto .SQL_TYPE_LONG , Length : 4 },
39+ },
40+ }}
41+ row , err := sd .EncodeBatchRow ([][]byte {tc .value , {0 , 0 , 0 , 42 }})
42+ if tc .wantErr {
43+ require .ErrorContains (t , err , "exceeds declared length" )
44+ require .Nil (t , row )
45+ return
46+ }
47+ require .NoError (t , err )
48+ want := []byte {0 , 0 , 0 , 0 }
49+ if tc .value == nil {
50+ want [0 ] = 1
51+ }
52+ want = append (want , tc .want ... )
53+ want = append (want , 0 , 0 , 0 , 42 )
54+ require .Equal (t , want , row , "CHAR padding must preserve the following parameter" )
55+ })
56+ }
57+ }
58+
59+ func BenchmarkEncodeBatchRowPadding (b * testing.B ) {
60+ for _ , tc := range []struct {
61+ name string
62+ fields []fbproto.FieldDescriptor
63+ values [][]byte
64+ }{
65+ {"ascii_country_char2" , []fbproto.FieldDescriptor {{Type : fbproto .SQL_TYPE_TEXT , Subtype : 2 , Length : 2 }}, [][]byte {[]byte ("DE" )}},
66+ {"ascii_code_char8" , []fbproto.FieldDescriptor {{Type : fbproto .SQL_TYPE_TEXT , Subtype : 2 , Length : 8 }}, [][]byte {[]byte ("AB-1042" )}},
67+ {"ascii_uuid_char36" , []fbproto.FieldDescriptor {{Type : fbproto .SQL_TYPE_TEXT , Subtype : 2 , Length : 36 }}, [][]byte {[]byte ("550e8400-e29b-41d4-a716-446655440000" )}},
68+ {"ascii_label_char32" , []fbproto.FieldDescriptor {{Type : fbproto .SQL_TYPE_TEXT , Subtype : 2 , Length : 32 }}, [][]byte {[]byte ("North warehouse" )}},
69+ {"utf8_name_char32" , []fbproto.FieldDescriptor {{Type : fbproto .SQL_TYPE_TEXT , Subtype : 4 , Length : 32 * 4 }}, [][]byte {[]byte ("Иван Петров" )}},
70+ {"utf8_label_char64" , []fbproto.FieldDescriptor {{Type : fbproto .SQL_TYPE_TEXT , Subtype : 4 , Length : 64 * 4 }}, [][]byte {[]byte ("Основной склад в Москве" )}},
71+ {"octets_id_char16" , []fbproto.FieldDescriptor {{Type : fbproto .SQL_TYPE_TEXT , Subtype : fbproto .CS_BINARY , Length : 16 }}, [][]byte {{0x55 , 0x0e , 0x84 , 0 , 0xe2 , 0x9b , 0x41 , 0xd4 , 0xa7 , 0x16 , 0x44 , 0x66 , 0x55 , 0x44 , 0 , 0 }}},
72+ {
73+ "mixed_row" ,
74+ []fbproto.FieldDescriptor {
75+ {Type : fbproto .SQL_TYPE_TEXT , Subtype : 2 , Length : 2 },
76+ {Type : fbproto .SQL_TYPE_TEXT , Subtype : 2 , Length : 8 },
77+ {Type : fbproto .SQL_TYPE_TEXT , Subtype : 4 , Length : 32 * 4 },
78+ {Type : fbproto .SQL_TYPE_TEXT , Subtype : fbproto .CS_BINARY , Length : 16 },
79+ },
80+ [][]byte {[]byte ("DE" ), []byte ("AB-1042" ), []byte ("Иван Петров" ), {0x55 , 0x0e , 0x84 , 0 , 0xe2 , 0x9b , 0x41 , 0xd4 , 0xa7 , 0x16 , 0x44 , 0x66 , 0x55 , 0x44 , 0 , 0 }},
81+ },
82+ } {
83+ b .Run (tc .name , func (b * testing.B ) {
84+ fields := []fbproto.FieldDescriptor {{Type : fbproto .SQL_TYPE_LONG , Length : 4 }}
85+ fields = append (fields , tc .fields ... )
86+ fields = append (fields , fbproto.FieldDescriptor {Type : fbproto .SQL_TYPE_LONG , Length : 4 })
87+ values := [][]byte {{0 , 0 , 0 , 42 }}
88+ values = append (values , tc .values ... )
89+ values = append (values , []byte {0 , 0 , 0 , 7 })
90+ sd := & StatementDescription {StatementInfo : & StatementInfo {ParametersDescriptors : fields }}
91+ b .ReportAllocs ()
92+ for b .Loop () {
93+ if _ , err := sd .EncodeBatchRow (values ); err != nil {
94+ b .Fatal (err )
95+ }
96+ }
97+ })
98+ }
99+ }
100+
13101type prepareCleanupTestConn struct {
14102 noopConn
15103 written bytes.Buffer
0 commit comments