forked from dapr/components-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql.go
127 lines (106 loc) · 3.88 KB
/
postgresql.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright 2021 The Dapr 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 postgresql
import (
"context"
"reflect"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/components-contrib/state"
"github.com/dapr/kit/logger"
)
// PostgreSQL state store.
type PostgreSQL struct {
state.BulkStore
logger logger.Logger
dbaccess dbAccess
}
type Options struct {
MigrateFn func(context.Context, PGXPoolConn, MigrateOptions) error
SetQueryFn func(*state.SetRequest, SetQueryOptions) string
ETagColumn string
}
type MigrateOptions struct {
Logger logger.Logger
StateTableName string
MetadataTableName string
}
type SetQueryOptions struct {
TableName string
ExpireDateValue string
}
// NewPostgreSQLStateStore creates a new instance of PostgreSQL state store.
func NewPostgreSQLStateStore(logger logger.Logger, opts Options) state.Store {
dba := newPostgresDBAccess(logger, opts)
s := newPostgreSQLStateStore(logger, dba)
s.BulkStore = state.NewDefaultBulkStore(s)
return s
}
// newPostgreSQLStateStore creates a newPostgreSQLStateStore instance of a PostgreSQL state store.
// This unexported constructor allows injecting a dbAccess instance for unit testing.
func newPostgreSQLStateStore(logger logger.Logger, dba dbAccess) *PostgreSQL {
return &PostgreSQL{
logger: logger,
dbaccess: dba,
}
}
// Init initializes the SQL server state store.
func (p *PostgreSQL) Init(ctx context.Context, metadata state.Metadata) error {
return p.dbaccess.Init(ctx, metadata)
}
// Features returns the features available in this state store.
func (p *PostgreSQL) Features() []state.Feature {
return []state.Feature{state.FeatureETag, state.FeatureTransactional, state.FeatureQueryAPI}
}
// Delete removes an entity from the store.
func (p *PostgreSQL) Delete(ctx context.Context, req *state.DeleteRequest) error {
return p.dbaccess.Delete(ctx, req)
}
// Get returns an entity from store.
func (p *PostgreSQL) Get(ctx context.Context, req *state.GetRequest) (*state.GetResponse, error) {
return p.dbaccess.Get(ctx, req)
}
// BulkGet performs a bulks get operations.
// Options are ignored because this component requests all values in a single query.
func (p *PostgreSQL) BulkGet(ctx context.Context, req []state.GetRequest, _ state.BulkGetOpts) ([]state.BulkGetResponse, error) {
return p.dbaccess.BulkGet(ctx, req)
}
// Set adds/updates an entity on store.
func (p *PostgreSQL) Set(ctx context.Context, req *state.SetRequest) error {
return p.dbaccess.Set(ctx, req)
}
// Multi handles multiple transactions. Implements TransactionalStore.
func (p *PostgreSQL) Multi(ctx context.Context, request *state.TransactionalStateRequest) error {
return p.dbaccess.ExecuteMulti(ctx, request)
}
// Query executes a query against store.
func (p *PostgreSQL) Query(ctx context.Context, req *state.QueryRequest) (*state.QueryResponse, error) {
return p.dbaccess.Query(ctx, req)
}
// Close implements io.Closer.
func (p *PostgreSQL) Close() error {
if p.dbaccess != nil {
return p.dbaccess.Close()
}
return nil
}
// Returns the dbaccess property.
// This method is used in tests.
func (p *PostgreSQL) GetDBAccess() dbAccess {
return p.dbaccess
}
func (p *PostgreSQL) GetComponentMetadata() map[string]string {
metadataStruct := postgresMetadataStruct{}
metadataInfo := map[string]string{}
metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, metadata.StateStoreType)
return metadataInfo
}