sqlabst

package module
v0.0.0-...-b8bcdad Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2022 License: MIT Imports: 4 Imported by: 1

README

nurcahyaari Go Reference

Table of Content

  1. sqlabst
  2. Getting started
  3. Usage

SQLABST

sqlabst is the acronym for SQL Abstraction, this is a simple sql abstraction to join sqlx.Tx and sqlx.DB in the same interface. under the hood sqlabst using sqlx

Getting started

This is an example to how to use this project locally

Installation

go get github.com/nurcahyaari/sqlabst

Usage

sqlabst is an abstraction from sqlx, so all of the sqlx's function is supported on sqlabst. the difference is only how sqlabst treats a transaction. because sqlabst combines sqlx.DB and sqlx.Tx, when you call a function that implements on sqlx.DB and sqlx.Tx it's by default called the sqlx.DB's function. but if you start the transaction the function will call its sqlx.Tx own

example:


// start transaction

type Product struct {
	ProductId int64  `db:"product_id"`
	Name      string `db:"name"`
}

type ProductList []*Product

func main() {
    log.Info().Msg("Initialize Mysql connection")

	dbHost := ""
	dbPort := ""
	dbName := ""
	dbUser := ""
	dbPass := ""

	sHost := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", dbUser, dbPass, dbHost, dbPort, dbName)

	db, err := sqlx.Connect("mysql", sHost)

	if err != nil {
		log.Err(err).Msgf("Error to loading Database %s", err)
		panic(err)
	}

	log.Info().Str("Name", dbName).Msg("Success connect to DB")

    sqlabst := sqlabst.NewSqlAbst(db)

    // this query will be fetch on sqlx.DB
    var productList ProductList
	query := "SELECT product_id, name FROM products WHERE product_id IN (?)"

	query, args, err := sqlx.In(query, id)
	if err != nil {
		return
	}

	err = sqlabst.SelectContext(ctx,
		&productList,
		query,
		args...)

    query := "INSERT INTO products (name) VALUES (?)"
	sqlabst.ExecContext(ctx, query, product.Name)


    //this query will be fetch on sqlx.Tx
    sqlabst.Beginx() // when transactions is started by default all of the functions that implement from sqlx.DB and sqlx.Tx will call the sqlx.Tx

    var productList ProductList
	query := "SELECT product_id, name FROM products WHERE product_id IN (?)"

	query, args, err := sqlx.In(query, id)
	if err != nil {
        sqlabst.Rollback()
		return
	}

	err = sqlabst.SelectContext(ctx,
		&productList,
		query,
		args...)
    if err != nil {
        sqlabst.Rollback()
		return
	}

    query := "INSERT INTO products (name) VALUES (?)"
	_, err = sqlabst.ExecContext(ctx, query, product.Name)
    if err != nil {
        sqlabst.Rollback()
        return
	}
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SqlAbst

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

func NewSqlAbst

func NewSqlAbst(db *sqlx.DB) *SqlAbst

func (*SqlAbst) Begin

func (s *SqlAbst) Begin() error

Begin begins a transaction and set the Tx object

This abstraction resulting sqlx.Tx cause the implementation using sqlx.Tx instead of sql.Tx

func (*SqlAbst) BeginTx

func (s *SqlAbst) BeginTx(ctx context.Context, opts *sql.TxOptions) error

BeginTx begins a transaction and set the Tx object

This abstraction resulting sqlx.Tx cause the implementation using sqlx.Tx instead of sql.Tx

func (*SqlAbst) BeginTxx

func (s *SqlAbst) BeginTxx(ctx context.Context, opts *sql.TxOptions) error

BeginTxx begins a transaction and set the Tx object

func (*SqlAbst) Beginx

func (s *SqlAbst) Beginx() error

Beginx begins a transaction and set the Tx object

func (*SqlAbst) Commit

func (s *SqlAbst) Commit() error

Commit commits the transaction and set nil to Tx object

func (SqlAbst) Exec

func (s SqlAbst) Exec(query string, args ...interface{}) (sql.Result, error)

func (SqlAbst) ExecContext

func (s SqlAbst) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (SqlAbst) Get

func (s SqlAbst) Get(dest interface{}, query string, args ...interface{}) error

func (SqlAbst) GetContext

func (s SqlAbst) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

func (SqlAbst) GetDB

func (s SqlAbst) GetDB() *sqlx.DB

func (*SqlAbst) MustBegin

func (s *SqlAbst) MustBegin()

MustBegin begins a transaction and set the Tx object

func (*SqlAbst) MustBeginTx

func (s *SqlAbst) MustBeginTx(ctx context.Context, opts *sql.TxOptions)

MustBeginTx begins a transaction and set the Tx object

func (SqlAbst) MustExec

func (s SqlAbst) MustExec(query string, args ...interface{}) sql.Result

func (SqlAbst) MustExecContext

func (s SqlAbst) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result

func (SqlAbst) NamedExec

func (s SqlAbst) NamedExec(query string, arg interface{}) (sql.Result, error)

func (SqlAbst) NamedExecContext

func (s SqlAbst) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)

func (SqlAbst) NamedQuery

func (s SqlAbst) NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)

func (SqlAbst) Prepare

func (s SqlAbst) Prepare(query string) (*sql.Stmt, error)

func (SqlAbst) PrepareContext

func (s SqlAbst) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

func (SqlAbst) PrepareNamed

func (s SqlAbst) PrepareNamed(query string) (*sqlx.NamedStmt, error)

func (SqlAbst) PrepareNamedContext

func (s SqlAbst) PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error)

func (SqlAbst) Preparex

func (s SqlAbst) Preparex(query string) (*sqlx.Stmt, error)

func (SqlAbst) PreparexContext

func (s SqlAbst) PreparexContext(ctx context.Context, query string) (*sqlx.Stmt, error)

func (SqlAbst) Query

func (s SqlAbst) Query(query string, args ...interface{}) (*sql.Rows, error)

func (SqlAbst) QueryContext

func (s SqlAbst) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (SqlAbst) QueryRow

func (s SqlAbst) QueryRow(query string, args ...interface{}) *sql.Row

func (SqlAbst) QueryRowContext

func (s SqlAbst) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

func (SqlAbst) QueryRowx

func (s SqlAbst) QueryRowx(query string, args ...interface{}) *sqlx.Row

func (SqlAbst) QueryRowxContext

func (s SqlAbst) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row

func (SqlAbst) Queryx

func (s SqlAbst) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)

func (SqlAbst) QueryxContext

func (s SqlAbst) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)

func (SqlAbst) Rebind

func (s SqlAbst) Rebind(query string) string

func (*SqlAbst) Rollback

func (s *SqlAbst) Rollback() error

Rollback aborts the transaction and set nil to Tx object

func (SqlAbst) Select

func (s SqlAbst) Select(dest interface{}, query string, args ...interface{}) error

func (SqlAbst) SelectContext

func (s SqlAbst) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

Jump to

Keyboard shortcuts

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