databases

package module
v0.0.0-...-11e646a Latest Latest
Warning

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

Go to latest
Published: May 14, 2016 License: MIT Imports: 3 Imported by: 1

README

databases

Features

  • Paginator - iterate through entities in the Database by using Offset and Limit in the background to "page" through your entries but in your code all you need is to call Next()
  • Sql Builders (JOINS not yet supported)
  • Dialect abstraction

Code generator

For a sql model with code generator see gensql. It currently generates entities, iterators and repositories.

Usage of DeferredRollbackIfNotHandled

This method should be used with the defer keyword. You can almost think of it like the native golang os.File method Close() that we defer so often. This is a typical file open/close example:

file, err := os.Open("/file/path")
if err != nil {
    log.Fatal(err)
}

doSomething()

defer file.Close()

And now we can use DeferredRollbackIfNotHandled the same way:

tx, err := db.BeginTx()
if err != nil {
    log.Fatalf("Cannot begin transaction, error: %s", err.Error())
}

doSomething()

defer tx.DeferredRollbackIfNotHandled()

In this example if we did a Commit/Rollback as part of doSomething, the DeferredRollbackIfNotHandled would actually do nothing. If we however did not do anything, the transaction will automatically (at defer time) be rolled back.

Note that the DeferredRollbackIfNotHandled also returns an error (which would come from the Rollback), if we want to act upon that we could do this:

tx, err := db.BeginTx()
if err != nil {
    log.Fatalf("Cannot begin transaction, error: %s", err.Error())
}

doSomething()

defer func(){
    if err = tx.DeferredRollbackIfNotHandled(); err != nil {
        //Handle the deferred (rollback) error here
    }
}()

Acknowledgments

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database interface {
	IsErrNoRows(err error) bool

	Select(dest interface{}, query string, args ...interface{}) error
	Exec(query string, args ...interface{}) (ExecResult, error)
	QueryRow(query string, args ...interface{}) ResultRow
	Query(query string, args ...interface{}) (ResultRows, error)

	BeginTx() (Database, error)
	CommitTx() error
	RollbackTx() error

	//DeferredRollbackIfNotHandled must be used with the `defer` keyword to finish a transaction - see github repo readme for usage example
	DeferredRollbackIfNotHandled() error
}

type Dialect

type Dialect interface {
	sqlbuilder.Dialect
}
var (
	MysqlDialect    Dialect = &mysql.Dialect{}
	SqliteDialect   Dialect = &mysql.Dialect{} //Currently same as mysql
	PostgresDialect Dialect = &postgres.Dialect{}
)

type ExecResult

type ExecResult interface {
	LastInsertId() (int64, error)
	RowsAffected() (int64, error)
}

type ResultRow

type ResultRow interface {
	Scan(dest ...interface{}) error
}

type ResultRows

type ResultRows interface {
	Next() bool
	StructScan(dest interface{}) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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