Files
budgeteer/postgres/conn.go

72 lines
1.4 KiB
Go

package postgres
import (
"database/sql"
"embed"
"fmt"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/pressly/goose/v3"
)
//go:embed schema/*.sql
var migrations embed.FS
// Connect to a database
func Connect(server string, user string, password string, database string) (*Queries, *sql.DB, error) {
connString := fmt.Sprintf("postgres://%s:%s@%s/%s", user, password, server, database)
conn, err := sql.Open("pgx", connString)
if err != nil {
return nil, nil, err
}
goose.SetBaseFS(migrations)
if err = goose.Up(conn, "schema"); err != nil {
return nil, nil, err
}
return New(conn), conn, nil
}
func (tx Transaction) GetAmount() float64 {
var amount float64
err := tx.Amount.AssignTo(&amount)
if err != nil {
panic(err)
}
return amount
}
func (tx Transaction) GetPositive() bool {
amount := tx.GetAmount()
return amount >= 0
}
func (tx GetTransactionsForBudgetRow) GetAmount() float64 {
var amount float64
err := tx.Amount.AssignTo(&amount)
if err != nil {
panic(err)
}
return amount
}
func (tx GetTransactionsForBudgetRow) GetPositive() bool {
amount := tx.GetAmount()
return amount >= 0
}
func (tx GetAccountsWithBalanceRow) GetBalance() float64 {
var balance float64
err := tx.Balance.AssignTo(&balance)
if err != nil {
panic(err)
}
return balance
}
func (tx GetAccountsWithBalanceRow) GetPositive() bool {
balance := tx.GetBalance()
return balance >= 0
}