2023-12-16 07:20:01 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2024-07-23 19:21:02 +00:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
querier "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2023-12-16 07:20:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2024-07-23 19:21:02 +00:00
|
|
|
Username string `koanf:"username"`
|
|
|
|
Password string `koanf:"password"`
|
|
|
|
Port int `koanf:"port"`
|
|
|
|
Host string `koanf:"host"`
|
|
|
|
DBName string `koanf:"db_name"`
|
2023-12-16 07:20:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 07:22:14 +00:00
|
|
|
type DB struct {
|
2024-07-23 19:21:02 +00:00
|
|
|
config Config
|
|
|
|
db *querier.SqlDB
|
|
|
|
mu sync.Mutex
|
|
|
|
statements map[string]*sql.Stmt
|
2023-12-16 07:20:01 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 21:25:20 +00:00
|
|
|
func (db *DB) Conn() *querier.SqlDB {
|
2024-07-23 19:21:02 +00:00
|
|
|
return db.db
|
2023-12-16 07:20:01 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 19:21:02 +00:00
|
|
|
// TODO: this temporary to ignore linter error (magic number).
|
2024-01-01 07:22:14 +00:00
|
|
|
const (
|
2024-07-23 19:21:02 +00:00
|
|
|
dbMaxConnLifetime = time.Minute * 3
|
|
|
|
dbMaxOpenConns = 10
|
|
|
|
dbMaxIdleConns = 10
|
2024-01-01 07:22:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func New(config Config) *DB {
|
2024-07-23 19:21:02 +00:00
|
|
|
// parseTime=true changes the output type of DATE and DATETIME values to time.Time
|
|
|
|
// instead of []byte / string
|
|
|
|
// The date or datetime like 0000-00-00 00:00:00 is converted into zero value of time.Time
|
|
|
|
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true",
|
|
|
|
config.Username, config.Password, config.Host, config.Port, config.DBName))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("can't open mysql db: %w", err))
|
|
|
|
}
|
2023-12-16 07:20:01 +00:00
|
|
|
|
2024-07-23 19:21:02 +00:00
|
|
|
// See "Important settings" section.
|
|
|
|
db.SetConnMaxLifetime(dbMaxConnLifetime)
|
|
|
|
db.SetMaxOpenConns(dbMaxOpenConns)
|
|
|
|
db.SetMaxIdleConns(dbMaxIdleConns)
|
2023-12-16 07:20:01 +00:00
|
|
|
|
2024-07-23 19:21:02 +00:00
|
|
|
return &DB{
|
|
|
|
config: config,
|
|
|
|
db: &querier.SqlDB{DB: db},
|
|
|
|
statements: make(map[string]*sql.Stmt),
|
|
|
|
}
|
2023-12-16 07:20:01 +00:00
|
|
|
}
|
2024-07-21 14:16:01 +00:00
|
|
|
|
|
|
|
func (p *DB) PrepareStatement(key string, query string) (*sql.Stmt, error) {
|
2024-07-23 19:21:02 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2024-07-21 14:16:01 +00:00
|
|
|
|
2024-07-23 19:21:02 +00:00
|
|
|
if stmt, ok := p.statements[key]; ok {
|
|
|
|
return stmt, nil
|
|
|
|
}
|
2024-07-21 14:16:01 +00:00
|
|
|
|
2024-07-23 19:21:02 +00:00
|
|
|
stmt, err := p.db.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.statements[key] = stmt
|
2024-07-21 14:16:01 +00:00
|
|
|
|
2024-07-23 19:21:02 +00:00
|
|
|
return stmt, nil
|
2024-07-21 14:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *DB) CloseStatements() error {
|
2024-07-23 19:21:02 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
2024-07-21 14:16:01 +00:00
|
|
|
|
2024-07-23 19:21:02 +00:00
|
|
|
for _, stmt := range p.statements {
|
|
|
|
err := stmt.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.statements = make(map[string]*sql.Stmt)
|
|
|
|
return nil
|
2024-07-21 14:16:01 +00:00
|
|
|
}
|