package connection

import (
	"github.com/jmoiron/sqlx"
	_ "github.com/mattn/go-sqlite3"
	"go_project/internal/configuration"
	"go_project/pkg/behaivor"
)

func OpenDefault() (db *sqlx.DB, err error) {
	connectionParameters := configuration.GetDatabaseLocation()
	return Open(connectionParameters)
}

func Open(connectionParameters string) (db *sqlx.DB, err error) {
	db, err = sqlx.Open("sqlite3", connectionParameters)
	if err != nil {
		return nil, err
	}

	err = db.Ping()
	if err != nil {
		return nil, err
	}

	return db, err
}

// Closing the database with error checking
func MustClose(db *sqlx.DB) {
	err := db.Close()
	behaivor.Anxiety("Database closing error", err)
}
