-
Notifications
You must be signed in to change notification settings - Fork 141
/
error.go
75 lines (66 loc) · 1.63 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
// 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 (
"database/sql/driver"
"fmt"
"strings"
"unsafe"
"github.com/alexbrainman/odbc/api"
)
func IsError(ret api.SQLRETURN) bool {
return !(ret == api.SQL_SUCCESS || ret == api.SQL_SUCCESS_WITH_INFO)
}
type DiagRecord struct {
State string
NativeError int
Message string
}
func (r *DiagRecord) String() string {
return fmt.Sprintf("{%s} %s", r.State, r.Message)
}
type Error struct {
APIName string
Diag []DiagRecord
}
func (e *Error) Error() string {
ss := make([]string, len(e.Diag))
for i, r := range e.Diag {
ss[i] = r.String()
}
return e.APIName + ": " + strings.Join(ss, "\n")
}
func NewError(apiName string, handle interface{}) error {
h, ht, herr := ToHandleAndType(handle)
if herr != nil {
return herr
}
err := &Error{APIName: apiName}
var ne api.SQLINTEGER
var msglen api.SQLSMALLINT
state := make([]uint16, 6)
msg := make([]uint16, api.SQL_MAX_MESSAGE_LENGTH)
for i := 1; ; i++ {
ret := api.SQLGetDiagRec(ht, h, api.SQLSMALLINT(i),
(*api.SQLWCHAR)(unsafe.Pointer(&state[0])), &ne,
(*api.SQLWCHAR)(unsafe.Pointer(&msg[0])),
api.SQLSMALLINT(len(msg)), &msglen)
if ret == api.SQL_NO_DATA {
break
}
if IsError(ret) {
return fmt.Errorf("SQLGetDiagRec failed: ret=%d", ret)
}
r := DiagRecord{
State: api.UTF16ToString(state),
NativeError: int(ne),
Message: api.UTF16ToString(msg),
}
if r.State == "08S01" {
return driver.ErrBadConn
}
err.Diag = append(err.Diag, r)
}
return err
}