2022-07-29 06:29:21 +02:00
|
|
|
// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2022-06-21 17:52:36 +02:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2023-06-05 16:38:58 +02:00
|
|
|
"database/sql"
|
2022-06-21 17:52:36 +02:00
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2023-06-05 16:38:58 +02:00
|
|
|
"github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/qustavo/sqlhooks/v2"
|
2022-06-21 17:52:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dbConnOnce sync.Once
|
|
|
|
dbConnInstance *DBConnection
|
|
|
|
)
|
|
|
|
|
|
|
|
type DBConnection struct {
|
2023-02-21 15:22:05 +01:00
|
|
|
DB *sqlx.DB
|
|
|
|
Driver string
|
2022-06-21 17:52:36 +02:00
|
|
|
}
|
|
|
|
|
2023-06-01 08:03:12 +02:00
|
|
|
type DatabaseOptions struct {
|
|
|
|
URL string
|
|
|
|
MaxOpenConnections int
|
|
|
|
MaxIdleConnections int
|
|
|
|
ConnectionMaxLifetime time.Duration
|
|
|
|
ConnectionMaxIdleTime time.Duration
|
|
|
|
}
|
|
|
|
|
2022-06-21 17:52:36 +02:00
|
|
|
func Connect(driver string, db string) {
|
|
|
|
var err error
|
|
|
|
var dbHandle *sqlx.DB
|
|
|
|
|
|
|
|
dbConnOnce.Do(func() {
|
2023-06-01 08:03:12 +02:00
|
|
|
opts := DatabaseOptions{
|
|
|
|
URL: db,
|
2023-06-01 13:57:35 +02:00
|
|
|
MaxOpenConnections: 4,
|
|
|
|
MaxIdleConnections: 4,
|
2023-06-01 08:03:12 +02:00
|
|
|
ConnectionMaxLifetime: time.Hour,
|
|
|
|
ConnectionMaxIdleTime: time.Hour,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch driver {
|
|
|
|
case "sqlite3":
|
2023-06-05 16:38:58 +02:00
|
|
|
sql.Register("sqlite3WithHooks", sqlhooks.Wrap(&sqlite3.SQLiteDriver{}, &Hooks{}))
|
2023-06-01 08:03:12 +02:00
|
|
|
|
|
|
|
// - Set WAL mode (not strictly necessary each time because it's persisted in the database, but good for first run)
|
|
|
|
// - Set busy timeout, so concurrent writers wait on each other instead of erroring immediately
|
|
|
|
// - Enable foreign key checks
|
|
|
|
opts.URL += "?_journal=WAL&_timeout=5000&_fk=true"
|
|
|
|
|
2023-06-05 16:38:58 +02:00
|
|
|
dbHandle, err = sqlx.Open("sqlite3WithHooks", opts.URL)
|
|
|
|
// dbHandle, err = sqlx.Open("sqlite3", opts.URL)
|
2022-06-21 17:52:36 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-06-01 08:03:12 +02:00
|
|
|
case "mysql":
|
|
|
|
opts.URL += "?multiStatements=true"
|
|
|
|
dbHandle, err = sqlx.Open("mysql", opts.URL)
|
2022-06-21 17:52:36 +02:00
|
|
|
if err != nil {
|
2023-01-23 18:48:06 +01:00
|
|
|
log.Fatalf("sqlx.Open() error: %v", err)
|
2022-06-21 17:52:36 +02:00
|
|
|
}
|
2023-06-01 08:03:12 +02:00
|
|
|
default:
|
2023-01-23 18:48:06 +01:00
|
|
|
log.Fatalf("unsupported database driver: %s", driver)
|
2022-06-21 17:52:36 +02:00
|
|
|
}
|
|
|
|
|
2023-06-01 08:03:12 +02:00
|
|
|
dbHandle.SetMaxOpenConns(opts.MaxOpenConnections)
|
|
|
|
dbHandle.SetMaxIdleConns(opts.MaxIdleConnections)
|
|
|
|
dbHandle.SetConnMaxLifetime(opts.ConnectionMaxLifetime)
|
|
|
|
dbHandle.SetConnMaxIdleTime(opts.ConnectionMaxIdleTime)
|
|
|
|
|
2023-02-21 15:22:05 +01:00
|
|
|
dbConnInstance = &DBConnection{DB: dbHandle, Driver: driver}
|
2023-04-21 12:59:27 +02:00
|
|
|
err = checkDBVersion(driver, dbHandle.DB)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-06-21 17:52:36 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetConnection() *DBConnection {
|
|
|
|
if dbConnInstance == nil {
|
2023-01-23 18:48:06 +01:00
|
|
|
log.Fatalf("Database connection not initialized!")
|
2022-06-21 17:52:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return dbConnInstance
|
|
|
|
}
|