Skip to content

Commit 7326681

Browse files
author
James Cor
committed
added collations
1 parent d80cd5c commit 7326681

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

sql/types/datetime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ func (t datetimeType) ToSQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sq
481481
}
482482
switch t.baseType {
483483
case sqltypes.Date:
484-
// TODO: move this to values
484+
// TODO: move this to values package
485485
x := values.ReadUint32(v.Val)
486486
y := x >> 16
487487
m := (x & (255 << 8)) >> 8

sql/types/enum.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package types
1717
import (
1818
"context"
1919
"fmt"
20+
"github.com/dolthub/go-mysql-server/sql/values"
2021
"reflect"
2122
"strconv"
2223
"strings"
@@ -268,6 +269,33 @@ func (t EnumType) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltypes.Va
268269
return sqltypes.MakeTrusted(sqltypes.Enum, val), nil
269270
}
270271

272+
func (t EnumType) ToSQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sqltypes.Value, error) {
273+
if v.IsNull() {
274+
return sqltypes.NULL, nil
275+
}
276+
277+
idx := values.ReadUint16(v.Val)
278+
value, _ := t.At(int(idx))
279+
280+
charset := ctx.GetCharacterSetResults()
281+
if charset == sql.CharacterSet_Unspecified || charset == sql.CharacterSet_binary {
282+
charset = t.collation.CharacterSet()
283+
}
284+
285+
// TODO: write append style encoder
286+
res, ok := charset.Encoder().Encode(encodings.StringToBytes(value)) // TODO: use unsafe string to byte
287+
if !ok {
288+
// return snippet of the converted value
289+
if len(value) > 50 {
290+
value = value[:50]
291+
}
292+
value = strings.ToValidUTF8(value, string(utf8.RuneError))
293+
return sqltypes.Value{}, sql.ErrCharSetFailedToEncode.New(charset.Name(), utf8.ValidString(value), value)
294+
}
295+
296+
return sqltypes.MakeTrusted(sqltypes.Enum, res), nil
297+
}
298+
271299
// String implements Type interface.
272300
func (t EnumType) String() string {
273301
return t.StringWithTableCollation(sql.Collation_Default)

sql/types/set.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package types
1717
import (
1818
"context"
1919
"fmt"
20+
"github.com/dolthub/go-mysql-server/sql/values"
2021
"math"
2122
"math/bits"
2223
"reflect"
@@ -261,6 +262,35 @@ func (t SetType) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltypes.Val
261262
return sqltypes.MakeTrusted(sqltypes.Set, val), nil
262263
}
263264

265+
func (t SetType) ToSQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sqltypes.Value, error) {
266+
if v.IsNull() {
267+
return sqltypes.NULL, nil
268+
}
269+
270+
bits := values.ReadUint64(v.Val)
271+
value, err := t.BitsToString(bits)
272+
if err != nil {
273+
return sqltypes.Value{}, err
274+
}
275+
276+
resultCharset := ctx.GetCharacterSetResults()
277+
if resultCharset == sql.CharacterSet_Unspecified || resultCharset == sql.CharacterSet_binary {
278+
resultCharset = t.collation.CharacterSet()
279+
}
280+
281+
// TODO: write append style encoder
282+
res, ok := resultCharset.Encoder().Encode(encodings.StringToBytes(value)) // TODO: use unsafe string to byte
283+
if !ok {
284+
if len(value) > 50 {
285+
value = value[:50]
286+
}
287+
value = strings.ToValidUTF8(value, string(utf8.RuneError))
288+
return sqltypes.Value{}, sql.ErrCharSetFailedToEncode.New(resultCharset.Name(), utf8.ValidString(value), value)
289+
}
290+
291+
return sqltypes.MakeTrusted(sqltypes.Set, res), nil
292+
}
293+
264294
// String implements Type interface.
265295
func (t SetType) String() string {
266296
return t.StringWithTableCollation(sql.Collation_Default)

sql/types/strings.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,6 @@ func (t StringType) ToSQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sqlt
796796
return sqltypes.NULL, nil
797797
}
798798

799-
// TODO: collations
800799
// TODO: deal with casting numbers?
801800
// No need to use dest buffer as we have already allocated []byte
802801
var err error
@@ -806,8 +805,20 @@ func (t StringType) ToSQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sqlt
806805
return sqltypes.Value{}, err
807806
}
808807
}
808+
charset := ctx.GetCharacterSetResults()
809+
if charset == sql.CharacterSet_Unspecified || charset == sql.CharacterSet_binary {
810+
charset = t.collation.CharacterSet()
811+
}
812+
res, ok := charset.Encoder().Encode(v.Val)
813+
if !ok {
814+
if len(v.Val) > 50 {
815+
v.Val = v.Val[:50]
816+
}
817+
snippetStr := strings2.ToValidUTF8(string(v.Val), string(utf8.RuneError))
818+
return sqltypes.Value{}, sql.ErrCharSetFailedToEncode.New(charset.Name(), utf8.ValidString(snippetStr), v.Val)
819+
}
809820

810-
return sqltypes.MakeTrusted(t.baseType, v.Val), nil
821+
return sqltypes.MakeTrusted(t.baseType, res), nil
811822
}
812823

813824
// String implements Type interface.

0 commit comments

Comments
 (0)