octobe

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2021 License: MIT Imports: 3 Imported by: 0

README

Alt text

License Tag Version Codacy Badge

A slim golang package for programmers that love to write raw SQL, but has a problem with boilerplate code. This package will help you structure and unify the way you work with your database.

https://pkg.golang.ir/github.com/Kansuler/octobe

Usage

Basic

Run a simple query

func Method(db *sql.DB, ctx context.Context) (p product, err error) {
  ob := octobe.New(db, octobe.SuppressError(sql.ErrNoRows))
  scheme := ob.Begin(ctx)
  
  seg := scheme.Segment(`
    SELECT name FROM products WHERE id = $1;
  `)
  
  seg.Arguments(1)
  
  err = seg.QueryRow(&p.Name)
  return
}
Transaction

Run a query with transaction, and with a handler.

// InsertProduct will take a pointer of a product, and insert it
// This method could be in a separate package.
func InsertProduct(p *Product) octobe.Handler {
  return func(scheme *octobe.Scheme) error {
    seg := scheme.Segment(`
      INSERT INTO
        products(name)
      VALUES($1)
      RETURNING id
    `)

    seg.Arguments(p.Name)

    return seg.Insert(&p.ID)
  }
}

func Method(db *sql.DB, ctx context.Context) error {
  ob := octobe.New(db)
  scheme, err := ob.BeginTx(ctx)
  if err != nil {
    return err
  }
  
  defer tx.WatchRollback(func() error {
    return err
  })
  
  p := Product{Name: "home made baguette"}
  err = scheme.Handle(InsertProduct(&p))
  if err != nil {
    return err
  }
  
  return scheme.Commit()
}
WatchTransaction

This is a method that can watch the whole transaction, and where you don't have to define rollback or commit.

// InsertProduct will take a pointer of a product, and insert it
// This method could be in a separate package.
func InsertProduct(p *Product) octobe.Handler {
  return func(scheme *octobe.Scheme) error {
    seg := scheme.Segment(`
      INSERT INTO
        products(name)
      VALUES($1)
      RETURNING id
    `)

    seg.Arguments(p.Name)

    return seg.Insert(&p.ID)
  }
}

func Method(db *sql.DB, ctx context.Context) error {
  ob := octobe.New(db)
  
  p := Product{Name: "home made baguette"}
  return ob.WatchTransaction(ctx, func(scheme *octobe.Scheme) error {
    return scheme.Handle(InsertProduct(&p))
  })
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNeedInput = errors.New("insert method require at least one argument")
View Source
var ErrUsed = errors.New("this query has already executed")

ErrUsed is an error that emits if used is true on Segment.

Functions

This section is empty.

Types

type Handler

type Handler func(scheme *Scheme) error

Handler is a signature that can be used for handling database segments in a separate function

type Octobe

type Octobe struct {
	// DB is the database instance
	DB *sql.DB
	// contains filtered or unexported fields
}

Octobe struct that holds the database session

func New

func New(db *sql.DB, opts ...Option) Octobe

New initiates a DB instance and connection.

func (Octobe) Begin

func (ob Octobe) Begin(ctx context.Context) (scheme Scheme)

Begin initiates a query run, but not as a transaction

func (Octobe) BeginTx

func (ob Octobe) BeginTx(ctx context.Context, opts ...*sql.TxOptions) (scheme Scheme, err error)

BeginTx initiates a transaction against database

func (Octobe) WatchTransaction

func (ob Octobe) WatchTransaction(ctx context.Context, cb func(scheme *Scheme) error, opts ...*sql.TxOptions) error

WatchTransaction will perform the whole transaction, or do rollback if error occurred.

type Option added in v0.1.1

type Option interface {
	Type() string
}

Option interface that tells what type of option it is

func SuppressError added in v0.1.1

func SuppressError(err error) Option

SuppressError will tell the library to suppress the error if it occurs in this lib or sql libs

type Scheme

type Scheme struct {
	// contains filtered or unexported fields
}

Scheme holds context for the duration of the transaction

func (*Scheme) Commit

func (scheme *Scheme) Commit() error

Commit will commit a transaction

func (*Scheme) Handle

func (scheme *Scheme) Handle(handler Handler) error

Handle is a method that handle a handler

func (*Scheme) Rollback

func (scheme *Scheme) Rollback() error

Rollback will rollback a transaction

func (*Scheme) Segment

func (scheme *Scheme) Segment(query string) *Segment

Segment created a new query within a database transaction

func (*Scheme) WatchRollback

func (scheme *Scheme) WatchRollback(cb func() error)

WatchRollback will perform a rollback if an error is given This method can be used as a defer in the function that performs the database operations.

type Segment

type Segment struct {
	// contains filtered or unexported fields
}

Segment is a specific query that can be run only once it keeps a few fields for keeping track on the segment

func (*Segment) Arguments

func (segment *Segment) Arguments(args ...interface{})

Arguments receives unknown amount of arguments to use in the query

func (*Segment) Exec

func (segment *Segment) Exec() error

Exec will execute a query. Used for inserts or updates

func (*Segment) Insert

func (segment *Segment) Insert(dest ...interface{}) error

Insert will perform a query, and will also take destination pointers for returning data. Use Exec of Insert if you do not expect returning values Insert needs at least one argument, otherwise use Exec

func (*Segment) Query

func (segment *Segment) Query(cb func(*sql.Rows) error) error

Query will perform a normal query against database that returns rows

func (*Segment) QueryRow

func (segment *Segment) QueryRow(dest ...interface{}) error

QueryRow will return one result and put them into destination pointers

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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