dbutil

package
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2021 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package dbutil implements utilities for opening and configuring connections to MySQL-like databases, with optional TLS support.

Functions in this file are not thread-safe. However, the returned *sql.DB is. Sane defaults are assumed: utf8mb4 encoding, UTC timezone, parsing date/time into time.Time.

Index

Constants

View Source
const SQLConfigFileDescription = `` /* 819-byte string literal not displayed */

Description of the SQL configuration file format.

View Source
const SQLCreateTableSuffix = "CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"

SQL statement suffix to be appended when creating tables.

Variables

This section is empty.

Functions

func NewSQLDBConnFromFile

func NewSQLDBConnFromFile(sqlConfigFile, txIsolation string) (*sql.DB, error)

Convenience function to parse and activate the configuration file and open a connection to the SQL database. If multiple connections with the same configuration are needed, a single ActivateSQLConfigFromFile() and multiple NewSQLDbConn() calls are recommended instead.

Types

type ActiveSQLConfig

type ActiveSQLConfig struct {
	// contains filtered or unexported fields
}

ActiveSQLConfig represents a SQL configuration that has been activated by registering the TLS configuration (if applicable). It can be used for opening SQL database connections.

func ActivateSQLConfigFromFile

func ActivateSQLConfigFromFile(sqlConfigFile string) (*ActiveSQLConfig, error)

Convenience function to parse and activate the SQL configuration file. Certificate paths that aren't absolute are interpreted relative to the directory containing sqlConfigFile.

func (*ActiveSQLConfig) NewSQLDBConn

func (sqlConfig *ActiveSQLConfig) NewSQLDBConn(txIsolation string) (*sql.DB, error)

Opens a connection to the SQL database using the provided configuration. Sets the specified transaction isolation (see link below). https://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_tx_isolation

type SQLConfig

type SQLConfig struct {
	// DataSourceName is the connection string as required by go-sql-driver:
	// "[username[:password]@][protocol[(address)]]/dbname";
	// database name must be specified, query parameters are not supported.
	DataSourceName string `json:"dataSourceName"`
	// TLSDisable, if set to true, uses an unencrypted connection;
	// otherwise, the following fields are mandatory.
	TLSDisable bool `json:"tlsDisable"`
	// TLSServerName is the domain name of the SQL server for TLS.
	TLSServerName string `json:"tlsServerName"`
	// RootCertPath is the root certificate of the SQL server for TLS.
	RootCertPath string `json:"rootCertPath"`
	// ClientCertPath is the client certificate for TLS.
	ClientCertPath string `json:"clientCertPath"`
	// ClientKeyPath is the client private key for TLS.
	ClientKeyPath string `json:"clientKeyPath"`
}

SQLConfig holds the fields needed to connect to a SQL instance and to configure TLS encryption of the information sent over the wire. It must be activated via Activate() before use.

func ParseSQLConfigFromFile

func ParseSQLConfigFromFile(sqlConfigFile string) (*SQLConfig, error)

Parses the SQL configuration file pointed to by sqlConfigFile (format described in SqlConfigFileDescription; also see links below). https://github.com/go-sql-driver/mysql/#dsn-data-source-name https://github.com/go-sql-driver/mysql/#tls

func (*SQLConfig) Activate

func (sc *SQLConfig) Activate(certBaseDir string) (*ActiveSQLConfig, error)

Activates the SQL configuration by registering the TLS configuration with go-mysql-driver (if TLSDisable is not set). Certificate paths from SqlConfig that aren't absolute are interpreted relative to certBaseDir. For more information see https://github.com/go-sql-driver/mysql/#tls

Jump to

Keyboard shortcuts

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