db

package
v0.0.0-...-88dbfc4 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2023 License: AGPL-3.0 Imports: 6 Imported by: 0

README

Getting Started with Postgres access library

  • Please update this document with useful information when you happen to come across it.

pgxpool usage

Sample code demonstrating how pgxpool Acquire and Release is used inside threads.

package main

import (
	"context"
	"fmt"
	"sync"

	"github.com/jackc/pgx/v5/pgxpool"
)

func main() {
	// Create a connection pool
	pool, err := pgxpool.Connect(context.Background(), "postgres://username:password@localhost/database")
	if err != nil {
		fmt.Println("Error creating connection pool:", err)
		return
	}
	defer pool.Close()

	// Create a wait group to track the goroutines
	var wg sync.WaitGroup

	// Launch a goroutine for each query we want to execute
	for i := 1; i <= 10; i++ {
		wg.Add(1)
		go func(id int) {
			defer wg.Done()

			// Acquire a connection from the pool
			conn, err := pool.Acquire(context.Background())
			if err != nil {
				fmt.Println("Error acquiring connection:", err)
				return
			}
			defer conn.Release()

			// Execute a query using the connection
			row := conn.QueryRow(context.Background(), "SELECT name FROM users WHERE id=$1", id)

			// Scan the result of the query into a variable
			var name string
			err = row.Scan(&name)
			if err != nil {
				fmt.Println("Error scanning result:", err)
				return
			}

			fmt.Println("Name:", name)
		}(i)
	}

	// Wait for all goroutines to finish
	wg.Wait()
}

Documentation

Index

Constants

View Source
const POSTGRES_DRIVER_TIMEOUT = 10

Variables

View Source
var ErrProfiletMultipleRowsAffected = fmt.Errorf("more than one, row was affected in a single row operation")
View Source
var ErrProfiletMultipleRowsRetunred = fmt.Errorf("more than one, row was returned when was expected")
View Source
var ErrProfiletNotFound = fmt.Errorf("row not found")

Functions

This section is empty.

Types

type PostgresDriver

type PostgresDriver struct {
	Pool *pgxpool.Pool
	Log  *logrus.Logger
}

func NewPostgresDriver

func NewPostgresDriver(connectionUrl string, log *logrus.Logger) (*PostgresDriver, error)

func (*PostgresDriver) QueryRow

func (pd *PostgresDriver) QueryRow(sqlStatement string, structure interface{}, args ...any) error

func (*PostgresDriver) QueryRows

func (pd *PostgresDriver) QueryRows(sqlStatement string, structure interface{}, args ...any) error

func (*PostgresDriver) TransactOneRow

func (pd *PostgresDriver) TransactOneRow(sqlStatement string, args ...any) error

Jump to

Keyboard shortcuts

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