Mainflux.mainflux/consumers/notifiers/postgres/database.go

71 lines
2.0 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package postgres
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
notifiers "github.com/mainflux/mainflux/consumers/notifiers"
"github.com/mainflux/mainflux/pkg/errors"
"github.com/opentracing/opentracing-go"
)
var _ Database = (*database)(nil)
type database struct {
db *sqlx.DB
}
// Database provides a database interface
type Database interface {
NamedExecContext(context.Context, string, interface{}) (sql.Result, error)
QueryRowxContext(context.Context, string, ...interface{}) *sqlx.Row
NamedQueryContext(context.Context, string, interface{}) (*sqlx.Rows, error)
GetContext(context.Context, interface{}, string, ...interface{}) error
}
// NewDatabase creates a SubscriptionsDatabase instance
func NewDatabase(db *sqlx.DB) Database {
return &database{
db: db,
}
}
func (dm database) NamedExecContext(ctx context.Context, query string, args interface{}) (sql.Result, error) {
addSpanTags(ctx, query)
result, err := dm.db.NamedExecContext(ctx, query, args)
if pqErr, ok := err.(*pq.Error); ok && errDuplicate == pqErr.Code.Name() {
return result, errors.Wrap(notifiers.ErrConflict, err)
}
return result, err
}
func (dm database) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
addSpanTags(ctx, query)
return dm.db.QueryRowxContext(ctx, query, args...)
}
func (dm database) NamedQueryContext(ctx context.Context, query string, args interface{}) (*sqlx.Rows, error) {
addSpanTags(ctx, query)
return dm.db.NamedQueryContext(ctx, query, args)
}
func (dm database) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
addSpanTags(ctx, query)
return dm.db.GetContext(ctx, dest, query, args...)
}
func addSpanTags(ctx context.Context, query string) {
span := opentracing.SpanFromContext(ctx)
if span != nil {
span.SetTag("sql.statement", query)
span.SetTag("span.kind", "client")
span.SetTag("peer.service", "postgres")
span.SetTag("db.type", "sql")
}
}