github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/vent/sqldb/adapters/db_adapter.go (about)

     1  package adapters
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hyperledger/burrow/vent/types"
     8  	"github.com/jmoiron/sqlx"
     9  )
    10  
    11  // DBAdapter implements database dependent interface
    12  type DBAdapter interface {
    13  	Open(dbURL string) (*sqlx.DB, error)
    14  	// TODO: incrementally refactor DBAdapter to be responsible for actually _doing_ the queries,
    15  	// TODO: legacy stringly queries:
    16  	// TypeMapping maps generic SQL column types to db adapter dependent column types
    17  	TypeMapping(sqlColumnType types.SQLColumnType) (string, error)
    18  	// ErrorEquals compares generic SQL errors to db adapter dependent errors
    19  	ErrorEquals(err error, sqlErrorType types.SQLErrorType) bool
    20  	// SecureColumnName returns columns with proper delimiters to ensure well formed column names
    21  	SecureName(name string) string
    22  	// CreateTableQuery builds a CREATE TABLE query to create a new table
    23  	CreateTableQuery(tableName string, columns []*types.SQLTableColumn) (string, string)
    24  	// FindTableQuery builds a SELECT query to check if a table exists
    25  	FindTableQuery() string
    26  	// TableDefinitionQuery builds a SELECT query to get a table structure from the Dictionary table
    27  	TableDefinitionQuery() string
    28  	// AlterColumnQuery builds an ALTER COLUMN query to alter a table structure (only adding columns is supported)
    29  	AlterColumnQuery(tableName, columnName string, sqlColumnType types.SQLColumnType, length, order int) (string, string)
    30  	// SelectRowQuery builds a SELECT query to get row values
    31  	SelectRowQuery(tableName, fields, indexValue string) string
    32  	// SelectLogQuery builds a SELECT query to get all tables involved in a given block transaction
    33  	SelectLogQuery() string
    34  	// InsertLogQuery builds an INSERT query to store data in Log table
    35  	InsertLogQuery() string
    36  	// UpsertQuery builds an INSERT... ON CONFLICT (or similar) query to upsert data in event tables based on PK
    37  	UpsertQuery(table *types.SQLTable, row types.EventDataRow) (types.UpsertDeleteQuery, interface{}, error)
    38  	// DeleteQuery builds a DELETE FROM event tables query based on PK
    39  	DeleteQuery(table *types.SQLTable, row types.EventDataRow) (types.UpsertDeleteQuery, error)
    40  	// RestoreDBQuery builds a list of sql clauses needed to restore the db to a point in time
    41  	RestoreDBQuery() string
    42  	// CleanDBQueries returns necessary queries to clean the database
    43  	CleanDBQueries() types.SQLCleanDBQuery
    44  	// DropTableQuery builds a DROP TABLE query to delete a table
    45  	DropTableQuery(tableName string) string
    46  	// Get the schema qualified name of the given table
    47  	SchemaName(tableName string) string
    48  }
    49  
    50  type DBNotifyTriggerAdapter interface {
    51  	// Create a SQL function that notifies on channel with the payload of columns - the payload containing the value
    52  	// of each column will be sent once whenever any of the columns changes. Expected to replace existing function.
    53  	CreateNotifyFunctionQuery(function, channel string, columns ...string) string
    54  	// Create a trigger that fires the named function after any operation on a row in table. Expected to replace existing
    55  	// trigger.
    56  	CreateTriggerQuery(triggerName, tableName, functionName string) string
    57  }
    58  
    59  // clean queries from tabs, spaces  and returns
    60  func clean(parameter string) string {
    61  	replacer := strings.NewReplacer("\n", " ", "\t", "")
    62  	return replacer.Replace(parameter)
    63  }
    64  
    65  func Cleanf(format string, args ...interface{}) string {
    66  	return clean(fmt.Sprintf(format, args...))
    67  }