Skip to content

Commit 386c430

Browse files
authored
introduce sql query support (#809)
introduce sql query support Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent 142b090 commit 386c430

File tree

11 files changed

+599
-128
lines changed

11 files changed

+599
-128
lines changed

token/services/db/sql/common/common.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,16 @@ func QueryUnique[T any](db *sql.DB, query string, args ...any) (T, error) {
2828
}
2929
return result, err
3030
}
31+
32+
type Closer interface {
33+
Close() error
34+
}
35+
36+
func Close(closer Closer) {
37+
if closer == nil {
38+
return
39+
}
40+
if err := closer.Close(); err != nil {
41+
logger.Errorf("failed closing connection: %s", err)
42+
}
43+
}

token/services/db/sql/common/identity.go

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,21 @@ func NewIdentityDB(db *sql.DB, tablePrefix string, createSchema bool, signerInfo
8585
}
8686

8787
func (db *IdentityDB) AddConfiguration(wp driver.IdentityConfiguration) error {
88-
query := fmt.Sprintf("INSERT INTO %s (id, type, url, conf, raw) VALUES ($1, $2, $3, $4, $5)", db.table.IdentityConfigurations)
88+
query, err := NewInsertInto(db.table.IdentityConfigurations).Rows("id, type, url, conf, raw").Compile()
89+
if err != nil {
90+
return errors.Wrapf(err, "failed compiling query")
91+
}
8992
logger.Debug(query)
9093

91-
_, err := db.db.Exec(query, wp.ID, wp.Type, wp.URL, wp.Config, wp.Raw)
94+
_, err = db.db.Exec(query, wp.ID, wp.Type, wp.URL, wp.Config, wp.Raw)
9295
return err
9396
}
9497

9598
func (db *IdentityDB) IteratorConfigurations(configurationType string) (driver.Iterator[driver.IdentityConfiguration], error) {
96-
query := fmt.Sprintf("SELECT id, url, conf, raw FROM %s WHERE type = $1", db.table.IdentityConfigurations)
99+
query, err := NewSelect("id, url, conf, raw").From(db.table.IdentityConfigurations).Where("type = $1").Compile()
100+
if err != nil {
101+
return nil, errors.Wrapf(err, "failed compiling query")
102+
}
97103
logger.Debug(query)
98104
rows, err := db.db.Query(query, configurationType)
99105
if err != nil {
@@ -103,10 +109,11 @@ func (db *IdentityDB) IteratorConfigurations(configurationType string) (driver.I
103109
}
104110

105111
func (db *IdentityDB) ConfigurationExists(id, typ string) (bool, error) {
106-
result, err := QueryUnique[string](db.db,
107-
fmt.Sprintf("SELECT id FROM %s WHERE id=$1 AND type=$2", db.table.IdentityConfigurations),
108-
id, typ,
109-
)
112+
query, err := NewSelect("id").From(db.table.IdentityConfigurations).Where("id=$1 AND type=$2").Compile()
113+
if err != nil {
114+
return false, errors.Wrapf(err, "failed compiling query")
115+
}
116+
result, err := QueryUnique[string](db.db, query, id, typ)
110117
if err != nil {
111118
return false, errors.Wrapf(err, "failed getting configuration for [%s:%s]", id, typ)
112119
}
@@ -115,12 +122,15 @@ func (db *IdentityDB) ConfigurationExists(id, typ string) (bool, error) {
115122
}
116123

117124
func (db *IdentityDB) StoreIdentityData(id []byte, identityAudit []byte, tokenMetadata []byte, tokenMetadataAudit []byte) error {
118-
//logger.Infof("store identity data for [%s] from [%s]", view.Identity(id), string(debug.Stack()))
119-
query := fmt.Sprintf("INSERT INTO %s (identity_hash, identity, identity_audit_info, token_metadata, token_metadata_audit_info) VALUES ($1, $2, $3, $4, $5)", db.table.IdentityInfo)
125+
// logger.Infof("store identity data for [%s] from [%s]", view.Identity(id), string(debug.Stack()))
126+
query, err := NewInsertInto(db.table.IdentityInfo).Rows("identity_hash, identity, identity_audit_info, token_metadata, token_metadata_audit_info").Compile()
127+
if err != nil {
128+
return errors.Wrapf(err, "failed compiling query")
129+
}
120130
logger.Debug(query)
121131

122132
h := token.Identity(id).String()
123-
_, err := db.db.Exec(query, h, id, identityAudit, tokenMetadata, tokenMetadataAudit)
133+
_, err = db.db.Exec(query, h, id, identityAudit, tokenMetadata, tokenMetadataAudit)
124134
if err != nil {
125135
// does the record already exists?
126136
auditInfo, err2 := db.GetAuditInfo(id)
@@ -142,12 +152,15 @@ func (db *IdentityDB) GetAuditInfo(id []byte) ([]byte, error) {
142152
h := token.Identity(id).String()
143153

144154
value, _, err := db.auditInfoCache.GetOrLoad(h, func() ([]byte, error) {
145-
//logger.Infof("get identity data for [%s] from [%s]", view.Identity(id), string(debug.Stack()))
146-
query := fmt.Sprintf("SELECT identity_audit_info FROM %s WHERE identity_hash = $1", db.table.IdentityInfo)
155+
// logger.Infof("get identity data for [%s] from [%s]", view.Identity(id), string(debug.Stack()))
156+
query, err := NewSelect("identity_audit_info").From(db.table.IdentityInfo).Where("identity_hash = $1").Compile()
157+
if err != nil {
158+
return nil, errors.Wrapf(err, "failed compiling query")
159+
}
147160
logger.Debug(query)
148161
row := db.db.QueryRow(query, h)
149162
var info []byte
150-
err := row.Scan(&info)
163+
err = row.Scan(&info)
151164
if err == nil {
152165
return info, nil
153166
}
@@ -161,13 +174,16 @@ func (db *IdentityDB) GetAuditInfo(id []byte) ([]byte, error) {
161174

162175
func (db *IdentityDB) GetTokenInfo(id []byte) ([]byte, []byte, error) {
163176
h := token.Identity(id).String()
164-
//logger.Infof("get identity data for [%s] from [%s]", view.Identity(id), string(debug.Stack()))
165-
query := fmt.Sprintf("SELECT token_metadata, token_metadata_audit_info FROM %s WHERE identity_hash = $1", db.table.IdentityInfo)
177+
// logger.Infof("get identity data for [%s] from [%s]", view.Identity(id), string(debug.Stack()))
178+
query, err := NewSelect("token_metadata, token_metadata_audit_info").From(db.table.IdentityInfo).Where("identity_hash = $1").Compile()
179+
if err != nil {
180+
return nil, nil, errors.Wrapf(err, "failed compiling query")
181+
}
166182
logger.Debug(query)
167183
row := db.db.QueryRow(query, h)
168184
var tokenMetadata []byte
169185
var tokenMetadataAuditInfo []byte
170-
err := row.Scan(&tokenMetadata, &tokenMetadataAuditInfo)
186+
err = row.Scan(&tokenMetadata, &tokenMetadataAuditInfo)
171187
if err != nil {
172188
if errors.Is(err, sql.ErrNoRows) {
173189
return nil, nil, nil
@@ -178,12 +194,15 @@ func (db *IdentityDB) GetTokenInfo(id []byte) ([]byte, []byte, error) {
178194
}
179195

180196
func (db *IdentityDB) StoreSignerInfo(id, info []byte) error {
181-
query := fmt.Sprintf("INSERT INTO %s (identity_hash, identity, info) VALUES ($1, $2, $3)", db.table.Signers)
197+
query, err := NewInsertInto(db.table.Signers).Rows("identity_hash, identity, info").Compile()
198+
if err != nil {
199+
return errors.Wrapf(err, "failed compiling query")
200+
}
182201
h := token.Identity(id).String()
183202
if logger.IsEnabledFor(zapcore.DebugLevel) {
184203
logger.Debugf("store signer info [%s]: [%s][%s]", query, h, hash.Hashable(info))
185204
}
186-
_, err := db.db.Exec(query, h, id, info)
205+
_, err = db.db.Exec(query, h, id, info)
187206
if err != nil {
188207
if exists, err2 := db.SignerInfoExists(id); err2 == nil && exists {
189208
logger.Debugf("signer info [%s] exists, no error to return", h)
@@ -200,11 +219,14 @@ func (db *IdentityDB) SignerInfoExists(id []byte) (bool, error) {
200219
h := token.Identity(id).String()
201220

202221
value, _, err := db.signerInfoCache.GetOrLoad(h, func() (bool, error) {
203-
query := fmt.Sprintf("SELECT info FROM %s WHERE identity_hash = $1", db.table.Signers)
222+
query, err := NewSelect("info").From(db.table.Signers).Where("identity_hash = $1").Compile()
223+
if err != nil {
224+
return false, errors.Wrapf(err, "failed compiling query")
225+
}
204226
logger.Debug(query)
205227
row := db.db.QueryRow(query, h)
206228
var info []byte
207-
err := row.Scan(&info)
229+
err = row.Scan(&info)
208230
if err == nil {
209231
return true, nil
210232
}
@@ -218,11 +240,14 @@ func (db *IdentityDB) SignerInfoExists(id []byte) (bool, error) {
218240

219241
func (db *IdentityDB) GetSignerInfo(identity []byte) ([]byte, error) {
220242
h := token.Identity(identity).String()
221-
query := fmt.Sprintf("SELECT info FROM %s WHERE identity_hash = $1", db.table.Signers)
243+
query, err := NewSelect("info").From(db.table.Signers).Where("identity_hash = $1").Compile()
244+
if err != nil {
245+
return nil, errors.Wrapf(err, "failed compiling query")
246+
}
222247
logger.Debug(query)
223248
row := db.db.QueryRow(query, h)
224249
var info []byte
225-
err := row.Scan(&info)
250+
err = row.Scan(&info)
226251
if err != nil {
227252
if errors.Is(err, sql.ErrNoRows) {
228253
return nil, nil

0 commit comments

Comments
 (0)