Matrix/utils/database/eventDatabaseConnector.go

279 lines
5.3 KiB
Go
Raw Normal View History

2020-10-01 15:45:57 +00:00
package database
2020-09-30 08:28:49 +00:00
import (
"database/sql"
2020-10-01 15:45:57 +00:00
"fmt"
"log"
2020-09-30 08:28:49 +00:00
"nutfactory.org/Matrix/entities/event"
2020-10-01 15:45:57 +00:00
"nutfactory.org/Matrix/entities/transaction"
2020-09-30 08:28:49 +00:00
)
2020-10-01 15:45:57 +00:00
func initEventTable(db *sql.DB) (err error) {
log.Printf("Init Event Table")
statement, err := db.Prepare(`CREATE TABLE IF NOT EXISTS event (
id TEXT PRIMARY KEY,
roomId TEXT,
txnId TEXT,
eventType TEXT,
content TEXT,
parentId TEXT,
depth INTEGER
)`)
if err != nil {
return
}
2020-09-30 08:28:49 +00:00
statement.Exec()
2020-10-01 15:45:57 +00:00
/*
newEvent := &event.Event{
Id: "test",
RoomId: "test",
EventType: "test",
Content: "{TEST}",
ParentId: "test1",
Depth: 0,
}
err = CreateEvent(db, newEvent, "test")
if err != nil {
log.Printf("Error Create: %s", err)
return
}
eventsRoom, err := ReadEventsFromRoom(db, "test")
if err != nil {
log.Printf("Error Read User: %s", err)
return
}
log.Println(eventsRoom)
eventsTxn, err := ReadEventsFromTransaction(db, "test")
if err != nil {
log.Printf("Error Read User: %s", err)
return
}
log.Println(eventsTxn)
newEvent.Content = "{TEST123}"
err = UpdateEvent(db, newEvent)
if err != nil {
log.Printf("Error Update: %s", err)
return
}
err = DeleteEvent(db, newEvent.Id)
if err != nil {
log.Printf("Error Delete: %s", err)
return
}
readEvent, err := ReadEvent(db, "test")
if err != nil {
log.Printf("Error Read: %s", err)
return
}
if readEvent != nil {
log.Printf("Event ID: %s RoomId: %s EventType: %s Content: %s ParentId: %s Depth: %s",
readEvent.Id, readEvent.RoomId, readEvent.EventType, readEvent.Content, readEvent.ParentId, readEvent.Depth)
} else {
log.Printf("No Event found")
}
*/
return
2020-09-30 08:28:49 +00:00
}
2020-10-01 15:45:57 +00:00
func CreateEvent(db *sql.DB, event *event.Event, txnId string) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO event
(id, roomId, txnId, eventType, content, parentId, depth)
VALUES
(?, ?, ?, ?, ?, ?, ?)`)
2020-09-30 08:28:49 +00:00
2020-10-01 15:45:57 +00:00
tx, err := db.Begin()
if err != nil {
return
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
_, err = stmt.Exec(event.Id, event.RoomId, txnId, event.EventType, event.Content, event.ParentId, event.Depth)
if err != nil {
return
}
tx.Commit()
return
}
func CreateEventsFromTransaction(db *sql.DB, transaction *transaction.Transaction) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO event
(id, roomId, txnId, eventType, content, parentId, depth)
VALUES
(?, ?, ?, ?, ?, ?, ?)`)
tx, err := db.Begin()
if err != nil {
return
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
for _, pdu := range transaction.PDUS {
_, err = stmt.Exec(pdu.Id, pdu.RoomId, transaction.Id, pdu.EventType, pdu.Content, pdu.ParentId, pdu.Depth)
if err != nil {
return
}
}
tx.Commit()
return
}
func ReadEvent(db *sql.DB, id string) (foundEvent *event.Event, err error) {
queryStmt := fmt.Sprintf(`SELECT id, roomId, eventType, content, parentId, depth
FROM event
WHERE id = '%s'`, id)
rows, err := db.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
if rows.Next() {
foundEvent = &event.Event{}
err = rows.Scan(&foundEvent.Id,
&foundEvent.RoomId,
&foundEvent.EventType,
&foundEvent.Content,
&foundEvent.ParentId,
&foundEvent.Depth,
)
if err != nil {
return
}
}
return
}
func ReadEventsFromRoom(db *sql.DB, roomId string) (events map[string]*event.Event, err error) {
queryStmt := fmt.Sprintf(`SELECT id, roomId, eventType, content, parentId, depth
FROM event
WHERE roomId = '%s'`, roomId)
rows, err := db.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
events = make(map[string]*event.Event)
for rows.Next() {
foundEvent := &event.Event{}
err = rows.Scan(&foundEvent.Id,
&foundEvent.RoomId,
&foundEvent.EventType,
&foundEvent.Content,
&foundEvent.ParentId,
&foundEvent.Depth,
)
if err != nil {
return
}
events[foundEvent.Id] = foundEvent
}
return
2020-09-30 08:28:49 +00:00
}
2020-10-01 15:45:57 +00:00
func ReadEventsFromTransaction(db *sql.DB, txnId string) (events map[string]*event.Event, err error) {
queryStmt := fmt.Sprintf(`SELECT id, roomId, eventType, content, parentId, depth
FROM event
WHERE txnId = '%s'`, txnId)
rows, err := db.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
2020-09-30 08:28:49 +00:00
2020-10-01 15:45:57 +00:00
events = make(map[string]*event.Event)
for rows.Next() {
foundEvent := &event.Event{}
err = rows.Scan(
&foundEvent.Id,
&foundEvent.RoomId,
&foundEvent.EventType,
&foundEvent.Content,
&foundEvent.ParentId,
&foundEvent.Depth,
)
if err != nil {
return
}
events[foundEvent.Id] = foundEvent
}
return
2020-09-30 08:28:49 +00:00
}
2020-10-01 15:45:57 +00:00
func UpdateEvent(db *sql.DB, event *event.Event) (err error) {
sqlStmt := fmt.Sprintf(`UPDATE event SET
roomId = ?,
eventType = ?,
content = ?,
parentId = ?,
depth = ?
WHERE id = ?`)
tx, err := db.Begin()
if err != nil {
return
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
2020-09-30 08:28:49 +00:00
2020-10-01 15:45:57 +00:00
_, err = stmt.Exec(
event.RoomId,
event.EventType,
event.Content,
event.ParentId,
event.Depth,
event.Id,
)
if err != nil {
return
}
tx.Commit()
return
2020-09-30 08:28:49 +00:00
}
2020-10-01 15:45:57 +00:00
func DeleteEvent(db *sql.DB, id string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM event
WHERE id = '%s'`, id)
tx, err := db.Begin()
if err != nil {
return
}
_, err = db.Exec(queryStmt)
if err != nil {
return
}
2020-09-30 08:28:49 +00:00
2020-10-01 15:45:57 +00:00
tx.Commit()
return
2020-09-30 08:28:49 +00:00
}