sqlv

package module
v0.0.0-...-a251415 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2020 License: MIT Imports: 11 Imported by: 0

README

sqlv

sqlv is a wrapper for sqlx where there is a master connection and multiple readonly/slave connections.

This is still in beta. Any Feedback / Suggestions will be appreciated. Have not preformed any go tests yet. Will add those soon.

Get started
// create master connection
master := sqlv.Connection{
    Username:     "master_username",
    Password:     "foo!bar",
    DatabaseName: "foo_bar_db",
    Host:         "192.168.0.1",
    Port:         "3306",
    Type:         "master",
    TLS: sqlv.TLSConfig{ // optional
        Enabled:    true,
        Cert:       "/etc/...",
        Key:        "/etc/...",
        CA:         "/etc/...",
        ServerName: "master_server_name",
    },
}

// create one or more slave connections to master

// slave 1
slaveOne := sqlv.Connection{
    Username:     "slave_username",
    Password:     "foo!bar",
    DatabaseName: "foo_bar_db",
    Host:         "192.168.0.2",
    Port:         "3306",
    Type:         "slave",
    TLS: sqlv.TLSConfig{ // optional
        Enabled:    true,
        Cert:       "/etc/...",
        Key:        "/etc/...",
        CA:         "/etc/...",
        ServerName: "slave_server_name_one",
    },
}

// slave 2
slaveTwo := sqlv.Connection{
    Username:     "slave_username",
    Password:     "foo!bar",
    DatabaseName: "foo_bar_db",
    Host:         "192.168.0.3",
    Port:         "3306",
    Type:         "slave",
    TLS: sqlv.TLSConfig{ // optional
        Enabled:    true,
        Cert:       "/etc/...",
        Key:        "/etc/...",
        CA:         "/etc/...",
        ServerName: "slave_server_name_two",
    },
}

// init mysql connections and assign global variable of 'db'
db, err = sqlv.Open([]sqlv.Connection{
    master,
    slaveOne,
    slaveTwo,
})
if err != nil {
    log.Fatalf("database connections failed: %s", err.Error())
}

use db as you would for sqlx. select and get will load balance between each slave instance and write operations will use the master connection. If no slave connections master will be used for all connections.

Documentation

Index

Constants

View Source
const (
	// Master read / write database
	Master = "master"

	// Slave or read only database
	Slave = "slave"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	Username     string
	Password     string
	DatabaseName string
	Host         string
	Port         int
	Type         string // master or slave
	TLS          TLSConfig
}

Connection connection details

type DB

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

DB database connection

func Open

func Open(connections []Connection) (*DB, error)

Open connections to mysql databases

func (*DB) Begin

func (db *DB) Begin() (*sql.Tx, error)

Begin starts a transaction on the master. The isolation level is dependent on the driver.

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a transaction with the provided context on the master.

func (*DB) BeginTxx

func (db *DB) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error)

BeginTxx starts a transaction with the provided context on the master.

func (*DB) Close

func (db *DB) Close() error

Close closes all physical databases releasing any open resources.

func (*DB) DatabaseMaster

func (db *DB) DatabaseMaster() *sql.DB

DatabaseMaster relates sql package master database connection

func (*DB) DatabaseSlave

func (db *DB) DatabaseSlave(i int) *sql.DB

DatabaseSlave relates sql package slave database connection

func (*DB) Exec

func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error)

Exec executes a query without returning any rows.

func (*DB) ExecContext

func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext executes a query without returning any rows.

func (*DB) Get

func (db *DB) Get(des interface{}, query string, args ...interface{}) error

Get gets a single row query into an interface

func (*DB) GetContext

func (db *DB) GetContext(ctx context.Context, des interface{}, query string, args ...interface{}) error

GetContext gets a single row query into an interface

func (*DB) InGetContext

func (db *DB) InGetContext(ctx context.Context, des interface{}, inQuery string, inArgs ...interface{}) error

In Get Context

func (*DB) InSelectContext

func (db *DB) InSelectContext(ctx context.Context, des interface{}, inQuery string, inArgs ...interface{}) error

In Select Context

func (*DB) Master

func (db *DB) Master() *sqlx.DB

Master returns the master physical database

func (*DB) Ping

func (db *DB) Ping() error

Ping verifies if a connection to each physical database is still alive,

func (*DB) PingContext

func (db *DB) PingContext(ctx context.Context) error

PingContext verifies if a connection to each physical database is still alive

func (*DB) Query

func (db *DB) Query(query string, args ...interface{}) (*sql.Rows, error)

Query executes a query that returns rows, typically a SELECT.

func (*DB) QueryContext

func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext executes a query that returns rows, typically a SELECT.

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...interface{}) *sql.Row

QueryRow executes a query that is expected to return at most one row.

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext executes a query that is expected to return at most one row.

func (*DB) QueryRowx

func (db *DB) QueryRowx(query string, args ...interface{}) *sqlx.Row

QueryRowx

func (*DB) QueryRowxContext

func (db *DB) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row

QueryRowxContext

func (*DB) Queryx

func (db *DB) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)

Queryx returns the query x

func (*DB) QueryxContext

func (db *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)

QueryxContext returns the query x with context

func (*DB) Select

func (db *DB) Select(des interface{}, query string, args ...interface{}) error

Select executes a query that is expected to return at most one row if not more

func (*DB) SelectContext

func (db *DB) SelectContext(ctx context.Context, des interface{}, query string, args ...interface{}) error

SelectContext executes a query that is expected to return at most one row if not more

func (*DB) SetConnMaxLifetime

func (db *DB) SetConnMaxLifetime(d time.Duration)

SetConnMaxLifetime sets the maximum amount of time a connection may be reused.

func (*DB) SetMaxIdleConns

func (db *DB) SetMaxIdleConns(n int)

SetMaxIdleConns sets the maximum number of connections in the idle

func (*DB) SetMaxOpenConns

func (db *DB) SetMaxOpenConns(n int)

SetMaxOpenConns sets the maximum number of open connections

func (*DB) Slave

func (db *DB) Slave() *sqlx.DB

Slave returns one of the physical databases which is a slave

type TLSConfig

type TLSConfig struct {
	Enabled    bool
	Cert       string
	Key        string
	CA         string
	ServerName string
}

TLSConfig tls connection details

Jump to

Keyboard shortcuts

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