This SQL library provides a simple and efficient way to interact with PostgreSQL databases in Go applications. It wraps the sqlx
package, offering an easy-to-use interface for database operations including queries, transactions, and connection management.
- Connection management for PostgreSQL
- Support for regular queries and transactions
- Context-aware database operations
- Easy-to-use methods for common database operations (Select, Get, Exec, Query, QueryRow, Prepare)
- Database health check functionality
To use this SQL library in your Go project, run:
go get github.com/ahghazey/sql
To create a new PostgreSQL connection manager:
import "github.com/ahghazey/sql"
connectionString := "postgres://username:password@localhost/dbname?sslmode=disable"
cm, err := sql.NewPgConnectionManager(connectionString)
if err != nil {
// Handle error
}
defer cm.Close()
To perform a query:
query := cm.Query()
var users []User
err := query.Select(context.Background(), &users, "SELECT * FROM users WHERE active = $1", true)
if err != nil {
// Handle error
}
To perform operations within a transaction:
tx := cm.Transaction()
err := tx.Begin(context.Background())
if err != nil {
// Handle error
}
// Perform operations
_, err = tx.Exec(context.Background(), "INSERT INTO users (name, email) VALUES ($1, $2)", "folan el folany", "[email protected]")
if err != nil {
tx.Rollback(context.Background())
// Handle error
}
err = tx.Commit(context.Background())
if err != nil {
// Handle error
}
To check the health of the database connection:
err := cm.CheckDatabaseHealth()
if err != nil {
// Handle error
}
- NewPgConnectionManager(connectionString string) (*PgConnectionManager, error)
- Close() error
- Query() *Query
- Transaction() *Transaction
- CheckDatabaseHealth() error
- Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- Query(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
- QueryRow(ctx context.Context, query string, args ...interface{}) *sqlx.Row
- Prepare(ctx context.Context, query string) (*sqlx.Stmt, error)
- Close() error
- Begin(ctx context.Context) error
- Commit(ctx context.Context) error
- Rollback(ctx context.Context) error
- Query(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
- QueryRow(ctx context.Context, query string, args ...interface{}) *sqlx.Row
- Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- Prepare(ctx context.Context, query string) (*sqlx.Stmt, error)
- Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- Close() error