Skip to content

Instantly share code, notes, and snippets.

@rodrwan
Created October 5, 2018 13:16
Show Gist options
  • Save rodrwan/cfbeabbe6625d64318886a57cfe6c878 to your computer and use it in GitHub Desktop.
Save rodrwan/cfbeabbe6625d64318886a57cfe6c878 to your computer and use it in GitHub Desktop.
package postgres
import (
"unicode"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
)
// Postgres error codes
const (
UniqueViolation pq.ErrorCode = "23505"
ForeignKeyViolation pq.ErrorCode = "23503"
InvalidTextRepresentation pq.ErrorCode = "22P02"
NoDataFound pq.ErrorCode = "P0002"
)
// New create a new connection to postgres
func New(dsn string) (*sqlx.DB, error) {
db, err := sqlx.Connect("postgres", dsn)
if err != nil {
return nil, err
}
db.SetMaxIdleConns(4)
db.SetMaxOpenConns(16)
return db, nil
}
func nfcString(nfd string) (nfc string) {
isMn := func(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
nfc, _, _ = transform.String(t, nfd)
return
}
// SQLExecutor provides an abstraction layer over a SQL executor.
type SQLExecutor interface {
sqlx.Queryer
sqlx.Execer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment