queryparts

package
v0.0.32 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package queryparts provides struct defitintions that abstract the necessary parts of a SQL query such as joins, column aliases, order by clauses, etc.

Index

Constants

View Source
const (
	AliasedField string = "%[1]v.%[2]v"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldDescriptor

type FieldDescriptor struct {
	Alias   string
	Table   string
	Column  string
	RefPath string
}

FieldDescriptor holds the table/field info for an aliased field

type Join

type Join struct {
	Type        string
	Parent      *Table
	ParentField string
	JoinField   string
	Table       *Table
}

Join holds a very simple join definition, including a pointer to the parent table and the joined table and type of join

func (*Join) Build

func (j *Join) Build(parentAlias string) string

Build will turn the current join into the needed SQL, adding all of the aliases to the join. It will return something like:

t1.customer ON t1.parent_id = t0.id

func (*Join) Columns

func (j *Join) Columns() []string

Columns gets the join columns including the proper alias

type OrderByRequest

type OrderByRequest struct {
	Field      string
	Descending bool
}

OrderByRequest holds information about a request to order by a field

Picard will read the column name from the struct field's tag and add that to the ORDER BY clause in the SQL query.

Example:

results, err := p.FilterModel(picard.FilterRequest{
	FilterModel: tableA{},
	OrderBy: []qp.OrderByRequest{
		{
			Field:      "FieldA",
			Descending: true,
		},
	},
})

// SELECT ... ORDER BY t0.field_a DESC

type Table

type Table struct {
	Alias   string
	RefPath string
	Name    string

	Joins        []Join
	Wheres       sql.And
	MultiTenancy sql.Eq
	// contains filtered or unexported fields
}

Table represents a select, and is the root of the structure. Start here to build a query by calling

tbl := New("my_table")

func New

func New(name string) *Table

New returns a new table.

func NewAliased

func NewAliased(name string, alias string, refPath string) *Table

NewAliased returns a new table with the given alias

func (*Table) AddColumns

func (t *Table) AddColumns(cols []string)

AddColumns adds an array of columns to the current table, adding the aliases

func (*Table) AddMultitenancyWhere

func (t *Table) AddMultitenancyWhere(column string, val interface{})

AddMultitenancyWhere creates a multitenancy WHERE condition

func (*Table) AddWhere

func (t *Table) AddWhere(column string, val interface{})

AddWhere adds one where clause, WHERE {field} = {val}

func (*Table) AddWhereGroup

func (t *Table) AddWhereGroup(group sql.Sqlizer)

AddWhereGroup adds a grouping of ORS or ANDs to the where clause

func (*Table) AppendJoin

func (t *Table) AppendJoin(tbl, joinField, parentField, jType string, counter *int) *Table

AppendJoin adds a join with the proper aliasing, including any columns requested from that table

func (*Table) AppendJoinTable

func (t *Table) AppendJoinTable(tbl *Table, joinField, parentField, jType string) *Table

AppendJoinTable adds a join with the proper aliasing, including any columns requested from that table

func (*Table) BuildSQL

func (t *Table) BuildSQL() sql.SelectBuilder

BuildSQL returns a squirrel SelectBuilder, which can be used to execute the query or to just add more to the query

func (*Table) Columns

func (t *Table) Columns() []string

Columns gets the join columns including the proper alias

func (*Table) DeleteSQL

func (t *Table) DeleteSQL() sql.DeleteBuilder

DeleteSQL returns a squirrel SelectBuilder, which can be used to execute the query or to just add more to the query

func (*Table) FieldAliases

func (t *Table) FieldAliases() map[string]FieldDescriptor

FieldAliases returns a map of all columns on a table and that table's joins.

func (*Table) ToSQL

func (t *Table) ToSQL() (string, []interface{}, error)

ToSQL returns the SQL statement, as it currently stands.

Jump to

Keyboard shortcuts

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