Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RowsColumnTypeDatabaseTypeName #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions column.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (l *BufferLen) Bind(h api.SQLHSTMT, idx int, ctype api.SQLSMALLINT, buf []b
// Column provides access to row columns.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add

Fixes #174

to the end of your commit message. So future code readers can determine why we made that change.

type Column interface {
Name() string
DatabaseTypeName() string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes require a new test in mssql_test.go that demonstrates / verifies new functionality. Otherwise it is impossible for me to judge if new coded works or not.

If you don't have MS SQL Server to test with, see Makefile with instructions on how to run MS SQL Server in Docker on Linux.

Bind(h api.SQLHSTMT, idx int) (bool, error)
Value(h api.SQLHSTMT, idx int) (driver.Value, error)
}
Expand Down Expand Up @@ -121,6 +122,71 @@ func (c *BaseColumn) Name() string {
return c.name
}

func (c *BaseColumn) DatabaseTypeName() string {
switch c.SQLType {
case api.SQL_CHAR:
return "CHAR"
case api.SQL_NUMERIC:
return "NUMERIC"
case api.SQL_DECIMAL:
return "DECIMAL"
case api.SQL_INTEGER:
return "INTEGER"
case api.SQL_SMALLINT:
return "SMALLINT"
case api.SQL_FLOAT:
return "FLOAT"
case api.SQL_REAL:
return "REAL"
case api.SQL_DOUBLE:
return "DOUBLE"
case api.SQL_DATETIME:
return "DATETIME"
case api.SQL_TIME:
return "TIME"
case api.SQL_VARCHAR:
return "VARCHAR"
case api.SQL_TYPE_DATE:
return "TYPE_DATE"
case api.SQL_TYPE_TIME:
return "TYPE_TIME"
case api.SQL_TYPE_TIMESTAMP:
return "TYPE_TIMESTAMP"
case api.SQL_TIMESTAMP:
return "TIMESTAMP"
case api.SQL_LONGVARCHAR:
return "LONGVARCHAR"
case api.SQL_BINARY:
return "BINARY"
case api.SQL_VARBINARY:
return "VARBINARY"
case api.SQL_LONGVARBINARY:
return "LONGVARBINARY"
case api.SQL_BIGINT:
return "BIGINT"
case api.SQL_TINYINT:
return "TINYINT"
case api.SQL_BIT:
return "BIT"
case api.SQL_WCHAR:
return "WCHAR"
case api.SQL_WVARCHAR:
return "WVARCHAR"
case api.SQL_WLONGVARCHAR:
return "WLONGVARCHAR"
case api.SQL_GUID:
return "GUID"
case api.SQL_SIGNED_OFFSET:
return "SIGNED_OFFSET"
case api.SQL_UNSIGNED_OFFSET:
return "UNSIGNED_OFFSET"
case api.SQL_UNKNOWN_TYPE:
return ""
default:
return ""
}
}

func (c *BaseColumn) Value(buf []byte) (driver.Value, error) {
var p unsafe.Pointer
if len(buf) > 0 {
Expand Down
14 changes: 14 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,17 @@ func (r *Rows) NextResultSet() error {
}
return nil
}

// Implement RowsColumnTypeDatabaseTypeName interface in order to return column types.
// https://github.com/golang/go/blob/e22a14b7eb1e4a172d0c20d14a0d2433fdf20e5c/src/database/sql/driver/driver.go#L469-L477
//
// From the docs:
// RowsColumnTypeDatabaseTypeName may be implemented by Rows. It should return the
// database system type name without the length. Type names should be uppercase.
// Examples of returned types: "VARCHAR", "NVARCHAR", "VARCHAR2", "CHAR", "TEXT",
// "DECIMAL", "SMALLINT", "INT", "BIGINT", "BOOL", "[]BIGINT", "JSONB", "XML",
// "TIMESTAMP".
func (rs *Rows) ColumnTypeDatabaseTypeName(i int) string {
return rs.os.Cols[i].DatabaseTypeName()

}