Documentation
¶
Overview ¶
Package mud provides a database ORM (Object-Relational Mapping) implementation with support for SQLite, MySQL, and SQL Server databases.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides a database ORM (Object-Relational Mapping) implementation with support for SQLite, MySQL, and SQL Server databases.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides a database ORM (Object-Relational Mapping) implementation with support for SQLite, MySQL, and SQL Server databases.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides a database ORM (Object-Relational Mapping) implementation with support for SQLite, MySQL, and SQL Server databases.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides a database ORM (Object-Relational Mapping) implementation with support for SQLite, MySQL, and SQL Server databases.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides a database ORM (Object-Relational Mapping) implementation with support for SQLite, MySQL, and SQL Server databases.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides interfaces for database operations in the ORM.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides database management interfaces and implementations.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides base model functionality for database entities.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides interfaces and utilities for database model management.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides a simple ORM implementation for MS SQL Server databases.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides a simple ORM implementation for MySQL databases.
Copyright (c) 2025 DaggerTech. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file. Package mud provides a simple ORM implementation for SQLite databases.
Index ¶
- func Fetch[T Modeller](db *DB, criteria ...interface{}) ([]*T, error)
- func First[T Modeller](db *DB, criteria ...interface{}) (*T, error)
- func FromID[T Modeller](db *DB, id string) (*T, error)
- func GetTableName(m Modeller) string
- func GetTypeName(m Manager) string
- func Range[T Modeller](db *DB, criteria ...interface{}) iter.Seq[*T]
- func Version() string
- type Config
- type Criteria
- type DB
- func (db *DB) BeginTransaction() (*sql.Tx, error)
- func (db *DB) Close()
- func (db *DB) CommitTransaction(tx *sql.Tx) error
- func (db *DB) Count(m Modeller, criteria ...interface{}) int
- func (db *DB) Fetch(mdl Modeller, criteria ...interface{}) ([]Modeller, error)
- func (db *DB) First(m Modeller, criteria ...interface{}) (Modeller, error)
- func (db *DB) Range(mdl Modeller, criteria ...interface{}) iter.Seq[Modeller]
- func (db *DB) RawExecute(sql string, tx ...*sql.Tx) error
- func (db *DB) RawScalar(sql string, tx ...*sql.Tx) (interface{}, bool)
- func (db *DB) RawSelect(qry string, tx ...*sql.Tx) ([]map[string]interface{}, error)
- func (db *DB) Refresh(m Modeller) error
- func (db *DB) Remove(m Modeller, tx ...*sql.Tx) error
- func (db *DB) RemoveMany(m Modeller, c *Criteria, tx ...*sql.Tx) (int, error)
- func (db *DB) RollbackTransaction(tx *sql.Tx) error
- func (db *DB) Save(m Modeller, tx ...*sql.Tx) error
- type ErrNoResults
- type FieldSize
- type MSSQLManager
- func (m *MSSQLManager) BuildQuery(where string, order string, limit string, offset string) string
- func (m *MSSQLManager) ConnectionString(cfg *Config) (string, error)
- func (m *MSSQLManager) GetDB() *DB
- func (m *MSSQLManager) IdentityString(f string) string
- func (m *MSSQLManager) IndexCreate() string
- func (m *MSSQLManager) LimitString(c *Criteria) string
- func (m *MSSQLManager) OffsetString(c *Criteria) string
- func (m *MSSQLManager) Operators() []string
- func (m *MSSQLManager) SetDB(db *DB)
- func (m *MSSQLManager) TableCreate() string
- func (m *MSSQLManager) TableExistsQuery(name string) string
- type Manager
- type Model
- type Modeller
- type MySQLManager
- func (m *MySQLManager) BuildQuery(where string, order string, limit string, offset string) string
- func (m *MySQLManager) ConnectionString(cfg *Config) (string, error)
- func (m *MySQLManager) GetDB() *DB
- func (m *MySQLManager) IdentityString(f string) string
- func (m *MySQLManager) IndexCreate() string
- func (m *MySQLManager) LimitString(c *Criteria) string
- func (m *MySQLManager) OffsetString(c *Criteria) string
- func (m *MySQLManager) Operators() []string
- func (m *MySQLManager) SetDB(db *DB)
- func (m *MySQLManager) TableCreate() string
- func (m *MySQLManager) TableExistsQuery(name string) string
- type Remover
- type Restorer
- type SqliteManager
- func (m *SqliteManager) BuildQuery(where string, order string, limit string, offset string) string
- func (m *SqliteManager) ConnectionString(cfg *Config) (string, error)
- func (m *SqliteManager) GetDB() *DB
- func (m *SqliteManager) IdentityString(f string) string
- func (m *SqliteManager) IndexCreate() string
- func (m *SqliteManager) LimitString(c *Criteria) string
- func (m *SqliteManager) OffsetString(c *Criteria) string
- func (m *SqliteManager) Operators() []string
- func (m *SqliteManager) SetDB(db *DB)
- func (m *SqliteManager) TableCreate() string
- func (m *SqliteManager) TableExistsQuery(name string) string
- type Updater
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetTableName ¶
GetTableName determines the database table name for a model. If the model is a pointer, it dereferences it to get the actual type name. The table name is derived from the struct type name.
func GetTypeName ¶
GetTypeName returns the type name of the manager implementation
Types ¶
type Config ¶
type Config struct { // Type specifies the type of database (sqlite, mysql, sqlserver) Type string `json:"type"` // Host specifies the hostname or IP address of the database server // This is optional for SQLite databases Host string `json:"host,omitzero"` // Database specifies the name of the database to connect to Database string `json:"database"` // User specifies the username for database authentication // This is optional for SQLite databases User string `json:"user,omitzero"` // Password specifies the password for database authentication // This is optional for SQLite databases Password string `json:"password,omitzero"` // Deletable indicates whether records can be permanently deleted // When false, records are soft-deleted (marked with delete date) Deletable bool `json:"deletable,omitzero"` // DisabledTransactions indicates whether database transactions should be disabled // When true, each operation will be executed independently DisabledTransactions bool `json:"disabledTransactions,omitzero"` }
Config represents the configuration for a database connection. It contains all the necessary information to establish and configure a database connection.
type Criteria ¶
type Criteria struct { // Where defines the WHERE condition for the query Where interface{} // Order defines the ORDER BY condition for the query Order interface{} // Limit specifies the maximum number of rows to return Limit int // Offset specifies the number of rows to skip Offset int // IncDeleted indicates whether to include soft-deleted records IncDeleted bool }
Criteria is used to safely build search criteria for database queries. It provides a structured way to define WHERE, ORDER BY, LIMIT, and OFFSET conditions.
func (Criteria) LimitString ¶
LimitString returns the limiter in SQL format @receiver c @return string
func (Criteria) OffsetString ¶
OffsetString returns the offset in SQL format @receiver c @return string
func (Criteria) OrderString ¶
OrderString returns the ORDER BY condition in SQL format. It converts the criteria's Order condition into a properly formatted SQL ORDER BY clause. Parameters:
mgr: The database manager used to format the ORDER BY condition
Returns:
A string containing the SQL ORDER BY clause
func (Criteria) WhereString ¶
WhereString returns the WHERE condition in SQL format. It converts the criteria's Where condition into a properly formatted SQL WHERE clause. Parameters:
mgr: The database manager used to format the WHERE condition
Returns:
A string containing the SQL WHERE clause
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
func (*DB) CommitTransaction ¶
commitTransaction commites the transaction to the database @param tx
func (*DB) Count ¶
Count returns the number of rows in the database that match the criteria @param criteria @return int
func (*DB) Fetch ¶
Fetch populates the slice with models from the database that match the criteria. Returns an error if this fails @param criteria @return []*T @return error
func (*DB) First ¶
First returns the first model that matches the criteria @param criteria @return *T @return error
func (*DB) RawExecute ¶
RawExecute executes a sql statement on the database, without returning a value Not recommended for general use - can break shadowing @param sql @return bool
func (*DB) RawScalar ¶
RawScalar exeutes a raw sql statement that returns a single value Not recommended for general use @param sql @return interface{} @return bool
func (*DB) RawSelect ¶
RawSelect executes a raw sql statement on the database Not recommended for general use @param sql @return []map
func (*DB) RemoveMany ¶
RemoveMany removes all models of the specified type that match the criteria @param c @return int @return bool
type ErrNoResults ¶
type ErrNoResults struct { // Err contains the underlying error that caused the no results condition Err error }
ErrNoResults represents an error that occurs when a database query returns no results. This error is typically returned when a query expects at least one result but finds none.
func NoResults ¶
func NoResults(msg string) ErrNoResults
NoResults creates a new ErrNoResults error with the specified message. This function is used to wrap errors when a database query returns no results. Parameters:
msg: The error message to include in the error
Returns:
A new ErrNoResults error
func (ErrNoResults) Error ¶
func (e ErrNoResults) Error() string
Error returns the error message for ErrNoResults. This method implements the error interface for ErrNoResults. Returns:
The error message as a string
type FieldSize ¶
type FieldSize struct { // Size represents the maximum length of the field Size int // Decimal represents the number of decimal places for numeric types Decimal int }
FieldSize represents the size and precision of a database field. It is used to store the maximum length and decimal places for numeric types.
func NewSize ¶
NewSize creates a new FieldSize with the specified size and decimal places. This function is used to define the size constraints for database fields. Parameters:
sz: The maximum length of the field dec: The number of decimal places (for numeric types)
Returns:
A new FieldSize instance
func (FieldSize) String ¶
String returns a string representation of the field size. For numeric types with decimal places, it returns "size,decimal". For other types, it returns just the size. This method is used for generating SQL schema definitions. Returns:
A string representation of the field size
type MSSQLManager ¶
type MSSQLManager struct {
// contains filtered or unexported fields
}
MSSQLManager implements the database management interface for Microsoft SQL Server. It handles SQL Server specific query generation and database operations.
func (*MSSQLManager) BuildQuery ¶
BuildQuery combines WHERE, ORDER BY, LIMIT, and OFFSET clauses into a complete query string.
func (*MSSQLManager) ConnectionString ¶
func (m *MSSQLManager) ConnectionString(cfg *Config) (string, error)
ConnectionString generates a SQL Server connection string from the provided configuration. Returns an error if the configuration is nil or missing required fields.
func (*MSSQLManager) GetDB ¶
func (m *MSSQLManager) GetDB() *DB
GetDB returns the current database connection.
func (*MSSQLManager) IdentityString ¶
func (m *MSSQLManager) IdentityString(f string) string
IdentityString wraps a field name in square brackets for SQL Server identifier escaping.
func (*MSSQLManager) IndexCreate ¶
func (m *MSSQLManager) IndexCreate() string
IndexCreate returns the SQL Server index creation query template.
func (*MSSQLManager) LimitString ¶
func (m *MSSQLManager) LimitString(c *Criteria) string
LimitString generates the SQL Server specific FETCH NEXT clause for result limiting. Returns an empty string if criteria is nil or limit is less than 1.
func (*MSSQLManager) OffsetString ¶
func (m *MSSQLManager) OffsetString(c *Criteria) string
OffsetString generates the SQL Server specific OFFSET clause. Returns an empty string if criteria is nil or offset is less than 1.
func (*MSSQLManager) Operators ¶
func (m *MSSQLManager) Operators() []string
Operators returns a list of SQL Server compatible operator formats for query building. These formats include comparison, LIKE, IN, BETWEEN, and NULL check operators.
func (*MSSQLManager) SetDB ¶
func (m *MSSQLManager) SetDB(db *DB)
SetDB assigns a database connection to the manager.
func (*MSSQLManager) TableCreate ¶
func (m *MSSQLManager) TableCreate() string
TableCreate returns the SQL Server table creation query template. The template includes a check to prevent creating duplicate tables.
func (*MSSQLManager) TableExistsQuery ¶
func (m *MSSQLManager) TableExistsQuery(name string) string
TableExistsQuery generates a query to check if a table exists in the database.
type Manager ¶
type Manager interface { // SetDB assigns a database connection to the manager SetDB(db *DB) // GetDB returns the current database connection GetDB() *DB // ConnectionString generates a database-specific connection string from the configuration ConnectionString(cfg *Config) (string, error) // LimitString generates the database-specific LIMIT clause LimitString(c *Criteria) string // OffsetString generates the database-specific OFFSET clause OffsetString(c *Criteria) string // IdentityString wraps field names with database-specific identifier quotes IdentityString(f string) string // BuildQuery combines WHERE, ORDER BY, LIMIT, and OFFSET clauses into a query BuildQuery(where string, order string, limit string, offset string) string // TableExistsQuery generates a query to check if a table exists TableExistsQuery(name string) string // Operators returns a list of database-specific operator formats Operators() []string // TableCreate returns the database-specific table creation template TableCreate() string // IndexCreate returns the database-specific index creation template IndexCreate() string }
Manager defines the interface for database-specific operations. Each supported database type (SQLite, MySQL, SQL Server) implements this interface to provide its specific SQL syntax and behavior.
func GetManager ¶
GetManager creates and returns a database-specific Manager implementation based on the configuration. Supported database types are: "sqlite", "mysql", and "sqlserver". Returns an error if an unsupported database type is specified or if config is nil.
type Model ¶
type Model struct { // Unique identifier for the record ID *string // Timestamp when the record was created CreateDate time.Time // Timestamp of the last update LastUpdate time.Time // Timestamp when the record was soft deleted (nil if active) DeleteDate *time.Time // contains filtered or unexported fields }
Model represents the base structure for all database entities. It provides common fields and functionality for tracking creation, updates, and soft deletion of records.
func CreateModel ¶
func CreateModel() Model
CreateModel initializes a new Model instance with current timestamps. This should be called when creating new database entities.
func (*Model) Disable ¶
func (m *Model) Disable()
Disable marks the model as soft deleted by setting its DeleteDate to current time.
func (Model) IsDeleted ¶
IsDeleted checks if the model has been soft deleted. Returns true if DeleteDate is not nil, indicating the record is deleted.
func (Model) IsNew ¶
IsNew checks if the model is a new record (has not been saved to database). Returns true if the ID is nil, indicating the record hasn't been assigned an ID.
func (Model) StandingData ¶
StandingData returns a list of default records for the model. This can be overridden by implementing models to provide seed data.
type Modeller ¶
type Modeller interface { // StandingData returns the standing data for the model. // This is used to provide seed or default data for the model. StandingData() []Modeller // GetID returns the ID of the model. // Returns nil if the model hasn't been saved to the database. GetID() *string // IsNew returns true if the model has yet to be saved to the database. IsNew() bool // IsDeleted returns true if the model has been marked as deleted. // This is used for soft deletion support. IsDeleted() bool }
Modeller defines the interface for database model objects. Any struct that implements this interface can be used as a database model.
type MySQLManager ¶
type MySQLManager struct {
// contains filtered or unexported fields
}
MySQLManager implements the database management interface for MySQL. It handles MySQL specific query generation and database operations.
func (*MySQLManager) BuildQuery ¶
BuildQuery combines WHERE, ORDER BY, LIMIT, and OFFSET clauses into a complete query string.
func (*MySQLManager) ConnectionString ¶
func (m *MySQLManager) ConnectionString(cfg *Config) (string, error)
ConnectionString generates a MySQL connection string from the provided configuration. Returns an error if the configuration is nil or missing required fields.
func (*MySQLManager) GetDB ¶
func (m *MySQLManager) GetDB() *DB
GetDB returns the current database connection.
func (*MySQLManager) IdentityString ¶
func (m *MySQLManager) IdentityString(f string) string
IdentityString wraps a field name in backticks for MySQL identifier escaping.
func (*MySQLManager) IndexCreate ¶
func (m *MySQLManager) IndexCreate() string
IndexCreate returns the MySQL index creation query template.
func (*MySQLManager) LimitString ¶
func (m *MySQLManager) LimitString(c *Criteria) string
LimitString generates the MySQL specific LIMIT clause for result limiting. Returns an empty string if criteria is nil or limit is less than 1.
func (*MySQLManager) OffsetString ¶
func (m *MySQLManager) OffsetString(c *Criteria) string
OffsetString generates the MySQL specific OFFSET clause. Returns an empty string if criteria is nil or offset is less than 1.
func (*MySQLManager) Operators ¶
func (m *MySQLManager) Operators() []string
Operators returns a list of MySQL compatible operator formats for query building. These formats include comparison, LIKE, IN, BETWEEN, and NULL check operators.
func (*MySQLManager) SetDB ¶
func (m *MySQLManager) SetDB(db *DB)
SetDB assigns a database connection to the manager.
func (*MySQLManager) TableCreate ¶
func (m *MySQLManager) TableCreate() string
TableCreate returns the MySQL table creation query template. The template includes IF NOT EXISTS to prevent duplicate table creation errors.
func (*MySQLManager) TableExistsQuery ¶
func (m *MySQLManager) TableExistsQuery(name string) string
TableExistsQuery generates a query to check if a table exists in the database.
type Remover ¶
type Remover interface{}
Remover defines the interface for objects that can be removed from the database.
type Restorer ¶
type Restorer interface { // Restore generates a restore query for the object using the provided manager. // Returns the generated query string and any error encountered. Restore(mgr Manager) error }
Restorer defines the interface for objects that can be restored from the database.
type SqliteManager ¶
type SqliteManager struct {
// contains filtered or unexported fields
}
SqliteManager implements the database management interface for SQLite. It handles SQLite specific query generation and database operations.
func (*SqliteManager) BuildQuery ¶
BuildQuery combines WHERE, ORDER BY, LIMIT, and OFFSET clauses into a complete query string.
func (*SqliteManager) ConnectionString ¶
func (m *SqliteManager) ConnectionString(cfg *Config) (string, error)
ConnectionString generates a SQLite connection string from the provided configuration. Returns an error if the configuration is nil or missing required fields. Note that SQLite only requires the database path, unlike other SQL databases.
func (*SqliteManager) GetDB ¶
func (m *SqliteManager) GetDB() *DB
GetDB returns the current database connection.
func (*SqliteManager) IdentityString ¶
func (m *SqliteManager) IdentityString(f string) string
IdentityString wraps a field name in double quotes for SQLite identifier escaping.
func (*SqliteManager) IndexCreate ¶
func (m *SqliteManager) IndexCreate() string
IndexCreate returns the SQLite index creation query template.
func (*SqliteManager) LimitString ¶
func (m *SqliteManager) LimitString(c *Criteria) string
LimitString generates the SQLite specific LIMIT clause for result limiting. Returns an empty string if criteria is nil or limit is less than 1.
func (*SqliteManager) OffsetString ¶
func (m *SqliteManager) OffsetString(c *Criteria) string
OffsetString generates the SQLite specific OFFSET clause. Returns an empty string if criteria is nil or offset is less than 1.
func (*SqliteManager) Operators ¶
func (m *SqliteManager) Operators() []string
Operators returns a list of SQLite compatible operator formats for query building. These formats include comparison, LIKE, IN, BETWEEN, and NULL check operators.
func (*SqliteManager) SetDB ¶
func (m *SqliteManager) SetDB(db *DB)
SetDB assigns a database connection to the manager.
func (*SqliteManager) TableCreate ¶
func (m *SqliteManager) TableCreate() string
TableCreate returns the SQLite table creation query template. The template includes IF NOT EXISTS to prevent duplicate table creation errors.
func (*SqliteManager) TableExistsQuery ¶
func (m *SqliteManager) TableExistsQuery(name string) string
TableExistsQuery generates a query to check if a table exists in the database. Uses the sqlite_master system table to check for table existence.