Documentation
¶
Index ¶
- Constants
- Variables
- func CT_GetObject[T attrs.Definer](identifier any) (interface{}, error)
- func CT_ListObjects[T attrs.Definer](amount, offset uint) ([]interface{}, error)
- func CT_ListObjectsByIDs[T attrs.Definer](i []interface{}) ([]interface{}, error)
- func CountObjects[T attrs.Definer](obj T) (int64, error)
- func CreateObject[T attrs.Definer](obj T) error
- func DefinerListToList[T attrs.Definer](list []attrs.Definer) []T
- func DeleteObject[T attrs.Definer](obj T) (int64, error)
- func GetObject[T attrs.Definer](identifier any) (T, error)
- func ListObjects[T attrs.Definer](offset, limit uint64, ordering ...string) ([]T, error)
- func ListObjectsByIDs[T attrs.Definer, T2 any](offset, limit uint64, ids []T2) ([]T, error)
- func RegisterCompiler(driver driver.Driver, compiler func(model attrs.Definer) QueryCompiler)
- func RegisterDriver(driver driver.Driver, database string, supportsReturning ...SupportsReturning)
- func RegisterLookup(lookup string, ...)
- func SaveObject[T attrs.Definer](obj T) error
- func UpdateObject[T attrs.Definer](obj T) (int64, error)
- type CountQuery
- type DB
- type ErrorQuery
- type ExistsQuery
- type ExprGroup
- func (g *ExprGroup) And(exprs ...Expression) Expression
- func (g *ExprGroup) Args() []any
- func (g *ExprGroup) Clone() Expression
- func (g *ExprGroup) IsNot() bool
- func (g *ExprGroup) Not(not bool) Expression
- func (g *ExprGroup) Or(exprs ...Expression) Expression
- func (g *ExprGroup) SQL(sb *strings.Builder)
- func (g *ExprGroup) With(d driver.Driver, m attrs.Definer, quote string) Expression
- type ExprNode
- func (e *ExprNode) And(exprs ...Expression) Expression
- func (e *ExprNode) Args() []any
- func (e *ExprNode) Clone() Expression
- func (e *ExprNode) IsNot() bool
- func (e *ExprNode) Not(not bool) Expression
- func (e *ExprNode) Or(exprs ...Expression) Expression
- func (e *ExprNode) SQL(sb *strings.Builder)
- func (e *ExprNode) With(d driver.Driver, m attrs.Definer, quote string) Expression
- type Expression
- type FieldInfo
- type GenericQueryBuilder
- func (g *GenericQueryBuilder) BuildCountQuery(ctx context.Context, qs *QuerySet, where []Expression, joins []JoinDef, ...) CountQuery
- func (g *GenericQueryBuilder) BuildCreateQuery(ctx context.Context, qs *QuerySet, fields FieldInfo, primary attrs.Field, ...) Query[[]interface{}]
- func (g *GenericQueryBuilder) BuildDeleteQuery(ctx context.Context, qs *QuerySet, where []Expression, joins []JoinDef, ...) CountQuery
- func (g *GenericQueryBuilder) BuildSelectQuery(ctx context.Context, qs *QuerySet, fields []FieldInfo, where []Expression, ...) Query[[][]interface{}]
- func (g *GenericQueryBuilder) BuildUpdateQuery(ctx context.Context, qs *QuerySet, fields FieldInfo, where []Expression, ...) CountQuery
- func (g *GenericQueryBuilder) CommitTransaction() error
- func (g *GenericQueryBuilder) DB() DB
- func (g *GenericQueryBuilder) InTransaction() bool
- func (g *GenericQueryBuilder) Quote() (string, string)
- func (g *GenericQueryBuilder) RollbackTransaction() error
- func (g *GenericQueryBuilder) StartTransaction(ctx context.Context) (Transaction, error)
- func (g *GenericQueryBuilder) SupportsReturning() SupportsReturning
- type JoinDef
- type LogicalOp
- type OrderBy
- type Query
- type QueryCompiler
- type QueryObject
- type QuerySet
- func (qs *QuerySet) All() Query[[]attrs.Definer]
- func (qs *QuerySet) Clone() *QuerySet
- func (qs *QuerySet) Compiler() QueryCompiler
- func (qs *QuerySet) Count() CountQuery
- func (qs *QuerySet) Create(value attrs.Definer) Query[attrs.Definer]
- func (qs *QuerySet) DB() DB
- func (qs *QuerySet) Delete() CountQuery
- func (qs *QuerySet) Distinct() *QuerySet
- func (qs *QuerySet) Exists() ExistsQuery
- func (qs *QuerySet) ExplicitSave() *QuerySet
- func (qs *QuerySet) Filter(key interface{}, vals ...interface{}) *QuerySet
- func (qs *QuerySet) First() Query[attrs.Definer]
- func (qs *QuerySet) ForUpdate() *QuerySet
- func (qs *QuerySet) Get() Query[attrs.Definer]
- func (qs *QuerySet) GetOrCreate(value attrs.Definer) (attrs.Definer, error)
- func (qs *QuerySet) GoString() string
- func (qs *QuerySet) GroupBy(fields ...string) *QuerySet
- func (qs *QuerySet) Having(key interface{}, vals ...interface{}) *QuerySet
- func (qs *QuerySet) Last() Query[attrs.Definer]
- func (qs *QuerySet) Limit(n int) *QuerySet
- func (qs *QuerySet) Model() attrs.Definer
- func (qs *QuerySet) Offset(n int) *QuerySet
- func (qs *QuerySet) OrderBy(fields ...string) *QuerySet
- func (qs *QuerySet) Reverse() *QuerySet
- func (qs *QuerySet) Select(fields ...string) *QuerySet
- func (w *QuerySet) String() string
- func (qs *QuerySet) Union(f func(*QuerySet) *QuerySet) *QuerySet
- func (qs *QuerySet) Update(value attrs.Definer) CountQuery
- func (qs *QuerySet) ValuesList(fields ...string) ValuesListQuery
- type RawExpr
- func (e *RawExpr) And(exprs ...Expression) Expression
- func (e *RawExpr) Args() []any
- func (e *RawExpr) Clone() Expression
- func (e *RawExpr) IsNot() bool
- func (e *RawExpr) Not(not bool) Expression
- func (e *RawExpr) Or(exprs ...Expression) Expression
- func (e *RawExpr) SQL(sb *strings.Builder)
- func (e *RawExpr) With(d driver.Driver, m attrs.Definer, quote string) Expression
- type SignalSave
- type SupportsReturning
- type Transaction
- type Union
- type ValuesListQuery
Constants ¶
const ( ErrNoDatabase errs.Error = "No database connection" ErrUnknownDriver errs.Error = "Unknown driver" ErrNoTableName errs.Error = "No table name" ErrNoWhereClause errs.Error = "No where clause in query" ErrFieldNull errs.Error = "Field cannot be null" ErrLastInsertId errs.Error = "Last insert id is not valid" ErrUnsupportedLookup errs.Error = "Unsupported lookup type" ErrNoResults errs.Error = "No results found" ErrNoRows errs.Error = "No rows in result set" ErrMultipleRows errs.Error = "Multiple rows in result set" ErrTransactionStarted errs.Error = "Transaction already started" ErrFailedStartTransaction errs.Error = "Failed to start transaction" ErrNoTransaction errs.Error = "Transaction was not started" )
const MAX_GET_RESULTS = 21
Variables ¶
var ( // Signal to be executed before inserting / updating a model instance. SignalPreModelSave = signals_saving.Get("queries.model.pre_save") // Signal to be executed after inserting / updating a model instance. SignalPostModelSave = signals_saving.Get("queries.model.post_save") // Signal to be executed before deleting a model instance. SignalPreModelDelete = signals_deleting.Get("queries.model.pre_delete") // Signal to be executed after deleting a model instance. SignalPostModelDelete = signals_deleting.Get("queries.model.post_delete") )
var (
LogQueries = true
)
Functions ¶
func CT_GetObject ¶
CT_GetObject retrieves an object from the database by its identifier.
This is a function with the CT_ prefix to indicate that it is a function to be used in a `contenttypes.ContentTypeDefinition` context.
func CT_ListObjects ¶
CT_ListObjects lists objects from the database.
This is a function with the CT_ prefix to indicate that it is a function to be used in a `contenttypes.ContentTypeDefinition` context.
func CT_ListObjectsByIDs ¶
CT_ListObjectsByIDs lists objects from the database by their IDs.
This is a function with the CT_ prefix to indicate that it is a function to be used in a `contenttypes.ContentTypeDefinition` context.
func CountObjects ¶
CountObjects counts the number of objects in the database.
func CreateObject ¶
CreateObject creates a new object in the database and sets its default values.
It sends a pre-save signal before saving and a post-save signal after saving.
If the object implements the models.Saver interface, it will call the Save method instead of executing a query.
func DefinerListToList ¶ added in v1.0.7
func DeleteObject ¶
DeleteObject deletes an object from the database.
It sends a pre-delete signal before deleting and a post-delete signal after deleting.
If the object implements the models.Deleter interface, it will call the Delete method instead of executing a query.
func GetObject ¶
GetObject retrieves an object from the database by its identifier.
It takes an identifier as a parameter and returns the object of type T.
The identifier can be any type, but it is expected to be the primary key of the object.
func ListObjects ¶
ListObjects lists objects from the database.
It takes an offset and a limit as parameters and returns a slice of objects of type T.
func ListObjectsByIDs ¶
ListObjectsByIDs lists objects from the database by their IDs.
It takes an offset, limit, and a slice of IDs as parameters and returns a slice of objects of type T.
func RegisterCompiler ¶ added in v1.0.7
func RegisterCompiler(driver driver.Driver, compiler func(model attrs.Definer) QueryCompiler)
func RegisterDriver ¶ added in v1.0.2
func RegisterDriver(driver driver.Driver, database string, supportsReturning ...SupportsReturning)
RegisterDriver registers a driver with the given database name.
This is used to determine the database type when using sqlx.
If your driver is not one of: - github.com/go-sql-driver/mysql.MySQLDriver - github.com/mattn/go-sqlite3.SQLiteDriver - github.com/jackc/pgx/v5/stdlib.Driver
Then it explicitly needs to be registered here.
func RegisterLookup ¶ added in v1.0.7
func SaveObject ¶
SaveObject saves an object to the database.
It checks if the primary key is set. If it is not set, it creates a new object. If it is set, it updates the existing object.
It sends a pre-save signal before saving and a post-save signal after saving.
If the object implements the models.Saver interface, it will call the Save method instead of executing a query.
func UpdateObject ¶
UpdateObject updates an existing object in the database.
It sends a pre-save signal before saving and a post-save signal after saving.
If the object implements the models.Saver interface, it will call the Save method instead of executing a query.
Types ¶
type CountQuery ¶ added in v1.0.3
type ErrorQuery ¶ added in v1.0.7
type ErrorQuery[T any] struct { Obj attrs.Definer Compile QueryCompiler Err error }
func (*ErrorQuery[T]) Args ¶ added in v1.0.7
func (e *ErrorQuery[T]) Args() []any
func (*ErrorQuery[T]) Compiler ¶ added in v1.0.8
func (e *ErrorQuery[T]) Compiler() QueryCompiler
func (*ErrorQuery[T]) Exec ¶ added in v1.0.7
func (e *ErrorQuery[T]) Exec() (T, error)
func (*ErrorQuery[T]) Model ¶ added in v1.0.7
func (e *ErrorQuery[T]) Model() attrs.Definer
func (*ErrorQuery[T]) SQL ¶ added in v1.0.7
func (e *ErrorQuery[T]) SQL() string
type ExistsQuery ¶ added in v1.0.3
type ExprGroup ¶
type ExprGroup struct {
// contains filtered or unexported fields
}
ExprGroup
func (*ExprGroup) And ¶
func (g *ExprGroup) And(exprs ...Expression) Expression
func (*ExprGroup) Clone ¶
func (g *ExprGroup) Clone() Expression
func (*ExprGroup) Not ¶
func (g *ExprGroup) Not(not bool) Expression
func (*ExprGroup) Or ¶
func (g *ExprGroup) Or(exprs ...Expression) Expression
type ExprNode ¶
type ExprNode struct {
// contains filtered or unexported fields
}
func (*ExprNode) And ¶
func (e *ExprNode) And(exprs ...Expression) Expression
func (*ExprNode) Clone ¶
func (e *ExprNode) Clone() Expression
func (*ExprNode) Not ¶
func (e *ExprNode) Not(not bool) Expression
func (*ExprNode) Or ¶
func (e *ExprNode) Or(exprs ...Expression) Expression
type Expression ¶
type Expression interface { SQL(sb *strings.Builder) Args() []any IsNot() bool Not(b bool) Expression And(...Expression) Expression Or(...Expression) Expression Clone() Expression With(d driver.Driver, model attrs.Definer, quote string) Expression }
func And ¶
func And(exprs ...Expression) Expression
func Or ¶
func Or(exprs ...Expression) Expression
type FieldInfo ¶ added in v1.0.3
type GenericQueryBuilder ¶ added in v1.0.7
type GenericQueryBuilder struct {
// contains filtered or unexported fields
}
func (*GenericQueryBuilder) BuildCountQuery ¶ added in v1.0.7
func (g *GenericQueryBuilder) BuildCountQuery( ctx context.Context, qs *QuerySet, where []Expression, joins []JoinDef, groupBy []FieldInfo, limit int, offset int, ) CountQuery
func (*GenericQueryBuilder) BuildCreateQuery ¶ added in v1.0.7
func (*GenericQueryBuilder) BuildDeleteQuery ¶ added in v1.0.7
func (g *GenericQueryBuilder) BuildDeleteQuery( ctx context.Context, qs *QuerySet, where []Expression, joins []JoinDef, groupBy []FieldInfo, ) CountQuery
func (*GenericQueryBuilder) BuildSelectQuery ¶ added in v1.0.7
func (*GenericQueryBuilder) BuildUpdateQuery ¶ added in v1.0.7
func (g *GenericQueryBuilder) BuildUpdateQuery( ctx context.Context, qs *QuerySet, fields FieldInfo, where []Expression, joins []JoinDef, groupBy []FieldInfo, values []any, ) CountQuery
func (*GenericQueryBuilder) CommitTransaction ¶ added in v1.0.7
func (g *GenericQueryBuilder) CommitTransaction() error
func (*GenericQueryBuilder) DB ¶ added in v1.0.7
func (g *GenericQueryBuilder) DB() DB
func (*GenericQueryBuilder) InTransaction ¶ added in v1.0.7
func (g *GenericQueryBuilder) InTransaction() bool
func (*GenericQueryBuilder) Quote ¶ added in v1.0.7
func (g *GenericQueryBuilder) Quote() (string, string)
func (*GenericQueryBuilder) RollbackTransaction ¶ added in v1.0.7
func (g *GenericQueryBuilder) RollbackTransaction() error
func (*GenericQueryBuilder) StartTransaction ¶ added in v1.0.7
func (g *GenericQueryBuilder) StartTransaction(ctx context.Context) (Transaction, error)
func (*GenericQueryBuilder) SupportsReturning ¶ added in v1.0.7
func (g *GenericQueryBuilder) SupportsReturning() SupportsReturning
type QueryCompiler ¶ added in v1.0.7
type QueryCompiler interface { // DB returns the database connection used by the query compiler. // // If a transaction was started, it will return the transaction instead of the database connection. DB() DB // Quote returns the quotes used by the database. // // This is used to quote table and field names. // For example, MySQL uses backticks (`) and PostgreSQL uses double quotes ("). Quote() (front string, back string) // SupportsReturning returns the type of returning supported by the database. // It can be one of the following: // // - SupportsReturningNone: no returning supported // - SupportsReturningLastInsertId: last insert id supported // - SupportsReturningColumns: returning columns supported SupportsReturning() SupportsReturning // StartTransaction starts a new transaction. StartTransaction(ctx context.Context) (Transaction, error) // CommitTransaction commits the current ongoing transaction. CommitTransaction() error // RollbackTransaction rolls back the current ongoing transaction. RollbackTransaction() error // InTransaction returns true if the current query compiler is in a transaction. InTransaction() bool // BuildSelectQuery builds a select query with the given parameters. BuildSelectQuery( ctx context.Context, qs *QuerySet, fields []FieldInfo, where []Expression, having []Expression, joins []JoinDef, groupBy []FieldInfo, orderBy []OrderBy, limit int, offset int, union []Union, forUpdate bool, distinct bool, ) Query[[][]interface{}] // BuildCountQuery builds a count query with the given parameters. BuildCountQuery( ctx context.Context, qs *QuerySet, where []Expression, joins []JoinDef, groupBy []FieldInfo, limit int, offset int, ) CountQuery // BuildCreateQuery builds a create query with the given parameters. BuildCreateQuery( ctx context.Context, qs *QuerySet, fields FieldInfo, primary attrs.Field, values []any, ) Query[[]interface{}] // BuildValuesListQuery builds a values list query with the given parameters. BuildUpdateQuery( ctx context.Context, qs *QuerySet, fields FieldInfo, where []Expression, joins []JoinDef, groupBy []FieldInfo, values []any, ) CountQuery // BuildUpdateQuery builds an update query with the given parameters. BuildDeleteQuery( ctx context.Context, qs *QuerySet, where []Expression, joins []JoinDef, groupBy []FieldInfo, ) CountQuery }
func Compiler ¶ added in v1.0.7
func Compiler(model attrs.Definer) QueryCompiler
func NewGenericQueryBuilder ¶ added in v1.0.7
func NewGenericQueryBuilder(model attrs.Definer) QueryCompiler
type QueryObject ¶ added in v1.0.7
type QueryObject[T1 any] struct { // contains filtered or unexported fields }
func (*QueryObject[T1]) Args ¶ added in v1.0.7
func (q *QueryObject[T1]) Args() []any
func (*QueryObject[T1]) Compiler ¶ added in v1.0.8
func (q *QueryObject[T1]) Compiler() QueryCompiler
func (*QueryObject[T1]) Exec ¶ added in v1.0.7
func (q *QueryObject[T1]) Exec() (T1, error)
func (*QueryObject[T1]) Model ¶ added in v1.0.7
func (q *QueryObject[T1]) Model() attrs.Definer
func (*QueryObject[T1]) SQL ¶ added in v1.0.7
func (q *QueryObject[T1]) SQL() string
type QuerySet ¶
type QuerySet struct {
// contains filtered or unexported fields
}
QuerySet is a struct that represents a query set in the database.
It contains methods to filter, order, and limit the results of a query.
It is used to build and execute queries against the database.
Every method on the queryset returns a new queryset, so that the original queryset is not modified.
It also has a chainable api, so that you can easily build complex queries by chaining methods together.
Queries are built internally with the help of the QueryCompiler interface, which is responsible for generating the SQL queries for the database.
func Objects ¶
Objects creates a new QuerySet for the given model.
It panics if: - the model is nil - the base query info cannot be retrieved
It returns a pointer to a new QuerySet.
The model must implement the Definer interface.
func (*QuerySet) Compiler ¶ added in v1.0.7
func (qs *QuerySet) Compiler() QueryCompiler
func (*QuerySet) Count ¶
func (qs *QuerySet) Count() CountQuery
func (*QuerySet) Delete ¶ added in v1.0.3
func (qs *QuerySet) Delete() CountQuery
func (*QuerySet) Exists ¶ added in v1.0.3
func (qs *QuerySet) Exists() ExistsQuery
func (*QuerySet) ExplicitSave ¶ added in v1.0.8
func (*QuerySet) GetOrCreate ¶ added in v1.0.7
func (*QuerySet) Update ¶ added in v1.0.3
func (qs *QuerySet) Update(value attrs.Definer) CountQuery
func (*QuerySet) ValuesList ¶ added in v1.0.3
func (qs *QuerySet) ValuesList(fields ...string) ValuesListQuery
type RawExpr ¶ added in v1.0.3
type RawExpr struct { Statement string Fields []string Params []any // contains filtered or unexported fields }
RawExpr is a function expression for SQL queries. It is used to represent a function call in SQL queries.
It can be used like so:
RawExpr{ // Represent the SQL function call, with each %s being replaced by the corresponding field in fields. sql: `SUBSTR(TRIM(%s, " "), 0, 2) = ?``, // The fields to be used in the SQL function call. Each field will be replaced by the corresponding value in args. fields: []string{"myField"}, // The arguments to be used in the SQL function call. Each argument will be replaced by the corresponding value in args. args: []any{"ab"}, }
func (*RawExpr) And ¶ added in v1.0.3
func (e *RawExpr) And(exprs ...Expression) Expression
func (*RawExpr) Clone ¶ added in v1.0.3
func (e *RawExpr) Clone() Expression
func (*RawExpr) Not ¶ added in v1.0.3
func (e *RawExpr) Not(not bool) Expression
func (*RawExpr) Or ¶ added in v1.0.3
func (e *RawExpr) Or(exprs ...Expression) Expression
type SignalSave ¶ added in v1.0.8
type SignalSave struct { Instance attrs.Definer Using QueryCompiler }
Signals are used to notify when a model instance is saved or deleted.
SignalSave is only meant to hold the model instance and the query compiler'
type SupportsReturning ¶ added in v1.0.7
type SupportsReturning string
const ( SupportsReturningNone SupportsReturning = "" SupportsReturningLastInsertId SupportsReturning = "last_insert_id" SupportsReturningColumns SupportsReturning = "columns" )