Skip to content

Commit

Permalink
Merge branch 'master' into unpool
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Songhurst committed Oct 30, 2017
2 parents 9e55ff3 + 05e4d77 commit a98713d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
45 changes: 33 additions & 12 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strings"
"log"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
5 changes: 4 additions & 1 deletion result.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"reflect"
"time"
)

type Result struct {
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit a98713d

Please sign in to comment.