dbabstract

package
v0.0.0-...-497a2b7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2020 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// TCP constant for connection type of tcp4 and tcp6
	TCP = "tcp"
	// TCP4 constant for connection type of tcp4
	TCP4 = "tcp4"
	// TCP6 constant for connection type of tcp6
	TCP6 = "tcp6"
	// UNIX constant for connection type of unix socket
	UNIX = "unix"
)

Variables

View Source
var (
	// ErrArgCount is the error returned when an incorrect number of arguments is given to SQLPrintf
	ErrArgCount = errors.New("incorrect number of arguments provided")
	// ErrArgNoEscape is the error return when an outer is provided but no escape is provided for an argument
	ErrArgNoEscape = errors.New("no escape character provided for argument")
	// ErrDatabaseConnectTypeUnsupported is the error returned for unsupported network types
	ErrDatabaseConnectTypeUnsupported = errors.New("unsupported connection type")
	// ErrDatabaseConnectTypeMissing is the error returned for missing database connection types
	ErrDatabaseConnectTypeMissing = errors.New("no database connection type provided")
	// ErrDatabaseHostMissing is the error returned for missing database hosts
	ErrDatabaseHostMissing = errors.New("no database host provided")
	// ErrDatabaseNameMissing is the error returned for missing database names
	ErrDatabaseNameMissing = errors.New("no database name provided")
	// ErrDatabasePortMissing is the error returned for missing database ports
	ErrDatabasePortMissing = errors.New("no database port provided")
	// ErrDatabaseSocketPathMissing is the error returned for missing database socket paths
	ErrDatabaseSocketPathMissing = errors.New("no database socket path provided")
	// ErrDataPathMissing is returned when no data path was provided
	ErrDataPathMissing = errors.New("no data path provided")
	// ErrDbEmpty is returned when no tables are found in a database
	ErrDbEmpty = errors.New("no tables found in database")
	// ErrSSLModeUnsupported is the error returned for unsupported SSL Modes for Postgres connections
	ErrSSLModeUnsupported = errors.New("unsupported ssl mode")
	// ErrVarNone is the error returned when there are no variables to replace in a query statement
	ErrVarNone = errors.New("no variables to replace in statement")
)

Functions

This section is empty.

Types

type DBArg

type DBArg struct {
	Arg    string // Arg holds the argument to be used
	Outer  string // Outer holds the enclosing byte for the argument. ie ', ", or `. If an outer is not needed, leave as ""
	Escape string // Escape holds the escape character to use to escape extra characters that match Outer
}

DBArg holds an argument for SQLPrintf

type DBHolder

type DBHolder interface {
	// Close is a wrapper function to sql.DB.Close()
	Close() error
	// DB returns the pointer to the open SQL database connection
	DB() *sql.DB
	// Driver returns the configured driver
	Driver() string
	// Format formats an SQL queries argument identifier for the underlying driver. This needs to be run
	// before Prepare().
	//
	// Replaces ? with $ or vice versa
	Format(query string) string
	// Open opens the connection to the database
	Open() error
	// Path returns the path used to connect to the database. This is useful for debug purposes.
	// the username and password are not stored.
	Path() string
	// SupportsPrep tells dbabstract if the database being connected to supports prepared statements.
	//
	// If the database does not support prepared statements, dbabstract falls back to using SQLPrintf for some
	// functionality.
	//
	// Please see the notes of SQLPrintf.
	SupportsPrep(bool)
	// SQLPrintf returns a formatted SQL string.
	// If an encapsulation character is provided, the argument will be encapsulated, and all matching encapsulation
	// characters in between will be escaped with the provided Escape character.
	//
	// Rather than adding "%s" and "%i" tags, SQL statement (format string) should be formatted with "$1" or
	// "?" just like a Prepared statement.
	//
	// The caller will need to know the proper encapsulation characters for the arguments provided as well as
	// the escape character used by the database.
	//
	// This should only be used when a prepared statement or an exec with arguments cannot be used.
	//
	// SQLPrintf also checks arguments for harmful keywords, returning an error if any are found.
	//
	// Examples:
	// DBArg{Arg: table'Name, ', \} will return 'table\'Name'
	// DBArg{Arg: DROP DATABASE blah, ', \} will return an error because DROP can be an SQL injection
	SQLPrintf(format string, args ...DBArg) (string, error)
	// TableExists checks for the existence of a table in the database for that specified driver.
	// logger should be the function you wish to have used for logging (ie log.Debug)
	// The query statement for checking if the table exists will be logged
	// If logging isn't desired, logger should be nil
	TableExists(table string, logger func(args ...interface{})) (bool, error)
	// TableList returns an array of table names for the currently connected database
	// logger should be the function you wish to have used for logging (ie log.Debug)
	// If logging isn't desired, logger should be nil
	TableList(logger func(args ...interface{})) ([]string, error)
}

DBHolder holds the open database connection and methods for interacting with the database.

func NewDBHolder

func NewDBHolder(opts DBOpts) (DBHolder, error)

NewDBHolder returns an initialized DBHolder for operating on a configured database

type DBOpts

type DBOpts struct {
	DataDir     string // path to the database data directory (only applicable with sqlite3)
	Driver      string // database driver to use
	ConnectType string // tcp or unix (tcp must set host and port, unix must set path)
	Host        string // address that database server is listening on
	Port        int    // port that the database server is listening on
	SocketPath  string // path to unix socket to use
	Username    string // username to use when connecting to the database server
	Password    string // password to use when connecting to the database server
	DBName      string // database name to connect to (for sqlite3, this is the filename without the .db extension)
	Fallback    string // An application name to fall back to if one isn't provided
	SSLMode     string // whether or not to use ssl (default is require) valid are disable, require, verify-ca, verify-full
	SSLCert     string // path to PEM encoded cert file
	SSLKey      string // path to PEM encoded key file
	SSLRootCert string // path to PEM encoded root cert file
}

DBOpts holds database configuration options

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL