Skip to content

Commit

Permalink
Merge pull request #9 from KowalskiPiotr98/transaction-support
Browse files Browse the repository at this point in the history
Add transaction support
  • Loading branch information
KowalskiPiotr98 authored Dec 5, 2023
2 parents 6ef84bf + 04b3186 commit dcb41d6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions connection_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ func GetConnection() Connector {
return connection
}

func BeginTransaction() (*Transaction, error) {
if connection == nil {
panic("database connection not initialised")
}

tx, err := connection.database.Begin()
if err != nil {
return nil, err
}
return newTransaction(tx), nil
}

func CloseConnection() error {
if connection == nil {
return nil
Expand Down
34 changes: 34 additions & 0 deletions transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gotabase

import "database/sql"

type Transaction struct {
tx *sql.Tx
}

func newTransaction(tx *sql.Tx) *Transaction {
return &Transaction{tx: tx}
}

var _ Connector = (*Transaction)(nil)

func (t *Transaction) QueryRow(sql string, args ...interface{}) (Row, error) {
row := t.tx.QueryRow(sql, args...)
return row, nil
}

func (t *Transaction) QueryRows(sql string, args ...interface{}) (Rows, error) {
return t.tx.Query(sql, args...)
}

func (t *Transaction) Exec(sql string, args ...interface{}) (Result, error) {
return t.tx.Exec(sql, args...)
}

func (t *Transaction) Commit() error {
return t.tx.Commit()
}

func (t *Transaction) Rollback() error {
return t.tx.Rollback()
}

0 comments on commit dcb41d6

Please sign in to comment.