Skip to content

Commit

Permalink
Replaced calls to panic with returned error
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Songhurst authored and alexbrainman committed Feb 21, 2018
1 parent 75ee9a4 commit bfee230
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
5 changes: 4 additions & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func initDriver() error {
return NewError("SQLAllocHandle", api.SQLHENV(in))
}
drv.h = api.SQLHENV(out)
drv.Stats.updateHandleCount(api.SQL_HANDLE_ENV, 1)
err := drv.Stats.updateHandleCount(api.SQL_HANDLE_ENV, 1)
if err != nil {
return err
}

// will use ODBC v3
ret = api.SQLSetEnvUIntPtrAttr(drv.h, api.SQL_ATTR_ODBC_VERSION, api.SQL_OV_ODBC3, 0)
Expand Down
5 changes: 4 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func (e *Error) Error() string {
}

func NewError(apiName string, handle interface{}) error {
h, ht := ToHandleAndType(handle)
h, ht, herr := ToHandleAndType(handle)
if herr != nil {
return herr
}
err := &Error{APIName: apiName}
var ne api.SQLINTEGER
state := make([]uint16, 6)
Expand Down
14 changes: 8 additions & 6 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/alexbrainman/odbc/api"
)

func ToHandleAndType(handle interface{}) (h api.SQLHANDLE, ht api.SQLSMALLINT) {
func ToHandleAndType(handle interface{}) (h api.SQLHANDLE, ht api.SQLSMALLINT, err error) {
switch v := handle.(type) {
case api.SQLHENV:
if v == api.SQLHENV(api.SQL_NULL_HANDLE) {
Expand All @@ -26,20 +26,22 @@ func ToHandleAndType(handle interface{}) (h api.SQLHANDLE, ht api.SQLSMALLINT) {
ht = api.SQL_HANDLE_STMT
h = api.SQLHANDLE(v)
default:
panic(fmt.Errorf("unexpected handle type %T", v))
err = fmt.Errorf("unexpected handle type %T", v)
}
return h, ht
return h, ht, err
}

func releaseHandle(handle interface{}) error {
h, ht := ToHandleAndType(handle)
h, ht, err := ToHandleAndType(handle)
if err != nil {
return err
}
ret := api.SQLFreeHandle(ht, h)
if ret == api.SQL_INVALID_HANDLE {
return fmt.Errorf("SQLFreeHandle(%d, %d) returns SQL_INVALID_HANDLE", ht, h)
}
if IsError(ret) {
return NewError("SQLFreeHandle", handle)
}
drv.Stats.updateHandleCount(ht, -1)
return nil
return drv.Stats.updateHandleCount(ht, -1)
}
5 changes: 4 additions & 1 deletion odbcstmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func (c *Conn) PrepareODBCStmt(query string) (*ODBCStmt, error) {
return nil, c.newError("SQLAllocHandle", c.h)
}
h := api.SQLHSTMT(out)
drv.Stats.updateHandleCount(api.SQL_HANDLE_STMT, 1)
err := drv.Stats.updateHandleCount(api.SQL_HANDLE_STMT, 1)
if err != nil {
return nil, err
}

b := api.StringToUTF16(query)
ret = api.SQLPrepare(h, (*api.SQLWCHAR)(unsafe.Pointer(&b[0])), api.SQL_NTS)
Expand Down
2 changes: 1 addition & 1 deletion param.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (p *Parameter) BindValue(h api.SQLHSTMT, idx int, v driver.Value) error {
sqltype = api.SQL_BINARY
}
default:
panic(fmt.Errorf("unsupported type %T", v))
return fmt.Errorf("unsupported type %T", v)
}
ret := api.SQLBindParameter(h, api.SQLUSMALLINT(idx+1),
api.SQL_PARAM_INPUT, ctype, sqltype, size, decimal,
Expand Down
5 changes: 3 additions & 2 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Stats struct {
mu sync.Mutex
}

func (s *Stats) updateHandleCount(handleType api.SQLSMALLINT, change int) {
func (s *Stats) updateHandleCount(handleType api.SQLSMALLINT, change int) error {
s.mu.Lock()
defer s.mu.Unlock()
switch handleType {
Expand All @@ -29,6 +29,7 @@ func (s *Stats) updateHandleCount(handleType api.SQLSMALLINT, change int) {
case api.SQL_HANDLE_STMT:
s.StmtCount += change
default:
panic(fmt.Errorf("unexpected handle type %d", handleType))
return fmt.Errorf("unexpected handle type %d", handleType)
}
return nil
}

0 comments on commit bfee230

Please sign in to comment.