-
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from bonitoo-io/pr-152-again
Add Column Metadata
- Loading branch information
Showing
10 changed files
with
422 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package sqlmock | ||
|
||
import "reflect" | ||
|
||
// Column is a mocked column Metadata for rows.ColumnTypes() | ||
type Column struct { | ||
name string | ||
dbType string | ||
nullable bool | ||
nullableOk bool | ||
length int64 | ||
lengthOk bool | ||
precision int64 | ||
scale int64 | ||
psOk bool | ||
scanType reflect.Type | ||
} | ||
|
||
func (c *Column) Name() string { | ||
return c.name | ||
} | ||
|
||
func (c *Column) DbType() string { | ||
return c.dbType | ||
} | ||
|
||
func (c *Column) IsNullable() (bool, bool) { | ||
return c.nullable, c.nullableOk | ||
} | ||
|
||
func (c *Column) Length() (int64, bool) { | ||
return c.length, c.lengthOk | ||
} | ||
|
||
func (c *Column) PrecisionScale() (int64, int64, bool) { | ||
return c.precision, c.scale, c.psOk | ||
} | ||
|
||
func (c *Column) ScanType() reflect.Type { | ||
return c.scanType | ||
} | ||
|
||
// NewColumn returns a Column with specified name | ||
func NewColumn(name string) *Column { | ||
return &Column{ | ||
name: name, | ||
} | ||
} | ||
|
||
// Nullable returns the column with nullable metadata set | ||
func (c *Column) Nullable(nullable bool) *Column { | ||
c.nullable = nullable | ||
c.nullableOk = true | ||
return c | ||
} | ||
|
||
// OfType returns the column with type metadata set | ||
func (c *Column) OfType(dbType string, sampleValue interface{}) *Column { | ||
c.dbType = dbType | ||
c.scanType = reflect.TypeOf(sampleValue) | ||
return c | ||
} | ||
|
||
// WithLength returns the column with length metadata set. | ||
func (c *Column) WithLength(length int64) *Column { | ||
c.length = length | ||
c.lengthOk = true | ||
return c | ||
} | ||
|
||
// WithPrecisionAndScale returns the column with precision and scale metadata set. | ||
func (c *Column) WithPrecisionAndScale(precision, scale int64) *Column { | ||
c.precision = precision | ||
c.scale = scale | ||
c.psOk = true | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package sqlmock | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestColumn(t *testing.T) { | ||
now, _ := time.Parse(time.RFC3339, "2020-06-20T22:08:41Z") | ||
column1 := NewColumn("test").OfType("VARCHAR", "").Nullable(true).WithLength(100) | ||
column2 := NewColumn("number").OfType("DECIMAL", float64(0.0)).Nullable(false).WithPrecisionAndScale(10, 4) | ||
column3 := NewColumn("when").OfType("TIMESTAMP", now) | ||
|
||
if column1.ScanType().Kind() != reflect.String { | ||
t.Errorf("string scanType mismatch: %v", column1.ScanType()) | ||
} | ||
if column2.ScanType().Kind() != reflect.Float64 { | ||
t.Errorf("float scanType mismatch: %v", column2.ScanType()) | ||
} | ||
if column3.ScanType() != reflect.TypeOf(time.Time{}) { | ||
t.Errorf("time scanType mismatch: %v", column3.ScanType()) | ||
} | ||
|
||
nullable, ok := column1.IsNullable() | ||
if !nullable || !ok { | ||
t.Errorf("'test' column should be nullable") | ||
} | ||
nullable, ok = column2.IsNullable() | ||
if nullable || !ok { | ||
t.Errorf("'number' column should not be nullable") | ||
} | ||
nullable, ok = column3.IsNullable() | ||
if ok { | ||
t.Errorf("'when' column nullability should be unknown") | ||
} | ||
|
||
length, ok := column1.Length() | ||
if length != 100 || !ok { | ||
t.Errorf("'test' column wrong length") | ||
} | ||
length, ok = column2.Length() | ||
if ok { | ||
t.Errorf("'number' column is not of variable length type") | ||
} | ||
length, ok = column3.Length() | ||
if ok { | ||
t.Errorf("'when' column is not of variable length type") | ||
} | ||
|
||
_, _, ok = column1.PrecisionScale() | ||
if ok { | ||
t.Errorf("'test' column not applicable") | ||
} | ||
precision, scale, ok := column2.PrecisionScale() | ||
if precision != 10 || scale != 4 || !ok { | ||
t.Errorf("'number' column not applicable") | ||
} | ||
_, _, ok = column3.PrecisionScale() | ||
if ok { | ||
t.Errorf("'when' column not applicable") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.