forked from alexbrainman/odbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.go
47 lines (42 loc) · 1.06 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
45
46
47
// 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/lmtyler/odbc/api"
)
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) {
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:
err = fmt.Errorf("unexpected handle type %T", v)
}
return h, ht, err
}
func releaseHandle(handle interface{}) error {
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)
}
return drv.Stats.updateHandleCount(ht, -1)
}