Documentation
¶
Index ¶
- Constants
- func Connect(connURL, driver string) (*sqlx.DB, error)
- func CreateModel[T any](m *T) error
- func DeleteModel[T any](m *T) error
- func FindModels[T any](page, limit int, sortField string, sortDir qb.OrderByDir, ...) ([]T, int, error)
- func GetModel[T any](conditions ...qb.Condition) (*T, error)
- func GetModelBy[T any](field string, value any) (*T, error)
- func GetModelByID[T any](value any) (*T, error)
- func GetModelWhereEq[T any](field string, value any) (*T, error)
- func Load()
- func Register(driver IDatabase)
- func UpdateModel[T any](m *T) error
- type Column
- type DB
- type DBModel
- func (db *DBModel) Begin() *DBModel
- func (db *DBModel) Commit() error
- func (db *DBModel) Create(model any) (err error)
- func (db *DBModel) Delete(model any) error
- func (db *DBModel) Fetch(offset, fetch int) *DBModel
- func (db *DBModel) Find(model any) (total int, err error)
- func (db *DBModel) First(model any) (err error)
- func (db *DBModel) Get(model any, getType GetOne) (err error)
- func (db *DBModel) GroupBy(fields ...string) *DBModel
- func (db *DBModel) Having(field any, opt qb.WhereOpt, value any) *DBModel
- func (db *DBModel) Join(join qb.JoinType, table string, condition qb.Condition) *DBModel
- func (db *DBModel) Last(model any) (err error)
- func (db *DBModel) Limit(limit, offset int) *DBModel
- func (db *DBModel) Model(model any) *DBModel
- func (db *DBModel) Omit(columns ...any) *DBModel
- func (db *DBModel) OrderBy(field string, dir qb.OrderByDir) *DBModel
- func (db *DBModel) Raw(sqlStr string, args ...any) *DBModel
- func (db *DBModel) RemoveFetch() qb.Fetch
- func (db *DBModel) RemoveLimit() qb.Limit
- func (db *DBModel) Rollback() error
- func (db *DBModel) Select(columns ...any) *DBModel
- func (db *DBModel) Update(model any) (err error)
- func (db *DBModel) When(condition bool, groupCondition qb.FnWhereBuilder) *DBModel
- func (db *DBModel) Where(field any, opt qb.WhereOpt, value any) *DBModel
- func (db *DBModel) WhereGroup(groupCondition qb.FnWhereBuilder) *DBModel
- func (db *DBModel) WhereOr(field any, opt qb.WhereOpt, value any) *DBModel
- type GetOne
- type IDatabase
- type MetaData
- type Raw
- type Table
Constants ¶
const ( MODEL = "model" // Tag `model` used to store metadata for struct fields TABLE = "table" // Table name in the database TYPE = "type" // Column types for database representation REFERENCE = "ref" // Column reference to another table CASCADE = "cascade" // Cascade rules for DELETE and UPDATE RELATION = "rel" // Relation to another table NAME = "name" // Column name in the database )
Constants for model tag attributes and processing
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
Connect creates a connection to a database using the provided connection URL and driver name. Parameters: - connURL: A string representing the connection URL to the database. - driver: A string representing the name of the database driver (e.g., "postgres", "mysql"). Returns: - *sqlx.DB: A pointer to the sqlx.DB instance representing the database connection. - error: An error if the connection initialization fails.
func CreateModel ¶
CreateModel creates a new record of type T in the database. It begins a transaction, attempts to create the record, and commits the transaction. If an error occurs, the transaction is rolled back and the error is returned.
Parameters:
- m: A pointer to the model to be created.
Returns:
- error: An error object if an error occurs during the creation process.
func DeleteModel ¶
DeleteModel deletes a record of type T from the database. It begins a transaction, deletes the record, and commits the transaction. If an error occurs, the transaction is rolled back and the error is returned.
Parameters:
- m: A pointer to the model to be deleted.
Returns:
- error: An error object if an error occurs during the deletion process.
func FindModels ¶ added in v1.5.0
func FindModels[T any](page, limit int, sortField string, sortDir qb.OrderByDir, conditions ...qb.Condition) ([]T, int, error)
FindModels retrieves a paginated list of records of type T from the database that match the provided conditions.
Parameters:
- page (int): The current page number (1-based). Defaults to 0 if not provided.
- limit (int): The number of records to retrieve per page.
- conditions (...qb.Condition): Variadic list of qb.Condition specifying the field, operator, and value to filter the query.
Returns:
- ([]T): A slice of records of type T.
- (int): The total number of records that match the conditions.
- (error): An error object if an error occurs during the retrieval process.
func GetModel ¶
GetModel retrieves the first record of type T from the database that matches the provided conditions.
Parameters:
- m: A pointer to the model where the result will be stored.
- conditions: Variadic list of qb.Condition specifying the field, operator, and value to filter the query.
Returns:
- error: An error object if an error occurs during the retrieval process. Returns nil if the query succeeds. Logs unexpected errors.
func GetModelBy ¶ added in v1.5.0
GetModelBy allows filtering records of type T from the database by specifying a field and its required value.
This is a helper function that makes use of the GetModelWhereEq function to apply an equality condition.
Generic Type:
- T: The type of the model.
Parameters:
- field (string): The name of the database field to filter on.
- value (any): The value the specified field is required to equal.
Returns:
- *T: A pointer to the first matching record of type T retrieved from the database, or nil if no record is found.
- error: An error object if an error occurs during the retrieval process.
func GetModelByID ¶ added in v1.5.0
GetModelByID retrieves the first record of type T from the database where the "id" field matches the specified value.
This is a convenience wrapper around GetModelBy for querying records by their unique identifier.
Generic Type:
- T: The type of the model.
Parameters:
- value (any): The value of the "id" field to match against.
Returns:
- *T: A pointer to the retrieved model of type T, or nil if no matching record is found.
- error: An error object if an error occurs during the retrieval process.
func GetModelWhereEq ¶ added in v1.5.0
GetModelWhereEq retrieves the first record of type T from the database where the specified field equals the given value.
Parameters:
- m: A pointer to the model where the result will be stored.
- field: The name of the database field to filter by.
- value: The value to match the field against.
Returns:
- error: An error object if an error occurs during the retrieval process. Returns nil if the query succeeds.
func Load ¶
func Load()
Load initializes the database connection by loading it through the registered database driver and assigns it to the dbInstance. Panics if the database connection fails.
func Register ¶
func Register(driver IDatabase)
Register assigns a custom database driver to the dbDriver variable. Parameters: - driver (IDatabase): The custom database driver implementing the IDatabase interface.
func UpdateModel ¶
UpdateModel updates a record of type T in the database. It begins a transaction, updates the record, and commits the transaction. If an error occurs, the transaction is rolled back and the error is returned.
Parameters:
- m: A pointer to the model to be updated.
Returns:
- error: An error object if an error occurs during the update process.
Types ¶
type Column ¶
type Column struct {
Key string // Name of the struct field the column maps to
Name string // Name of the database column
Primary bool // Indicates if the column is a primary key
Types string // Data type of the column
Ref string // Reference to another table column
Relation string // Relation to another table
IsZero bool // Indicates if the column value is the zero value for its type
HasValue bool // Indicates if the column has a valid (non-zero) value
}
Column structure that maps a struct field to a database table column
type DBModel ¶
type DBModel struct {
// contains filtered or unexported fields
}
DBModel struct represents a database model with SQL builders and transaction handling.
func Instance ¶
func Instance() *DBModel
Instance creates and returns a new DBModel instance.
Returns:
*DBModel - A pointer to a new database model instance.
func (*DBModel) Begin ¶
Begin starts a new database transaction.
Returns:
- *DBModel: The DBModel instance with an active transaction.
func (*DBModel) Commit ¶
Commit commits the current database transaction.
Returns:
- error: An error, if any, that occurred during the commit process.
func (*DBModel) Create ¶
Create adds new data for a table via model type Slice, Struct, or *Struct.
Parameters:
- model: The data to be inserted. Accepts Slice, Map, Struct, or *Struct types.
Returns:
- error: Returns an error if the operation fails.
func (*DBModel) Delete ¶
Delete performs the deletion of data for a given table using a model of type Struct or *Struct.
Parameters:
- model (any): The input model defining the data to delete. This can be a struct or a pointer to a struct.
Returns:
- error: Returns an error if the deletion process fails.
func (*DBModel) Fetch ¶
Fetch adds a FETCH clause to the query.
Parameters:
- offset (int): The offset for fetching rows.
- fetch (int): The number of rows to fetch.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) Find ¶
Find searches for multiple rows in the database based on query criteria.
Parameters:
- model (any): A pointer to the slice where the retrieved rows will be stored.
Returns:
- total (int): The total number of rows matching the query criteria.
- err (error): An error object if any issues occur during the retrieval process; nil otherwise.
func (*DBModel) First ¶
First retrieves the first record ordered by primary key.
Parameters:
- model (any): A pointer to the model where the result will be stored.
Returns:
- err (error): An error object if any issues occur during the retrieval process; nil otherwise.
func (*DBModel) Get ¶
Get retrieves a single record from the database based on the specified strategy.
Parameters:
- model (any): A pointer to the model where the retrieved record will be stored.
- getType (GetOne): The strategy for selecting the record. Possible values are:
- GetFirst: Retrieve the first record ordered by primary key in ascending order.
- GetLast: Retrieve the last record ordered by primary key in descending order.
- TakeOne: Retrieve a random record.
Returns:
- err (error): An error object if any issues occur during the retrieval process; nil otherwise.
func (*DBModel) GroupBy ¶
GroupBy adds GROUP BY fields to the query.
Parameters:
- fields (...string): The fields to group by.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) Having ¶
Having adds a HAVING condition to the query.
Parameters:
- field (any): The field or column to filter.
- opt (qb.WhereOpt): The operator to use.
- value (any): The value to compare against.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) Join ¶
Join adds a JOIN clause to the query.
Parameters:
- join (qb.JoinType): The type of join (e.g., INNER, LEFT).
- table (string): The name of the table to join.
- condition (qb.Condition): The condition for the join.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) Last ¶
Last retrieves the last record ordered by primary key in descending order.
Parameters:
- model (any): A pointer to the model where the result will be stored.
Returns:
- err (error): An error object if any issues occur during the retrieval process; nil otherwise.
func (*DBModel) Limit ¶
Limit adds a LIMIT clause to the query.
Parameters:
- limit (int): The maximum number of rows to return.
- offset (int): The number of rows to skip.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) Model ¶
Model sets a specific model for the builder.
Parameters:
- model (any): The model instance to operate on.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) Omit ¶
Omit excludes specific columns from retrieval.
Parameters:
- columns (...any): Variadic list of columns to omit.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) OrderBy ¶
func (db *DBModel) OrderBy(field string, dir qb.OrderByDir) *DBModel
OrderBy adds an ORDER BY clause to the query.
Parameters:
- field (string): The field to sort by.
- dir (qb.OrderByDir): The sorting direction (e.g., ASC or DESC).
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) Raw ¶
Raw builds a query from raw SQL.
Parameters:
- sqlStr (string): The raw SQL query string.
- args (...any): Variadic arguments for the query placeholders.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) RemoveFetch ¶
RemoveFetch removes the FETCH clause from the query.
Returns:
- qb.Fetch: The removed fetch settings.
func (*DBModel) RemoveLimit ¶
RemoveLimit removes the LIMIT clause from the query.
Returns:
- qb.Limit: The removed limit settings.
func (*DBModel) Rollback ¶
Rollback rolls back the current database transaction.
Returns:
- error: An error, if any, that occurred during the rollback process.
func (*DBModel) Select ¶
Select specifies the list of columns to retrieve.
Parameters:
- columns (...any): Variadic list of columns to include in the SELECT clause.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) Update ¶
Update modifies data for a table using a model of type Struct or *Struct.
Parameters:
- model (any): The data model used for updating the table. It can be of type Struct, *Struct, or a map.
Returns:
- error: Returns an error if the update process fails.
func (*DBModel) When ¶
func (db *DBModel) When(condition bool, groupCondition qb.FnWhereBuilder) *DBModel
When conditionally applies a WHERE condition if the provided condition is TRUE.
Parameters:
- condition (bool): Determines whether the condition should be applied.
- groupCondition (qb.FnWhereBuilder): The function to build the condition.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) Where ¶
Where adds a WHERE condition to the query.
Parameters:
- field (any): The field or column to filter.
- opt (qb.WhereOpt): The operator to use (e.g., equals, greater than).
- value (any): The value to compare against.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
func (*DBModel) WhereGroup ¶
func (db *DBModel) WhereGroup(groupCondition qb.FnWhereBuilder) *DBModel
WhereGroup combines multiple WHERE conditions into a group.
Parameters:
- groupCondition (qb.FnWhereBuilder): The function to build grouped conditions.
Returns:
- *DBModel: A reference to the DBModel instance for chaining.
type GetOne ¶
type GetOne int
GetOne represents a strategy for retrieving a single record from the database. Possible values are GetFirst, GetLast, and TakeOne.
type IDatabase ¶
type IDatabase interface {
// Load establishes a connection to the database and returns the instance of sqlx.DB or an error if the connection fails.
Load() (*sqlx.DB, error)
}
IDatabase interface defines a contract for database loading operations.
type Raw ¶
type Raw struct {
// contains filtered or unexported fields
}
Raw struct represents a raw SQL query with its arguments.
type Table ¶
type Table struct {
Name string // Name of the table
Columns []Column // List of columns in the table
Primaries []Column // List of primary key columns
Values map[string]any // Values of the table columns
Relation []*Table // Related tables for relational mapping
HasData bool // Indicates if the table has valid data
}
Table structure that maps a Go struct to a database table