sqlgen

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package sqlgen allows for SQL queries to be automatically generated.

Index

Constants

View Source
const DoNothing = `on conflict do nothing`

DoNothing is useful as the conflict argument of inserter.Exec

View Source
const Upsert = `on conflict {{.Candidates}} do update set {{.NonCandidates}} = {{.NSNonCandidates "excluded"}}`

Upsert is useful as the conflict argument of inserter.Exec

Variables

This section is empty.

Functions

func ReportInsert

func ReportInsert(log logging.LogSink, started time.Time, mainTable string, sql string, rowcount int, err error, vals ...interface{})

ReportInsert traces SQL inserts

func ReportSelect

func ReportSelect(log logging.LogSink, started time.Time, mainTable string, sql string, rowcount int, err error, vals ...interface{})

ReportSelect traces SQL selects

func ReportUpdate

func ReportUpdate(log logging.LogSink, started time.Time, mainTable string, sql string, rowcount int, err error, vals ...interface{})

ReportUpdate traces SQL updates

func SingleRow

func SingleRow(rf func(RowDef)) func(FieldSet)

SingleRow is a shorthand to allow you to insert single rows easily.

Types

type FieldDefFunc

type FieldDefFunc func(fmt, col string, vals ...interface{})

FieldDefFunc represents RowDef.FD and RowDef.CF.

type FieldSet

type FieldSet interface {
	// It adds a row to the set.
	Row(func(RowDef))

	// Potent returns true if this FieldSet would produce a useful SQL query.
	// If Potent() returns false, there's no purpose in using this FieldSet with the database.
	Potent() bool

	// InsertSQL generates and returns an INSERT query to send to the database to insert this FieldSet.
	// The conflict string is used to resolve duplicate rows in an upserty kind of a way.
	// It allows Go template formatting.
	InsertSQL(table string, conflict string) string

	// InsertValues produces a slice of values for INSERTs, such that the positions of values will match up
	// with SQL replacements in an InsertSQL call.
	InsertValues() []interface{}

	// RowCount returns the number of rows in the FieldSet.
	RowCount() int
}

A FieldSet collects the requirements of a set of fields, and then helps generate SQL related to it.

func NewFieldset

func NewFieldset() FieldSet

NewFieldset returns a new FieldSet

type Inserter

type Inserter interface {
	Exec(table string, conflict string, fn func(FieldSet)) error
}

An Inserter performs inserts on a database.

func NewInserter

func NewInserter(ctx context.Context, log logging.LogSink, tx *sql.Tx) Inserter

NewInserter creates an inserter struct, that helps build and execute INSERT queries.

type RowDef

type RowDef interface {
	// FD is short for Field Definition - used for defining fields in the row.
	FD(fmt string, col string, vals ...interface{})

	// CF is short for Candidate Field - used to define fields in the row that participate in distinguishing the row.
	// Two rows with the same values for their "CFs" will be considered the same.
	CF(fmt string, col string, vals ...interface{})

	KV(col string, val interface{})
}

A RowDef serves to define individual rows within a FieldSet

Jump to

Keyboard shortcuts

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