Skip to content

Commit

Permalink
Add some type support.
Browse files Browse the repository at this point in the history
I think ideally we'd be able to get the name from the driver rather than
defaulting to the c-api's name, but I'm not sure how possible that is at
this time.

This seems like a fine intermediary for now.

Closes #3

Co-authored-by:  Roman TSovanyan <[email protected]>
  • Loading branch information
jpalawaga and covrom committed Nov 1, 2021
1 parent dfef8d2 commit 00d27f8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ func TestMYSQLScanType(t *testing.T) {

for _, colType := range cTypes {
typ := *colType
assert.Equal(t, reflect.TypeOf(int64(0)), typ.ScanType())
assert.Equal(t, reflect.TypeOf(int32(0)), typ.ScanType())
assert.Equal(t, "SQL_C_LONG", typ.DatabaseTypeName())
}
rows.Close()

Expand Down
51 changes: 51 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,54 @@ func (r *Rows) ColumnTypeScanType(index int) reflect.Type {
func (r *Rows) ColumnTypeNullable(index int) (nullable, ok bool) {
return r.s.cols[index].Nullable()
}

// ColumnTypeDatabaseTypeName return the database system type name.
func (r *Rows) ColumnTypeDatabaseTypeName(index int) string {
switch x := r.s.cols[index].(type) {
case *BindableColumn:
return cTypeString(x.CType)
case *NonBindableColumn:
return cTypeString(x.CType)
}
return ""
}

func cTypeString(ct api.SQLSMALLINT) string {
switch ct {
case api.SQL_C_CHAR:
return "SQL_C_CHAR"
case api.SQL_C_LONG:
return "SQL_C_LONG"
case api.SQL_C_SHORT:
return "SQL_C_SHORT"
case api.SQL_C_FLOAT:
return "SQL_C_FLOAT"
case api.SQL_C_DOUBLE:
return "SQL_C_DOUBLE"
case api.SQL_C_NUMERIC:
return "SQL_C_NUMERIC"
case api.SQL_C_DATE:
return "SQL_C_DATE"
case api.SQL_C_TIME:
return "SQL_C_TIME"
case api.SQL_C_TYPE_TIMESTAMP:
return "SQL_C_TYPE_TIMESTAMP"
case api.SQL_C_TIMESTAMP:
return "SQL_C_TIMESTAMP"
case api.SQL_C_BINARY:
return "SQL_C_BINARY"
case api.SQL_C_BIT:
return "SQL_C_BIT"
case api.SQL_C_WCHAR:
return "SQL_C_WCHAR"
case api.SQL_C_DEFAULT:
return "SQL_C_DEFAULT"
case api.SQL_C_SBIGINT:
return "SQL_C_SBIGINT"
case api.SQL_C_UBIGINT:
return "SQL_C_UBIGINT"
case api.SQL_C_GUID:
return "SQL_C_GUID"
}
return ""
}

0 comments on commit 00d27f8

Please sign in to comment.