sqlstore.go
sqlstore.go - Overview
-
Overview This file defines interfaces and types for interacting with a SQL database, providing abstractions for database connections, transactions, and dialect-specific operations.
-
Detailed Documentation
Type: SQLStoreTxOptions
- Purpose: Alias for
sql.TxOptions
, representing transaction options for the SQL store. - Type:
sql.TxOptions
Interface: SQLStore
- Purpose: Defines the main interface for interacting with the SQL database. It provides methods for accessing the underlying database connections (both
sql.DB
,sqlx.DB
andbun.DB
), running transactions, and retrieving the database dialect.- Methods:
SQLDB()
BunDB()
SQLxDB()
Dialect()
RunInTxCtx()
BunDBCtx()
- Methods:
Method: SQLDB()
- Purpose: Returns the underlying
sql.DB
instance. - Returns:
*sql.DB
- The underlyingsql.DB
instance.
Method: BunDB()
- Purpose: Returns an instance of
bun.DB
. This is the recommended way to interact with the database. - Returns:
*bun.DB
- An instance ofbun.DB
.
Method: SQLxDB()
- Purpose: Returns an instance of
sqlx.DB
. This is the legacy ORM used. - Returns:
*sqlx.DB
- An instance ofsqlx.DB
.
Method: Dialect()
- Purpose: Returns the dialect of the database.
- Returns:
SQLDialect
- The dialect of the database.
Method: RunInTxCtx()
- Purpose: Runs the given callback in a transaction. It creates and injects a new context with the transaction. If a transaction is present in the context, it will be used.
- Parameters:
ctx
(context.Context
): The context in which to run the transaction.opts
(*SQLStoreTxOptions
): Transaction options. Can benil
to use default options.cb
(func(ctx context.Context) error
): The callback function to execute within the transaction.
- Returns:
error
- An error if the transaction fails or the callback returns an error. Returnsnil
on success.
Method: BunDBCtx()
- Purpose: Returns an instance of
bun.IDB
for the given context. If a transaction is present in the context, it will be used. Otherwise, the default will be used. - Parameters:
ctx
(context.Context
): The context.
- Returns:
bun.IDB
- An instance ofbun.IDB
.
Interface: SQLStoreHook
- Purpose: Defines an interface for SQL store hooks.
- Extends:
bun.QueryHook
Interface: SQLDialect
- Purpose: Defines an interface for database dialect-specific operations.
- Methods:
MigrateIntToTimestamp()
MigrateIntToBoolean()
AddNotNullDefaultToColumn()
GetColumnType()
ColumnExists()
RenameColumn()
RenameTableAndModifyModel()
UpdatePrimaryKey()
AddPrimaryKey()
- Methods:
Method: MigrateIntToTimestamp()
- Purpose: Migrates an integer column to a timestamp column.
- Parameters:
ctx
(context.Context
): The context.db
(bun.IDB
): The bun database instance.table
(string
): The table name.column
(string
): The column name.
- Returns:
error
- An error if the migration fails.
Method: MigrateIntToBoolean()
- Purpose: Migrates an integer column to a boolean column.
- Parameters:
ctx
(context.Context
): The context.db
(bun.IDB
): The bun database instance.table
(string
): The table name.column
(string
): The column name.
- Returns:
error
- An error if the migration fails.
Method: AddNotNullDefaultToColumn()
- Purpose: Adds a NOT NULL constraint with a default value to a column.
- Parameters:
ctx
(context.Context
): The context.db
(bun.IDB
): The bun database instance.table
(string
): The table name.column
(string
): The column name.dataType
(string
): The data type of the column.defaultValue
(string
): The default value for the column.
- Returns:
error
- An error if the operation fails.
Method: GetColumnType()
- Purpose: Gets the data type of a column.
- Parameters:
ctx
(context.Context
): The context.db
(bun.IDB
): The bun database instance.table
(string
): The table name.column
(string
): The column name.
- Returns:
string
- The data type of the column. - Returns:
error
- An error if the operation fails.
Method: ColumnExists()
- Purpose: Checks if a column exists in a table.
- Parameters:
ctx
(context.Context
): The context.db
(bun.IDB
): The bun database instance.table
(string
): The table name.column
(string
): The column name.
- Returns:
bool
- True if the column exists, false otherwise. - Returns:
error
- An error if the operation fails.
Method: RenameColumn()
- Purpose: Renames a column in a table.
- Parameters:
ctx
(context.Context
): The context.db
(bun.IDB
): The bun database instance.table
(string
): The table name.oldColumnName
(string
): The old column name.newColumnName
(string
): The new column name.
- Returns:
bool
- True if the column was renamed, false otherwise. - Returns:
error
- An error if the operation fails.
Method: RenameTableAndModifyModel()
- Purpose: Renames a table and modifies the corresponding model.
- Parameters:
ctx
(context.Context
): The context.db
(bun.IDB
): The bun database instance.oldModel
(interface{}
): The old model.newModel
(interface{}
): The new model.columnsToRename
([]string
): The columns to be renamed.callback
(func(context.Context) error
): A callback function to execute after renaming the table.
- Returns:
error
- An error if the operation fails.
Method: UpdatePrimaryKey()
- Purpose: Updates the primary key of a table.
- Parameters:
ctx
(context.Context
): The context.db
(bun.IDB
): The bun database instance.oldModel
(interface{}
): The old model.newModel
(interface{}
): The new model.columnToUpdate
(string
): The column to be updated.callback
(func(context.Context) error
): A callback function to execute after updating the primary key.
Method: AddPrimaryKey()
- Purpose: Adds a primary key to a table.
- Parameters:
ctx
(context.Context
): The context.db
(bun.IDB
): The bun database instance.oldModel
(interface{}
): The old model.newModel
(interface{}
): The new model.columnToUpdate
(string
): The column to be updated.callback
(func(context.Context) error
): A callback function to execute after updating the primary key.
Include in Getting Started: NO