/
builder.go
96 lines (68 loc) · 3.41 KB
/
builder.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2016 Granitic. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be found in the LICENSE file at the root of this project.
/*
Package rdbms provides the RdbmsAccess facility which gives application code access to an RDBMS (SQL database).
The RdbmsAccess facility is described in detail at http://granitic.io/1.0/ref/rdbms-access and the programmatic
interface that applications will use for executing SQL is described in the rdbms package documentation.
The purpose of this facility is to create an rdbms.RdbmsClientManager that will be injected into your application
components. In turn, the rdbms.RdbmsClientManager will be used by your application to create instances of rdbms.RDBMSClient
which provide the interface for executing SQL queries and managing transactions.
*/
package rdbms
import (
"github.com/graniticio/granitic/config"
"github.com/graniticio/granitic/facility/querymanager"
"github.com/graniticio/granitic/instance"
"github.com/graniticio/granitic/ioc"
"github.com/graniticio/granitic/logging"
"github.com/graniticio/granitic/rdbms"
)
const rdbmsClientManagerName = instance.FrameworkPrefix + "RdbmsClientManager"
const providerDecorator = instance.FrameworkPrefix + "DbProviderDecorator"
const managerDecorator = instance.FrameworkPrefix + "DbClientManagerDecorator"
const sharedClientLog = instance.FrameworkPrefix + "RdbmsClient"
// Creates an instance of rdbms.RDBMSClientManager that can be injected into your application components.
type RdbmsAccessFacilityBuilder struct {
}
// See FacilityBuilder.BuildAndRegister
func (rafb *RdbmsAccessFacilityBuilder) BuildAndRegister(lm *logging.ComponentLoggerManager, ca *config.ConfigAccessor, cn *ioc.ComponentContainer) error {
manager := new(rdbms.GraniticRdbmsClientManager)
ca.Populate("RdbmsAccess", manager)
proto := ioc.CreateProtoComponent(manager, rdbmsClientManagerName)
proto.AddDependency("QueryManager", querymanager.QueryManagerComponentName)
cn.AddProto(proto)
pd := new(databaseProviderDecorator)
pd.receiver = manager
cn.WrapAndAddProto(providerDecorator, pd)
if manager.DisableAutoInjection {
return nil
}
md := new(clientManagerDecorator)
md.fieldNames = manager.InjectFieldNames
md.manager = manager
cn.WrapAndAddProto(managerDecorator, md)
manager.SharedLog = lm.CreateLogger(sharedClientLog)
return nil
}
// See FacilityBuilder.FacilityName
func (rafb *RdbmsAccessFacilityBuilder) FacilityName() string {
return "RdbmsAccess"
}
// DependsOnFacilities returns the other faclities that must be enabled in order to use the RdbmsAccess facility. You must
// enable the QueryManager facility.
func (rafb *RdbmsAccessFacilityBuilder) DependsOnFacilities() []string {
return []string{querymanager.QueryManagerFacilityName}
}
// Finds implementations of rdbms.DatabaseProvider in the IoC container and injects them into the RDBMSClientManager
type databaseProviderDecorator struct {
receiver rdbms.ProviderComponentReceiver
}
// OfInterest returns true if the subject component implements rdbms.DatabaseProvider
func (dpd *databaseProviderDecorator) OfInterest(subject *ioc.Component) bool {
_, found := subject.Instance.(rdbms.DatabaseProvider)
return found
}
// DecorateComponent injects the DatabaseProvider into the subject component.
func (dpd *databaseProviderDecorator) DecorateComponent(subject *ioc.Component, cc *ioc.ComponentContainer) {
dpd.receiver.RegisterProvider(subject)
}