diff --git a/conn_test.go b/conn_test.go index 0791608..0757c68 100644 --- a/conn_test.go +++ b/conn_test.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "strings" + "log" "sync" "testing" "time" @@ -64,6 +65,33 @@ create table [dbo].[tm] ( )`, } +func TestMain(m *testing.M) { + + err := runCreateDBScripts() + if err != nil { + log.Fatal(err) + } + + os.Exit(m.Run()) +} + +func runCreateDBScripts() error { + conn, err := NewConn(testDbConnStr(1)) + if err != nil { + return err + } + defer conn.Close() + + for _, s := range CREATE_DB_SCRIPTS { + _, err := conn.Exec(s) + if err != nil { + return fmt.Errorf("Error running create db scripts with sql: %v\n%v", s, err) + } + } + + return nil +} + func ConnectToTestDb(t *testing.T) *Conn { conn, err := NewConn(testDbConnStr(1)) if err != nil { @@ -102,16 +130,6 @@ func TestItIsSafeToCloseFailedConnection(t *testing.T) { assert.True(t, conn.isDead()) } -func TestCreateTable(t *testing.T) { - conn := ConnectToTestDb(t) - assert.NotNil(t, conn) - defer conn.Close() - for _, s := range CREATE_DB_SCRIPTS { - _, err := conn.Exec(s) - assert.Nil(t, err) - } -} - func TestStoredProcedureReturnValue(t *testing.T) { conn := ConnectToTestDb(t) if conn == nil { @@ -444,8 +462,11 @@ func testNvarcharMax(t *testing.T, str string) { assert.Nil(t, err) val, err := c.SelectValue("select nvarchar_max from dbo.freetds_types where int = 3") assert.Nil(t, err) - //t.Logf("nvarchar_max: %v", val) - assert.Equal(t, str, val) + //t.Logf("nvarchar_max len: %d", len(fmt.Sprintf("%s", val))) + strVal, ok := val.(string) + assert.True(t, ok) + assert.Equal(t, len(str), len(strVal)) + assert.EqualValues(t, str, strVal) } func TestTypes(t *testing.T) { diff --git a/fetch.go b/fetch.go index 4239184..b0bd3dc 100644 --- a/fetch.go +++ b/fetch.go @@ -110,7 +110,11 @@ func (conn *Conn) fetchResults() ([]*Result, error) { } if data != nil { // @see https://github.com/golang/go/wiki/cgo - col.buffer = C.GoBytes(unsafe.Pointer(data), C.int(size+1)) + if col.typ == SYBBINARY || col.typ == SYBIMAGE { + size++ + } + //fmt.Printf("col.typ: %d\n", col.typ) + col.buffer = C.GoBytes(unsafe.Pointer(data), C.int(size)) } } diff --git a/result.go b/result.go index b32063b..48a0ece 100644 --- a/result.go +++ b/result.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "reflect" + "time" ) type Result struct { @@ -156,7 +157,9 @@ func (r *Result) scanStruct(s *reflect.Value) error { func asStructPointer(p interface{}) *reflect.Value { sp := reflect.ValueOf(p) - if sp.Kind() == reflect.Ptr { + if _, ok := p.(*time.Time); ok { + return nil + } else if sp.Kind() == reflect.Ptr { s := sp.Elem() if s.Kind() == reflect.Struct { return &s diff --git a/result_test.go b/result_test.go index ca8889d..967a4f8 100644 --- a/result_test.go +++ b/result_test.go @@ -57,6 +57,17 @@ func TestResultScan(t *testing.T) { assert.Equal(t, f, float64(123.45)) } +func TestResultScanSingleTime(t *testing.T) { + var tm time.Time + r := NewResult() + r.addColumn("tm", 0, 0) + r.addValue(0, 0, now) + assert.True(t, r.Next()) + err := r.Scan(&tm) + assert.Nil(t, err) + assert.Equal(t, tm, now) +} + func TestResultCurrentRow(t *testing.T) { r := testResult() assert.Equal(t, -1, r.CurrentRow())