Skip to content
Snippets Groups Projects
db.go 1.24 KiB
Newer Older
Gabriel Zachmann's avatar
Gabriel Zachmann committed
package db

import (
	"database/sql"
	"fmt"
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	"time"
Gabriel Zachmann's avatar
Gabriel Zachmann committed

	log "github.com/sirupsen/logrus"

Gabriel Zachmann's avatar
Gabriel Zachmann committed
	"github.com/zachmann/mytoken/internal/config"

	// mysql driver
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

var dbCon *sqlx.DB

// Connect connects to the database using the mytoken config
Gabriel Zachmann's avatar
Gabriel Zachmann committed
func Connect() error {
	dsn := fmt.Sprintf("%s:%s@%s(%s)/%s", config.Get().DB.User, config.Get().DB.Password, "tcp", config.Get().DB.Host, config.Get().DB.DB)
	dbTmp, err := sqlx.Connect("mysql", dsn)
	if err != nil {
		return err
	}
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	dbTmp.SetConnMaxLifetime(time.Minute * 4)
	dbTmp.SetMaxOpenConns(10)
	dbTmp.SetMaxIdleConns(10)
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	dbCon = dbTmp
	return nil
}

// DB returns the database connection
Gabriel Zachmann's avatar
Gabriel Zachmann committed
func DB() *sqlx.DB {
	return dbCon
}

// NewNullString creates a new sql.NullString from the given string
Gabriel Zachmann's avatar
Gabriel Zachmann committed
func NewNullString(s string) sql.NullString {
	if len(s) == 0 {
		return sql.NullString{}
	}
	return sql.NullString{
		String: s,
		Valid:  true,
	}
}
// Transact does a database transaction for the passed function
func Transact(fn func(*sqlx.Tx) error) error {
	tx, err := DB().Beginx()
	if err != nil {
		return err
	}
	err = fn(tx)
	if err != nil {
		if e := tx.Rollback(); e != nil {
			log.WithError(err).Error()
		}
		return err
	}
	return tx.Commit()
}