forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn.go
82 lines (71 loc) · 2.25 KB
/
conn.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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package sqldb defines an interface for low level db connection.
package sqldb
import (
"fmt"
"sync"
"github.com/youtube/vitess/go/sqltypes"
querypb "github.com/youtube/vitess/go/vt/proto/query"
)
// NewConnFunc is a factory method that creates a Conn instance
// using given ConnParams.
type NewConnFunc func(params ConnParams) (Conn, error)
var (
defaultConn NewConnFunc
// mu protects conns.
mu sync.Mutex
conns = make(map[string]NewConnFunc)
)
// Conn defines the behavior for the low level db connection
type Conn interface {
// ExecuteFetch executes the query on the connection
ExecuteFetch(query string, maxrows int, wantfields bool) (*sqltypes.Result, error)
// ExecuteStreamFetch starts a streaming query to db server. Use FetchNext
// on the Connection until it returns nil or error
ExecuteStreamFetch(query string) error
// Close closes the db connection
Close()
// IsClosed returns if the connection was ever closed
IsClosed() bool
// CloseResult finishes the result set
CloseResult()
// Fields returns the current fields description for the query
Fields() ([]*querypb.Field, error)
// ID returns the connection id.
ID() int64
// FetchNext returns the next row for a query
FetchNext() ([]sqltypes.Value, error)
}
// RegisterDefault registers the default connection function.
// Only one default can be registered.
func RegisterDefault(fn NewConnFunc) {
if defaultConn != nil {
panic("default connection initialized more than once")
}
defaultConn = fn
}
// Register registers a db connection.
func Register(name string, fn NewConnFunc) {
mu.Lock()
defer mu.Unlock()
if _, ok := conns[name]; ok {
panic(fmt.Sprintf("register a registered key: %s", name))
}
conns[name] = fn
}
// Connect returns a sqldb.Conn using the default connection creation function.
func Connect(params ConnParams) (Conn, error) {
// Use a lock-free fast path for default.
if params.Engine == "" {
return defaultConn(params)
}
mu.Lock()
defer mu.Unlock()
fn, ok := conns[params.Engine]
if !ok {
panic(fmt.Sprintf("connection function not found for engine: %s", params.Engine))
}
return fn(params)
}