db

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2024 License: MIT Imports: 12 Imported by: 4

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckColumns

func CheckColumns(v interface{}, cols ...string) error

CheckColumns will return a non-nil error if one or more columns from cols is missing from the underlying struct.

Example
s := struct {
	ID          int    `json:"id" db:"-"`                // omit
	Name        string `json:"name"`                     // column: name
	Age         int    `json:"age"`                      // column: age
	Address     string `json:"address"`                  // column: address
	PhoneNumber string `json:"phone_number" db:"number"` // column: number
}{
	10,
	"Susan",
	14,
	"123 Main Street",
	"111-222-4567",
}

err := CheckColumns(s, "name", "age", "address", "number")
if err != nil {
	fmt.Println(err)
}

err = CheckColumns(s, "id", "phone_number")
if err != nil {
	fmt.Println(err)
}
Output:

columns not found: id, phone_number

func Columns

func Columns(v interface{}) (cols []string)

Columns returns all the struct table columns according to the 'db' and 'json' meta tag values.

func MissingColumns

func MissingColumns(v interface{}, cols ...string) (missing []string)

MissingColumns returns a list of columns from cols that were not found in the provided struct.

func MySQL

func MySQL(un, pass, host, dbName string) (*sql.DB, error)

MySQL is a convenience initializer to obtain a MySQL DB connection.

Note that this connection has an option to set transaction isolation level to 'serializable' to enforce more true atomic batch loading.

func MySQLTx added in v0.4.0

func MySQLTx(un, pass, host, dbName string, serializable bool) (*sql.DB, error)

MySQLTx is a convenience initializer to obtain a MySQL DB connection.

Note that this connection will set the default transaction isolation level to 'serializable' to enforce more true atomic batch loading.

func PGSSL added in v0.19.0

func PGSSL(user, pass, host, dbName, sslMode, cert, key, caCert string) (*sql.DB, error)

PGSSL is a convenience initializer to obtain a Postgres DB connection using ssl certs

Note that this connection will set the default transaction isolation level to 'serializable' to enforce more true atomic batch loading.

func PGx added in v0.19.0

func PGx(user, pass, host, dbName string) (*sqlx.DB, error)

PGx is a convenience initializer to obtain a Postgres sqlx.DB connection

Note that this connection will set the default transaction isolation level to 'serializable' to enforce more true atomic batch loading.

func PGxSSL added in v0.19.0

func PGxSSL(user, pass, host, dbName, sslMode, cert, key, caCert string) (*sqlx.DB, error)

PGxSSL is a convenience initializer to obtain a Postgres sqlx.DB connection using ssl certs

Note that this connection will set the default transaction isolation level to 'serializable' to enforce more true atomic batch loading.

func Postgres

func Postgres(un, pass, host, dbName string) (*sql.DB, error)

Postgres is a convenience initializer to obtain a Postgres DB connection.

Note that this connection will set the default transaction isolation level to 'serializable' to enforce more true atomic batch loading.

func PostgresTx added in v0.4.0

func PostgresTx(un, pass, host, dbName string, serializable bool) (*sql.DB, error)

PostgresTx is a convenience initializer to obtain a Postgres DB connection

Note that this connection has an option to set transaction isolation level to 'serializable' to enforce more true atomic batch loading.

func Values

func Values(v interface{}, cols ...string) (row []interface{})

Values prepares a row insert for struct v for the provided columns.

Example
s := struct {
	ID   int    `db:"id"`
	Name string `db:"name"`
	Age  int    `db:"age"`
}{
	1,
	"Albert",
	42,
}
fmt.Println(Values(s, "id", "age", "name"))
Output:

[1 42 Albert]

Types

type BatchLoader

type BatchLoader interface {
	// Delete takes a delete query string with optional vals values
	// and will be executed in the transaction before bulk inserts.
	// The delete will be rolled back if there was a problem anywhere
	// during the transaction.
	//
	// The delete statement will not be executed until Commit is
	// called.
	//
	// If query does not end with a ';' to end the statement then
	// a semicolon will be added. (necessary?)
	Delete(query string, vals ...interface{})

	// AddRow will add a row to the totals rows that will be prepared,
	// executed and committed when Commit is called. No validation is performed
	// when calling AddRow but if the len of any row provided to AddRow != len(cols)
	// then Commit will return an error without starting the transaction.
	// Other types of errors, such as problems with the row values will be detected
	// by the specific db server or by the underlying go adapter. Either way, such
	// errors will be detected and returned only after a call to Commit.
	AddRow(row []interface{})

	// Commit will execute the delete query and efficiently insert all rows. The
	// delete and inserts will all occur in a single transaction. If there is
	// a problem during the transaction then the transaction will be rolled back.
	//
	// In the presence of a delete query the stat.Stats will do its best to
	// populate the number of rows deleted from the underlying adapter.
	//
	// Cancelling ctx will cancel the transaction and rollback. A cancelled context
	// will result in Commit returning a non-nil error.
	//
	// Calling Commit more than once is allowed and will repeat the entire transaction.
	//
	// The order of cols is important and must match the order of row values when
	// calling AddRow.
	Commit(ctx context.Context, tableName string, cols ...string) (batch.Stats, error)
}

BatchLoader implementations should have an initializer that also pings the db to check the connection.

func NewBatchLoader

func NewBatchLoader(dbType string, sqlDB *sql.DB) BatchLoader

NewBatchLoader will create a BatchLoader. dbType should be: * "postgres" for Postgres loading * "mysql" for MySQL loading * "nop" for using the nop batch loader "nop://", "nop://commit_err"

Other adapters have not been tested but will likely work if they support transactions and the '?' execution placeholder value.

type Options added in v0.26.0

type Options struct {
	Username     string `toml:"username" commented:"true"`
	Password     string `toml:"password" commented:"true"`
	Host         string `toml:"host" comment:"host can be 'host:port', 'host', 'host:' or ':port'"`
	DBName       string `toml:"dbname"`
	Serializable bool   `toml:"serializable" comment:"set isolation level to serializable, required for proper writing to database" commented:"true"`
	SSL          SSL    `toml:"SSL"`
}

func (Options) MySQL added in v0.26.0

func (o Options) MySQL() (*sql.DB, error)

func (*Options) PG added in v0.26.0

func (o *Options) PG() (*sql.DB, error)

func (Options) PGx added in v0.26.0

func (o Options) PGx() (*sqlx.DB, error)

type SSL added in v0.26.0

type SSL struct {
	// Mode-PG: disable, allow, prefer, *require*, verify-ca, verify-full
	// Mode-MySQL: DISABLED, PREFERRED, REQUIRED, VERIFY_CA, VERIFY_IDENTITY
	Mode     string `toml:"mode" comment:"require is set if certs found"`
	Cert     string `toml:"cert"`
	Key      string `toml:"key"`
	Rootcert string `toml:"rootcert"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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