146 lines
2.9 KiB
Go
146 lines
2.9 KiB
Go
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
|
|
"nutfactory.org/Matrix/entities/transaction"
|
|
)
|
|
|
|
func initTransactionTable(db *sql.DB) (err error) {
|
|
log.Printf("Init Transaction Table")
|
|
statement, err := db.Prepare(`CREATE TABLE IF NOT EXISTS txn (
|
|
id TEXT PRIMARY KEY,
|
|
origin TEXT,
|
|
timestamp INTEGER
|
|
)`)
|
|
if err != nil {
|
|
return
|
|
}
|
|
statement.Exec()
|
|
|
|
/*
|
|
newTransaction := &transaction.Transaction{Id: "test", Origin: "test.de", Timestamp: 1234}
|
|
err = CreateTransaction(db, newTransaction)
|
|
if err != nil {
|
|
log.Printf("Error Create: %s", err)
|
|
return
|
|
}
|
|
newTransaction.Origin = "test2.de"
|
|
err = UpdateTransaction(db, newTransaction)
|
|
if err != nil {
|
|
log.Printf("Error Update: %s", err)
|
|
return
|
|
}
|
|
err = DeleteTransaction(db, newTransaction.Id)
|
|
if err != nil {
|
|
log.Printf("Error Delete: %s", err)
|
|
return
|
|
}
|
|
readTransaction, err := ReadTransaction(db, newTransaction.Id)
|
|
if err != nil {
|
|
log.Printf("Error Read: %s", err)
|
|
return
|
|
}
|
|
if readTransaction != nil {
|
|
log.Printf("Transaction ID: %s Origin: %s Timestamp: %s PDUS: %s", readTransaction.Id, readTransaction.Origin, readTransaction.Timestamp, readTransaction.PDUS)
|
|
} else {
|
|
log.Printf("No Transaction found")
|
|
}
|
|
*/
|
|
return
|
|
}
|
|
|
|
func CreateTransaction(db *sql.DB, transaction *transaction.Transaction) (err error) {
|
|
sqlStmt := fmt.Sprintf(`INSERT INTO txn
|
|
(id, origin, timestamp)
|
|
VALUES
|
|
(?, ?, ?)`)
|
|
|
|
tx, err := 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 {
|
|
return
|
|
}
|
|
tx.Commit()
|
|
return
|
|
}
|
|
|
|
func ReadTransaction(db *sql.DB, id string) (foundTransaction *transaction.Transaction, err error) {
|
|
queryStmt := fmt.Sprintf(`SELECT id, origin, timestamp
|
|
FROM txn
|
|
WHERE id = '%s'`, id)
|
|
|
|
rows, err := db.Query(queryStmt)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
if rows.Next() {
|
|
foundTransaction = &transaction.Transaction{}
|
|
err = rows.Scan(&foundTransaction.Id, &foundTransaction.Origin, &foundTransaction.Timestamp)
|
|
if err != nil {
|
|
return
|
|
}
|
|
foundTransaction.PDUS, err = ReadEventsFromTransaction(db, foundTransaction.Id)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func UpdateTransaction(db *sql.DB, transaction *transaction.Transaction) (err error) {
|
|
sqlStmt := fmt.Sprintf(`UPDATE txn SET
|
|
origin = ?,
|
|
timestamp = ?
|
|
WHERE id = ?`)
|
|
|
|
tx, err := 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 {
|
|
return
|
|
}
|
|
|
|
tx.Commit()
|
|
return
|
|
}
|
|
|
|
func DeleteTransaction(db *sql.DB, id string) (err error) {
|
|
queryStmt := fmt.Sprintf(`DELETE FROM txn
|
|
WHERE id = '%s'`, id)
|
|
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
_, err = db.Exec(queryStmt)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
tx.Commit()
|
|
return
|
|
}
|