Files
simpleorm/dbconnection.go
gdulai a6ff01d694
Go Tests / test (push) Failing after 6s
Transaction implementation (#2)
Reviewed-on: #2
2026-05-24 16:40:04 +00:00

51 lines
971 B
Go

package simpleorm
import (
"database/sql"
log "gitlab.com/gdulai/simpleloglvl"
)
type DBConnection struct {
db *sql.DB
State int
}
func OpenConnection(driver string, dsn string) *DBConnection {
// Open encrypted database
db, err := sql.Open(driver, dsn)
if err != nil {
log.LogError("Failed to open db connection: %s", err)
return &DBConnection{db: nil, State: -1}
}
return &DBConnection{db: db, State: 1}
}
func (c *DBConnection) Exec(sql string) (sql.Result, error) {
// Force a real DB interaction
result, err := c.db.Exec(sql)
if err != nil {
log.LogFatalError("DB execution failed: %", err)
return nil, err
}
return result, nil
}
func (c *DBConnection) Prepare(sql string) (*sql.Stmt, error) {
return c.db.Prepare(sql)
}
func (c *DBConnection) Begin() (*sql.Tx, error) {
return c.db.Begin()
}
func (c *DBConnection) Close() (bool, error) {
err := c.db.Close()
if err != nil {
return false, err
}
c = nil
return true, nil
}