diff --git a/int.go b/int.go index 56e1f7b..58393e3 100644 --- a/int.go +++ b/int.go @@ -23,6 +23,10 @@ func (i Int) ToInt() int { return int(i) } +func (i Int) ToInt16() int16 { + return int16(i) +} + func (i Int) ToInt32() int32 { return int32(i) } @@ -35,6 +39,10 @@ func (i Int) ToIntPtr() *int { return toIntPtr(int(i)) } +func (i Int) ToInt16Ptr() *int16 { + return toInt16Ptr(int16(i)) +} + func (i Int) ToInt32Ptr() *int32 { return toInt32Ptr(i.ToInt32()) } diff --git a/int_ptr.go b/int_ptr.go index b445bd5..7cb4e16 100644 --- a/int_ptr.go +++ b/int_ptr.go @@ -5,6 +5,10 @@ import ( "fmt" ) +func toInt16Ptr(i int16) *int16 { + return &i +} + func toIntPtr(i int) *int { return &i } @@ -21,6 +25,10 @@ type IntPtr struct { i *int } +type Int16Ptr struct { + i *int16 +} + type Int32Ptr struct { i *int32 } @@ -97,3 +105,26 @@ func (p *Int64Ptr) Scan(value interface{}) error { return nil } + +func (p Int16Ptr) Value() (driver.Value, error) { + return p.i, nil +} + +func (p *Int16Ptr) Scan(value interface{}) error { + if value == nil { + p.i = nil + return nil + } + + if bv, err := driver.Int32.ConvertValue(value); err != nil { + if v, ok := bv.(int16); ok { + *p = Int16Ptr{ + i: toInt16Ptr(v), + } + } + } else { + return fmt.Errorf("failed to scan Int16Ptr:\n%v", err) + } + + return nil +}