dalc

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2020 License: GPL-3.0 Imports: 9 Imported by: 0

README

dalc

Database access common layer for go.

Feature

  • Simple.
  • No-reflect cost.
  • Using callback function to decrease range times.
  • Expandability.
  • It is more convenient to use with dalg.

Usage

go get -u github.com/pharosnet/dalc

type SomeTableRow struct {

    // Fields mapped database columns
    // when using dalc.Scan(), then add col tag 
    Id string `col:"id"`

}

// insert func
// ctx := dal.WithPreparer(parentCtx, tx)
func Insert(ctx context.Context, rows... SomeTableInsert) (affected int64, err error) {

	affected, err = dalc.Execute(ctx, insertSql, 
		func(ctx context.Context, stmt *sql.Stmt, row interface{}) (result sql.Result, err error) {
		    someTableRow, ok := row.(*SomeTableRow)
		    if !ok {
		    	// 
		    }
		    result, err = stmt.ExecContext(ctx, someTableRow.Fields...)
		    return
	}, rows...)

	// other logic code

	return 
}

// ctx := dal.WithPreparer(parentCtx, tx or db)
func UseList() {

	someViews := make([]*SomeView , 0, 1)

	err = dalc.Query(ctx, ql, func(ctx context.Context, rows *sql.Rows, rowErr error) error {

			// check rowErr

			// rows -> SomeTableRow -> someView or use dalc.Scan(rows, &view)

			// someViews append someView
            return
	}, args...)

	// ...

}

License

GNU GENERAL PUBLIC LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute(ctx context.Context, query string, fn ExecuteFunc, rows ...interface{}) (affected int64, err error)

func Query

func Query(ctx context.Context, query string, fn QueryScanRangeFn, args ...interface{}) (err error)

func Scan

func Scan(rows *sql.Rows, v interface{}) (err error)

func SetLog

func SetLog(logger Log)

func WithPreparable

func WithPreparable(parent context.Context, p Preparable) context.Context

Types

type ExecuteFunc

type ExecuteFunc func(ctx context.Context, stmt *sql.Stmt, row interface{}) (result sql.Result, err error)

type Log

type Log interface {
	Printf(formatter string, args ...interface{})
}

type NullJson

type NullJson struct {
	Bytes []byte `json:"-"`
	Valid bool   `json:"-"`
}

func NewJson

func NewJson(v interface{}) (*NullJson, error)

type NullTime

type NullTime struct {
	Time  time.Time
	Valid bool
}

func NowTime

func NowTime() *NullTime

func (*NullTime) Scan

func (n *NullTime) Scan(value interface{}) error

func (NullTime) Value

func (n NullTime) Value() (driver.Value, error)

type Preparable

type Preparable interface {
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

type QueryScanRangeFn

type QueryScanRangeFn func(ctx context.Context, rows *sql.Rows, rowErr error) (err error)

type Varchar added in v1.3.0

type Varchar []uint8

func NewVarchar added in v1.3.1

func NewVarchar(s string) Varchar

func (Varchar) String added in v1.3.0

func (v Varchar) String() string

Directories

Path Synopsis
cmd
dalc Module

Jump to

Keyboard shortcuts

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