Skip to content

Commit da9f18c

Browse files
committed
Fix: support image type for bulkcopy
1 parent 0c78094 commit da9f18c

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

bulkcopy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ func (b *Bulk) makeParam(val DataValue, col columnStruct) (res param, err error)
599599
buf[i] = ub[j]
600600
}
601601
res.buffer = buf
602-
case typeBigVarBin, typeBigBinary:
602+
case typeBigVarBin, typeBigBinary, typeImage:
603603
switch val := val.(type) {
604604
case []byte:
605605
res.ti.Size = len(val)

bulkcopy_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ func testBulkcopy(t *testing.T, guidConversion bool) {
200200
{"test_int16nvarchar", int16(1234), "1234"},
201201
{"test_int8nvarchar", int8(12), "12"},
202202
{"test_intnvarchar", 1234, "1234"},
203+
{"test_image", []byte("1"), nil},
204+
{"test_imagen", nil, nil},
203205
}
204206

205207
columns := make([]string, len(testValues))
@@ -449,6 +451,8 @@ func setupTable(ctx context.Context, t *testing.T, conn *sql.Conn, tableName str
449451
[test_nullint32] [int] NULL,
450452
[test_nullint16] [smallint] NULL,
451453
[test_nulltime] [datetime] NULL,
454+
[test_image] [image] NOT NULL,
455+
[test_imagen] [image] NULL,
452456
CONSTRAINT [PK_` + tableName + `_id] PRIMARY KEY CLUSTERED
453457
(
454458
[id] ASC

types.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,16 @@ func writeVarLen(w io.Writer, ti *typeInfo, out bool, encoding msdsn.EncodeParam
254254
if err = binary.Write(w, binary.LittleEndian, uint32(ti.Size)); err != nil {
255255
return
256256
}
257-
if err = writeCollation(w, ti.Collation); err != nil {
258-
return
257+
258+
// COLLATION occurs only if the type is BIGCHARTYPE, BIGVARCHARTYPE, TEXTTYPE, NTEXTTYPE,
259+
// NCHARTYPE, or NVARCHARTYPE.
260+
//
261+
// https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tds/cbe9c510-eae6-4b1f-9893-a098944d430a
262+
switch ti.TypeId {
263+
case typeText, typeNText:
264+
if err = writeCollation(w, ti.Collation); err != nil {
265+
return
266+
}
259267
}
260268
ti.Writer = writeLongLenType
261269
default:
@@ -1333,6 +1341,8 @@ func makeDecl(ti typeInfo) string {
13331341
return "ntext"
13341342
case typeUdt:
13351343
return ti.UdtInfo.TypeName
1344+
case typeImage:
1345+
return "image"
13361346
case typeGuid:
13371347
return "uniqueidentifier"
13381348
case typeTvp:

0 commit comments

Comments
 (0)