-
Notifications
You must be signed in to change notification settings - Fork 4
/
base.go
82 lines (68 loc) · 1.92 KB
/
base.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
// mysqlx - MySQL driver for Go's database/sql package and MySQL X Protocol.
// Copyright (c) 2017-2018 Alexey Palazhchenko
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package mysqlx
import (
"database/sql/driver"
"errors"
"fmt"
)
const bug = true
func bugf(format string, a ...interface{}) error {
msg := fmt.Sprintf(format, a...) + "\nPlease report this bug: https://github.com/AlekSi/mysqlx/issues\n"
if bug {
panic(msg)
}
return errors.New(msg)
}
// Severity represents Error severity level.
type Severity byte
const (
// SeverityError indicates the current message sequence is aborted for the given error
// and the session is ready for more.
SeverityError Severity = 0
// SeverityFatal indicates the client should not expect the server to continue handling any further messages
// and should close the connection.
SeverityFatal Severity = 1
)
func (s Severity) String() string {
switch s {
case SeverityError:
return "ERROR"
case SeverityFatal:
return "FATAL"
default:
return fmt.Sprintf("Severity %d", s)
}
}
// Error represents MySQL X Protocol error message.
// It's not used for transport-level errors.
type Error struct {
Severity Severity
Code uint32
SQLState string
Msg string
}
// Error returns error string in a formal similar to mysql and mysqlsh client programs.
func (e *Error) Error() string {
return fmt.Sprintf("%s %d (%s): %s", e.Severity, e.Code, e.SQLState, e.Msg)
}
type execResult struct {
lastInsertId int64
rowsAffected int64
}
func (r execResult) LastInsertId() (int64, error) {
return r.lastInsertId, nil
}
func (r execResult) RowsAffected() (int64, error) {
return r.rowsAffected, nil
}
// check interfaces
var (
_ fmt.Stringer = SeverityError
_ error = (*Error)(nil)
_ driver.Result = execResult{}
)