github.com/Tri-stone/burrow@v0.25.0/vent/sqldb/adapters/db_adapter.go (about)

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