From 840ec1bba5bafee7c8fba15d5ab3c091a6d754c7 Mon Sep 17 00:00:00 2001 From: maixl Date: Tue, 2 Apr 2024 12:04:12 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E8=A7=A3=E5=86=B3varchar=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E4=B8=AD=E6=96=87=E8=A2=AB=E6=88=AA=E6=96=AD=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- param.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/param.go b/param.go index f66d00d..ed9b38b 100644 --- a/param.go +++ b/param.go @@ -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. @@ -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) } From 3a2bd2738f8da3f824e87b0a2191c8e96db8742b Mon Sep 17 00:00:00 2001 From: maixl Date: Tue, 2 Apr 2024 18:04:55 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E9=83=A8=E5=88=86=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=8F=AA=E8=BF=94=E5=9B=9E2=E4=B8=AA?= =?UTF-8?q?=E5=AD=97=E8=8A=82=E7=9A=84=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- column.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/column.go b/column.go index 6dcbb17..ef5a7fd 100644 --- a/column.go +++ b/column.go @@ -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 || (*l>>32 == 0 && int32(*l) == api.SQL_NULL_DATA) } func (l *BufferLen) GetData(h api.SQLHSTMT, idx int, ctype api.SQLSMALLINT, buf []byte) api.SQLRETURN { From 100a1c6a6a5d0d80b1107f79f156cb57c28bd158 Mon Sep 17 00:00:00 2001 From: zhangyongding <59941594@qq.com> Date: Sun, 15 Sep 2024 18:15:59 +0800 Subject: [PATCH 3/4] add test --- column.go | 2 +- column_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 column_test.go diff --git a/column.go b/column.go index ef5a7fd..9daa02b 100644 --- a/column.go +++ b/column.go @@ -17,7 +17,7 @@ import ( type BufferLen api.SQLLEN func (l *BufferLen) IsNull() bool { - return *l == api.SQL_NULL_DATA || (*l>>32 == 0 && int32(*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 { diff --git a/column_test.go b/column_test.go new file mode 100644 index 0000000..928022f --- /dev/null +++ b/column_test.go @@ -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) { + 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) + } + }) + } +} From d3bdf232872e487f32577808f9e8f4b1e558891c Mon Sep 17 00:00:00 2001 From: zhangyongding <59941594@qq.com> Date: Wed, 9 Oct 2024 13:11:56 +0800 Subject: [PATCH 4/4] add test --- mssql_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mssql_test.go b/mssql_test.go index fc91098..90c99eb 100644 --- a/mssql_test.go +++ b/mssql_test.go @@ -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))},