2020-10-11 21:11:30 +00:00
|
|
|
package event
|
2020-10-03 12:22:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-10-12 14:16:28 +00:00
|
|
|
"git.nutfactory.org/hoernschen/Matrix/utils/database"
|
2020-10-03 12:22:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func CreateTransaction(transaction *Transaction) (err error) {
|
|
|
|
sqlStmt := fmt.Sprintf(`INSERT INTO txn
|
|
|
|
(id, origin, timestamp)
|
|
|
|
VALUES
|
|
|
|
(?, ?, ?)`)
|
|
|
|
|
|
|
|
tx, err := database.DB.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(sqlStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
_, err = stmt.Exec(transaction.Id, transaction.Origin, transaction.Timestamp)
|
|
|
|
if err != nil {
|
2020-10-17 10:07:39 +00:00
|
|
|
tx.Rollback()
|
2020-10-03 12:22:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadTransaction(id string) (foundTransaction *Transaction, err error) {
|
|
|
|
queryStmt := fmt.Sprintf(`SELECT id, origin, timestamp
|
|
|
|
FROM txn
|
|
|
|
WHERE id = '%s'`, id)
|
|
|
|
|
|
|
|
rows, err := database.DB.Query(queryStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
foundTransaction = &Transaction{}
|
|
|
|
err = rows.Scan(&foundTransaction.Id, &foundTransaction.Origin, &foundTransaction.Timestamp)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-11 21:11:30 +00:00
|
|
|
foundTransaction.PDUS, err = ReadEventsFromTransaction(foundTransaction.Id)
|
2020-10-03 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateTransaction(transaction *Transaction) (err error) {
|
|
|
|
sqlStmt := fmt.Sprintf(`UPDATE txn SET
|
|
|
|
origin = ?,
|
|
|
|
timestamp = ?
|
|
|
|
WHERE id = ?`)
|
|
|
|
|
|
|
|
tx, err := database.DB.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(sqlStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
_, err = stmt.Exec(transaction.Origin, transaction.Timestamp, transaction.Id)
|
|
|
|
if err != nil {
|
2020-10-17 10:07:39 +00:00
|
|
|
tx.Rollback()
|
2020-10-03 12:22:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteTransaction(id string) (err error) {
|
|
|
|
queryStmt := fmt.Sprintf(`DELETE FROM txn
|
|
|
|
WHERE id = '%s'`, id)
|
|
|
|
|
|
|
|
tx, err := database.DB.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-17 10:07:39 +00:00
|
|
|
_, err = tx.Exec(queryStmt)
|
2020-10-03 12:22:29 +00:00
|
|
|
if err != nil {
|
2020-10-17 10:07:39 +00:00
|
|
|
tx.Rollback()
|
2020-10-03 12:22:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
return
|
|
|
|
}
|