/
db_handler.go
59 lines (50 loc) · 2.18 KB
/
db_handler.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
// Copyright 2020 the go-etl 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 rdbm
import (
"database/sql"
"github.com/Breeze0806/go-etl/config"
rdbmreader "github.com/Breeze0806/go-etl/datax/plugin/reader/rdbm"
"github.com/Breeze0806/go-etl/storage/database"
)
//DbHandler 数据库执行句柄封装
type DbHandler interface {
Execer(name string, conf *config.JSON) (Execer, error) //通过数据库名name和配置获取执行器
Config(conf *config.JSON) (Config, error) //通过配置获取关系型数据库配置
TableParam(config Config, execer Execer) database.Parameter //通过关系型数据库配置和执行器获取表参数
}
//BaseDbHandler 基础数据库执行句柄封装
type BaseDbHandler struct {
newExecer func(name string, conf *config.JSON) (Execer, error)
opts *sql.TxOptions
}
//NewBaseDbHandler 通过获取执行器函数newExecer和数据库事务执行选项opts创建数据库执行句柄封装
func NewBaseDbHandler(newExecer func(name string, conf *config.JSON) (Execer, error), opts *sql.TxOptions) *BaseDbHandler {
return &BaseDbHandler{
newExecer: newExecer,
opts: opts,
}
}
//Execer 通过数据库名name和配置获取执行器
func (d *BaseDbHandler) Execer(name string, conf *config.JSON) (Execer, error) {
return d.newExecer(name, conf)
}
//Config 通过配置获取关系型数据库配置
func (d *BaseDbHandler) Config(conf *config.JSON) (Config, error) {
return NewBaseConfig(conf)
}
//TableParam 通过关系型数据库配置和执行器获取表参数
func (d *BaseDbHandler) TableParam(config Config, execer Execer) database.Parameter {
return rdbmreader.NewTableParam(config, execer, d.opts)
}