Documentation ¶
Index ¶
- func IsAlreadyExistsError(err error) bool
- func IsEmptyResultSetError(err error) bool
- type Connection
- func (c *Connection) BatchInsert(tableName string, count int, mapFn func(int) map[string]interface{}) error
- func (c *Connection) CheckSchema() error
- func (c *Connection) Delete(dq squirrel.DeleteBuilder) error
- func (c *Connection) Exec(query string, values ...interface{}) error
- func (c *Connection) Get(model interface{}, sb squirrel.SelectBuilder) error
- func (c *Connection) Insert(tableName string, valueMap map[string]interface{}) (int64, error)
- func (c *Connection) MigrateUp() error
- func (c *Connection) NewTransaction(ctx context.Context) (*Transactable, error)
- func (c *Connection) RetrieveUserByID(userID int64) (models.User, error)
- func (c *Connection) RetrieveUserBySlug(slug string) (models.User, error)
- func (c *Connection) RetrieveUserIDBySlug(slug string) (int64, error)
- func (c *Connection) RetrieveUserWithAuthDataByID(userID int64) (models.UserWithAuthData, error)
- func (c *Connection) RetrieveUserWithAuthDataBySlug(slug string) (models.UserWithAuthData, error)
- func (c *Connection) Select(modelSlice interface{}, sb squirrel.SelectBuilder) error
- func (c *Connection) Update(ub squirrel.UpdateBuilder) error
- func (c *Connection) WithTx(ctx context.Context, fn func(tx *Transactable)) error
- type Transactable
- func (tx *Transactable) BatchInsert(tableName string, count int, mapFn func(int) map[string]interface{}) error
- func (tx *Transactable) Commit() error
- func (tx *Transactable) Delete(dq squirrel.DeleteBuilder) error
- func (tx *Transactable) Error() error
- func (tx *Transactable) Exec(s squirrel.Sqlizer) error
- func (tx *Transactable) Get(model interface{}, sb squirrel.SelectBuilder) error
- func (tx *Transactable) Insert(tableName string, valueMap map[string]interface{}) (int64, error)
- func (tx *Transactable) Rollback() error
- func (tx *Transactable) Select(modelSlice interface{}, sb squirrel.SelectBuilder) error
- func (tx *Transactable) Update(ub squirrel.UpdateBuilder) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAlreadyExistsError ¶
IsAlreadyExistsError returns true if the passed error is a database error resulting from attempting to insert a row with a unique/primary key that already exists
func IsEmptyResultSetError ¶
IsEmptyResultSetError returns true if the passed error is a database error resulting from querying a table expecting 1 row (with db.Get) but recieving 0
Types ¶
type Connection ¶
Connection contains the infrastructure needed to manage the database connection
func NewConnection ¶
func NewConnection(dsn string, migrationsDirPath string) (*Connection, error)
NewConnection establishes a new connection to the databse server
func NewTestConnection ¶
func NewTestConnection(t *testing.T, dbName string) *Connection
NewTestConnection creates a new empty database, and applies for integration/service tests
func NewTestConnectionFromNonStandardMigrationPath ¶
func NewTestConnectionFromNonStandardMigrationPath(t *testing.T, dbName, migrationsDirPath string) *Connection
NewTestConnectionFromNonStandardMigrationPath is identical to NewTestConnection, but allows the user to specify where the migrations dir is
func (*Connection) BatchInsert ¶
func (c *Connection) BatchInsert(tableName string, count int, mapFn func(int) map[string]interface{}) error
BatchInsert is similar to Insert, but instead is designed for multiple inserts. Note that only a single SQL query is run here.
Parameters:
tableName: the name of the target table count: the number of items needed to insert mapFn: A function that produces a single set of values for a new database row.
Note that this will be called <count> times
Returns: an error if the insert fails
func (*Connection) CheckSchema ¶
func (c *Connection) CheckSchema() error
CheckSchema checks the database schema against the migrations and returns an error if they don't match
func (*Connection) Delete ¶
func (c *Connection) Delete(dq squirrel.DeleteBuilder) error
Delete removes records indicated by the given DeleteBuilder
func (*Connection) Exec ¶
func (c *Connection) Exec(query string, values ...interface{}) error
Exec wraps sqlx.Exec, adding multi-value support and query logging
func (*Connection) Get ¶
func (c *Connection) Get(model interface{}, sb squirrel.SelectBuilder) error
Get executes the provided SelectBuilder query, and marshals the response into the provided structure. Note: this is for retriving a single value -- i.e. not a row, but a cell
func (*Connection) Insert ¶
func (c *Connection) Insert(tableName string, valueMap map[string]interface{}) (int64, error)
Insert provides a generic way to insert a single row into the database. The function expects the table to insert into as well as a map of columnName : columnValue. Also assumes that created_at and updated_at columns exist (And are updated with the current time) Returns the inserted record id, or an error if the insert fails
func (*Connection) MigrateUp ¶
func (c *Connection) MigrateUp() error
MigrateUp performs all migrations
func (*Connection) NewTransaction ¶
func (c *Connection) NewTransaction(ctx context.Context) (*Transactable, error)
NewTransaction creates a new Transactable, which can then be used to execute queries in that transaction. Note that this is NOT the preferred method. Typically, the WithTx method should be used, as this will ensure that no db resources are lost.
func (*Connection) RetrieveUserByID ¶
func (c *Connection) RetrieveUserByID(userID int64) (models.User, error)
RetrieveUserByID retrieves a full user from the users table given a user ID
func (*Connection) RetrieveUserBySlug ¶
func (c *Connection) RetrieveUserBySlug(slug string) (models.User, error)
RetrieveUserBySlug retrieves a full user from the users table give a user slug
func (*Connection) RetrieveUserIDBySlug ¶
func (c *Connection) RetrieveUserIDBySlug(slug string) (int64, error)
RetrieveUserIDBySlug retrieves a user's ID from a given slug. Likely faster than retriving the full record, so this is preferred if all you need to the slug/id conversion
func (*Connection) RetrieveUserWithAuthDataByID ¶
func (c *Connection) RetrieveUserWithAuthDataByID(userID int64) (models.UserWithAuthData, error)
RetrieveUserWithAuthDataByID retrieves a full user from the users table given that user's ID. Includes data from the auth_scheme_data table (namely, scheme names)
func (*Connection) RetrieveUserWithAuthDataBySlug ¶
func (c *Connection) RetrieveUserWithAuthDataBySlug(slug string) (models.UserWithAuthData, error)
RetrieveUserWithAuthDataBySlug retrieves a full user from the users table given a slug. Includes data from the auth_scheme_data table (namely, scheme names)
func (*Connection) Select ¶
func (c *Connection) Select(modelSlice interface{}, sb squirrel.SelectBuilder) error
Select executes the provided SelectBuilder query, and marshals the response into the provided slice. Note: this is for retriving multiple results, or "rows"
func (*Connection) Update ¶
func (c *Connection) Update(ub squirrel.UpdateBuilder) error
Update executes the provided UpdateBuilder, in addition to the "updated_at" field, using the server time.
func (*Connection) WithTx ¶
func (c *Connection) WithTx(ctx context.Context, fn func(tx *Transactable)) error
WithTx provides a scoped route to executing queries inside a transaction. Simply pass a function that uses the provided transaction to this method. At the end of the function, Commit will be called, which will either apply those actions to the database, or roll them back to the initial state
Note 1: The provided function will not _necessarily_ be called. In situations where a transaction cannot be created, the function will be bypassed. Note 2: If the provided function needs to be exited early, you can call Rollback or Commit to ensure the desired outcome, and then return from the function as usual.
type Transactable ¶
type Transactable struct {
// contains filtered or unexported fields
}
Transactable is a wrapped sql.Tx (sql transaction). It provides two pieces of functionality. First: It allows execution of standard sql methods. These are all done in a transaction, on a single connection to the database -- the result being that either all of the queries succeed, or none of them do. Ultimately, this helps prevent the database from getting into an inconsistent state Second: this structure helps track errors that occur during the execution process. Any error that occurs during this processing is recorded, and can be retrieved by using the Error method. Internally, this is also used to determine whether we should commit the changes, or roll them back. Note that any error that occurs short-circuits any other call to be a no-op (except Rollback and Commit)
func (*Transactable) BatchInsert ¶
func (tx *Transactable) BatchInsert(tableName string, count int, mapFn func(int) map[string]interface{}) error
BatchInsert executes a SQL INSERT query, for multiple value sets. This executes only a single query, but allows the caller to provide multiple db rows. The mapFn parameter should return back data for the i'th row to be added Returns an error if an error has been encountered.
func (*Transactable) Commit ¶
func (tx *Transactable) Commit() error
Commit attempts to apply the transaction to the database. If an error has been encountered, this method will instead attempt to rollback the transaction. An error is returned if any part of the transaction failed to execute, including the commit itself.
func (*Transactable) Delete ¶
func (tx *Transactable) Delete(dq squirrel.DeleteBuilder) error
Delete executes a SQL DELETE query
func (*Transactable) Error ¶
func (tx *Transactable) Error() error
Error retrieves the recorded error, if any (nil otherwise)
func (*Transactable) Exec ¶
func (tx *Transactable) Exec(s squirrel.Sqlizer) error
Exec exectues any SQL query. Used primarily in situations where you need to interact with the dbms directly, or in rare situations where squirrel does not provide sufficient query modeling capability
func (*Transactable) Get ¶
func (tx *Transactable) Get(model interface{}, sb squirrel.SelectBuilder) error
Get executes a SQL SELECT query given a reference to the database model and the squirrel SelectBuilder that returns a single row. If no rows are returned, or multiple rows are returned from the SQL query, an error is returned from this method.
func (*Transactable) Insert ¶
func (tx *Transactable) Insert(tableName string, valueMap map[string]interface{}) (int64, error)
Insert executes a SQL INSERT query, given the tablename along with a columnName:value map. Returns the new row ID added (via LastInsertId), along with an error, if one was encountered
func (*Transactable) Rollback ¶
func (tx *Transactable) Rollback() error
Rollback aborts the transaction, in effect rolling the database back to its initial state. Returns an error if the rollback encounters an error itself
func (*Transactable) Select ¶
func (tx *Transactable) Select(modelSlice interface{}, sb squirrel.SelectBuilder) error
Select executes a SQL SELECT query, given the provided squirrel SelectBuilder and a reference to the database model. Results will be stored in the reference if any rows are returned.
func (*Transactable) Update ¶
func (tx *Transactable) Update(ub squirrel.UpdateBuilder) error
Update executes a SQL UPDATE query, and sets the updated_at column to the server time