statement

package
v0.2.13 Latest Latest
Warning

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

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

README

norm/statement

Example

	type Part struct {
		ID string
		Name string
	}

	part := Part{
		Name: "part01"
	}

	query, err := statement.Select().Comment("request-id: ?", id).
		WithRecursive(
			"included_parts",
			Select().Columns("sub_part", "part", "quantity").
				From("parts").Where("part = ?", part.Name).
				UnionAll(
					Select().Columns("p.sub_part", "p.part", "p.quantity").
						From("included_parts AS pr").
						JoinInner("parts AS p", "p.part = pr.sub_part"),
				),
		).Columns("sub_part", "SUM(quantity) as total_quantity").
			From("included_parts").GroupBy("sub_part").String()

	if err != nil {
		// handle error
	}

	rows, err := tx.Query(query)
	// handle rows.....

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyWithAlias will be returned when the a alias for a with clause is empty
	ErrEmptyWithAlias = fmt.Errorf("statement: empty with clause alias")

	// ErrInvalidArgNumber will be returned when there is a mismatch between placeholders and values for interpolation.
	ErrInvalidArgNumber = fmt.Errorf("statement: invalid number of arguments")
)

Functions

func InterfaceSlice

func InterfaceSlice(slice interface{}) []interface{}

InterfaceSlice converts any slice to a []interface{}

Types

type Buffer

type Buffer interface {
	WriteString(s string) (int, error)
	String() string
}

Buffer represents the write buffer for building statements. Fits nicely with a strings.Builder or a bytes.Buffer.

type DDL

type DDL struct {
	*Part
	// contains filtered or unexported fields
}

DDL represents a data definition statement.

func Alter

func Alter(query string, values ...interface{}) *DDL

Alter creates a new `ALTER` DDL statement.

func Create

func Create(query string, values ...interface{}) *DDL

Create creates a new `CREATE` DDL statement.

func Drop

func Drop(query string, values ...interface{}) *DDL

Drop creates a new `DROP` DDL statement.

func Truncate

func Truncate(query string, values ...interface{}) *DDL

Truncate creates a new `TRUNCATE` DDL statement.

func (*DDL) Build

func (s *DDL) Build(buf Buffer) (err error)

Build builds the statement into the given buffer.

func (*DDL) Comment

func (s *DDL) Comment(c string, values ...interface{}) *DDL

Comment adds a SQL comment to the generated query. Each call to comment creates a new `-- <comment>` line.

func (*DDL) String

func (s *DDL) String() (q string, err error)

String builds the statement and returns the resulting query string.

type DeleteStatement

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

DeleteStatement statement.

func Delete

func Delete() (s *DeleteStatement)

Delete creates a new `DELETE` statement.

func (*DeleteStatement) Build

func (s *DeleteStatement) Build(buf Buffer) (err error)

Build builds the statement into the given buffer.

func (*DeleteStatement) Comment

func (s *DeleteStatement) Comment(c string, values ...interface{}) *DeleteStatement

Comment adds a SQL comment to the generated query. Each call to comment creates a new `-- <comment>` line.

func (*DeleteStatement) From

func (s *DeleteStatement) From(table string) *DeleteStatement

From sets the table name or for the `FROM` clause.

func (*DeleteStatement) Returning

func (s *DeleteStatement) Returning(columns ...string) *DeleteStatement

Returning adds a `RETURNING columns` clause.

func (*DeleteStatement) String

func (s *DeleteStatement) String() (q string, err error)

String builds the statement and returns the resulting query string.

func (*DeleteStatement) Where

func (s *DeleteStatement) Where(q string, values ...interface{}) *DeleteStatement

Where adds a `WHERE` clause, multiple calls to Where are `ANDed` together.

func (*DeleteStatement) WhereIn

func (s *DeleteStatement) WhereIn(column string, values ...interface{}) *DeleteStatement

WhereIn adds a `WHERE IN (values)` clause, multiple calls to WhereIn are `ANDed` together.

func (*DeleteStatement) With

func (s *DeleteStatement) With(alias string, stmt Statement) *DeleteStatement

With adds a `WITH alias AS (stmt)`

type Ident added in v0.2.12

type Ident string

Ident type is handled as an user provided identifier as is in the resulting query

type InsertStatement

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

InsertStatement statement.

func Insert

func Insert() (s *InsertStatement)

Insert creates a new `INSERT` statement.

func (*InsertStatement) Build

func (s *InsertStatement) Build(buf Buffer) (err error)

Build builds the statement into the given buffer.

func (*InsertStatement) Columns

func (s *InsertStatement) Columns(columns ...string) (st *InsertStatement)

Columns specifies the columns for the `INSERT` statement.

func (*InsertStatement) Comment

func (s *InsertStatement) Comment(c string, values ...interface{}) *InsertStatement

Comment adds a SQL comment to the generated query. Each call to comment creates a new `-- <comment>` line.

func (*InsertStatement) Into

func (s *InsertStatement) Into(table string) (st *InsertStatement)

Into specifies the table on which to perform the insert

func (*InsertStatement) OnConflict added in v0.2.1

func (s *InsertStatement) OnConflict(q string, values ...interface{}) (st *InsertStatement)

OnConflict adds a `ON CONFLICT` clause.

func (*InsertStatement) Record

func (s *InsertStatement) Record(structValue interface{}) (st *InsertStatement)

Record add the values from the given struct for insert. If no columns where specified before calling Record(), the columns will be defined by the struct fields.

func (*InsertStatement) Returning

func (s *InsertStatement) Returning(columns ...string) *InsertStatement

Returning adds a `RETURNING columns` clause.

func (*InsertStatement) String

func (s *InsertStatement) String() (q string, err error)

String builds the statement and returns the resulting query string.

func (*InsertStatement) Values

func (s *InsertStatement) Values(values ...interface{}) (st *InsertStatement)

Values specifies the values for the `VALUES` clause.

func (*InsertStatement) ValuesSelect

func (s *InsertStatement) ValuesSelect(values *SelectStatement) (st *InsertStatement)

ValuesSelect specifies a Select statement from which values will be inserted.

func (*InsertStatement) With

func (s *InsertStatement) With(alias string, stmt Statement) *InsertStatement

With adds a `WITH alias AS (stmt)`

type Join

type Join string

Join types

var (
	// InnerJoin type
	InnerJoin Join = "INNER JOIN"
	// LeftOuterJoin type
	LeftOuterJoin Join = "LEFT OUTER JOIN"
	// RightOuterJoin type
	RightOuterJoin Join = "RIGHT OUTER JOIN"
	// FullOuterJoin type
	FullOuterJoin Join = "FULL OUTER JOIN"
)

type Part added in v0.2.2

type Part struct {
	Query  string
	Values []interface{}
}

Part is a query fragment that satisfies the statement.Statement interface

func (*Part) Build added in v0.2.2

func (p *Part) Build(buf Buffer) (err error)

Build builds the part into the given buffer.

func (*Part) String added in v0.2.2

func (p *Part) String() (q string, err error)

String builds the part and returns the resulting query.

type SelectStatement

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

SelectStatement statement.

func Select

func Select() *SelectStatement

Select creates a new `SELECT` statement.

func (*SelectStatement) Build

func (s *SelectStatement) Build(buf Buffer) (err error)

Build builds the statement into the given buffer.

func (*SelectStatement) Column added in v0.2.9

func (s *SelectStatement) Column(q string, values ...interface{}) *SelectStatement

Column append the given column to the `SELECT`. Column appends to the existing columns already specified. Used for more ellaborate column specification.

func (*SelectStatement) Columns

func (s *SelectStatement) Columns(columns ...interface{}) *SelectStatement

Columns set the `SELECT` columns. Columns overwrites any previously set columns for this statement.

func (*SelectStatement) Comment

func (s *SelectStatement) Comment(c string, values ...interface{}) *SelectStatement

Comment adds a SQL comment to the generated query. Each call to comment creates a new `-- <comment>` line.

func (*SelectStatement) Distinct

func (s *SelectStatement) Distinct() *SelectStatement

Distinct adds a `DISTINCT` clause.

func (*SelectStatement) ForUpdate

func (s *SelectStatement) ForUpdate() *SelectStatement

ForUpdate a `FOR UPDATE` clause.

func (*SelectStatement) From

func (s *SelectStatement) From(table interface{}) *SelectStatement

From sets the table name or *Select statement for the `FROM` clause.

func (*SelectStatement) GroupBy

func (s *SelectStatement) GroupBy(columns ...string) *SelectStatement

GroupBy adds a `GROUP BY columns` clause.

func (*SelectStatement) Having

func (s *SelectStatement) Having(q string, values ...interface{}) *SelectStatement

Having adds a `HAVING` clause, multiple calls to Having are `ANDed` together.

func (*SelectStatement) Join

func (s *SelectStatement) Join(join Join, table, cond string, values ...interface{}) *SelectStatement

Join adds a `JOIN ...` clause.

func (*SelectStatement) JoinFull

func (s *SelectStatement) JoinFull(table, cond string, values ...interface{}) *SelectStatement

JoinFull adds a `FULL OUTER JOIN` clause.

func (*SelectStatement) JoinInner

func (s *SelectStatement) JoinInner(table, cond string, values ...interface{}) *SelectStatement

JoinInner adds a `INNER JOIN` clause.

func (*SelectStatement) JoinLeft

func (s *SelectStatement) JoinLeft(table, cond string, values ...interface{}) *SelectStatement

JoinLeft adds a `LEFT OUTER JOIN` clause.

func (*SelectStatement) JoinRight

func (s *SelectStatement) JoinRight(table, cond string, values ...interface{}) *SelectStatement

JoinRight adds a `RIGHT OUTER JOIN` clause.

func (*SelectStatement) Limit

func (s *SelectStatement) Limit(n int64) *SelectStatement

Limit adds a `LIMIT n` clause.

func (*SelectStatement) Offset

func (s *SelectStatement) Offset(n int64) *SelectStatement

Offset adds a `OFFSET n` clause, only if LIMIT is also set.

func (*SelectStatement) OrderAsc

func (s *SelectStatement) OrderAsc(columns ...string) *SelectStatement

OrderAsc adds a `ORDER BY columns ASC` clause.

func (*SelectStatement) OrderDesc

func (s *SelectStatement) OrderDesc(columns ...string) *SelectStatement

OrderDesc adds a `ORDER BY columns DESC` clause.

func (*SelectStatement) SkipLocked

func (s *SelectStatement) SkipLocked() *SelectStatement

SkipLocked adds a `SKIP LOCKED` clause.

func (*SelectStatement) String

func (s *SelectStatement) String() (q string, err error)

String builds the statement and returns the resulting query string.

func (*SelectStatement) Union

func (s *SelectStatement) Union(stmt Statement) *SelectStatement

Union adds a `UNION` clause.

func (*SelectStatement) UnionAll

func (s *SelectStatement) UnionAll(stmt Statement) *SelectStatement

UnionAll adds a `UNION ALL` clause.

func (*SelectStatement) Where

func (s *SelectStatement) Where(q string, values ...interface{}) *SelectStatement

Where adds a `WHERE` clause, multiple calls to Where are `ANDed` together.

func (*SelectStatement) WhereIn

func (s *SelectStatement) WhereIn(column string, values ...interface{}) *SelectStatement

WhereIn adds a `WHERE IN (values)` clause, multiple calls to WhereIn are `ANDed` together.

func (*SelectStatement) With

func (s *SelectStatement) With(alias string, stmt Statement) *SelectStatement

With adds a `WITH alias AS (stmt)`

func (*SelectStatement) WithRecursive

func (s *SelectStatement) WithRecursive(alias string, stmt Statement) *SelectStatement

WithRecursive adds a `WITH RECURSIVE alias AS (stmt)`

type Statement

type Statement interface {
	Build(Buffer) error
	String() (q string, err error)
}

Statement represents the statement builder interface.

type UpdateStatement

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

UpdateStatement statement.

func Update

func Update() (s *UpdateStatement)

Update creates a new update statement

func (*UpdateStatement) Build

func (s *UpdateStatement) Build(buf Buffer) (err error)

Build builds the statement into the given buffer.

func (*UpdateStatement) Comment

func (s *UpdateStatement) Comment(c string, values ...interface{}) *UpdateStatement

Comment adds a SQL comment to the generated query. Each call to comment creates a new `-- <comment>` line.

func (*UpdateStatement) Returning

func (s *UpdateStatement) Returning(columns ...string) *UpdateStatement

Returning adds a `RETURNING columns` clause.

func (*UpdateStatement) Set

func (s *UpdateStatement) Set(column string, value interface{}) *UpdateStatement

Set adds a `SET column = value` clause, multiple calls to set append additional updates `SET column = value, column = value`

func (*UpdateStatement) SetMap

func (s *UpdateStatement) SetMap(m map[string]interface{}) *UpdateStatement

SetMap specifies a map of column-value pairs to be updated.

func (*UpdateStatement) String

func (s *UpdateStatement) String() (q string, err error)

String builds the statement and returns the resulting query string.

func (*UpdateStatement) Table

func (s *UpdateStatement) Table(table string) *UpdateStatement

Table specifies the table for update.

func (*UpdateStatement) Where

func (s *UpdateStatement) Where(q string, values ...interface{}) *UpdateStatement

Where adds a `WHERE` clause, multiple calls to Where are `ANDed` together.

func (*UpdateStatement) WhereIn

func (s *UpdateStatement) WhereIn(column string, values ...interface{}) *UpdateStatement

WhereIn adds a `WHERE IN (values)` clause, multiple calls to WhereIn are `ANDed` together.

func (*UpdateStatement) With

func (s *UpdateStatement) With(alias string, stmt Statement) *UpdateStatement

With adds a `WITH alias AS (stmt)` clause.

Jump to

Keyboard shortcuts

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