Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ac68aed86 | |||
| bc1b0ff731 |
37
database/connect.go
Normal file
37
database/connect.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Connect(connStr string) (*pgxpool.Pool, error) {
|
||||||
|
pool, err := pgxpool.New(context.Background(), connStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return pool, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsDuplicatedRow(err error) error {
|
||||||
|
var pgErr *pgconn.PgError
|
||||||
|
|
||||||
|
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||||
|
return errors.New("duplicated row found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NoRowsInQuerySet(err error) error {
|
||||||
|
if err == pgx.ErrNoRows {
|
||||||
|
return errors.New("no rows found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user