Documentation ¶
Overview ¶
Package pgtalk provides functionality to create and execute type-safe Postgres Queries and Mutations using generated struct-per-table.
Index ¶
- Constants
- Variables
- func IndentedSQL(some SQLWriter) string
- func IsNotNull(e SQLExpression) nullCheck
- func IsNull(e SQLExpression) nullCheck
- func NewBooleanAccess(info ColumnInfo, writer fieldAccessFunc) booleanAccess
- func NewBytesAccess(info ColumnInfo, valueWriter func(dest any) *string) bytesAccess
- func NewFloat64Access(info ColumnInfo, writer fieldAccessFunc) float64Access
- func NewInt32Access(info ColumnInfo, valueWriter func(dest any) any) int32Access
- func NewInt64Access(info ColumnInfo, valueWriter func(dest any) any) int64Access
- func NewJSONAccess(info ColumnInfo, valueWriter func(dest any) any) jsonAccess
- func NewTextAccess(info ColumnInfo, writer fieldAccessFunc) textAccess
- func NewTimeAccess(info ColumnInfo, valueWriter fieldAccessFunc) timeAccess
- func SQL(some SQLWriter) string
- func SQLAs(sql, name string) *computedField
- func StringWithFields(v any, includePresent bool) string
- type ColumnAccessor
- type ColumnInfo
- type FieldAccess
- func (FieldAccess) And(e SQLExpression) SQLExpression
- func (a FieldAccess[T]) AppendScannable(list []any) []any
- func (a FieldAccess[T]) Column() ColumnInfo
- func (a FieldAccess[T]) Compare(operator string, operand any) binaryExpression
- func (a FieldAccess[T]) Concat(resultName string, ex SQLExpression) ColumnAccessor
- func (a FieldAccess[T]) Equals(operand any) binaryExpression
- func (a FieldAccess[T]) FieldValueToScan(entity any) any
- func (a FieldAccess[T]) Get(values map[string]any) any
- func (a FieldAccess[T]) GreaterThan(operand any) binaryExpression
- func (a FieldAccess[T]) In(values ...T) binaryExpression
- func (a FieldAccess[T]) IsNotNull() binaryExpression
- func (a FieldAccess[T]) IsNull() binaryExpression
- func (a FieldAccess[T]) LessThan(operand any) binaryExpression
- func (FieldAccess) Or(e SQLExpression) SQLExpression
- func (a FieldAccess[T]) Set(v T) FieldAccess[T]
- func (a FieldAccess[T]) TableAlias(alias string) FieldAccess[T]
- func (a FieldAccess[T]) ValueToInsert() any
- type MutationSet
- func (m MutationSet[T]) Exec(ctx context.Context, conn querier, parameters ...*QueryParameter) ResultIterator[T]
- func (m MutationSet[T]) On() MutationSet[T]
- func (m MutationSet[T]) Returning(columns ...ColumnAccessor) MutationSet[T]
- func (m MutationSet[T]) SQLOn(w WriteContext)
- func (m MutationSet[T]) Where(condition SQLExpression) MutationSet[T]
- type NullJSON
- type QueryParameter
- type QuerySet
- func (QuerySet) And(e SQLExpression) SQLExpression
- func (q QuerySet[T]) Ascending() QuerySet[T]
- func (q QuerySet[T]) Descending() QuerySet[T]
- func (q QuerySet[T]) Distinct() QuerySet[T]
- func (d QuerySet[T]) Exec(ctx context.Context, conn querier, parameters ...*QueryParameter) (list []*T, err error)
- func (d QuerySet[T]) ExecIntoMaps(ctx context.Context, conn querier, parameters ...*QueryParameter) (list []map[string]any, err error)
- func (q QuerySet[T]) Exists() unaryExpression
- func (d QuerySet[T]) FullJoin(otherQuerySet querySet) join
- func (q QuerySet[T]) GroupBy(cas ...ColumnAccessor) QuerySet[T]
- func (q QuerySet[T]) Having(condition SQLExpression) QuerySet[T]
- func (d QuerySet[T]) Iterate(ctx context.Context, conn querier, parameters ...*QueryParameter) (*resultIterator[T], error)
- func (d QuerySet[T]) Join(otherQuerySet querySet) join
- func (d QuerySet[T]) LeftOuterJoin(otherQuerySet querySet) join
- func (q QuerySet[T]) Limit(limit int) QuerySet[T]
- func (q QuerySet[T]) Named(preparedName string) QuerySet[T]
- func (q QuerySet[T]) Offset(offset int) QuerySet[T]
- func (QuerySet) Or(e SQLExpression) SQLExpression
- func (q QuerySet[T]) OrderBy(cas ...SQLWriter) QuerySet[T]
- func (d QuerySet[T]) RightJoin(otherQuerySet querySet) joindeprecated
- func (d QuerySet[T]) RightOuterJoin(otherQuerySet querySet) join
- func (q QuerySet[T]) SQLOn(w WriteContext)
- func (q QuerySet[T]) TableAlias(alias string) QuerySet[T]
- func (q QuerySet[T]) Where(condition SQLExpression) QuerySet[T]
- type ResultIterator
- type SQLExpression
- type SQLFunction
- type SQLLiteral
- type SQLWriter
- type TableInfo
- type WriteContext
Constants ¶
const ( IsPrimary = true NotPrimary = false NotNull = true Nullable = false )
const ( MutationDelete = iota MutationInsert MutationUpdate )
const HideNilValues = true
Variables ¶
var EmptyColumnAccessor = []ColumnAccessor{}
Functions ¶
func IndentedSQL ¶ added in v1.0.1
IndentedSQL returns source with tabs and lines trying to have a formatted view.
func IsNotNull ¶
func IsNotNull(e SQLExpression) nullCheck
IsNotNull returns an expression with the IS NOT NULL condition
func IsNull ¶
func IsNull(e SQLExpression) nullCheck
IsNull returns an expression with the IS NULL condition
func NewBooleanAccess ¶
func NewBooleanAccess(info ColumnInfo, writer fieldAccessFunc) booleanAccess
func NewBytesAccess ¶
func NewBytesAccess(info ColumnInfo, valueWriter func(dest any) *string) bytesAccess
func NewFloat64Access ¶
func NewFloat64Access(info ColumnInfo, writer fieldAccessFunc) float64Access
func NewInt32Access ¶ added in v0.29.2
func NewInt32Access( info ColumnInfo, valueWriter func(dest any) any) int32Access
func NewInt64Access ¶
func NewInt64Access( info ColumnInfo, valueWriter func(dest any) any) int64Access
func NewJSONAccess ¶ added in v1.0.0
func NewJSONAccess(info ColumnInfo, valueWriter func(dest any) any) jsonAccess
func NewTextAccess ¶
func NewTextAccess(info ColumnInfo, writer fieldAccessFunc) textAccess
func NewTimeAccess ¶
func NewTimeAccess(info ColumnInfo, valueWriter fieldAccessFunc) timeAccess
func SQLAs ¶ added in v0.23.1
func SQLAs(sql, name string) *computedField
SQLAs returns a ColumnAccessor with a customer SQL expression.
The named result will be available using the GetExpressionResult method of the record type.
func StringWithFields ¶
Types ¶
type ColumnAccessor ¶
type ColumnAccessor interface { SQLWriter Name() string ValueToInsert() any Column() ColumnInfo // FieldValueToScan returns the address of the value of the field in the entity FieldValueToScan(entity any) any // AppendScannable collects values for scanning by a result Row // Cannot use ValueToInsert because that looses type information such that the Scanner will use default mapping AppendScannable(list []any) []any // Get accesses the value from a map. // (unfortunately, Go methods cannot have additional type parameters: // Get[V](values map[string]any) V ) Get(values map[string]any) any }
type ColumnInfo ¶
type ColumnInfo struct {
// contains filtered or unexported fields
}
func MakeColumnInfo ¶
func MakeColumnInfo(tableInfo TableInfo, columnName string, isPrimary bool, isNotNull bool, _ uint16) ColumnInfo
MakeColumnInfo creates a ColumnInfo describing a column in a table. The last argument is now ignored (used to be table attribute number, field ordinal).
func (ColumnInfo) Name ¶
func (c ColumnInfo) Name() string
func (ColumnInfo) SQLOn ¶
func (c ColumnInfo) SQLOn(w WriteContext)
func (ColumnInfo) TableAlias ¶ added in v0.14.0
func (c ColumnInfo) TableAlias(alias string) ColumnInfo
TableAlias changes the table alias for this column info.
type FieldAccess ¶
type FieldAccess[T any] struct { ColumnInfo // contains filtered or unexported fields }
func NewFieldAccess ¶
func NewFieldAccess[T any]( info ColumnInfo, writer func(dest any) any) FieldAccess[T]
func (FieldAccess) And ¶ added in v0.21.0
func (FieldAccess) And(e SQLExpression) SQLExpression
func (FieldAccess[T]) AppendScannable ¶ added in v0.16.0
func (a FieldAccess[T]) AppendScannable(list []any) []any
AppendScannable is part of ColumnAccessor
func (FieldAccess[T]) Column ¶
func (a FieldAccess[T]) Column() ColumnInfo
func (FieldAccess[T]) Compare ¶ added in v0.14.0
func (a FieldAccess[T]) Compare(operator string, operand any) binaryExpression
func (FieldAccess[T]) Concat ¶ added in v0.15.0
func (a FieldAccess[T]) Concat(resultName string, ex SQLExpression) ColumnAccessor
func (FieldAccess[T]) Equals ¶
func (a FieldAccess[T]) Equals(operand any) binaryExpression
Equals returns a SQLExpression
func (FieldAccess[T]) FieldValueToScan ¶ added in v0.15.2
func (a FieldAccess[T]) FieldValueToScan(entity any) any
func (FieldAccess[T]) Get ¶ added in v0.16.0
func (a FieldAccess[T]) Get(values map[string]any) any
Get returns the value for its columnName from a map (row).
func (FieldAccess[T]) GreaterThan ¶ added in v1.3.0
func (a FieldAccess[T]) GreaterThan(operand any) binaryExpression
GreaterThan returns a SQLExpression
func (FieldAccess[T]) In ¶ added in v0.14.0
func (a FieldAccess[T]) In(values ...T) binaryExpression
In returns a binary expression to check that the value of the fieldAccess is in the values collection.
func (FieldAccess[T]) IsNotNull ¶ added in v1.0.0
func (a FieldAccess[T]) IsNotNull() binaryExpression
IsNotNull creates a SQL Expresion with IS NOT NULL.
func (FieldAccess[T]) IsNull ¶ added in v1.0.0
func (a FieldAccess[T]) IsNull() binaryExpression
IsNull creates a SQL Expresion with IS NULL.
func (FieldAccess[T]) LessThan ¶
func (a FieldAccess[T]) LessThan(operand any) binaryExpression
LessThan returns a SQLExpression
func (FieldAccess) Or ¶ added in v0.21.0
func (FieldAccess) Or(e SQLExpression) SQLExpression
func (FieldAccess[T]) Set ¶
func (a FieldAccess[T]) Set(v T) FieldAccess[T]
Set returns a new FieldAccess[T] with a value to set on a T.
func (FieldAccess[T]) TableAlias ¶ added in v0.14.0
func (a FieldAccess[T]) TableAlias(alias string) FieldAccess[T]
TableAlias changes the table alias for this column accessor.
func (FieldAccess[T]) ValueToInsert ¶
func (a FieldAccess[T]) ValueToInsert() any
type MutationSet ¶
type MutationSet[T any] struct { // contains filtered or unexported fields }
func MakeMutationSet ¶
func MakeMutationSet[T any](tableInfo TableInfo, selectors []ColumnAccessor, operationType int) MutationSet[T]
func (MutationSet[T]) Exec ¶
func (m MutationSet[T]) Exec(ctx context.Context, conn querier, parameters ...*QueryParameter) ResultIterator[T]
Pre: must be run inside transaction
func (MutationSet[T]) Returning ¶
func (m MutationSet[T]) Returning(columns ...ColumnAccessor) MutationSet[T]
func (MutationSet[T]) SQLOn ¶
func (m MutationSet[T]) SQLOn(w WriteContext)
SQLOn returns the full SQL mutation query
func (MutationSet[T]) Where ¶
func (m MutationSet[T]) Where(condition SQLExpression) MutationSet[T]
type NullJSON ¶ added in v1.0.0
NullJSON is a value that can scan a Nullable value to an empty interface (any)
type QueryParameter ¶ added in v0.23.0
type QueryParameter struct {
// contains filtered or unexported fields
}
QueryParameter captures any value as a parameter to use in a SQL query or mutation.
func NewParameter ¶ added in v0.23.0
func NewParameter(value any) *QueryParameter
TODO can we use type parameterization here? func NewParameter[T any](value T) *QueryParameter[T] { return &QueryParameter{value: value} }
func (QueryParameter) And ¶ added in v0.23.0
func (QueryParameter) And(e SQLExpression) SQLExpression
func (QueryParameter) Or ¶ added in v0.23.0
func (QueryParameter) Or(e SQLExpression) SQLExpression
func (QueryParameter) SQLOn ¶ added in v0.23.0
func (a QueryParameter) SQLOn(w WriteContext)
SQLOn is part of SQLWriter
type QuerySet ¶
type QuerySet[T any] struct { // contains filtered or unexported fields }
func MakeQuerySet ¶
func MakeQuerySet[T any](tableInfo TableInfo, selectors []ColumnAccessor) QuerySet[T]
func (QuerySet) And ¶ added in v0.21.0
func (QuerySet) And(e SQLExpression) SQLExpression
func (QuerySet[T]) Descending ¶
Descending is a SQL instruction for DESC sort option
func (QuerySet[T]) Exec ¶
func (d QuerySet[T]) Exec(ctx context.Context, conn querier, parameters ...*QueryParameter) (list []*T, err error)
func (QuerySet[T]) ExecIntoMaps ¶ added in v0.15.2
func (d QuerySet[T]) ExecIntoMaps(ctx context.Context, conn querier, parameters ...*QueryParameter) (list []map[string]any, err error)
ExecIntoMaps executes the query and returns a list of generic maps (column->value). This can be used if you do not want to get full records types or have multiple custom values.
func (QuerySet[T]) GroupBy ¶
func (q QuerySet[T]) GroupBy(cas ...ColumnAccessor) QuerySet[T]
GroupBy is a SQL instruction
func (QuerySet[T]) Having ¶
func (q QuerySet[T]) Having(condition SQLExpression) QuerySet[T]
func (QuerySet[T]) Iterate ¶
func (d QuerySet[T]) Iterate(ctx context.Context, conn querier, parameters ...*QueryParameter) (*resultIterator[T], error)
func (QuerySet[T]) LeftOuterJoin ¶
func (d QuerySet[T]) LeftOuterJoin(otherQuerySet querySet) join
func (QuerySet) Or ¶ added in v0.21.0
func (QuerySet) Or(e SQLExpression) SQLExpression
func (QuerySet[T]) RightOuterJoin ¶ added in v1.4.2
func (d QuerySet[T]) RightOuterJoin(otherQuerySet querySet) join
func (QuerySet[T]) SQLOn ¶
func (q QuerySet[T]) SQLOn(w WriteContext)
func (QuerySet[T]) TableAlias ¶ added in v0.14.0
TableAlias will override the default table or view alias
func (QuerySet[T]) Where ¶
func (q QuerySet[T]) Where(condition SQLExpression) QuerySet[T]
Where is a SQL instruction
type ResultIterator ¶
type ResultIterator[T any] interface { // Close closes the rows of the iterator, making the connection ready for use again. It is safe // to call Close after rows is already closed. // Close is called implicitly when no return results are expected. Close() // Err returns the Query error if any Err() error // HasNext returns true if a more results are available. If not then Close is called implicitly. HasNext() bool // Next returns the next row populated in a T. Next() (*T, error) }
ResultIterator is returned from executing a Query (or Mutation).
type SQLExpression ¶
type SQLExpression interface { SQLWriter And(expr SQLExpression) SQLExpression Or(expr SQLExpression) SQLExpression }
var EmptyCondition SQLExpression = noCondition{}
func NewSQLConstant ¶ added in v1.2.0
func NewSQLConstant(value any) SQLExpression
type SQLFunction ¶ added in v0.24.0
type SQLFunction struct { Name string Arguments []SQLExpression }
SQLFunction is for calling any Postgres standard function with zero or more arguments as part of your query. Typically used together with pgtalk.SQLAs.
func NewSQLFunction ¶ added in v0.24.0
func NewSQLFunction(name string, arguments ...SQLExpression) SQLFunction
NewSQLFunction creates a new SQLFunction value.
func (SQLFunction) SQLOn ¶ added in v0.24.0
func (f SQLFunction) SQLOn(w WriteContext)
SQLOn is part of SQLWriter
type SQLLiteral ¶ added in v1.0.0
type SQLLiteral struct {
Literal string
}
func (SQLLiteral) SQLOn ¶ added in v1.0.0
func (l SQLLiteral) SQLOn(w WriteContext)
type SQLWriter ¶
type SQLWriter interface { // SQLOn writes a valid SQL on a Writer in a context SQLOn(w WriteContext) }
type TableInfo ¶
type TableInfo struct { Name string Schema string Alias string // Columns are all known for this table ;initialized by the package Columns []ColumnAccessor }
func (TableInfo) WithColumns ¶ added in v0.15.0
func (t TableInfo) WithColumns(list []ColumnAccessor) TableInfo
type WriteContext ¶ added in v0.14.0
type WriteContext interface { Write(p []byte) (n int, err error) WithAlias(tableName, alias string) WriteContext TableAlias(tableName, defaultAlias string) string }
func NewWriteContext ¶ added in v0.14.0
func NewWriteContext(w io.Writer) WriteContext
NewWriteContext returns a new WriteContext to produce SQL
Source Files ¶
- access.go
- access_bool.go
- access_bytes.go
- access_float.go
- access_int32.go
- access_int64.go
- access_json.go
- access_text.go
- access_time.go
- assertions.go
- column_info.go
- computed_field.go
- doc.go
- fieldaccess.go
- function.go
- interfaces.go
- iterator.go
- join.go
- mutationset.go
- nulljson.go
- operators.go
- query_parameter.go
- queryset.go
- sql_writing.go
- table_info.go