dbm

package module
v0.0.0-...-57657e7 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2018 License: MIT Imports: 10 Imported by: 0

README

go-dbm Travis-CI GoDoc

Simple CRUD wrapper on *sql.DB with powerfull struct unpacking

go get github.com/RadekD/go-dbm

Usage

import "github.com/RadekD/go-dbm"

var crud *dbm.CRUD
func main() {
    var err error

    crud, err = dbm.Open("mysql", "root@tcp(127.0.0.1:3306)/test?collation=utf8mb4_unicode_ci&parseTime=true")
    if err != nil {
        log.Fatal("invalid connection")
    }
    var mystruct struct{
        Name string
    }
    err = crud.Select(&mystruct, "SELECT Name FROM test")
    if err != nil {
        log.Println("handle error")
    }
}

Licence

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidPkField = errors.New("crud: struct with invalid pk field")

ErrInvalidPkField means that provided struct do not have field with `db:",pk"` tag

View Source
var ErrNotAStruct = errors.New("crud: data not a struct")

ErrNotAStruct means that provided data is not a struct and insert cannot be performed

View Source
var ErrTooManyColumns = errors.New("crud: too many columns")

ErrTooManyColumns means that provided query in Select function returned more columns that holder can hold

View Source
var ErrTooManyRows = errors.New("crud: too many rows")

ErrTooManyRows means that provided query in Select function returned more rows that holder can hold usually that means that holder is not a slice or lacking a LIMIT in query

Functions

This section is empty.

Types

type CRUD

type CRUD struct {
	*sql.DB
	// contains filtered or unexported fields
}

func Open

func Open(driverName, dataSourceName string) (*CRUD, error)

Open uses sql.Open to create new pool and return CRUD instance

func (*CRUD) Begin

func (c *CRUD) Begin() (*CRUDTx, error)

Begin creates new transaction wrapped with CRUDTx struct

func (*CRUD) BeginTx

func (c *CRUD) BeginTx(ctx context.Context, opts *sql.TxOptions) (*CRUDTx, error)

BeginTx creates new transaction with given context and TxOptions wrapped with CRUDTx struct

func (*CRUD) Delete

func (c *CRUD) Delete(table string, data interface{}) (sql.Result, error)

Delete created and performs DELETE FROM table WHERE ... LIMIT 1 If data parameter is not a struct ErrNotAStruct will be returned If data do not have field with `db:",pk"` tag ErrInvalidPkField will be returned

func (*CRUD) DeleteContext

func (c *CRUD) DeleteContext(ctx context.Context, table string, data interface{}) (sql.Result, error)

DeleteContext created and performs DELETE FROM table WHERE ... LIMIT 1 with a given context If data parameter is not a struct ErrNotAStruct will be returned If data do not have field with `db:",pk"` tag ErrInvalidPkField will be returned

func (*CRUD) Insert

func (c *CRUD) Insert(table string, data interface{}) (sql.Result, error)

Insert created and performs INSERT INTO ... query If data parameter is not a struct ErrNotAStruct will be returned

func (*CRUD) InsertContext

func (c *CRUD) InsertContext(ctx context.Context, table string, data interface{}) (sql.Result, error)

InsertContext created and performs INSERT INTO ... query with a given context If data parameter is not a struct ErrNotAStruct will be returned

func (*CRUD) Select

func (c *CRUD) Select(holder interface{}, query string, args ...interface{}) error

Select is used for performing `SELECT` query and unpacking the result into `holder` Holder can be any type (byte, int, string, bool, struct) and slice of any type If holder is not a slice and query returns more than one row ErrTooManyRows will be returned If holder is byte, int, string or bool and query returns a more than one column ErrTooManyColumns will be returned

func (*CRUD) SelectContext

func (c *CRUD) SelectContext(ctx context.Context, holder interface{}, query string, args ...interface{}) error

SelectContext is used for performing `SELECT` query and unpacking the result into `holder` Holder can be any type (byte, int, string, bool, struct) and slice of any type If holder is not a slice and query returns more than one row ErrTooManyRows will be returned If holder is byte, int, string or bool and query returns a more than one column ErrTooManyColumns will be returned

func (*CRUD) Update

func (c *CRUD) Update(table string, data interface{}) (sql.Result, error)

Update create and performs UPDATE table SET ... WHERE `pk` = ? query If data parameter is not a struct ErrNotAStruct will be returned If data do not have field with `db:",pk"` tag ErrInvalidPkField will be returned

func (*CRUD) UpdateContext

func (c *CRUD) UpdateContext(ctx context.Context, table string, data interface{}) (sql.Result, error)

UpdateContext create and performs UPDATE table SET ... WHERE `pk` = ? LIMIT 1 query with a given context If data parameter is not a struct ErrNotAStruct will be returned If data do not have field with `db:",pk"` tag ErrInvalidPkField will be returned

type CRUDTx

type CRUDTx struct {
	CRUD
	*sql.Tx
	// contains filtered or unexported fields
}

CRUDTx hold transation creates by crud.Begin / crud.BeingTx

Jump to

Keyboard shortcuts

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