sql

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

Go SQL Library

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.

Features

  • 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

Installation

To use this SQL library in your Go project, run:

go get github.com/ahghazey/sql

Usage

Initializing the Connection

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()
Performing Queries

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
}
Using Transactions

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", "folan@example.com")
if err != nil {
tx.Rollback(context.Background())
// Handle error
}

err = tx.Commit(context.Background())
if err != nil {
// Handle error
}
Checking Database Health

To check the health of the database connection:

err := cm.CheckDatabaseHealth()
if err != nil {
    // Handle error
}

API Reference

Connection Manager
  • NewPgConnectionManager(connectionString string) (*PgConnectionManager, error)
  • Close() error
  • Query() *Query
  • Transaction() *Transaction
  • CheckDatabaseHealth() error
Query
  • 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
Transaction
  • 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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection interface {
	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
}

type PgConnectionManager

type PgConnectionManager struct {
	// contains filtered or unexported fields
}

func NewPgConnectionManager

func NewPgConnectionManager(connectionString string) (*PgConnectionManager, error)

func (*PgConnectionManager) CheckDatabaseHealth

func (cm *PgConnectionManager) CheckDatabaseHealth() error

func (*PgConnectionManager) Close

func (cm *PgConnectionManager) Close() error

func (*PgConnectionManager) Query

func (cm *PgConnectionManager) Query() *Query

func (*PgConnectionManager) Transaction

func (cm *PgConnectionManager) Transaction() *Transaction

type Query

type Query struct {
	// contains filtered or unexported fields
}

func NewQuery

func NewQuery(db *sqlx.DB) *Query

func (*Query) Close

func (q *Query) Close() error

func (*Query) Exec

func (q *Query) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*Query) Get

func (q *Query) Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error

func (*Query) Prepare

func (q *Query) Prepare(ctx context.Context, query string) (*sqlx.Stmt, error)

func (*Query) Query

func (q *Query) Query(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)

func (*Query) QueryRow

func (q *Query) QueryRow(ctx context.Context, query string, args ...interface{}) *sqlx.Row

func (*Query) Select

func (q *Query) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error

type TX

type TX interface {
	Begin(ctx context.Context) error
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

type Transaction

type Transaction struct {
	// contains filtered or unexported fields
}

func NewTransaction

func NewTransaction(db *sqlx.DB) *Transaction

func (*Transaction) Begin

func (t *Transaction) Begin(ctx context.Context) error

func (*Transaction) Close

func (t *Transaction) Close() error

func (*Transaction) Commit

func (t *Transaction) Commit(ctx context.Context) error

func (*Transaction) Exec

func (t *Transaction) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*Transaction) Get

func (t *Transaction) Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error

func (*Transaction) Prepare

func (t *Transaction) Prepare(ctx context.Context, query string) (*sqlx.Stmt, error)

func (*Transaction) Query

func (t *Transaction) Query(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)

func (*Transaction) QueryRow

func (t *Transaction) QueryRow(ctx context.Context, query string, args ...interface{}) *sqlx.Row

func (*Transaction) Rollback

func (t *Transaction) Rollback(ctx context.Context) error

func (*Transaction) Select

func (t *Transaction) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error

Jump to

Keyboard shortcuts

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