sqldimel

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: BSD-3-Clause Imports: 5 Imported by: 0

README

sqldimel

SQLDimel is an SQL DML query builder for Golang.

It just generates the INSERT, UPDATE and DELETE queries to be passed to the underlining database.

Using a processor, it is possible to customize the way parameters are generated on the SQL (with ?, $1, etc).

Processors using "?" and "$1, $2" are included.

NOTE: when using WHERE, ALWAYS use "?", this is the default placeholder that will be replaced by the processor.

INSTALLATION

go get github.com/RangelReale/sqldimel

import "github.com/RangelReale/sqldimel"

USAGE

b := sqldimel.NewBuilder("user")
b.Add("id", 1).
	Add("name", "Monte Marto").
	Add("dob", time.Now()).
	Add("optional", nil).
	Add("weight", 80.2).
	Where("id = ? and weight > ?", 1, 70.2)

// db = *sql.DB
res, err = b.Exec(db, sqldimel.UPDATE)
// or
// res, err = db.Exec(b.Output(sqldimel.UPDATE), b.OutputParams(sqldimel.UPDATE))

Documentation

Overview

Package sqldimel provides a SQL DML query builder.

Package sqldimel provides a SQL DML query builder.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuildProcessorDefault

type BuildProcessorDefault struct {
}

func NewBuildProcessorDefault

func NewBuildProcessorDefault() *BuildProcessorDefault

BuildProcessorDefault generates parameters using the character "?"

func (*BuildProcessorDefault) BeginParams

func (p *BuildProcessorDefault) BeginParams()

func (*BuildProcessorDefault) NextParam

func (p *BuildProcessorDefault) NextParam(fieldname string) string

type BuildProcessorNumeric

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

func NewBuildProcessorNumeric

func NewBuildProcessorNumeric() *BuildProcessorNumeric

BuildProcessorNumeric generates parameters using the character "$" followed by a sequential number starting with 1

func (*BuildProcessorNumeric) BeginParams

func (p *BuildProcessorNumeric) BeginParams()

func (*BuildProcessorNumeric) NextParam

func (p *BuildProcessorNumeric) NextParam(fieldname string) (ret string)

type Builder

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

Builder is the generation class

func NewBuilder

func NewBuilder(table string) *Builder

Creates a new builder for the specified table, with a default processor

func NewBuilderProc

func NewBuilderProc(table string, processor BuilderProcessor) *Builder

Creates a new builder for the specified table and processor

func (*Builder) Add

func (b *Builder) Add(fieldname string, value interface{}) *Builder

Add a field and value to the builder

func (*Builder) AllowEmptyWhere

func (b *Builder) AllowEmptyWhere(value bool)

Sets if empty Where is allowed (if not, UPDATE / DELETE without where will return blank)

func (*Builder) Exec

func (b *Builder) Exec(db Execer, dmltype DMLType) (res sql.Result, err error)

Execute the current SQL on the database

func (*Builder) ExecTx

func (b *Builder) ExecTx(tx Execer, dmltype DMLType) (res sql.Result, err error)

Execute the current SQL on the transaction (compatibility with previous version)

func (*Builder) Output

func (b *Builder) Output(dmltype DMLType) string

Returns the generated SQL. It should be used together with OutputParams to execute on the database. May return blank if "Where" not allowed to be empty

func (*Builder) OutputAll

func (b *Builder) OutputAll(dmltype DMLType) (string, []interface{})

Returns the SQL and parameters at the same time

func (*Builder) OutputParams

func (b *Builder) OutputParams(dmltype DMLType) []interface{}

Returns the parameters to be passed to the database execution

func (*Builder) Where

func (b *Builder) Where(query string, args ...interface{}) *Builder

Add a query string to be put on the WHERE part if needed by the DML / ALWAYS use ? to indicate parameter positions

type BuilderProcessor

type BuilderProcessor interface {
	// Initialize parameter generation
	BeginParams()
	// Generates the next field name
	NextParam(fieldname string) string
}

BuilderProcessor generates parameter names compatible with the database

type DMLType

type DMLType int

DMLType is a custom type to identify the type of DML to generate (INSERT, UPDATE or DELETE)

const (
	INSERT DMLType = iota
	UPDATE
	DELETE
)

type Execer added in v1.1.1

type Execer interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
}

type MultiBuilder added in v1.1.0

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

func NewMultiBuilder added in v1.1.0

func NewMultiBuilder(table string, fields []string) *MultiBuilder

Creates a new multi builder

func NewMultiBuilderProc added in v1.1.0

func NewMultiBuilderProc(table string, processor BuilderProcessor, fields []string) *MultiBuilder

Creates a new multi builder for the specified table and processor

func (*MultiBuilder) ClearData added in v1.1.0

func (mb *MultiBuilder) ClearData()

func (*MultiBuilder) CreateData added in v1.1.0

func (mb *MultiBuilder) CreateData() *MultiBuilderData

func (*MultiBuilder) DataLen added in v1.1.0

func (mb *MultiBuilder) DataLen() int

func (*MultiBuilder) Exec added in v1.1.0

func (mb *MultiBuilder) Exec(db *sql.DB) (res sql.Result, err error)

Execute the current SQL on the database

func (*MultiBuilder) ExecTx added in v1.1.0

func (mb *MultiBuilder) ExecTx(tx *sql.Tx) (res sql.Result, err error)

Execute the current SQL on the transaction

func (*MultiBuilder) HasData added in v1.1.0

func (mb *MultiBuilder) HasData() bool

func (*MultiBuilder) Output added in v1.1.0

func (mb *MultiBuilder) Output() (string, []interface{})

type MultiBuilderData added in v1.1.0

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

func NewMultiBuilderData added in v1.1.0

func NewMultiBuilderData() *MultiBuilderData

func (*MultiBuilderData) Add added in v1.1.0

func (mbd *MultiBuilderData) Add(fieldname string, value interface{}) *MultiBuilderData

Jump to

Keyboard shortcuts

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