Documentation
¶
Index ¶
- Constants
- Variables
- func AnyToInt(v any) int
- func AnyToInt64(v any) int64
- func AnyToString(value any) string
- func AnyToTime(value any) time.Time
- func ConvertTo(value any, targetType reflect.Type) (any, error)
- func DbIn(count int) string
- func ParseUTCDateTime(value string) (time.Time, error)
- func StringValue(value *string) string
- type DAO
- type DBModel
- type ModelDAO
- type ModelDAOAttacher
- type ModelMetadata
- type ModelRecord
- func (m *ModelRecord[T]) AttachModelDAO(dao *ModelDAO[T], self T)
- func (m *ModelRecord[T]) Clean()
- func (m *ModelRecord[T]) Delete() int64
- func (m *ModelRecord[T]) Dirty(column string)
- func (m *ModelRecord[T]) DirtyAll()
- func (m *ModelRecord[T]) FromAPI(apiData map[string]any) T
- func (m *ModelRecord[T]) GetID() int64
- func (m *ModelRecord[T]) GetModelMetadata() *ModelMetadata
- func (m *ModelRecord[T]) GetValue(columnName string) any
- func (m *ModelRecord[T]) Insert() int64
- func (m *ModelRecord[T]) InsertE() (int64, error)
- func (m *ModelRecord[T]) InsertIgnore() int64
- func (m *ModelRecord[T]) IsClean() bool
- func (m *ModelRecord[T]) IsDirty() bool
- func (m *ModelRecord[T]) Load(id int64) T
- func (m *ModelRecord[T]) LoadE(id int64) (T, error)
- func (m *ModelRecord[T]) Save() int64
- func (m *ModelRecord[T]) SaveE() (int64, error)
- func (m *ModelRecord[T]) SetID(id int64)
- func (m *ModelRecord[T]) SetValue(columnName string, value any) bool
- func (m *ModelRecord[T]) ToAPI() map[string]any
- func (m *ModelRecord[T]) ToJSON() string
- func (m *ModelRecord[T]) ToJSONIndent() string
- func (m *ModelRecord[T]) ToWhere(query *bun.SelectQuery, args map[string]any) *bun.SelectQuery
- func (m *ModelRecord[T]) ToWhereE(query *bun.SelectQuery, args map[string]any) (*bun.SelectQuery, error)
- func (m *ModelRecord[T]) Update() int64
- func (m *ModelRecord[T]) UpdateE() (int64, error)
Constants ¶
const ( DialectMySQL = "mysql" DialectSQLite = "sqlite" DialectPostgreSQL = "pgsql" )
const ( MaxOpenConns = 25 MaxIdleConns = 25 MaxLifetime = 5 * time.Minute )
Variables ¶
var ErrBadArgs = errors.New("jackgodb: bad query argument")
ErrBadArgs is returned by ToWhereE when an argument has the wrong shape.
var ErrNoChanges = errors.New("jackgodb: no changes to update")
ErrNoChanges is returned by UpdateE when the record has no dirty columns.
var ErrNotFound = errors.New("jackgodb: record not found")
ErrNotFound is returned by LoadE when no row matches the id.
var ErrUnknownKey = errors.New("jackgodb: unknown column key")
ErrUnknownKey is returned by ToWhereE when a where or order key is not an alias or column.
var (
SQLDebug = false
)
Functions ¶
func AnyToInt64 ¶
func AnyToString ¶
func StringValue ¶
Types ¶
type DAO ¶
func OpenMySQLDBConnection ¶
func OpenPostgreSQLDBConnection ¶ added in v1.2.0
func OpenSQLiteDBConnection ¶
type ModelDAO ¶
func NewModelDAO ¶
func (*ModelDAO[T]) AttachList ¶
func (dao *ModelDAO[T]) AttachList(recordList []T)
type ModelDAOAttacher ¶
type ModelMetadata ¶
type ModelRecord ¶
type ModelRecord[T DBModel] struct { Dao *ModelDAO[T] Self T // contains filtered or unexported fields }
func (*ModelRecord[T]) AttachModelDAO ¶
func (m *ModelRecord[T]) AttachModelDAO(dao *ModelDAO[T], self T)
func (*ModelRecord[T]) Clean ¶
func (m *ModelRecord[T]) Clean()
func (*ModelRecord[T]) Delete ¶
func (m *ModelRecord[T]) Delete() int64
func (*ModelRecord[T]) Dirty ¶
func (m *ModelRecord[T]) Dirty(column string)
func (*ModelRecord[T]) DirtyAll ¶ added in v1.5.0
func (m *ModelRecord[T]) DirtyAll()
DirtyAll marks every non-primary-key column dirty, so the next Update or Save writes the whole row. Use it when you have changed a record in ways SetValue did not track. Do not use it to save a partial change: any column you did not load will be overwritten with its zero value.
func (*ModelRecord[T]) FromAPI ¶
func (m *ModelRecord[T]) FromAPI(apiData map[string]any) T
func (*ModelRecord[T]) GetID ¶
func (m *ModelRecord[T]) GetID() int64
func (*ModelRecord[T]) GetModelMetadata ¶
func (m *ModelRecord[T]) GetModelMetadata() *ModelMetadata
func (*ModelRecord[T]) GetValue ¶
func (m *ModelRecord[T]) GetValue(columnName string) any
func (*ModelRecord[T]) Insert ¶
func (m *ModelRecord[T]) Insert() int64
Insert is the logging wrapper around InsertE. It returns 0 on failure. Use InsertE to see why.
func (*ModelRecord[T]) InsertE ¶ added in v1.6.0
func (m *ModelRecord[T]) InsertE() (int64, error)
InsertE inserts the record and returns its new ID. A unique-constraint violation is returned as an error. Unlike Insert it does not use INSERT OR IGNORE.
func (*ModelRecord[T]) InsertIgnore ¶
func (m *ModelRecord[T]) InsertIgnore() int64
func (*ModelRecord[T]) IsClean ¶
func (m *ModelRecord[T]) IsClean() bool
func (*ModelRecord[T]) IsDirty ¶
func (m *ModelRecord[T]) IsDirty() bool
func (*ModelRecord[T]) Load ¶
func (m *ModelRecord[T]) Load(id int64) T
func (*ModelRecord[T]) LoadE ¶ added in v1.6.0
func (m *ModelRecord[T]) LoadE(id int64) (T, error)
LoadE loads the record by id. It returns ErrNotFound when no row matches. On any error it returns the zero value of T, so callers should check err before using the result.
func (*ModelRecord[T]) Save ¶
func (m *ModelRecord[T]) Save() int64
func (*ModelRecord[T]) SaveE ¶ added in v1.6.0
func (m *ModelRecord[T]) SaveE() (int64, error)
SaveE inserts when the ID is zero, otherwise updates. Unlike Save it returns the error.
func (*ModelRecord[T]) SetID ¶
func (m *ModelRecord[T]) SetID(id int64)
func (*ModelRecord[T]) SetValue ¶
func (m *ModelRecord[T]) SetValue(columnName string, value any) bool
func (*ModelRecord[T]) ToAPI ¶
func (m *ModelRecord[T]) ToAPI() map[string]any
func (*ModelRecord[T]) ToJSON ¶ added in v1.3.0
func (m *ModelRecord[T]) ToJSON() string
func (*ModelRecord[T]) ToJSONIndent ¶ added in v1.3.0
func (m *ModelRecord[T]) ToJSONIndent() string
func (*ModelRecord[T]) ToWhere ¶
func (m *ModelRecord[T]) ToWhere(query *bun.SelectQuery, args map[string]any) *bun.SelectQuery
ToWhere is the logging wrapper around ToWhereE. On error it logs and returns the query with the error's clauses skipped, so callers that ignore errors do not get an unfiltered query. Use ToWhereE to see the error.
func (*ModelRecord[T]) ToWhereE ¶ added in v1.6.0
func (m *ModelRecord[T]) ToWhereE(query *bun.SelectQuery, args map[string]any) (*bun.SelectQuery, error)
ToWhereE applies limits, order and where from args to query. Keys in "where" and "order" must be an alias or a real column name. Anything else returns ErrUnknownKey, so no raw SQL is accepted from keys.
Supported args:
"limits": int, or [offset, limit]
"order": {"column": "asc"|"desc"}, or ["column", ...]
"where": {"column": value} (equality), or ["column", ...] for a column-only key
(not supported: use query.Where directly for expressions)
func (*ModelRecord[T]) Update ¶
func (m *ModelRecord[T]) Update() int64
func (*ModelRecord[T]) UpdateE ¶ added in v1.6.0
func (m *ModelRecord[T]) UpdateE() (int64, error)
UpdateE updates the dirty columns and returns the rows affected. It returns ErrNoChanges when nothing is dirty, so "no changes" is no longer confused with failure.