Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsForeignKeyViolation ¶
IsForeignKeyViolation checks if the provided error is a foreign key violation error in PostgresSQL. It returns true if the error is a foreign key violation error (error code "23503"), and false otherwise.
func IsInvalidTextRepresentation ¶
IsInvalidTextRepresentation checks if the provided error is an invalid text representation error in PostgresSQL. It returns true if the error is an invalid text representation error (error code "22P02"), and false otherwise.
func IsNotNullViolation ¶
IsNotNullViolation checks if the provided error is a not null violation error in PostgresSQL. It returns true if the error is a not null violation error (error code "23502"), and false otherwise.
func IsUniqueViolation ¶
IsUniqueViolation checks if the provided error is a unique violation error in PostgresSQL. It returns true if the error is a unique violation error (error code "23505"), and false otherwise.
Types ¶
type Config ¶
type Config struct {
Database, Host, Username, Password, Params, LogMode string
Port, MaxIdleConn, MaxOpenConn int
DebugEnabled bool
}
Config is a struct that holds the configuration for the database connection. It contains the following fields: - Database: the name of the database - Host: the host of the database - Username: the username to connect to the database - Password: the password to connect to the database - Params: additional parameters for the database connection - LogMode: the log mode for the database connection - Port: the port number of the database - MaxIdleConn: the maximum number of idle connections for the database - MaxOpenConn: the maximum number of open connections for the database - DebugEnabled: a boolean indicating whether debug mode is enabled or not
func (Config) Close ¶
Close is a method on the Config struct that closes the database connection. It first retrieves the underlying sql.DB object from the gorm.DB object. If an error occurs during this process, it returns the error. If the retrieval is successful, it calls the Close method on the sql.DB object to close the database connection. If an error occurs while closing the database connection, it returns the error. If the database connection is successfully closed, it returns nil.
func (Config) Connect ¶
Connect is a method on the Config struct that establishes a connection to the database. It constructs the Data Source Name (DSN) using the configuration fields and opens a connection to the database. The connection is established only once using singleton mechanism to ensure that the connection is not re-established multiple times. If the connection is successfully established, it configures the connection pool settings and enables debug mode if specified.
Returns: - error: Any error that occurred during the connection process.
func (Config) DB ¶
DB is a method on the Config struct that retrieves the current database connection. It does not take any parameters. It returns the *gorm.DB object representing the current database connection.
func (Config) Ping ¶
Ping is a method on the Config struct that checks the database connection by sending a ping. If the database is reachable and responds to the ping, it returns nil. If the database is not reachable or does not respond to the ping, it returns an error.
func (Config) Reset ¶
Reset is a method on the Config struct that resets the database connection. It first resets the instance to allow the Connect method to be called again. Then it calls the Connect method to re-establish the database connection. If an error occurs during the connection process, it returns the error. If the connection is successfully re-established, it returns nil.
Returns: - error: Any error that occurred during the process.