db

package
v0.17.3 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2021 License: MIT Imports: 11 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 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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