-
Notifications
You must be signed in to change notification settings - Fork 4
/
global.go
81 lines (68 loc) · 1.88 KB
/
global.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package lodbc
import (
"database/sql"
"github.com/LukeMauldin/lodbc/odbc"
"time"
)
//Global variables
var (
queryTimeout = 240 * time.Second // Query timeout
)
// Shared global environment
var envHandle odbc.SQLHandle
// Allocates shared environment
func init() {
// Set environment handle for connection pooling
ret := odbc.SQLSetEnvAttr(envHandle, odbc.SQL_ATTR_CONNECTION_POOLING, odbc.SQL_CP_ONE_PER_DRIVER, 0)
if isError(ret) {
panic(errorEnvironment(envHandle))
}
// Allocate the environment handle
ret = odbc.SQLAllocHandle(odbc.SQL_HANDLE_ENV, 0, &envHandle)
if isError(ret) {
panic(errorEnvironment(envHandle))
}
// Set the environment handle to use ODBC v3
ret = odbc.SQLSetEnvAttr(envHandle, odbc.SQL_ATTR_ODBC_VERSION, odbc.SQL_OV_ODBC3, 0)
if isError(ret) {
panic(errorEnvironment(envHandle))
}
// Register with the SQL package
d := &lodbcDriver{}
sql.Register("lodbc", d)
}
// Frees environment handle -- calling this will make the lodbc package unusable because all setup is performed in init()
func FreeEnvironment() error {
ret := odbc.SQLFreeHandle(odbc.SQL_HANDLE_ENV, envHandle)
if isError(ret) {
return errorEnvironment(envHandle)
}
return nil
}
// Enumeration for supported ODBC version
type ODBCVersion int
const (
ODBCVersion_3 ODBCVersion = 1
ODBCVersion_380 ODBCVersion = 2
)
// Sets the ODBC version for the environment
func SetODBCVersion(version ODBCVersion) {
switch version {
case ODBCVersion_3:
ret := odbc.SQLSetEnvAttr(envHandle, odbc.SQL_ATTR_ODBC_VERSION, odbc.SQL_OV_ODBC3, 0)
if isError(ret) {
panic(errorEnvironment(envHandle))
}
break
case ODBCVersion_380:
ret := odbc.SQLSetEnvAttr(envHandle, odbc.SQL_ATTR_ODBC_VERSION, odbc.SQL_OV_ODBC3_80, 0)
if isError(ret) {
panic(errorEnvironment(envHandle))
}
break
}
}
//Sets the global query timeout
func SetQueryTimeout(timeout time.Duration) {
queryTimeout = timeout
}