forked from lunny/godbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.go
44 lines (40 loc) · 1.02 KB
/
handle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package odbc
import (
"fmt"
"github.com/lunny/godbc/api"
)
func ToHandleAndType(handle interface{}) (h api.SQLHANDLE, ht api.SQLSMALLINT) {
switch v := handle.(type) {
case api.SQLHENV:
if v == api.SQLHENV(api.SQL_NULL_HANDLE) {
ht = 0
} else {
ht = api.SQL_HANDLE_ENV
}
h = api.SQLHANDLE(v)
case api.SQLHDBC:
ht = api.SQL_HANDLE_DBC
h = api.SQLHANDLE(v)
case api.SQLHSTMT:
ht = api.SQL_HANDLE_STMT
h = api.SQLHANDLE(v)
default:
panic(fmt.Errorf("unexpected handle type %T", v))
}
return h, ht
}
func releaseHandle(handle interface{}) error {
h, ht := ToHandleAndType(handle)
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
}