postgres

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldValue

type FieldValue struct {
	Fields []string
	Values []string
}

FieldValue is a structure to build a PostgreSQL update query. It collects field names and their new values, and can generate an SQL update statement. It also tracks the last update time. The structure is designed to be used in a context where changes to fields need to be recorded

func (*FieldValue) AddBool

func (dst *FieldValue) AddBool(is bool, field string)

AddBool adds a new boolean value to the Fields and Values slices. It formats the boolean value as a string suitable for a PostgreSQL query.

Parameters:

  • is: the new boolean value to be added.
  • field: the name of the field being added, which will be appended to the Fields slice.

func (*FieldValue) AddInt8Slice added in v1.0.2

func (dst *FieldValue) AddInt8Slice(is []int8, field string)

AddInt8Slice adds a new slice of int8 values to the Fields and Values slices. It formats the slice as a PostgreSQL array string and appends it to the Values slice.

Parameters:

  • is: the new slice of int8 values to be added.
  • field: the name of the field being added, which will be appended to the Fields slice.

func (*FieldValue) AddInt16Slice added in v1.0.2

func (dst *FieldValue) AddInt16Slice(is []int16, field string)

AddInt16Slice adds a new slice of int16 values to the Fields and Values slices. It formats the slice as a PostgreSQL array string and appends it to the Values slice.

Parameters:

  • is: the new slice of int16 values to be added.
  • field: the name of the field being added, which will be appended to the

func (*FieldValue) AddInt32Slice added in v1.0.2

func (dst *FieldValue) AddInt32Slice(is []int32, field string)

AddInt32Slice adds a new slice of int32 values to the Fields and Values slices. It formats the slice as a PostgreSQL array string and appends it to the Values slice.

Parameters:

  • is: the new slice of int32 values to be added.
  • field: the name of the field being added, which will be appended to the

func (*FieldValue) AddInt64

func (dst *FieldValue) AddInt64(is int64, field string)

AddUInt64 adds a new int64 value to the Fields and Values slices. It formats the int64 value as a string for use in a PostgreSQL query.

Parameters:

  • is: the new uint64 value to be added.
  • field: the name of the field being added, which will be appended to the Fields slice.

func (*FieldValue) AddInt64Slice

func (dst *FieldValue) AddInt64Slice(is []int64, field string)

AddInt64Slice adds a new slice of int64 values to the Fields and Values slices. It formats the slice as a PostgreSQL array string and appends it to the Values slice.

Parameters:

  • is: the new slice of int64 values to be added.
  • field: the name of the field being added, which will be appended to the Fields slice.

func (*FieldValue) AddJSON

func (dst *FieldValue) AddJSON(is any, field string) error

AddJSON adds a new JSON value to the Fields and Values slices. It marshals the input interface into JSON format and appends it to the Values slice. If the marshaling fails, it logs an error message.

Parameters:

  • is: the input interface to be marshaled into JSON.
  • field: the name of the field being added, which will be appended to the Fields slice.

func (*FieldValue) AddString

func (dst *FieldValue) AddString(is, field string)

AddString adds a new string value to the Fields and Values slices. It formats the string value to be suitable for a PostgreSQL query by wrapping it in single quotes.

Parameters:

  • is: the new string value to be added.
  • field: the name of the field being added, which will be appended to the Fields slice.

func (*FieldValue) AddStringSlice

func (dst *FieldValue) AddStringSlice(is []string, field string)

AddStringSlice adds a new slice of string values to the Fields and Values slices. It formats the slice as a PostgreSQL array string and appends it to the Values slice.

Parameters:

  • is: the new slice of string values to be added.
  • field: the name of the field being added, which will be appended to the Fields slice.

func (*FieldValue) AddTime

func (dst *FieldValue) AddTime(is time.Time, field string)

AddTime adds a new time.Time value to the Fields and Values slices. It formats the time value in UTC and as a string suitable for a PostgreSQL query. Parameters:

  • is: the new time.Time value to be added.
  • field: the name of the field being added, which will be appended to the Fields slice.

func (*FieldValue) AddUInt64 added in v1.0.1

func (dst *FieldValue) AddUInt64(is uint64, field string)

AddUInt64 adds a new uint64 value to the Fields and Values slices. It formats the uint64 value as a string suitable for a PostgreSQL query.

Parameters:

  • is: the new uint64 value to be added.
  • field: the name of the field being added, which will be appended to the Fields slice.

func (*FieldValue) AddUUID added in v1.0.6

func (dst *FieldValue) AddUUID(is uuid.UUID, field string, nullable bool)

AddUUID adds a new UUID value to the Fields and Values slices. It formats the UUID as a string suitable for a PostgreSQL query.

Parameters:

  • is: the new UUID value to be added.
  • field: the name of the field being added, which will be appended to the Fields slice.
  • nullable: if true, allows the UUID to be nil (NULL) in the database.

func (*FieldValue) Bool

func (dst *FieldValue) Bool(was, is bool, field string)

Bool compares two boolean values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original boolean value.
  • is: the new boolean value.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) CustomUpdateQuery added in v1.0.8

func (dst *FieldValue) CustomUpdateQuery(table, where string) (string, time.Time)

CustomUpdateQuery generates a PostgreSQL update query string using the collected fields and values. It returns the query string and the current time as the update timestamp. If no fields are set, it returns an empty string and the zero time value.

Parameters:

  • table: the name of the table to update.
  • where: the WHERE clause to specify which record(s) to update.

Returns:

  • string: the generated SQL update query.
  • time.Time: the current time in UTC, representing the update timestamp.

func (*FieldValue) Int8

func (dst *FieldValue) Int8(was, is int8, field string)

Int8 compares two int8 values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original int8 value.
  • is: the new int8 value.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) Int8Slice added in v1.0.2

func (dst *FieldValue) Int8Slice(was, is []int8, field string)

Int8Slice compares two slices of int8 values and adds the new slice to the Fields and Values slices if they differ.

Parameters:

  • was: the original slice of int8 values.
  • is: the new slice of int8 values.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) Int16

func (dst *FieldValue) Int16(was, is int16, field string)

Int16 compares two int16 values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original int16 value.
  • is: the new int16 value.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) Int16Slice added in v1.0.2

func (dst *FieldValue) Int16Slice(was, is []int16, field string)

Int16Slice compares two slices of int16 values and adds the new slice to the Fields and Values slices if they differ.

Parameters:

  • was: the original slice of int16 values.
  • is: the new slice of int16 values.
  • field: the name of the field being compared, which will be added to the

func (*FieldValue) Int32

func (dst *FieldValue) Int32(was, is int32, field string)

Int32 compares two int32 values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original int32 value.
  • is: the new int32 value.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) Int32Slice added in v1.0.2

func (dst *FieldValue) Int32Slice(was, is []int32, field string)

Int32Slice compares two slices of int32 values and adds the new slice to the Fields and Values slices if they differ. Parameters:

  • was: the original slice of int32 values.
  • is: the new slice of int32 values.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) Int64

func (dst *FieldValue) Int64(was, is int64, field string)

Int64 compares two int64 values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original int64 value.
  • is: the new int64 value.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) Int64Slice

func (dst *FieldValue) Int64Slice(was, is []int64, field string)

Int64Slice compares two slices of int64 values and adds the new slice to the Fields and Values slices if they differ.

Parameters:

  • was: the original slice of int64 values.
  • is: the new slice of int64 values.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) String

func (dst *FieldValue) String(was, is, field string)

String compares two string values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original string value.
  • is: the new string value.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) StringSlice

func (dst *FieldValue) StringSlice(was, is []string, field string)

StringSlice compares two slices of string values and adds the new slice to the Fields and Values slices if they differ.

Parameters:

  • was: the original slice of string values.
  • is: the new slice of string values.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) Time

func (dst *FieldValue) Time(was, is time.Time, field string)

Time compares two time.Time values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original time.Time value.
  • is: the new time.Time value.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) UInt8

func (dst *FieldValue) UInt8(was, is uint8, field string)

UInt8 compares two uint8 values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original uint8 value.
  • is: the new uint8 value.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) UInt16 added in v1.0.1

func (dst *FieldValue) UInt16(was, is uint16, field string)

UInt16 compares two uint16 values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original uint16 value.
  • is: the new uint16 value.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) UInt32 added in v1.0.1

func (dst *FieldValue) UInt32(was, is uint32, field string)

UInt32 compares two uint32 values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original uint32 value.
  • is: the new uint32 value.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) UInt64 added in v1.0.1

func (dst *FieldValue) UInt64(was, is uint64, field string)

UInt64 compares two uint64 values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original uint64 value.
  • is: the new uint64 value.
  • field: the name of the field being compared, which will be added to the Fields slice.

func (*FieldValue) UUID added in v1.0.6

func (dst *FieldValue) UUID(was, is uuid.UUID, field string, nullable bool)

UUID compares two UUID values and adds the new value to the Fields and Values slices if they differ.

Parameters:

  • was: the original UUID value.
  • is: the new UUID value.
  • field: the name of the field being compared, which will be added to the Fields slice.
  • nullable: if true, allows the UUID to be nil (NULL) in the

func (*FieldValue) UpdateQuery

func (dst *FieldValue) UpdateQuery(table string, id any) (string, time.Time)

UpdateQuery generates a PostgreSQL update query string using the collected fields and values. It returns the query string and the current time as the update timestamp. If no fields are set, it returns an empty string and the zero time value.

Parameters:

  • table: the name of the table to update.
  • id: the identifier of the record to update.

Returns:

  • string: the generated SQL update query.
  • time.Time: the current time in UTC, representing the update timestamp.

type PostgresClient added in v1.0.2

type PostgresClient struct {
	logging.CustomLogger // CustomLogger: is an embedded field that allows the PostgresClient to use custom logging functionality.
	// contains filtered or unexported fields
}
var (
	PG PostgresClient // PG: is a global variable that holds the PostgreSQL client instance.
)

func (*PostgresClient) Client added in v1.0.3

func (dst *PostgresClient) Client() *pgxpool.Pool

Client returns the PostgreSQL connection pool. It is used to access the underlying pgxpool.Pool instance for executing queries and transactions. This function is typically called when you need to perform operations directly on the PostgreSQL database.

func (*PostgresClient) Count added in v1.0.2

func (dst *PostgresClient) Count(ctx context.Context, model string, query string) (uint64, error)

Return records count in database The function executes a SQL query to count the number of records in a specified model. It logs the time taken for the query execution and the query itself for debugging purposes. If the query execution is successful, it returns the count of records as a uint64. If an error occurs during the query execution, it returns an error.

Parameters:

  • ctx: The context for the operation, used for cancellation and timeout.
  • model: The name of the model being counted, used for logging.
  • query: The SQL query string for counting records in the database.

Returns:

  • A uint64 representing the number of records in the specified model.
  • An error if the operation fails, or nil if it succeeds.

func (*PostgresClient) Delete added in v1.0.2

func (dst *PostgresClient) Delete(ctx context.Context, model string, query string) (uint, error)

Delete data from database The function starts a transaction, executes the delete query, and returns the number of affected rows. If an error occurs during the transaction, it rolls back the transaction and returns the error. If the transaction is successful, it commits the transaction and returns the number of affected rows. The function logs the time taken for each step of the transaction for debugging purposes.

Parameters:

  • ctx: The context for the operation, used for cancellation and timeout.
  • model: The name of the model being deleted, used for logging.
  • query: The SQL query string for deleting data from the database.

Returns:

  • A uint representing the number of affected rows.
  • An error if the operation fails, or nil if it succeeds.

func (*PostgresClient) Exec added in v1.0.2

func (dst *PostgresClient) Exec(ctx context.Context, model string, query string) error

Execute query without result The function executes a SQL query without expecting any result. It logs the time taken for the query execution and the query itself for debugging purposes. If the query execution is successful, it returns nil. If an error occurs during the query execution, it returns the error. Parameters:

  • ctx: The context for the operation, used for cancellation and timeout.
  • model: The name of the model being queried, used for logging.
  • query: The SQL query string to be executed in the database.

Returns:

  • An error if the operation fails, or nil if it succeeds.

func (*PostgresClient) Insert added in v1.0.2

func (dst *PostgresClient) Insert(ctx context.Context, model string, query string) ([]uint, error)

Insert data into database and return inserted IDs The function starts a transaction, executes the insert query, and returns the IDs of the inserted records. If an error occurs during the transaction, it rolls back the transaction and returns the error. If the transaction is successful, it commits the transaction and returns the IDs of the inserted records. The function logs the time taken for each step of the transaction for debugging purposes.

Parameters:

  • ctx: The context for the operation, used for cancellation and timeout.
  • model: The name of the model being inserted, used for logging.
  • query: The SQL query string for inserting data into the database.

Returns:

  • A slice of uint containing the IDs of the inserted records.
  • An error if the operation fails, or nil if it succeeds.

func (*PostgresClient) InsertUUID added in v1.0.5

func (dst *PostgresClient) InsertUUID(ctx context.Context, model string, query string) ([]uuid.UUID, error)

InsertUUID data into database and return inserted UUIDs The function starts a transaction, executes the insert query, and returns the UUIDs of the inserted records. If an error occurs during the transaction, it rolls back the transaction and returns the error. If the transaction is successful, it commits the transaction and returns the UUIDs of the inserted records. The function logs the time taken for each step of the transaction for debugging purposes.

Parameters:

  • ctx: The context for the operation, used for cancellation and timeout.
  • model: The name of the model being inserted, used for logging.
  • query: The SQL query string for inserting data into the database.

Returns:

  • A slice of uuid.UUID containing the UUIDs of the inserted records.
  • An error if the operation fails, or nil if it succeeds.

func (*PostgresClient) LogSelect added in v1.0.2

func (dst *PostgresClient) LogSelect(ctx context.Context, model string, query string, start time.Time)

Put query string to the log The function logs the SQL query string along with the time taken for the query execution. It is typically used for debugging purposes to track the performance of SQL queries.

Parameters:

  • ctx: The context for the operation, used for cancellation and timeout.
  • model: The name of the model being queried, used for logging.
  • query: The SQL query string to be logged.

func (*PostgresClient) Max added in v1.0.2

func (dst *PostgresClient) Max(ctx context.Context, model, query string) (uint64, error)

Return maximum field value in database The function executes a SQL query to find the maximum value of a specified field in a model. It logs the time taken for the query execution and the query itself for debugging purposes. If the query execution is successful, it returns the maximum value as a uint64. If an error occurs during the query execution, it returns an error.

Parameters:

  • ctx: The context for the operation, used for cancellation and timeout.
  • model: The name of the model being queried, used for logging.
  • query: The SQL query string for finding the maximum value in the database.

Returns:

  • A uint64 representing the maximum value of the specified field in the model.
  • An error if the operation fails, or nil if it succeeds.

func (*PostgresClient) Select added in v1.0.3

func (dst *PostgresClient) Select(ctx context.Context, model string, query string, data any) error

Select data from database and scan into data structure The function executes a SQL query to select data from the database and scans the result into the provided data structure. It logs the time taken for the query execution and the query itself for debugging purposes. If the query execution is successful, it returns nil. If an error occurs during the query execution, it returns the error.

Parameters:

  • ctx: The context for the operation, used for cancellation and timeout.
  • model: The name of the model being queried, used for logging.
  • query: The SQL query string to be executed in the database.
  • data: A pointer to the data structure where the result will be scanned into.

func (*PostgresClient) Start added in v1.0.2

func (dst *PostgresClient) Start(ctx context.Context, host, username, password string, db string)

Start initializes the PostgreSQL connection pool with the provided credentials and database information. It logs an error and exits the application if the connection fails. The connection string is formatted as "postgres://username:password@host/db" Function detects if the host contains multiple addresses separated by commas and appends "?target_session_attrs=read-write" to the connection string for cluster setups. This allows the client to connect to a primary server for read-write operations. It also pings the database to ensure the connection is established. If the connection is successful, it logs the connection details.

Parameters:

  • ctx: The context for the operation, used for cancellation and timeout.
  • host: The host (with port) where the PostgreSQL database is running.
  • username: The username for the PostgreSQL database.
  • password: The password for the PostgreSQL database.
  • db: The name of the PostgreSQL database to connect to.

func (*PostgresClient) Stop added in v1.0.2

func (dst *PostgresClient) Stop(ctx context.Context)

Stop closes the PostgreSQL connection pool and logs a message indicating that the disconnection was successful. It does not return any error. It is typically called when the application is shutting down to ensure that all resources are released properly. It is important to call this function to avoid resource leaks and ensure that the application exits cleanly. It is recommended to call this function in a deferred manner after the connection pool is successfully created.

Parameters:

  • ctx: The context for the operation, used for cancellation and timeout.

func (*PostgresClient) Update added in v1.0.2

func (dst *PostgresClient) Update(ctx context.Context, model string, query string) (uint, error)

Update data in database and return affected rows count The function starts a transaction, executes the update query, and returns the number of affected rows. If an error occurs during the transaction, it rolls back the transaction and returns the error. If the transaction is successful, it commits the transaction and returns the number of affected rows. The function logs the time taken for each step of the transaction for debugging purposes.

Parameters:

  • ctx: The context for the operation, used for cancellation and timeout.
  • model: The name of the model being updated, used for logging.
  • query: The SQL query string for updating data in the database.

Returns:

  • A uint representing the number of affected rows.
  • An error if the operation fails, or nil if it succeeds.

Jump to

Keyboard shortcuts

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