github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/database/sqlcommon/nextpin_sql.go (about) 1 // Copyright © 2021 Kaleido, Inc. 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package sqlcommon 18 19 import ( 20 "context" 21 "database/sql" 22 "fmt" 23 24 sq "github.com/Masterminds/squirrel" 25 "github.com/kaleido-io/firefly/internal/i18n" 26 "github.com/kaleido-io/firefly/internal/log" 27 "github.com/kaleido-io/firefly/pkg/database" 28 "github.com/kaleido-io/firefly/pkg/fftypes" 29 ) 30 31 var ( 32 nextpinColumns = []string{ 33 "context", 34 "identity", 35 "hash", 36 "nonce", 37 } 38 nextpinFilterTypeMap = map[string]string{} 39 ) 40 41 func (s *SQLCommon) InsertNextPin(ctx context.Context, nextpin *fftypes.NextPin) (err error) { 42 ctx, tx, autoCommit, err := s.beginOrUseTx(ctx) 43 if err != nil { 44 return err 45 } 46 defer s.rollbackTx(ctx, tx, autoCommit) 47 48 sequence, err := s.insertTx(ctx, tx, 49 sq.Insert("nextpins"). 50 Columns(nextpinColumns...). 51 Values( 52 nextpin.Context, 53 nextpin.Identity, 54 nextpin.Hash, 55 nextpin.Nonce, 56 ), 57 ) 58 if err != nil { 59 return err 60 } 61 nextpin.Sequence = sequence 62 63 return s.commitTx(ctx, tx, autoCommit) 64 } 65 66 func (s *SQLCommon) nextpinResult(ctx context.Context, row *sql.Rows) (*fftypes.NextPin, error) { 67 nextpin := fftypes.NextPin{} 68 err := row.Scan( 69 &nextpin.Context, 70 &nextpin.Identity, 71 &nextpin.Hash, 72 &nextpin.Nonce, 73 &nextpin.Sequence, 74 ) 75 if err != nil { 76 return nil, i18n.WrapError(ctx, err, i18n.MsgDBReadErr, "nextpins") 77 } 78 return &nextpin, nil 79 } 80 81 func (s *SQLCommon) getNextPinPred(ctx context.Context, desc string, pred interface{}) (message *fftypes.NextPin, err error) { 82 cols := append([]string{}, nextpinColumns...) 83 cols = append(cols, s.provider.SequenceField("")) 84 rows, err := s.query(ctx, 85 sq.Select(cols...). 86 From("nextpins"). 87 Where(pred). 88 Limit(1), 89 ) 90 if err != nil { 91 return nil, err 92 } 93 defer rows.Close() 94 95 if !rows.Next() { 96 log.L(ctx).Debugf("NextPin '%s' not found", desc) 97 return nil, nil 98 } 99 100 nextpin, err := s.nextpinResult(ctx, rows) 101 if err != nil { 102 return nil, err 103 } 104 105 return nextpin, nil 106 } 107 108 func (s *SQLCommon) GetNextPinByContextAndIdentity(ctx context.Context, context *fftypes.Bytes32, identity string) (message *fftypes.NextPin, err error) { 109 return s.getNextPinPred(ctx, fmt.Sprintf("%s:%s", context, identity), sq.Eq{ 110 "context": context, 111 "identity": identity, 112 }) 113 } 114 115 func (s *SQLCommon) GetNextPinByHash(ctx context.Context, hash *fftypes.Bytes32) (message *fftypes.NextPin, err error) { 116 return s.getNextPinPred(ctx, hash.String(), sq.Eq{ 117 "hash": hash, 118 }) 119 } 120 121 func (s *SQLCommon) GetNextPins(ctx context.Context, filter database.Filter) (message []*fftypes.NextPin, err error) { 122 123 cols := append([]string{}, nextpinColumns...) 124 cols = append(cols, s.provider.SequenceField("")) 125 query, err := s.filterSelect(ctx, "", sq.Select(cols...).From("nextpins"), filter, nextpinFilterTypeMap) 126 if err != nil { 127 return nil, err 128 } 129 130 rows, err := s.query(ctx, query) 131 if err != nil { 132 return nil, err 133 } 134 defer rows.Close() 135 136 nextpin := []*fftypes.NextPin{} 137 for rows.Next() { 138 d, err := s.nextpinResult(ctx, rows) 139 if err != nil { 140 return nil, err 141 } 142 nextpin = append(nextpin, d) 143 } 144 145 return nextpin, err 146 147 } 148 149 func (s *SQLCommon) UpdateNextPin(ctx context.Context, sequence int64, update database.Update) (err error) { 150 151 ctx, tx, autoCommit, err := s.beginOrUseTx(ctx) 152 if err != nil { 153 return err 154 } 155 defer s.rollbackTx(ctx, tx, autoCommit) 156 157 query, err := s.buildUpdate(sq.Update("nextpins"), update, nodeFilterTypeMap) 158 if err != nil { 159 return err 160 } 161 query = query.Where(sq.Eq{s.provider.SequenceField(""): sequence}) 162 163 err = s.updateTx(ctx, tx, query) 164 if err != nil { 165 return err 166 } 167 168 return s.commitTx(ctx, tx, autoCommit) 169 } 170 171 func (s *SQLCommon) DeleteNextPin(ctx context.Context, sequence int64) (err error) { 172 173 ctx, tx, autoCommit, err := s.beginOrUseTx(ctx) 174 if err != nil { 175 return err 176 } 177 defer s.rollbackTx(ctx, tx, autoCommit) 178 179 err = s.deleteTx(ctx, tx, sq.Delete("nextpins").Where(sq.Eq{ 180 s.provider.SequenceField(""): sequence, 181 })) 182 if err != nil { 183 return err 184 } 185 186 return s.commitTx(ctx, tx, autoCommit) 187 }