database

package
v0.0.0-...-4e1f151 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2021 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package database provides real connection to the database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PgxPoolIface

type PgxPoolIface interface {
	Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
	Ping(ctx context.Context) error
}

PgxPoolIface defines a little interface for pgxpool functionality. Since in the real implementation we can use pgxpool.Pool, this interface exists mostly for testing purpose.

type URLPostgres

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

URLPostgres is responsible to connect URL with PostgreSQL.

func NewURLPostgres

func NewURLPostgres(pool PgxPoolIface) *URLPostgres

NewURLPostgres creates an instance of URLPostgres.

func (*URLPostgres) GetAll

func (ur *URLPostgres) GetAll(ctx context.Context) ([]*entity.URL, error)

GetAll gets all URLs from PostgreSQL. If there isn't any data, it returns empty list and nil error.

func (*URLPostgres) GetByCode

func (ur *URLPostgres) GetByCode(ctx context.Context, code string) (*entity.URL, error)

GetByCode gets a single URL by its code. It returns entity.ErrNotFound() if the URL can't be found.

func (*URLPostgres) Insert

func (ur *URLPostgres) Insert(ctx context.Context, url *entity.URL) error

Insert inserts a URL into PostgreSQL by running SQL INSERT query. It validates if the URL already exists.

func (*URLPostgres) IsAlive

func (ur *URLPostgres) IsAlive(ctx context.Context) bool

IsAlive must returns true if Postgres can connect without any problem. It basically calls Ping() method.

Jump to

Keyboard shortcuts

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