pgxbuilder

package module
v0.0.0-...-51728cf Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2022 License: MIT Imports: 2 Imported by: 0

README

pgxbuilder

Go Reference test workflow

An SQL string builder for pgx.

Installation

$ go get github.com/eddiewentw/pgxbuilder

Examples

Insert statement:

q := pgxbuilder.Insert("users", []string{"username", "password"}).
	Values("triangulum", "6p79P56wNG")

fmt.Println(q.String())
// INSERT INTO users(username, password) VALUES ($1, $2)

fmt.Println(q.Parameters())
// [triangulum 6p79P56wNG]

Select statement:

pgxbuilder.From("users").
	Where("username = 'triangulum'").
	String()
// SELECT * FROM users WHERE (username = 'triangulum')

Update statement:

pgxbuilder.Update("users").
	Set("username = 'mandolin'")
// UPDATE users SET username = 'mandolin'

Delete statement:

pgxbuilder.Delete("users").
	Where("username = 'mandolin'")
// DELETE FROM users WHERE (username = 'mandolin')

with pgx:

func addTask(description string) error {
	q := Insert("task", []string{"description"}).
		Values(description)
	_, err := conn.Exec(context.Background(), q.String(), q.Parameters()...)
	return err
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Query

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

func Delete

func Delete(table string) *Query

Delete starts a delete statement.

func From

func From(table string) *Query

From starts a select statement.

func Insert

func Insert(table string, columns []string) *Query

Insert starts a insert statement.

func Update

func Update(table string) *Query

Update starts a update statement.

func (*Query) And

func (q *Query) And(condition string, options ...whereOption) whereCondition

And concatenates where conditions with an AND operator.

func (*Query) Distinct

func (q *Query) Distinct() *Query

Distinct excludes all duplicate rows from the result set. One row will be kept from each group of duplicates.

func (*Query) DistinctOn

func (q *Query) DistinctOn(columns ...string) *Query

DistinctOn keeps only the first row of each set of rows where the given expressions evaluate to equal.

func (*Query) For

func (q *Query) For(lock string) *Query

For locks rows as they are obtained from the table.

func (*Query) GroupBy

func (q *Query) GroupBy(columns ...string) *Query

GroupBy groups together those rows in a table that have the same values in all the columns listed.

func (*Query) Limit

func (q *Query) Limit(count uint64) *Query

Limit specifies the maximum number of rows to return.

func (*Query) Offset

func (q *Query) Offset(start uint64) *Query

Offset specifies the number of rows to skip before starting to return rows.

func (*Query) Or

func (q *Query) Or(condition string, options ...whereOption) whereCondition

Or concatenates where conditions with an OR operator.

func (*Query) OrderBy

func (q *Query) OrderBy(expression string, options ...string) *Query

OrderBy add an expression to sort result rows.

func (*Query) Param

func (q *Query) Param(v ...interface{}) parameterOption

Param registers one or more parameters to this query. You can use Parameters to get all of them.

func (Query) Parameters

func (q Query) Parameters() []interface{}

Parameters returns all parameters in this query.

func (*Query) Returning

func (q *Query) Returning(columns ...string) *Query

Returning obtains data from modified rows.

func (*Query) Select

func (q *Query) Select(columns ...string) *Query

Select adds items to the select list.

func (*Query) Set

func (q *Query) Set(set string, params ...interface{}) *Query

Set adds a set clause.

func (Query) String

func (q Query) String() string

String returns an SQL string.

func (*Query) Values

func (q *Query) Values(params ...interface{}) *Query

Values registers values of new records.

func (*Query) Where

func (q *Query) Where(condition string, options ...whereOption) *Query

Where adds one or more where conditions. Use And and Or operators to combine multiple conditions.

Jump to

Keyboard shortcuts

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