Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve the issue of Chinese truncation during varchar insertion #195

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion column.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type BufferLen api.SQLLEN

func (l *BufferLen) IsNull() bool {
return *l == api.SQL_NULL_DATA
return *l == api.SQL_NULL_DATA || int64(*l) == 0x00000000ffffffff
}

func (l *BufferLen) GetData(h api.SQLHSTMT, idx int, ctype api.SQLSMALLINT, buf []byte) api.SQLRETURN {
Expand Down
49 changes: 49 additions & 0 deletions column_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2012 The Go Authors. All rights reserved.

// Use of this source code is governed by a BSD-style

// license that can be found in the LICENSE file.

package odbc

import (
"fmt"
"testing"
"unsafe"
)

func TestBufferLen_IsNull(t *testing.T) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see that you added test as I requested here #182 (comment) , but I was asking for a different test. Your test does not prove that there is a problem with current code.

Perhaps if you adjust TestMSSQLTypes

func TestMSSQLTypes(t *testing.T) {
or TestMSSQLTextColumnParamTypes
func TestMSSQLTextColumnParamTypes(t *testing.T) {
or any other existing test, then I can be convinced.

Thank you.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added test

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added test

Your test is wrong. For any Unicode values you should be using nvarchar instead of varchar type.

I just added this commit 25af894#diff-fa7d078a77ac77b6c32112f98183f4a221d30d0f460e50456b2ba995efaf0f23R675 with proper tests, and they all pass here.

I will not review your PR, because I don't believe it is required. Nothing is broken.

Alex

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue does not occur in all databases, only some databases exist. I think setting the size to p.size is the correct approach

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue does not occur in all databases, only some databases exist.

If you have a way for me to reproduce the issue, I will look at it again.

I think setting the size to p.size is the correct approach

I disagree.

tests := []struct {
name string
i interface{}
l BufferLen
want bool
}{
// TODO: Add test cases.
{name: "IsNull ", i: int64(-1), want: true},
{name: "IsNull ", i: int32(-1), want: true},
{name: "IsNull ", i: int64(0x00000000ffffffff), want: true},

{name: "NotNull ", i: int32(1)},
{name: "NotNull ", i: int32(0x7fffffff)},
{name: "NotNull ", i: int64(0x1ffffffff)},
}
for _, tt := range tests {
switch i := tt.i.(type) {
case int64:
if unsafe.Sizeof(tt.l) != 8 {
continue
}
tt.l = BufferLen(i)
tt.name += fmt.Sprintf("0x%016x", uint64(i))
case int32:
tt.l = BufferLen(i)
tt.name += fmt.Sprintf("0x%08x", uint32(i))
}
t.Run(tt.name, func(t *testing.T) {
if got := tt.l.IsNull(); got != tt.want {
t.Errorf("BufferLen.IsNull() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions mssql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ var typeTests = []typeTest{
{"select cast('abcde' as nvarchar(3))", match([]byte("abc"))},
{"select cast('' as nvarchar(5))", match([]byte(""))},
{"select cast(NULL as nvarchar(5))", match(nil)},
{"select cast('你好世界' as varchar(21))", match([]byte("你好世界"))},
{"select cast('Γεια σου κόσμε' as varchar(21))", match([]byte("Γεια σου κόσμε"))},

// datetime, smalldatetime
{"select cast('20151225' as datetime)", match(time.Date(2015, 12, 25, 0, 0, 0, 0, time.Local))},
Expand Down
6 changes: 6 additions & 0 deletions param.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func (p *Parameter) BindValue(h api.SQLHSTMT, idx int, v driver.Value, conn *Con
// https://docs.microsoft.com/en-us/sql/odbc/microsoft/microsoft-access-data-types
sqltype = api.SQL_WLONGVARCHAR
}
if p.isDescribed && p.Size > 0 {
size = p.Size
}
case int64:
if -0x80000000 < d && d < 0x7fffffff {
// Some ODBC drivers do not support SQL_BIGINT.
Expand Down Expand Up @@ -156,6 +159,9 @@ func (p *Parameter) BindValue(h api.SQLHSTMT, idx int, v driver.Value, conn *Con
default:
sqltype = api.SQL_BINARY
}
if p.isDescribed && p.Size > 0 {
size = p.Size
}
default:
return fmt.Errorf("unsupported type %T", v)
}
Expand Down