database

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2019 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDuplicateEntry = errors.New("duplicate entry found in database")

ErrDuplicateEntry is an error that occurs when a duplicate is saved to the DB.

View Source
var ErrNoResults = errors.New("no results found in database")

ErrNoResults is returned when you can't find a result for a search in the database.

View Source
var ErrObjectNotSaved = errors.New(
	"object cannot be updated without first being saved",
)

ErrObjectNotSaved is an error that occurs when a object is attempted to be updated but hasn't been saved yet.

Functions

func PostgresConnection

func PostgresConnection(connectionString string) (*sqlx.DB, error)

Types

type Manager

type Manager struct {
	*sqlx.DB
	// contains filtered or unexported fields
}

Manager is a database wrapper with a few helpful tools for working with objects.

func GetDatabaseManager

func GetDatabaseManager(
	databaseType dbtype.Type,
	username,
	password,
	serverAddress,
	port,
	dbName string,
) (*Manager, error)

GetDatabaseManager will init and retrieve a mysql database.

func NewManager

func NewManager() (*Manager, error)

func NewManagerFromExisting

func NewManagerFromExisting(
	databaseType dbtype.Type,
	existing *sql.DB,
	databaseName string,
) (*Manager, error)

NewManagerFromExisting will create a new Manager from an existing database.

func NewManagerFromPool

func NewManagerFromPool(connPool *pool.ConnectionPool) (*Manager, error)

func (*Manager) CreateTableForObject

func (m *Manager) CreateTableForObject(obj Object) error

CreateTableForObject tries to create a table for an object if it doesn't exist. It will read sqlType tags if available or try to infer based on type reflection what type of column to create. TODO: Handle non-mysql databases.

func (*Manager) DeleteObject

func (m *Manager) DeleteObject(obj Object) error

DeleteObject will delete an object.

func (*Manager) FindObject

func (m *Manager) FindObject(obj IDObject) error

FindObject finds an object by intelligently reading attributes of provided object.

func (*Manager) FindObjects

func (m *Manager) FindObjects(
	queryObject Object,
) (interface{}, error)

FindObjects will find all objects matching queryObject parameter and return an interface representing a pointer to a slice of pointers to the type of the queryObject with the values found. Ex: queryObject is a Post then the return value will be a *[]*Post.

func (*Manager) GetObject

func (m *Manager) GetObject(obj Object, id ID) error

GetObject gets an object by its ID.

func (*Manager) SaveObject

func (m *Manager) SaveObject(obj Object) error

SaveObject automatically saves an object to the database. TODO: Consider converting resolved name into snake case and using as

database table if database table unset.

func (*Manager) Transactionized

func (m *Manager) Transactionized(
	fn func(*sqlx.Tx) error,
) (err error)

Transactionized wraps the provided function in a transaction that automatically translates errors and rolls back any transactions in case of failure.

func (*Manager) UpdateObject

func (m *Manager) UpdateObject(obj Object) error

UpdateObject will take an object and write an appropriate update statement to update it's values.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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