query

package
v0.0.0-...-b066fa0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WhereID

func WhereID[Col ColName](b *Builder[Col])

Where ID writes a where clause in the form:

WHERE "id" = $1

Types

type Builder

type Builder[Col ColName] struct {
	stringx.Builder
	// contains filtered or unexported fields
}

Builder for queries

func (*Builder[Col]) Delete

func (b *Builder[Col]) Delete(schema, table string, wf WhereFunc[Col], returnColumns ...Col)

Delete builds a delete query. The WHERE clause must be written by the passed WhereFunc, which will not be called if nil. WARNING: a nil WhereFunc will result in deletion of all records in a table! See WriteReturnClause on when and how the RETURNING clause is written.

DELETE FROM "public"."simple" WHERE "id" = $1 RETURNING ("id, "title", "data");

func (*Builder[Col]) Insert

func (b *Builder[Col]) Insert(schema, table string, insertColumns []string, returnColumns ...Col)

Insert builds an insert query. See WriteReturnClause on when and how the RETURNING clause is written.

INSERT INTO "public"."simple_rw" ("id", "title") VALUES ($1, $2) RETURNING "id";

func (*Builder[Col]) Reset

func (b *Builder[Col]) Reset()

func (*Builder[Col]) Select

func (b *Builder[Col]) Select(schema, table string, columns []Col, wf WhereFunc[Col], orderBy OrderWriter[Col], limit int64)

Select builds a select query. The WHERE clause must be written by the passed WhereFunc, which will not be called if nil. The ORDER By clause is written when orderBy is not nil. The LIMIT clause is only written when greater than 0.

SELECT "id", "title", "price" FROM "public"."products" WHERE "id" = $1 ORDER BY "id" LIMIT 1;

func (*Builder[Col]) Update

func (b *Builder[Col]) Update(schema, table string, updateColumns []string, wf WhereFunc[Col], returnColumns ...Col)

Update builds a update query. The WHERE clause must be written by the passed WhereFunc, which will not be called if nil. WARNING: a nil WhereFunc will result in updates of all records in a table! See WriteReturnClause on when and how the RETURNING clause is written.

UPDATE "public"."simple" SET "title" = $1, "data" = $2 WHERE "id" = $3 RETURNING ("title", "data");

func (*Builder[Col]) WriteColumnSpec

func (b *Builder[Col]) WriteColumnSpec(columns []Col)

WriteColumnSpec writes the column specifier to the query. All column names are doube-quoted and comma seperated.

func (*Builder[Col]) WriteIdentifier

func (b *Builder[Col]) WriteIdentifier(schema, table string)

WriteIdentifier writes the full identifier of a table. This includes the schema, if it is not empty. Written names will be quoted.

func (*Builder[Col]) WritePosArgs

func (b *Builder[Col]) WritePosArgs(n int)

WritePosArgs writes n amount of positional arguments to the query, in the form of "$1, $2". WritePosArgs can be called multiple times while building a query, and all position values in the resulting query will be uniquely incremented values.

func (*Builder[Col]) WriteReturnClause

func (b *Builder[Col]) WriteReturnClause(columns []Col)

WriteReturnClause writes a RETURNING clause with a column spec. If columns has zero length, nothing will be written (no-op).

type ColName

type ColName interface {
	fmt.Stringer
}

ColName is the generic interface for column name specification. Typically, one can use a protobuf enum for column names. The enum entries have a String method, implementing this interface.

type Direction

type Direction int

Direction used in a ORDER BY clause.

const (
	Ascending Direction = iota
	Descending
)

type OrderWriter

type OrderWriter[Col ColName] interface {
	// contains filtered or unexported methods
}

OrderWriter writen the ORDER BY ... [ASC|DESC] clause.

func Order

func Order[Col ColName](direction Direction, columns ...Col) OrderWriter[Col]

Order returns an OrderWriter, which writes the ORDER BY "col1", "col2", "colN" [ASC|DESC] clause.

type Pool

type Pool[Col ColName] struct {
	// contains filtered or unexported fields
}

Pool utilizes a sync.Pool for efficient re-use of Builders.

func (*Pool[Col]) Get

func (p *Pool[Col]) Get() *Builder[Col]

Get a Builder from the Pool, growing it to the previous capacity.

func (*Pool[Col]) Put

func (p *Pool[Col]) Put(b *Builder[Col])

Put a Builder to the pool, resetting its state.

type WhereFunc

type WhereFunc[Col ColName] func(b *Builder[Col])

WhereFunc are callback functions that writes the "WHERE" clause to a query.

func WhereIDInFunc

func WhereIDInFunc[Col ColName](n int) WhereFunc[Col]

WhereIDInFunc returns a function which writes a where clause in the form:

WHERE "id" IN $1, $2, $N...

Jump to

Keyboard shortcuts

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