Documentation
¶
Index ¶
- Variables
- func Escaper(driverName string) names.Escaper
- func ModelToInsertData[T Model](model T, ignoreFields ...string) (columns []string, values []any)
- func ModelsToInsertData[T Model](models []T, ignoreFields ...string) (columns []string, values [][]any)
- func Placeholder(driverName string) builder.PlaceholderFormat
- func ScanCol[T any](row *sql.Rows, t T) error
- func ScanCols[T any](rows *sql.Rows, v *[]T) error
- func ScanModel[T Model](row *sql.Rows, m T) error
- func ScanModels[T Model](rows *sql.Rows, models *[]T) error
- type Config
- type DBProxy
- type DeleteStmt
- func (s *DeleteStmt[T]) Clone() *DeleteStmt[T]
- func (s *DeleteStmt[T]) Exec(ctx context.Context) (rowsAffected int64, err error)
- func (s *DeleteStmt[T]) From(from string) *DeleteStmt[T]
- func (s *DeleteStmt[T]) ID(id any) *DeleteStmt[T]
- func (s *DeleteStmt[T]) Limit(limit uint64) *DeleteStmt[T]
- func (s *DeleteStmt[T]) Offset(offset uint64) *DeleteStmt[T]
- func (s *DeleteStmt[T]) OrderBy(orderBys ...string) *DeleteStmt[T]
- func (s *DeleteStmt[T]) Prefix(sql string, args ...any) *DeleteStmt[T]
- func (s *DeleteStmt[T]) PrefixExpr(expr builder.Sqlizer) *DeleteStmt[T]
- func (s *DeleteStmt[T]) Suffix(sql string, args ...any) *DeleteStmt[T]
- func (s *DeleteStmt[T]) SuffixExpr(expr builder.Sqlizer) *DeleteStmt[T]
- func (s *DeleteStmt[T]) Where(pred any, args ...any) *DeleteStmt[T]
- type DialectConfig
- type Engine
- func (e *Engine) Close() error
- func (e *Engine) DriverName() string
- func (e *Engine) Escaper() names.Escaper
- func (e *Engine) Exec(ctx context.Context, query string, args ...any) (result sql.Result, err error)
- func (e *Engine) Exist(ctx context.Context, query string, args ...any) (exist bool, err error)
- func (e *Engine) IgnoreStrategy() IgnoreStrategy
- func (e *Engine) Placeholder() builder.PlaceholderFormat
- func (e *Engine) Query(ctx context.Context, query string, args ...any) (rows *sql.Rows, err error)
- func (e *Engine) SupportsForUpdate() bool
- func (e *Engine) SupportsLastInsertId() bool
- func (e *Engine) SupportsReturning() bool
- func (e *Engine) TX(ctx context.Context, fn func(context.Context) error) error
- func (e *Engine) TXWithOptions(ctx context.Context, opts *sql.TxOptions, fn func(context.Context) error) error
- type Execer
- type FieldDescriptor
- type FieldFlag
- type FileDescriptor
- type IgnoreStrategy
- type Import
- type InsertStmt
- func (s *InsertStmt[T]) AddModel(model T) *InsertStmt[T]
- func (s *InsertStmt[T]) AddModels(models ...T) *InsertStmt[T]
- func (s *InsertStmt[T]) Clone() *InsertStmt[T]
- func (s *InsertStmt[T]) Columns(columns ...string) *InsertStmt[T]
- func (s *InsertStmt[T]) Exec(ctx context.Context) (rowsAffected int64, err error)
- func (s *InsertStmt[T]) Ignore() *InsertStmt[T]
- func (s *InsertStmt[T]) Prefix(sql string, args ...any) *InsertStmt[T]
- func (s *InsertStmt[T]) PrefixExpr(expr builder.Sqlizer) *InsertStmt[T]
- func (s *InsertStmt[T]) Suffix(sql string, args ...any) *InsertStmt[T]
- func (s *InsertStmt[T]) SuffixExpr(expr builder.Sqlizer) *InsertStmt[T]
- func (s *InsertStmt[T]) Values(values ...any) *InsertStmt[T]
- type JSONFieldWrapper
- type Logger
- type Model
- type ModelDescriptor
- type Option
- func WithConnMaxIdleTime(connMaxIdleTime time.Duration) Option
- func WithConnMaxLifetime(connMaxLifetime time.Duration) Option
- func WithDialectConfig(dialect DialectConfig) Option
- func WithEscaper(escaper names.Escaper) Option
- func WithLogSQLArgs(enabled bool) Option
- func WithLogger(logger Logger) Option
- func WithMaxIdleConns(maxIdleConns int) Option
- func WithMaxOpenConns(maxOpenConns int) Option
- func WithPlaceholderFormat(format builder.PlaceholderFormat) Option
- func WithSupportsForUpdate(enabled bool) Option
- func WithSupportsLastInsertID(enabled bool) Option
- func WithSupportsReturning(enabled bool) Option
- type Querier
- type QueryColStmt
- func (s *QueryColStmt[T]) AddColumn(column any, args ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Clone() *QueryColStmt[T]
- func (s *QueryColStmt[T]) Column(column any, args ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) CrossJoin(join string, rest ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Distinct() *QueryColStmt[T]
- func (s *QueryColStmt[T]) Find(ctx context.Context) ([]T, error)
- func (s *QueryColStmt[T]) From(from string) *QueryColStmt[T]
- func (s *QueryColStmt[T]) FromSelect(from *builder.SelectBuilder, alias string) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Get(ctx context.Context) (T, bool, error)
- func (s *QueryColStmt[T]) GroupBy(groupBys ...string) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Having(pred any, rest ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) InnerJoin(join string, rest ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Join(join string, rest ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) JoinClause(pred any, args ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) LeftJoin(join string, rest ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Limit(limit uint64) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Offset(offset uint64) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Options(options ...string) *QueryColStmt[T]
- func (s *QueryColStmt[T]) OrderBy(orderBys ...string) *QueryColStmt[T]
- func (s *QueryColStmt[T]) OrderByClause(pred any, args ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Prefix(sql string, args ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) PrefixExpr(expr builder.Sqlizer) *QueryColStmt[T]
- func (s *QueryColStmt[T]) RemoveColumns() *QueryColStmt[T]
- func (s *QueryColStmt[T]) RemoveLimit() *QueryColStmt[T]
- func (s *QueryColStmt[T]) RemoveOffset() *QueryColStmt[T]
- func (s *QueryColStmt[T]) RightJoin(join string, rest ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Select(columns ...string) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Suffix(sql string, args ...any) *QueryColStmt[T]
- func (s *QueryColStmt[T]) SuffixExpr(expr builder.Sqlizer) *QueryColStmt[T]
- func (s *QueryColStmt[T]) Where(pred any, args ...any) *QueryColStmt[T]
- type QueryModelStmt
- func (s *QueryModelStmt[T]) AddColumn(column any, args ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Clone() *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Column(column any, args ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) CrossJoin(join string, rest ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Distinct() *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Exist(ctx context.Context) (bool, error)
- func (s *QueryModelStmt[T]) Find(ctx context.Context) ([]T, error)
- func (s *QueryModelStmt[T]) From(from string) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) FromSelect(from *builder.SelectBuilder, alias string) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Get(ctx context.Context) (T, error)
- func (s *QueryModelStmt[T]) GroupBy(groupBys ...string) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Having(pred any, rest ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) ID(id any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) InnerJoin(join string, rest ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Join(join string, rest ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) JoinClause(pred any, args ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) LeftJoin(join string, rest ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Limit(limit uint64) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Offset(offset uint64) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Options(options ...string) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) OrderBy(orderBys ...string) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) OrderByClause(pred any, args ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Page(ctx context.Context, page, size uint64) ([]T, uint64, error)
- func (s *QueryModelStmt[T]) Prefix(sql string, args ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) PrefixExpr(expr builder.Sqlizer) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) RemoveColumns() *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) RemoveLimit() *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) RemoveOffset() *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) RightJoin(join string, rest ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Select(columns ...string) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Suffix(sql string, args ...any) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) SuffixExpr(expr builder.Sqlizer) *QueryModelStmt[T]
- func (s *QueryModelStmt[T]) Where(pred any, args ...any) *QueryModelStmt[T]
- type Repository
- func (r *Repository[T]) Delete(ctx context.Context, id any) (rowsAffected int64, err error)
- func (r *Repository[T]) DeleteByField(ctx context.Context, field string, value any) (rowsAffected int64, err error)
- func (r *Repository[T]) Exist(ctx context.Context, id any) (bool, error)
- func (r *Repository[T]) ExistByField(ctx context.Context, field string, value any) (bool, error)
- func (r *Repository[T]) Get(ctx context.Context, id any) (T, error)
- func (r *Repository[T]) GetByField(ctx context.Context, field string, value any) (T, error)
- func (r *Repository[T]) Insert(ctx context.Context, model T) (rowsAffected int64, err error)
- func (r *Repository[T]) InsertAll(ctx context.Context, models []T) (rowsAffected int64, err error)
- func (r *Repository[T]) InsertIgnore(ctx context.Context, model T) (rowsAffected int64, err error)
- func (r *Repository[T]) InsertIgnoreAll(ctx context.Context, models []T) (rowsAffected int64, err error)
- func (r *Repository[T]) Lock(ctx context.Context, id any) (T, error)
- func (r *Repository[T]) LockByField(ctx context.Context, field string, value any) (T, error)
- func (r *Repository[T]) Update(ctx context.Context, model T) (rowsAffected int64, err error)
- func (r *Repository[T]) UpdateMap(ctx context.Context, id any, data map[string]any) (rowsAffected int64, err error)
- type Table
- type TableName
- type UnimplementedModel
- type UnimplementedTable
- type UpdateStmt
- func (s *UpdateStmt[T]) Clone() *UpdateStmt[T]
- func (s *UpdateStmt[T]) Exec(ctx context.Context) (rowsAffected int64, err error)
- func (s *UpdateStmt[T]) ID(id any) *UpdateStmt[T]
- func (s *UpdateStmt[T]) Limit(limit uint64) *UpdateStmt[T]
- func (s *UpdateStmt[T]) Offset(offset uint64) *UpdateStmt[T]
- func (s *UpdateStmt[T]) OrderBy(orderBys ...string) *UpdateStmt[T]
- func (s *UpdateStmt[T]) Prefix(sql string, args ...any) *UpdateStmt[T]
- func (s *UpdateStmt[T]) PrefixExpr(expr builder.Sqlizer) *UpdateStmt[T]
- func (s *UpdateStmt[T]) Set(column string, value any) *UpdateStmt[T]
- func (s *UpdateStmt[T]) SetMap(clauses map[string]any) *UpdateStmt[T]
- func (s *UpdateStmt[T]) SetModel(t T) *UpdateStmt[T]
- func (s *UpdateStmt[T]) Suffix(sql string, args ...any) *UpdateStmt[T]
- func (s *UpdateStmt[T]) SuffixExpr(expr builder.Sqlizer) *UpdateStmt[T]
- func (s *UpdateStmt[T]) Table(table string) *UpdateStmt[T]
- func (s *UpdateStmt[T]) Where(pred any, args ...any) *UpdateStmt[T]
Constants ¶
This section is empty.
Variables ¶
var FlagTagMap = map[FieldFlag]string{ FlagPrimaryKey: "primary_key", FlagAutoIncrement: "auto_increment", FlagJson: "json", FlagCreated: "created", FlagUpdated: "updated", FlagVersion: "version", }
FlagTagMap maps field flags to struct tag names.
Functions ¶
func ModelToInsertData ¶
ModelToInsertData builds insert columns and values for a single model.
func ModelsToInsertData ¶
func ModelsToInsertData[T Model](models []T, ignoreFields ...string) (columns []string, values [][]any)
ModelsToInsertData builds shared insert columns and per-row values for models.
func Placeholder ¶
func Placeholder(driverName string) builder.PlaceholderFormat
Placeholder returns the default placeholder format for a driver name.
func ScanCol ¶
ScanCol scans the first column of the first row into t. t must be a pointer (e.g. *int, *string) so the scanned value can be written back to the caller.
Types ¶
type Config ¶
type Config struct {
// Dialect holds SQL dialect behavior for generated statements.
Dialect DialectConfig
// contains filtered or unexported fields
}
Config holds engine settings assembled from Option values.
type DeleteStmt ¶
type DeleteStmt[T Table] struct { // contains filtered or unexported fields }
DeleteStmt is a fluent DELETE builder for table T.
func Delete ¶
func Delete[T Table](engine *Engine) *DeleteStmt[T]
Delete builds a DELETE statement for table T.
func (*DeleteStmt[T]) Clone ¶ added in v1.0.0
func (s *DeleteStmt[T]) Clone() *DeleteStmt[T]
Clone returns a copy of the statement state. Terminal methods still reset only the statement they are called on.
func (*DeleteStmt[T]) Exec ¶
func (s *DeleteStmt[T]) Exec(ctx context.Context) (rowsAffected int64, err error)
Exec executes the built DELETE statement.
func (*DeleteStmt[T]) From ¶
func (s *DeleteStmt[T]) From(from string) *DeleteStmt[T]
From overrides the target table name.
func (*DeleteStmt[T]) ID ¶
func (s *DeleteStmt[T]) ID(id any) *DeleteStmt[T]
ID adds a single-column primary key predicate using the model metadata.
func (*DeleteStmt[T]) Limit ¶
func (s *DeleteStmt[T]) Limit(limit uint64) *DeleteStmt[T]
Limit sets a LIMIT clause on the query.
func (*DeleteStmt[T]) Offset ¶
func (s *DeleteStmt[T]) Offset(offset uint64) *DeleteStmt[T]
Offset sets a OFFSET clause on the query.
func (*DeleteStmt[T]) OrderBy ¶
func (s *DeleteStmt[T]) OrderBy(orderBys ...string) *DeleteStmt[T]
OrderBy adds ORDER BY expressions to the query.
func (*DeleteStmt[T]) Prefix ¶
func (s *DeleteStmt[T]) Prefix(sql string, args ...any) *DeleteStmt[T]
Prefix adds an expression to the beginning of the query.
func (*DeleteStmt[T]) PrefixExpr ¶
func (s *DeleteStmt[T]) PrefixExpr(expr builder.Sqlizer) *DeleteStmt[T]
PrefixExpr adds an expression to the very beginning of the query.
func (*DeleteStmt[T]) Suffix ¶
func (s *DeleteStmt[T]) Suffix(sql string, args ...any) *DeleteStmt[T]
Suffix adds an expression to the end of the query.
func (*DeleteStmt[T]) SuffixExpr ¶
func (s *DeleteStmt[T]) SuffixExpr(expr builder.Sqlizer) *DeleteStmt[T]
SuffixExpr adds an expression to the end of the query.
func (*DeleteStmt[T]) Where ¶
func (s *DeleteStmt[T]) Where(pred any, args ...any) *DeleteStmt[T]
Where adds WHERE expressions to the query.
type DialectConfig ¶ added in v1.0.0
type DialectConfig struct {
// PlaceholderFormat specifies the format of placeholders used in SQL queries (e.g. ?, $1, :1).
PlaceholderFormat builder.PlaceholderFormat
// Escaper escapes table names and column names.
Escaper names.Escaper
// SupportsReturning indicates whether the dialect supports RETURNING clauses.
SupportsReturning bool
// SupportsLastInsertID indicates whether the dialect supports LastInsertId.
SupportsLastInsertID bool
// SupportsForUpdate indicates whether the dialect supports FOR UPDATE.
SupportsForUpdate bool
// IgnoreStrategy defines the dialect-specific INSERT IGNORE syntax.
IgnoreStrategy IgnoreStrategy
}
DialectConfig holds SQL dialect behavior used by an Engine.
func DefaultDialectConfig ¶ added in v1.0.0
func DefaultDialectConfig(driverName string) DialectConfig
DefaultDialectConfig returns the built-in dialect behavior for a driver name.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine wraps a sql.DB and the driver-specific behavior lorm needs. An Engine should be created once per database and used as a global singleton. Do not copy an Engine value; transaction isolation relies on pointer identity.
func NewEngine ¶
NewEngine opens a database connection and applies the provided options. It uses a background context for the initial connectivity check; use NewEngineContext to supply a context with timeout or cancellation.
func NewEngineContext ¶ added in v1.0.0
func NewEngineContext(ctx context.Context, driverName, dsn string, option ...Option) (*Engine, error)
NewEngineContext is like NewEngine but accepts a context for the initial connectivity check so callers can enforce a timeout.
func (*Engine) DriverName ¶
DriverName returns the configured database driver name.
func (*Engine) Exec ¶
func (e *Engine) Exec(ctx context.Context, query string, args ...any) (result sql.Result, err error)
Exec executes a statement against the current session or transaction.
func (*Engine) IgnoreStrategy ¶ added in v1.0.0
func (e *Engine) IgnoreStrategy() IgnoreStrategy
IgnoreStrategy returns the configured insert-ignore strategy for the dialect.
func (*Engine) Placeholder ¶
func (e *Engine) Placeholder() builder.PlaceholderFormat
Placeholder returns the placeholder format configured for the engine.
func (*Engine) SupportsForUpdate ¶ added in v1.0.0
SupportsForUpdate returns true if the database driver supports FOR UPDATE.
func (*Engine) SupportsLastInsertId ¶
SupportsLastInsertId returns true if the database driver supports LastInsertId
func (*Engine) SupportsReturning ¶
SupportsReturning returns true if the database driver supports RETURNING clause
func (*Engine) TXWithOptions ¶ added in v1.0.0
func (e *Engine) TXWithOptions(ctx context.Context, opts *sql.TxOptions, fn func(context.Context) error) error
TXWithOptions runs fn in a transaction using the provided sql.TxOptions.
Nested calls still reuse the existing transaction from the incoming context.
type Execer ¶
type Execer interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}
Execer executes SQL statements with context.
type FieldDescriptor ¶
type FieldDescriptor struct {
Name string
FullName string
DBField string
Type string
Flag FieldFlag
EnsureFullName string `json:",omitempty"`
EnsureType string `json:",omitempty"`
}
FieldDescriptor stores field information
type FieldFlag ¶
type FieldFlag uint8
FieldFlag describes special handling for a model field.
type FileDescriptor ¶
type FileDescriptor struct {
Path string
LormImportAlias string
Package string
Imports []*Import
Structs []*ModelDescriptor
}
FileDescriptor describes a source file used by lorm metadata/code generation.
func (*FileDescriptor) JsonMarshal ¶
func (d *FileDescriptor) JsonMarshal() string
JsonMarshal returns d encoded as JSON.
func (*FileDescriptor) RawVarPrefix ¶
func (d *FileDescriptor) RawVarPrefix() string
RawVarPrefix returns the stable generated variable prefix for the file.
type IgnoreStrategy ¶ added in v1.0.0
type IgnoreStrategy uint8
IgnoreStrategy defines how INSERT IGNORE is expressed in different SQL dialects.
const ( // IgnoreKeyword uses "INSERT IGNORE INTO ..." (MySQL/MariaDB default). IgnoreKeyword IgnoreStrategy = iota // IgnoreOrKeyword uses "INSERT OR IGNORE INTO ..." (SQLite). IgnoreOrKeyword // IgnoreConflictSuffix appends "ON CONFLICT DO NOTHING" (PostgreSQL). IgnoreConflictSuffix )
type InsertStmt ¶
type InsertStmt[T Table] struct { // contains filtered or unexported fields }
InsertStmt is a fluent INSERT builder for table T.
func Insert ¶
func Insert[T Table](engine *Engine) *InsertStmt[T]
Insert builds an INSERT statement for table T.
func (*InsertStmt[T]) AddModel ¶
func (s *InsertStmt[T]) AddModel(model T) *InsertStmt[T]
AddModel appends a model to the insert batch.
func (*InsertStmt[T]) AddModels ¶
func (s *InsertStmt[T]) AddModels(models ...T) *InsertStmt[T]
AddModels appends models to the insert batch.
func (*InsertStmt[T]) Clone ¶ added in v1.0.0
func (s *InsertStmt[T]) Clone() *InsertStmt[T]
Clone returns a copy of the statement state. Terminal methods still reset only the statement they are called on.
func (*InsertStmt[T]) Columns ¶
func (s *InsertStmt[T]) Columns(columns ...string) *InsertStmt[T]
Columns overrides the INSERT column list.
Prefer AddModel/AddModels for model-based inserts. When Columns/Values are used together with AddModel/AddModels, the model-derived values still take effect during Exec.
func (*InsertStmt[T]) Exec ¶
func (s *InsertStmt[T]) Exec(ctx context.Context) (rowsAffected int64, err error)
Exec executes the INSERT and backfills generated primary keys when possible.
Generated primary keys are only backfilled when the driver can return a stable one-to-one mapping between inserted rows and generated values. Batch inserts on LastInsertId-only dialects intentionally do not infer or synthesize per-row IDs.
func (*InsertStmt[T]) Ignore ¶
func (s *InsertStmt[T]) Ignore() *InsertStmt[T]
Ignore enables duplicate-conflict suppression for drivers that support it.
func (*InsertStmt[T]) Prefix ¶
func (s *InsertStmt[T]) Prefix(sql string, args ...any) *InsertStmt[T]
func (*InsertStmt[T]) PrefixExpr ¶
func (s *InsertStmt[T]) PrefixExpr(expr builder.Sqlizer) *InsertStmt[T]
func (*InsertStmt[T]) Suffix ¶
func (s *InsertStmt[T]) Suffix(sql string, args ...any) *InsertStmt[T]
func (*InsertStmt[T]) SuffixExpr ¶
func (s *InsertStmt[T]) SuffixExpr(expr builder.Sqlizer) *InsertStmt[T]
func (*InsertStmt[T]) Values ¶
func (s *InsertStmt[T]) Values(values ...any) *InsertStmt[T]
Values appends a raw VALUES row to the underlying builder.
Prefer AddModel/AddModels for model-based inserts. When Columns/Values are used together with AddModel/AddModels, the model-derived values still take effect during Exec.
type JSONFieldWrapper ¶
type JSONFieldWrapper struct {
// contains filtered or unexported fields
}
JSONFieldWrapper adapts a field value to database/sql and JSON interfaces.
func NewJSONFieldWrapper ¶
func NewJSONFieldWrapper(v any) *JSONFieldWrapper
NewJSONFieldWrapper wraps v so lorm can scan and write it as JSON.
func (*JSONFieldWrapper) MarshalJSON ¶
func (s *JSONFieldWrapper) MarshalJSON() ([]byte, error)
MarshalJSON returns the wrapped value encoded as JSON.
func (*JSONFieldWrapper) Scan ¶
func (s *JSONFieldWrapper) Scan(src any) error
Scan implements sql.Scanner for JSON-encoded columns.
func (*JSONFieldWrapper) String ¶
func (s *JSONFieldWrapper) String() string
String returns the JSON encoding of the wrapped value.
func (*JSONFieldWrapper) UnmarshalJSON ¶
func (s *JSONFieldWrapper) UnmarshalJSON(data []byte) error
UnmarshalJSON decodes JSON into the wrapped value.
type Logger ¶
type Logger interface {
DebugContext(ctx context.Context, msg string, args ...any)
InfoContext(ctx context.Context, msg string, args ...any)
WarnContext(ctx context.Context, msg string, args ...any)
ErrorContext(ctx context.Context, msg string, args ...any)
}
Logger is the minimal structured logger interface used by Engine.
type Model ¶
type Model interface {
// New create a new model instance
New() Model
// LormFieldPtr returns a field pointer by db field name. JSON fields should
// return a JSONFieldWrapper around the underlying field pointer.
LormFieldPtr(name string) any
// LormModelDescriptor return db fields of this Model
LormModelDescriptor() *ModelDescriptor
// contains filtered or unexported methods
}
Model describes the metadata and field access lorm needs for persistence.
type ModelDescriptor ¶
type ModelDescriptor struct {
Name string
TableName string
Fields []*FieldDescriptor
}
ModelDescriptor stores struct information
func (*ModelDescriptor) AllFields ¶
func (m *ModelDescriptor) AllFields() []string
AllFields returns all database column names in declaration order.
func (*ModelDescriptor) FlagFields ¶
func (m *ModelDescriptor) FlagFields(flag FieldFlag) []string
FlagFields returns database column names whose flags include flag.
type Option ¶
type Option func(*Config)
Option mutates Config during engine construction.
func WithConnMaxIdleTime ¶
WithConnMaxIdleTime sets the maximum idle time of a connection
func WithConnMaxLifetime ¶
WithConnMaxLifetime sets the maximum lifetime of a connection
func WithDialectConfig ¶ added in v1.0.0
func WithDialectConfig(dialect DialectConfig) Option
WithDialectConfig sets the dialect behavior as a single config value.
func WithLogSQLArgs ¶ added in v1.0.0
WithLogSQLArgs controls whether engine logs include SQL argument values.
func WithMaxIdleConns ¶
WithMaxIdleConns sets the maximum number of idle connections
func WithMaxOpenConns ¶
WithMaxOpenConns sets the maximum number of open connections
func WithPlaceholderFormat ¶
func WithPlaceholderFormat(format builder.PlaceholderFormat) Option
WithPlaceholderFormat sets the placeholder format
func WithSupportsForUpdate ¶ added in v1.0.0
WithSupportsForUpdate overrides FOR UPDATE support detection.
func WithSupportsLastInsertID ¶ added in v1.0.0
WithSupportsLastInsertID overrides LastInsertId support detection.
func WithSupportsReturning ¶ added in v1.0.0
WithSupportsReturning overrides RETURNING support detection.
type Querier ¶
type Querier interface {
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}
Querier executes SQL queries with context.
type QueryColStmt ¶
type QueryColStmt[T any] struct { // contains filtered or unexported fields }
QueryColStmt is a fluent SELECT builder that scans scalar values of T.
func QueryCol ¶
func QueryCol[T any](engine *Engine) *QueryColStmt[T]
QueryCol builds a SELECT statement that scans into a single column of T.
func (*QueryColStmt[T]) AddColumn ¶
func (s *QueryColStmt[T]) AddColumn(column any, args ...any) *QueryColStmt[T]
AddColumn adds a result column to the query. Unlike Select, AddColumn accepts args which will be bound to placeholders in the columns string, for example:
AddColumn("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)
func (*QueryColStmt[T]) Clone ¶ added in v1.0.0
func (s *QueryColStmt[T]) Clone() *QueryColStmt[T]
Clone returns a copy of the statement state. Terminal methods still reset only the statement they are called on.
func (*QueryColStmt[T]) Column ¶
func (s *QueryColStmt[T]) Column(column any, args ...any) *QueryColStmt[T]
Column adds a result column to the query. Unlike Select, Column accepts args which will be bound to placeholders in the columns string, for example:
AddColumn("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)
func (*QueryColStmt[T]) CrossJoin ¶
func (s *QueryColStmt[T]) CrossJoin(join string, rest ...any) *QueryColStmt[T]
CrossJoin adds a CROSS JOIN clause to the query.
func (*QueryColStmt[T]) Distinct ¶
func (s *QueryColStmt[T]) Distinct() *QueryColStmt[T]
Distinct adds a DISTINCT clause to the query.
func (*QueryColStmt[T]) Find ¶
func (s *QueryColStmt[T]) Find(ctx context.Context) ([]T, error)
Find returns all values from the first selected column.
func (*QueryColStmt[T]) From ¶
func (s *QueryColStmt[T]) From(from string) *QueryColStmt[T]
From sets the FROM clause of the query.
func (*QueryColStmt[T]) FromSelect ¶
func (s *QueryColStmt[T]) FromSelect(from *builder.SelectBuilder, alias string) *QueryColStmt[T]
FromSelect sets a subquery into the FROM clause of the query.
func (*QueryColStmt[T]) Get ¶
func (s *QueryColStmt[T]) Get(ctx context.Context) (T, bool, error)
Get returns the first column value and whether a row was found.
func (*QueryColStmt[T]) GroupBy ¶
func (s *QueryColStmt[T]) GroupBy(groupBys ...string) *QueryColStmt[T]
GroupBy adds GROUP BY expressions to the query.
func (*QueryColStmt[T]) Having ¶
func (s *QueryColStmt[T]) Having(pred any, rest ...any) *QueryColStmt[T]
Having adds an expression to the HAVING clause of the query.
See Where.
func (*QueryColStmt[T]) InnerJoin ¶
func (s *QueryColStmt[T]) InnerJoin(join string, rest ...any) *QueryColStmt[T]
InnerJoin adds a INNER JOIN clause to the query.
func (*QueryColStmt[T]) Join ¶
func (s *QueryColStmt[T]) Join(join string, rest ...any) *QueryColStmt[T]
Join adds a JOIN clause to the query.
func (*QueryColStmt[T]) JoinClause ¶
func (s *QueryColStmt[T]) JoinClause(pred any, args ...any) *QueryColStmt[T]
JoinClause adds a join clause to the query.
func (*QueryColStmt[T]) LeftJoin ¶
func (s *QueryColStmt[T]) LeftJoin(join string, rest ...any) *QueryColStmt[T]
LeftJoin adds a LEFT JOIN clause to the query.
func (*QueryColStmt[T]) Limit ¶
func (s *QueryColStmt[T]) Limit(limit uint64) *QueryColStmt[T]
Limit sets a LIMIT clause on the query.
func (*QueryColStmt[T]) Offset ¶
func (s *QueryColStmt[T]) Offset(offset uint64) *QueryColStmt[T]
Offset sets a OFFSET clause on the query.
func (*QueryColStmt[T]) Options ¶
func (s *QueryColStmt[T]) Options(options ...string) *QueryColStmt[T]
Options adds select option to the query
func (*QueryColStmt[T]) OrderBy ¶
func (s *QueryColStmt[T]) OrderBy(orderBys ...string) *QueryColStmt[T]
OrderBy adds ORDER BY expressions to the query.
func (*QueryColStmt[T]) OrderByClause ¶
func (s *QueryColStmt[T]) OrderByClause(pred any, args ...any) *QueryColStmt[T]
OrderByClause adds ORDER BY clause to the query.
func (*QueryColStmt[T]) Prefix ¶
func (s *QueryColStmt[T]) Prefix(sql string, args ...any) *QueryColStmt[T]
Prefix adds an expression to the beginning of the query
func (*QueryColStmt[T]) PrefixExpr ¶
func (s *QueryColStmt[T]) PrefixExpr(expr builder.Sqlizer) *QueryColStmt[T]
PrefixExpr adds an expression to the very beginning of the query
func (*QueryColStmt[T]) RemoveColumns ¶
func (s *QueryColStmt[T]) RemoveColumns() *QueryColStmt[T]
RemoveColumns remove all columns from query. Must add a new column with Column or Select methods, otherwise return a error.
func (*QueryColStmt[T]) RemoveLimit ¶
func (s *QueryColStmt[T]) RemoveLimit() *QueryColStmt[T]
RemoveLimit removes the LIMIT clause.
func (*QueryColStmt[T]) RemoveOffset ¶
func (s *QueryColStmt[T]) RemoveOffset() *QueryColStmt[T]
RemoveOffset removes OFFSET clause.
func (*QueryColStmt[T]) RightJoin ¶
func (s *QueryColStmt[T]) RightJoin(join string, rest ...any) *QueryColStmt[T]
RightJoin adds a RIGHT JOIN clause to the query.
func (*QueryColStmt[T]) Select ¶
func (s *QueryColStmt[T]) Select(columns ...string) *QueryColStmt[T]
Select set result columns to the query.
func (*QueryColStmt[T]) Suffix ¶
func (s *QueryColStmt[T]) Suffix(sql string, args ...any) *QueryColStmt[T]
Suffix adds an expression to the end of the query
func (*QueryColStmt[T]) SuffixExpr ¶
func (s *QueryColStmt[T]) SuffixExpr(expr builder.Sqlizer) *QueryColStmt[T]
SuffixExpr adds an expression to the end of the query
func (*QueryColStmt[T]) Where ¶
func (s *QueryColStmt[T]) Where(pred any, args ...any) *QueryColStmt[T]
Where adds an expression to the WHERE clause of the query.
Expressions are ANDed together in the generated SQL.
Where accepts several types for its pred argument:
nil OR "" - ignored.
string - SQL expression. If the expression has SQL placeholders then a set of arguments must be passed as well, one for each placeholder.
map[string]any OR Eq - map of SQL expressions to values. Each key is transformed into an expression like "<key> = ?", with the corresponding value bound to the placeholder. Nil, slices, arrays, pointers, and driver values are passed as one bound value; use builder.IsNull, builder.IsNotNull, builder.In, or builder.NotIn explicitly when you need those predicate forms.
Where will panic if pred isn't any of the above types.
type QueryModelStmt ¶
type QueryModelStmt[T Model] struct { // contains filtered or unexported fields }
QueryModelStmt is a fluent SELECT builder that scans rows into models of T.
func Query ¶
func Query[T Model](engine *Engine) *QueryModelStmt[T]
Query builds a SELECT statement that scans rows into models of T.
func (*QueryModelStmt[T]) AddColumn ¶
func (s *QueryModelStmt[T]) AddColumn(column any, args ...any) *QueryModelStmt[T]
AddColumn adds a result column to the query. Unlike Select, AddColumn accepts args which will be bound to placeholders in the columns string, for example:
AddColumn("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)
func (*QueryModelStmt[T]) Clone ¶ added in v1.0.0
func (s *QueryModelStmt[T]) Clone() *QueryModelStmt[T]
Clone returns a copy of the statement state. Terminal methods still reset only the statement they are called on.
func (*QueryModelStmt[T]) Column ¶
func (s *QueryModelStmt[T]) Column(column any, args ...any) *QueryModelStmt[T]
Column adds a result column to the query. Unlike Select, Column accepts args which will be bound to placeholders in the columns string, for example:
AddColumn("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)
func (*QueryModelStmt[T]) CrossJoin ¶
func (s *QueryModelStmt[T]) CrossJoin(join string, rest ...any) *QueryModelStmt[T]
CrossJoin adds a CROSS JOIN clause to the query.
func (*QueryModelStmt[T]) Distinct ¶
func (s *QueryModelStmt[T]) Distinct() *QueryModelStmt[T]
Distinct adds a DISTINCT clause to the query.
func (*QueryModelStmt[T]) Exist ¶
func (s *QueryModelStmt[T]) Exist(ctx context.Context) (bool, error)
Exist reports whether the query returns at least one row.
func (*QueryModelStmt[T]) Find ¶
func (s *QueryModelStmt[T]) Find(ctx context.Context) ([]T, error)
Find returns all matching models.
func (*QueryModelStmt[T]) From ¶
func (s *QueryModelStmt[T]) From(from string) *QueryModelStmt[T]
From sets the FROM clause of the query.
func (*QueryModelStmt[T]) FromSelect ¶
func (s *QueryModelStmt[T]) FromSelect(from *builder.SelectBuilder, alias string) *QueryModelStmt[T]
FromSelect sets a subquery into the FROM clause of the query.
func (*QueryModelStmt[T]) Get ¶
func (s *QueryModelStmt[T]) Get(ctx context.Context) (T, error)
Get returns the first matching model or the zero value when no row matches.
func (*QueryModelStmt[T]) GroupBy ¶
func (s *QueryModelStmt[T]) GroupBy(groupBys ...string) *QueryModelStmt[T]
GroupBy adds GROUP BY expressions to the query.
func (*QueryModelStmt[T]) Having ¶
func (s *QueryModelStmt[T]) Having(pred any, rest ...any) *QueryModelStmt[T]
Having adds an expression to the HAVING clause of the query.
See Where.
func (*QueryModelStmt[T]) ID ¶
func (s *QueryModelStmt[T]) ID(id any) *QueryModelStmt[T]
ID adds a single-column primary key predicate derived from the model metadata.
func (*QueryModelStmt[T]) InnerJoin ¶
func (s *QueryModelStmt[T]) InnerJoin(join string, rest ...any) *QueryModelStmt[T]
InnerJoin adds a INNER JOIN clause to the query.
func (*QueryModelStmt[T]) Join ¶
func (s *QueryModelStmt[T]) Join(join string, rest ...any) *QueryModelStmt[T]
Join adds a JOIN clause to the query.
func (*QueryModelStmt[T]) JoinClause ¶
func (s *QueryModelStmt[T]) JoinClause(pred any, args ...any) *QueryModelStmt[T]
JoinClause adds a join clause to the query.
func (*QueryModelStmt[T]) LeftJoin ¶
func (s *QueryModelStmt[T]) LeftJoin(join string, rest ...any) *QueryModelStmt[T]
LeftJoin adds a LEFT JOIN clause to the query.
func (*QueryModelStmt[T]) Limit ¶
func (s *QueryModelStmt[T]) Limit(limit uint64) *QueryModelStmt[T]
Limit sets a LIMIT clause on the query.
func (*QueryModelStmt[T]) Offset ¶
func (s *QueryModelStmt[T]) Offset(offset uint64) *QueryModelStmt[T]
Offset sets a OFFSET clause on the query.
func (*QueryModelStmt[T]) Options ¶
func (s *QueryModelStmt[T]) Options(options ...string) *QueryModelStmt[T]
Options adds select option to the query
func (*QueryModelStmt[T]) OrderBy ¶
func (s *QueryModelStmt[T]) OrderBy(orderBys ...string) *QueryModelStmt[T]
OrderBy adds ORDER BY expressions to the query.
func (*QueryModelStmt[T]) OrderByClause ¶
func (s *QueryModelStmt[T]) OrderByClause(pred any, args ...any) *QueryModelStmt[T]
OrderByClause adds ORDER BY clause to the query.
func (*QueryModelStmt[T]) Page ¶
Page returns the requested page of results together with the total row count.
func (*QueryModelStmt[T]) Prefix ¶
func (s *QueryModelStmt[T]) Prefix(sql string, args ...any) *QueryModelStmt[T]
Prefix adds an expression to the beginning of the query
func (*QueryModelStmt[T]) PrefixExpr ¶
func (s *QueryModelStmt[T]) PrefixExpr(expr builder.Sqlizer) *QueryModelStmt[T]
PrefixExpr adds an expression to the very beginning of the query
func (*QueryModelStmt[T]) RemoveColumns ¶
func (s *QueryModelStmt[T]) RemoveColumns() *QueryModelStmt[T]
RemoveColumns remove all columns from query. Must add a new column with Column or Select methods, otherwise return a error.
func (*QueryModelStmt[T]) RemoveLimit ¶
func (s *QueryModelStmt[T]) RemoveLimit() *QueryModelStmt[T]
RemoveLimit removes the LIMIT clause.
func (*QueryModelStmt[T]) RemoveOffset ¶
func (s *QueryModelStmt[T]) RemoveOffset() *QueryModelStmt[T]
RemoveOffset removes OFFSET clause.
func (*QueryModelStmt[T]) RightJoin ¶
func (s *QueryModelStmt[T]) RightJoin(join string, rest ...any) *QueryModelStmt[T]
RightJoin adds a RIGHT JOIN clause to the query.
func (*QueryModelStmt[T]) Select ¶
func (s *QueryModelStmt[T]) Select(columns ...string) *QueryModelStmt[T]
Select set result columns to the query.
func (*QueryModelStmt[T]) Suffix ¶
func (s *QueryModelStmt[T]) Suffix(sql string, args ...any) *QueryModelStmt[T]
Suffix adds an expression to the end of the query
func (*QueryModelStmt[T]) SuffixExpr ¶
func (s *QueryModelStmt[T]) SuffixExpr(expr builder.Sqlizer) *QueryModelStmt[T]
SuffixExpr adds an expression to the end of the query
func (*QueryModelStmt[T]) Where ¶
func (s *QueryModelStmt[T]) Where(pred any, args ...any) *QueryModelStmt[T]
Where adds an expression to the WHERE clause of the query.
Expressions are ANDed together in the generated SQL.
Where accepts several types for its pred argument:
nil OR "" - ignored.
string - SQL expression. If the expression has SQL placeholders then a set of arguments must be passed as well, one for each placeholder.
map[string]any OR Eq - map of SQL expressions to values. Each key is transformed into an expression like "<key> = ?", with the corresponding value bound to the placeholder. Nil, slices, arrays, pointers, and driver values are passed as one bound value; use builder.IsNull, builder.IsNotNull, builder.In, or builder.NotIn explicitly when you need those predicate forms.
Where will panic if pred isn't any of the above types.
type Repository ¶
Repository provides common CRUD helpers for table T.
func NewRepository ¶
func NewRepository[T Table](engine *Engine) *Repository[T]
NewRepository creates a Repository backed by engine.
func (*Repository[T]) DeleteByField ¶
func (r *Repository[T]) DeleteByField(ctx context.Context, field string, value any) (rowsAffected int64, err error)
DeleteByField deletes rows matching field = value.
func (*Repository[T]) ExistByField ¶
ExistByField reports whether any row matches field = value.
func (*Repository[T]) Get ¶
func (r *Repository[T]) Get(ctx context.Context, id any) (T, error)
Get loads a row by its single-column primary key.
func (*Repository[T]) GetByField ¶
GetByField loads the first row matching field = value.
func (*Repository[T]) Insert ¶
func (r *Repository[T]) Insert(ctx context.Context, model T) (rowsAffected int64, err error)
Insert inserts a model.
func (*Repository[T]) InsertAll ¶
func (r *Repository[T]) InsertAll(ctx context.Context, models []T) (rowsAffected int64, err error)
InsertAll inserts models in one batch.
func (*Repository[T]) InsertIgnore ¶
func (r *Repository[T]) InsertIgnore(ctx context.Context, model T) (rowsAffected int64, err error)
InsertIgnore inserts a model while ignoring duplicate conflicts when supported.
func (*Repository[T]) InsertIgnoreAll ¶
func (r *Repository[T]) InsertIgnoreAll(ctx context.Context, models []T) (rowsAffected int64, err error)
InsertIgnoreAll inserts models while ignoring duplicate conflicts when supported.
func (*Repository[T]) Lock ¶
func (r *Repository[T]) Lock(ctx context.Context, id any) (T, error)
Lock loads a row by primary key and appends FOR UPDATE when supported.
func (*Repository[T]) LockByField ¶
LockByField loads the first row matching field = value and locks it for update.
type TableName ¶
type TableName interface {
TableName() string
}
TableName lets a model override its database table name.
type UnimplementedModel ¶
type UnimplementedModel struct{}
UnimplementedModel can be embedded to satisfy the Model marker method.
type UnimplementedTable ¶
type UnimplementedTable struct{}
UnimplementedTable can be embedded to satisfy the Table marker methods.
type UpdateStmt ¶
type UpdateStmt[T Table] struct { // contains filtered or unexported fields }
UpdateStmt is a fluent UPDATE builder for table T.
func Update ¶
func Update[T Table](engine *Engine) *UpdateStmt[T]
Update builds an UPDATE statement for table T.
func (*UpdateStmt[T]) Clone ¶ added in v1.0.0
func (s *UpdateStmt[T]) Clone() *UpdateStmt[T]
Clone returns a copy of the statement state. Terminal methods still reset only the statement they are called on.
func (*UpdateStmt[T]) Exec ¶
func (s *UpdateStmt[T]) Exec(ctx context.Context) (rowsAffected int64, err error)
Exec executes the built UPDATE statement.
func (*UpdateStmt[T]) ID ¶
func (s *UpdateStmt[T]) ID(id any) *UpdateStmt[T]
ID adds a single-column primary key predicate derived from the model metadata.
func (*UpdateStmt[T]) Limit ¶
func (s *UpdateStmt[T]) Limit(limit uint64) *UpdateStmt[T]
Limit sets a LIMIT clause on the query.
func (*UpdateStmt[T]) Offset ¶
func (s *UpdateStmt[T]) Offset(offset uint64) *UpdateStmt[T]
Offset sets a OFFSET clause on the query.
func (*UpdateStmt[T]) OrderBy ¶
func (s *UpdateStmt[T]) OrderBy(orderBys ...string) *UpdateStmt[T]
OrderBy adds ORDER BY expressions to the query.
func (*UpdateStmt[T]) Prefix ¶
func (s *UpdateStmt[T]) Prefix(sql string, args ...any) *UpdateStmt[T]
Prefix adds an expression to the beginning of the query
func (*UpdateStmt[T]) PrefixExpr ¶
func (s *UpdateStmt[T]) PrefixExpr(expr builder.Sqlizer) *UpdateStmt[T]
PrefixExpr adds an expression to the very beginning of the query
func (*UpdateStmt[T]) Set ¶
func (s *UpdateStmt[T]) Set(column string, value any) *UpdateStmt[T]
Set adds SET clauses to the query.
func (*UpdateStmt[T]) SetMap ¶
func (s *UpdateStmt[T]) SetMap(clauses map[string]any) *UpdateStmt[T]
SetMap is a convenience method which calls .Set for each key/value pair in clauses.
func (*UpdateStmt[T]) SetModel ¶
func (s *UpdateStmt[T]) SetModel(t T) *UpdateStmt[T]
SetModel maps model fields into SET and WHERE clauses using descriptor metadata.
SetModel performs a full-field update for regular columns. Zero values are not ignored automatically, so partial updates should prefer SetMap/Set.
func (*UpdateStmt[T]) Suffix ¶
func (s *UpdateStmt[T]) Suffix(sql string, args ...any) *UpdateStmt[T]
Suffix adds an expression to the end of the query
func (*UpdateStmt[T]) SuffixExpr ¶
func (s *UpdateStmt[T]) SuffixExpr(expr builder.Sqlizer) *UpdateStmt[T]
SuffixExpr adds an expression to the end of the query
func (*UpdateStmt[T]) Table ¶
func (s *UpdateStmt[T]) Table(table string) *UpdateStmt[T]
Table overrides the target table name.
func (*UpdateStmt[T]) Where ¶
func (s *UpdateStmt[T]) Where(pred any, args ...any) *UpdateStmt[T]
Where adds WHERE expressions to the query.
See SelectBuilder.Where for more information.