query

package
v0.0.3-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package query is used to build SQL queries with Lingo tables / columns.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildWhereSQL

func BuildWhereSQL(d lingo.Dialect, values []lingo.Expression) (sql.Data, error)

BuildWhereSQL is to be used by custom queries to build a WHERE clause.

func ExpandTables

func ExpandTables(paths []lingo.Expression) []lingo.Expression

func JoinToSQL

func JoinToSQL(d lingo.Dialect, sep string, exp []lingo.Expression) (sql.Data, error)

JoinToSQL will call ToSQL on each exp, returning the error if one occurs, and then joins the SQL together with sep in between each sql.Data.

func NewErrAroundSQL

func NewErrAroundSQL(s sql.Data, err error) error

func NewNamedOnlyColumn

func NewNamedOnlyColumn(name, parent string) lingo.Column

NewNamedOnlyColumn creates a `lingo.Column` of which only the name of the column is filled out. Thus, when `ToSQL()` is called, only a single SQL with the value of `name` is returned.

Types

type DeleteQuery

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

func Delete

func Delete(from lingo.Table) *DeleteQuery

Delete allows deletion of an entity

func (DeleteQuery) Join

DELETE w FROM WorkRecord2 w LEFT JOIN Employee e ON EmployeeRun=EmployeeNo WHERE w.Company = '1' AND e.Date = '2013-05-06'

func (DeleteQuery) ToSQL

func (d DeleteQuery) ToSQL(dialect lingo.Dialect) (sql.Data, error)

func (DeleteQuery) Where

func (d DeleteQuery) Where(exp ...lingo.Expression) *DeleteQuery

type ErrAroundSQL

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

func (ErrAroundSQL) Error

func (e ErrAroundSQL) Error() string

func (ErrAroundSQL) SQL

func (e ErrAroundSQL) SQL() string

func (ErrAroundSQL) Unwrap

func (e ErrAroundSQL) Unwrap() error

type InsertQuery

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

func InsertInto

func InsertInto(entity lingo.Table) *InsertQuery

func (*InsertQuery) Columns

func (i *InsertQuery) Columns(columns ...lingo.Column) *InsertQuery

func (*InsertQuery) Select

func (i *InsertQuery) Select(q *SelectQuery) *InsertQuery

Select allows passing in a Select Query the following insert statements:

INSERT INTO table1 (uuid, name) SELECT UNHEX("1234567891230"), a.name FROM table2 as a LEFT JOIN table1 as b ON a.name=b.remote_name WHERE b.remote_name = 'other_table';

func (InsertQuery) ToSQL

func (i InsertQuery) ToSQL(d lingo.Dialect) (sql.Data, error)

func (*InsertQuery) Values

func (i *InsertQuery) Values(values ...lingo.Expression) *InsertQuery

Values allows inserting expressions with SQL functions:

INSERT INTO table1 (uuid, name) values (UNHEX("1234567891234"), 'name1');

func (*InsertQuery) ValuesConstants

func (i *InsertQuery) ValuesConstants(values ...interface{}) *InsertQuery

ValuesConstants allows inserting constant values:

INSERT INTO table1 (id, name, internal_name) values (123456, 'name1', 'internal_name');

type Modifier

type Modifier interface {
	IsZero() bool
	Limit() (limit uint64, wasSet bool)
	Offset() (offset uint64, wasSet bool)
}

Modifier has information to alter the query - post statements.

func Limit

func Limit(limit uint64) Modifier

Limit helps with limiting the number of rows returned

func Offset

func Offset(offset uint64) Modifier

Offset tells a query to start after a specific number of rows

func Page

func Page(limit, offset uint64) Modifier

Page helps with pagination by utilizing both Limit and Offset

type ModifyDialect

type ModifyDialect interface {
	Modify(modifier Modifier) (sql.Data, error)
}

ModifyDialect is used by the query builders to check if an interface is satisfied for a dialect.

type SelectQuery

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

func Select

func Select(paths ...lingo.Expression) *SelectQuery

func SelectFrom

func SelectFrom(e lingo.Table) *SelectQuery

func (*SelectQuery) From

func (q *SelectQuery) From(e lingo.Table) *SelectQuery
Example (Where)
package main

import (
	"fmt"

	"github.com/weworksandbox/lingo/dialect"
	"github.com/weworksandbox/lingo/internal/test/schema/tsakila/tfilmactor"
	"github.com/weworksandbox/lingo/query"
)

func main() {
	d, _ := dialect.NewDefault()

	fa := tfilmactor.As("fa")
	s, _ := query.Select(fa.FilmId()).From(fa).Where(fa.ActorId().Between(1, 10)).ToSQL(d)

	fmt.Println(s.String())
	fmt.Println(s.Values())
}
Output:

SELECT fa.film_id FROM film_actor AS fa WHERE fa.actor_id BETWEEN ? AND ?
[1 10]

func (*SelectQuery) Join

func (q *SelectQuery) Join(left lingo.Expression, joinType join.Type, on lingo.Expression) *SelectQuery

Join an expr with a specific joinType using an on statement.

func (*SelectQuery) OrderBy

func (q *SelectQuery) OrderBy(exp lingo.Expression, direction sort.Direction) *SelectQuery

func (*SelectQuery) Restrict

func (q *SelectQuery) Restrict(m Modifier) *SelectQuery

Restrict the query with things like limits and offsets.

Example
package main

import (
	"fmt"

	"github.com/weworksandbox/lingo/dialect"
	"github.com/weworksandbox/lingo/internal/test/schema/tsakila/tfilmactor"
	"github.com/weworksandbox/lingo/query"
)

func main() {
	d, _ := dialect.NewDefault()

	const maxPageNum = 1 // To limit output for example
	pageSize := uint64(150)
	fa := tfilmactor.As("fa")
	q := query.SelectFrom(fa)

	for page := uint64(0); page < maxPageNum; page++ {
		s, _ := q.Restrict(query.Page(pageSize, page*pageSize)).ToSQL(d)

		fmt.Println(s.String())
		fmt.Println(s.Values())

	}
}
Output:

SELECT fa.actor_id, fa.film_id, fa.last_update FROM film_actor AS fa LIMIT ? OFFSET ?
[150 0]

func (*SelectQuery) ToSQL

func (q *SelectQuery) ToSQL(d lingo.Dialect) (sql.Data, error)

func (*SelectQuery) Where

func (q *SelectQuery) Where(exp ...lingo.Expression) *SelectQuery

type UpdateQuery

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

func Update

func Update(table lingo.Table) *UpdateQuery

func (UpdateQuery) Set

func (u UpdateQuery) Set(exp ...lingo.Set) *UpdateQuery

func (UpdateQuery) ToSQL

func (u UpdateQuery) ToSQL(d lingo.Dialect) (sql.Data, error)

func (UpdateQuery) Where

func (u UpdateQuery) Where(exp ...lingo.Expression) *UpdateQuery

Jump to

Keyboard shortcuts

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