This repository was archived by the owner on Dec 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathreader.go
More file actions
115 lines (101 loc) · 3.38 KB
/
Copy pathreader.go
File metadata and controls
115 lines (101 loc) · 3.38 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
112
113
114
115
package reader
import (
"fmt"
"time"
"github.com/hellofresh/klepto/pkg/config"
"github.com/hellofresh/klepto/pkg/database"
)
type (
// Driver is a driver interface used to support multiple drivers
Driver interface {
// IsSupported checks if the driver is supported.
IsSupported(string) bool
// NewConnection takes the connection options and returns a new Reader.
NewConnection(ConnOpts) (Reader, error)
}
// Reader provides an interface to access database stores.
Reader interface {
// GetStructure returns the SQL used to create the database tables
GetStructure() (string, error)
// GetTables returns a list of all databases tables
GetTables() ([]string, error)
// GetColumns return a list of all columns for a given table
GetColumns(string) ([]string, error)
// FormatColumn returns a escaped table.column string
FormatColumn(tableName string, columnName string) string
// ReadTable returns a channel with all database rows
ReadTable(string, chan<- database.Row, ReadTableOpt) error
// Close closes the reader resources and releases them.
Close() error
}
// ReadTableOpt represents the read table options
ReadTableOpt struct {
// Columns contains the (quoted) column of the table
Columns []string
// Match is a condition field to dump only certain amount data
Match string
// Sort the results
Sorts map[string]string
// Limit defines a limit of results to be fetched
Limit uint64
// Relationships defines an slice of relationship definitions
Relationships []*RelationshipOpt
}
// RelationshipOpt represents the relationships options
RelationshipOpt struct {
// Table is the table name.
Table string
// ForeignKey is the table name foreign key.
ForeignKey string
// ReferencedTable is the referenced table name.
ReferencedTable string
// ReferencedKey is the referenced table primary key name.
ReferencedKey string
}
// ConnOpts are the options to create a connection
ConnOpts struct {
// DSN is the connection address.
DSN string
// Timeout is the timeout for read operations.
Timeout time.Duration
// MaxConnLifetime is the maximum amount of time a connection may be reused on the read database.
MaxConnLifetime time.Duration
// MaxConns is the maximum number of open connections to the read database.
MaxConns int
// MaxIdleConns is the maximum number of connections in the idle connection pool for the read database.
MaxIdleConns int
}
)
// NewReadTableOpt builds read table options from table config
func NewReadTableOpt(tableCfg *config.Table) ReadTableOpt {
rOpts := make([]*RelationshipOpt, len(tableCfg.Relationships))
for i, r := range tableCfg.Relationships {
rOpts[i] = &RelationshipOpt{
Table: r.Table,
ReferencedTable: r.ReferencedTable,
ReferencedKey: r.ReferencedKey,
ForeignKey: r.ForeignKey,
}
}
return ReadTableOpt{
Match: tableCfg.Filter.Match,
Sorts: tableCfg.Filter.Sorts,
Limit: tableCfg.Filter.Limit,
Relationships: rOpts,
}
}
// Connect acts as factory method that returns a reader from a DSN
func Connect(opts ConnOpts) (reader Reader, err error) {
drivers.Range(func(key, value interface{}) bool {
driver, ok := value.(Driver)
if !ok || !driver.IsSupported(opts.DSN) {
return true
}
reader, err = driver.NewConnection(opts)
return false
})
if reader == nil {
err = fmt.Errorf("unsupported dsn %q", opts.DSN)
}
return
}