Uh oh!
There was an error while loading. Please reload this page.
forked from CovenantSQL/CovenantSQL
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
Latest commit
111 lines (91 loc) · 2.73 KB
/
Copy pathconfig.go
File metadata and controls
111 lines (91 loc) · 2.73 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package client
import (
"net/url"
"strconv"
"strings"
)
const (
paramUseLeader="use_leader"
paramUseFollower="use_follower"
paramUseDirectRPC="use_direct_rpc"
paramMirror="mirror"
)
// Config is a configuration parsed from a DSN string.
typeConfigstruct {
DatabaseIDstring
// additional configs should be filled
// such as read/write/exec timeout
// currently no timeout is supported.
// UseLeader use leader nodes to do queries
UseLeaderbool
// UseFollower use follower nodes to do queries
UseFollowerbool
// UseDirectRPC use direct RPC to access the miner
UseDirectRPCbool
// Mirror option forces client to query from mirror server
Mirrorstring
}
// NewConfig creates a new config with default value.
funcNewConfig() *Config {
return&Config{UseLeader: true}
}
// FormatDSN formats the given Config into a DSN string which can be passed to the driver.
func (cfg*Config) FormatDSN() string {
// build query from arguments
u:=&url.URL{
Scheme: DBScheme,
Host: cfg.DatabaseID,
}
newQuery:=u.Query()
ifcfg.UseFollower {
newQuery.Add(paramUseFollower, strconv.FormatBool(cfg.UseFollower))
ifcfg.UseLeader {
newQuery.Add(paramUseLeader, strconv.FormatBool(cfg.UseLeader))
}
}
ifcfg.Mirror!="" {
newQuery.Add(paramMirror, cfg.Mirror)
}
ifcfg.UseDirectRPC {
newQuery.Add(paramUseDirectRPC, strconv.FormatBool(cfg.UseDirectRPC))
}
u.RawQuery=newQuery.Encode()
returnu.String()
}
// ParseDSN parse the DSN string to a Config.
funcParseDSN(dsnstring) (cfg*Config, errerror) {
if!strings.HasPrefix(dsn, DBScheme) &&!strings.HasPrefix(dsn, DBSchemeAlias) {
dsn=DBScheme+"://"+dsn
}
varu*url.URL
ifu, err=url.Parse(dsn); err!=nil {
returnnil, err
}
cfg=NewConfig()
cfg.DatabaseID=u.Host
q:=u.Query()
// option: use_leader, use_follower
cfg.UseLeader, _=strconv.ParseBool(q.Get(paramUseLeader))
cfg.UseFollower, _=strconv.ParseBool(q.Get(paramUseFollower))
if!cfg.UseLeader&&!cfg.UseFollower {
cfg.UseLeader=true
}
cfg.Mirror=q.Get(paramMirror)
cfg.UseDirectRPC, _=strconv.ParseBool(q.Get(paramUseDirectRPC))
returncfg, nil
}