-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alexandros Filios <[email protected]>
- Loading branch information
1 parent
3e6f757
commit 8ed155a
Showing
12 changed files
with
204 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver" | ||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func NewAuditInfoPersistence(writeDB *sql.DB, readDB *sql.DB, table string, errorWrapper driver.SQLErrorWrapper, ci Interpreter) *AuditInfoPersistence { | ||
return &AuditInfoPersistence{ | ||
table: table, | ||
errorWrapper: errorWrapper, | ||
readDB: readDB, | ||
writeDB: writeDB, | ||
ci: ci, | ||
} | ||
} | ||
|
||
type AuditInfoPersistence struct { | ||
table string | ||
errorWrapper driver.SQLErrorWrapper | ||
readDB *sql.DB | ||
writeDB *sql.DB | ||
ci Interpreter | ||
} | ||
|
||
func (db *AuditInfoPersistence) GetAuditInfo(id view.Identity) ([]byte, error) { | ||
where, params := Where(db.ci.Cmp("id", "=", id.UniqueID())) | ||
query := fmt.Sprintf("SELECT audit_info FROM %s %s", db.table, where) | ||
logger.Debug(query, params) | ||
|
||
return QueryUnique[[]byte](db.readDB, query, params...) | ||
} | ||
|
||
func (db *AuditInfoPersistence) PutAuditInfo(id view.Identity, info []byte) error { | ||
query := fmt.Sprintf("INSERT INTO %s (id, audit_info) VALUES ($1, $2)", db.table) | ||
logger.Debugf(query, id, info) | ||
_, err := db.writeDB.Exec(query, id.UniqueID(), info) | ||
if err != nil && errors.Is(db.errorWrapper.WrapError(err), driver.UniqueKeyViolation) { | ||
logger.Warnf("Audit info [%s] already in db. Skipping...", id) | ||
return nil | ||
} | ||
if err != nil { | ||
return errors.Wrapf(err, "failed executing query [%s]", query) | ||
} | ||
logger.Debugf("Signer [%s] registered", id) | ||
return nil | ||
} | ||
|
||
func (db *AuditInfoPersistence) CreateSchema() error { | ||
return InitSchema(db.writeDB, fmt.Sprintf(` | ||
CREATE TABLE IF NOT EXISTS %s ( | ||
id TEXT NOT NULL PRIMARY KEY, | ||
audit_info BYTEA NOT NULL | ||
);`, db.table)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
platform/view/services/db/driver/sql/postgres/auditinfo.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package postgres | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/common" | ||
) | ||
|
||
type AuditInfoPersistence struct { | ||
*common.AuditInfoPersistence | ||
} | ||
|
||
func NewAuditInfoPersistence(opts common.Opts, table string) (*AuditInfoPersistence, error) { | ||
readWriteDB, err := OpenDB(opts.DataSource, opts.MaxOpenConns, opts.MaxIdleConns, opts.MaxIdleTime) | ||
if err != nil { | ||
return nil, fmt.Errorf("error opening db: %w", err) | ||
} | ||
return newAuditInfoPersistence(readWriteDB, readWriteDB, table), nil | ||
} | ||
|
||
func newAuditInfoPersistence(readDB, writeDB *sql.DB, table string) *AuditInfoPersistence { | ||
return &AuditInfoPersistence{AuditInfoPersistence: common.NewAuditInfoPersistence(readDB, writeDB, table, &errorMapper{}, NewInterpreter())} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package sqlite | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/common" | ||
) | ||
|
||
type AuditInfoPersistence struct { | ||
*common.AuditInfoPersistence | ||
} | ||
|
||
func NewAuditInfoPersistence(opts common.Opts, table string) (*AuditInfoPersistence, error) { | ||
readDB, writeDB, err := openDB(opts.DataSource, opts.MaxOpenConns, opts.MaxIdleConns, opts.MaxIdleTime, opts.SkipPragmas) | ||
if err != nil { | ||
return nil, fmt.Errorf("error opening db: %w", err) | ||
} | ||
return newAuditInfoPersistence(readDB, writeDB, table), nil | ||
} | ||
|
||
func newAuditInfoPersistence(readDB, writeDB *sql.DB, table string) *AuditInfoPersistence { | ||
return &AuditInfoPersistence{AuditInfoPersistence: common.NewAuditInfoPersistence(readDB, writeDB, table, &errorMapper{}, NewInterpreter())} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package auditinfo | ||
|
||
import ( | ||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db" | ||
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver" | ||
) | ||
|
||
const ( | ||
persistenceOptsConfigKey = "fsc.auditinfo.persistence.opts" | ||
) | ||
|
||
func NewWithConfig(dbDriver driver.Driver, namespace string, cp db.Config) (driver.AuditInfoPersistence, error) { | ||
return dbDriver.NewAuditInfo(namespace, db.NewPrefixConfig(cp, persistenceOptsConfigKey)) | ||
} |