forked from SAP/go-dblib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isolationlevels.go
67 lines (56 loc) · 1.72 KB
/
isolationlevels.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
// SPDX-FileCopyrightText: 2020 SAP SE
//
// SPDX-License-Identifier: Apache-2.0
package dblib
import (
"database/sql"
"fmt"
)
// ASEIsolationLevel reflects the ASE isolation levels.
type ASEIsolationLevel int
// Valid ASE isolation levels.
const (
ASELevelInvalid ASEIsolationLevel = -1
ASELevelReadUncommitted ASEIsolationLevel = iota
ASELevelReadCommitted
ASELevelRepeatableRead
ASELevelSerializableRead
)
var (
// sql2ase maps sql.IsolationLevel to dblib.ASEIsolationLevel.
sql2ase = map[sql.IsolationLevel]ASEIsolationLevel{
sql.LevelDefault: ASELevelReadCommitted,
sql.LevelReadUncommitted: ASELevelReadUncommitted,
sql.LevelReadCommitted: ASELevelReadCommitted,
sql.LevelWriteCommitted: ASELevelInvalid,
sql.LevelRepeatableRead: ASELevelRepeatableRead,
sql.LevelSerializable: ASELevelSerializableRead,
sql.LevelLinearizable: ASELevelInvalid,
}
)
// ASEIsolationLevelFromGo take a database/sql.IsolationLevel and returns
// the relevant isolation level for ASE.
func ASEIsolationLevelFromGo(lvl sql.IsolationLevel) (ASEIsolationLevel, error) {
aseLvl, ok := sql2ase[lvl]
if !ok {
return ASELevelInvalid, fmt.Errorf("Unknown database/sql.IsolationLevel: %v", lvl)
}
if aseLvl == ASELevelInvalid {
return ASELevelInvalid, fmt.Errorf("Isolation level %v is not supported by ASE", lvl)
}
return aseLvl, nil
}
// ToGo returns the database/sql.IsolationLevel equivalent of the ASE
// isolation level.
func (lvl ASEIsolationLevel) ToGo() sql.IsolationLevel {
for sqlLvl, aseLvl := range sql2ase {
if aseLvl == lvl {
return sqlLvl
}
}
return sql.LevelDefault
}
// String implements the Stringer interface.
func (lvl ASEIsolationLevel) String() string {
return lvl.ToGo().String()
}