Skip to content

Commit

Permalink
strict Go before/after 1.8 version implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alespour committed Jun 22, 2020
1 parent c87ad07 commit 55292ff
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
4 changes: 2 additions & 2 deletions rows_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func (rs *rowSetsWithDefinition) ColumnTypeScanType(index int) reflect.Type {
return rs.getDefinition(index).ScanType()
}

// return definition of current set metadata
// return column definition from current set metadata
func (rs *rowSetsWithDefinition) getDefinition(index int) *Column {
return rs.sets[rs.pos].def[index]
}

// NewRowsWithColumnDefinition see PR-152
// NewRowsWithColumnDefinition return rows with columns metadata
func NewRowsWithColumnDefinition(columns ...*Column) *Rows {
cols := make([]string, len(columns))
for i, column := range columns {
Expand Down
23 changes: 1 addition & 22 deletions sqlmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// Sqlmock interface serves to create expectations
// for any kind of database action in order to mock
// and test real database behavior.
type Sqlmock interface {
type SqlmockCommon interface {
// ExpectClose queues an expectation for this database
// action to be triggered. the *ExpectedClose allows
// to mock database response
Expand Down Expand Up @@ -84,13 +84,6 @@ type Sqlmock interface {
// sql driver.Value slice or from the CSV string and
// to be used as sql driver.Rows.
NewRows(columns []string) *Rows

// NewRowsWithColumnDefinition allows Rows to be created from a
// sql driver.Value slice with a definition of sql metadata
NewRowsWithColumnDefinition(columns ...*Column) *Rows

// New Column allows to create a Column
NewColumn(name string) *Column
}

type sqlmock struct {
Expand Down Expand Up @@ -444,17 +437,3 @@ func (c *sqlmock) NewRows(columns []string) *Rows {
r.converter = c.converter
return r
}

// NewRowsWithColumnDefinition allows Rows to be created from a
// sql driver.Value slice with a definition of sql metadata
func (c *sqlmock) NewRowsWithColumnDefinition(columns ...*Column) *Rows {
r := NewRowsWithColumnDefinition(columns...)
r.converter = c.converter
return r
}

// NewColumn allows to create a Column that can be enhanced with metadata
// using OfType/Nullable/WithLength/WithPrecisionAndScale methods.
func (c *sqlmock) NewColumn(name string) *Column {
return NewColumn(name)
}
6 changes: 6 additions & 0 deletions sqlmock_before_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"time"
)

// Sqlmock interface for Go up to 1.7
type Sqlmock interface {
// Embed common methods
SqlmockCommon
}

type namedValue struct {
Name string
Ordinal int
Expand Down
27 changes: 27 additions & 0 deletions sqlmock_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ import (
"time"
)

// Sqlmock interface for Go 1.8+
type Sqlmock interface {
// Embed common methods
SqlmockCommon

// NewRowsWithColumnDefinition allows Rows to be created from a
// sql driver.Value slice with a definition of sql metadata
NewRowsWithColumnDefinition(columns ...*Column) *Rows

// New Column allows to create a Column
NewColumn(name string) *Column
}

// ErrCancelled defines an error value, which can be expected in case of
// such cancellation error.
var ErrCancelled = errors.New("canceling query due to user request")
Expand Down Expand Up @@ -327,3 +340,17 @@ func (c *sqlmock) exec(query string, args []driver.NamedValue) (*ExpectedExec, e
}

// @TODO maybe add ExpectedBegin.WithOptions(driver.TxOptions)

// NewRowsWithColumnDefinition allows Rows to be created from a
// sql driver.Value slice with a definition of sql metadata
func (c *sqlmock) NewRowsWithColumnDefinition(columns ...*Column) *Rows {
r := NewRowsWithColumnDefinition(columns...)
r.converter = c.converter
return r
}

// NewColumn allows to create a Column that can be enhanced with metadata
// using OfType/Nullable/WithLength/WithPrecisionAndScale methods.
func (c *sqlmock) NewColumn(name string) *Column {
return NewColumn(name)
}

0 comments on commit 55292ff

Please sign in to comment.