postgres

package
v0.0.0-...-128919f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsForeignKeyViolation

func IsForeignKeyViolation(err error) bool

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

func IsInvalidTextRepresentation(err error) bool

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

func IsNotNullViolation(err error) bool

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

func IsUniqueViolation(err error) bool

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

func (c Config) Close() error

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

func (c Config) Connect() error

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

func (c Config) DB() *gorm.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

func (c Config) Ping() error

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

func (c Config) Reset() error

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.

func (Config) SetDB

func (c Config) SetDB(conn *gorm.DB)

SetDB is a method on the Config struct that sets the database connection. It takes one parameter: - conn: The *gorm.DB object representing the database connection to be set. This method does not return any value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL