-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherror.go
88 lines (75 loc) · 2.47 KB
/
error.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
82
83
84
85
86
87
88
package lodbc
import (
"fmt"
"github.com/LukeMauldin/lodbc/odbc"
"strings"
"syscall"
"unsafe"
)
// Error constants
const (
sqlStateLength = 5
errorMaxMessageLength = 8000
)
// Contains all possible information about an ODBC error
// Exported so client programs can obtain additional error information
type StatusRecord struct {
State string
NativeError int
Message string
DriverInfo string
}
// Convert StatusRecord to string
func (sr *StatusRecord) toString() string {
if sr.DriverInfo != "" {
return fmt.Sprintf("{%s} \n%s \n%s", sr.State, sr.DriverInfo, sr.Message)
}
return fmt.Sprintf("{%s} %s", sr.State, sr.Message)
}
// Contains a slice of the error(s) returned by ODBCError
// Exported so client programs can obtain additional error information
// Implements Error() interface
type ODBCError struct {
StatusRecords []StatusRecord
}
// Implements Error() interface
func (e *ODBCError) Error() string {
statusStrings := make([]string, len(e.StatusRecords))
for i, sr := range e.StatusRecords {
statusStrings[i] = sr.toString()
}
return strings.Join(statusStrings, "\n")
}
// Checks for SQL error
func isError(ret odbc.SQLReturn) bool {
return !(ret == odbc.SQL_SUCCESS || ret == odbc.SQL_SUCCESS_WITH_INFO || ret == odbc.SQL_NO_DATA)
}
func errorEnvironment(handle odbc.SQLHandle) error {
return handleError(odbc.SQL_HANDLE_ENV, handle, "")
}
func errorConnection(handle odbc.SQLHandle) error {
return handleError(odbc.SQL_HANDLE_DBC, handle, "")
}
func errorStatement(handle odbc.SQLHandle, driverInfo string) error {
return handleError(odbc.SQL_HANDLE_STMT, handle, driverInfo)
}
func handleError(handleType odbc.SQLSMALLINT, handle odbc.SQLHandle, driverInfo string) error {
statusRecords := make([]StatusRecord, 0)
if handle != 0 {
for recNum := 1; ; recNum++ {
sqlState := make([]uint16, sqlStateLength+1)
var nativeError odbc.SQLINTEGER
message := make([]uint16, errorMaxMessageLength+1)
ret := odbc.SQLGetDiagRec(handleType, handle, odbc.SQLSMALLINT(recNum), uintptr(unsafe.Pointer(&sqlState[0])), &nativeError, uintptr(unsafe.Pointer(&message[0])), errorMaxMessageLength, nil)
if ret == odbc.SQL_NO_DATA {
break
} else if !isError(ret) {
sr := StatusRecord{State: syscall.UTF16ToString(sqlState), NativeError: int(nativeError), Message: syscall.UTF16ToString(message), DriverInfo: driverInfo}
statusRecords = append(statusRecords, sr)
} else {
break
}
}
}
return &ODBCError{StatusRecords: statusRecords}
}