209 lines
3.6 KiB
Go
209 lines
3.6 KiB
Go
|
package event
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"nutfactory.org/Matrix/utils/database"
|
||
|
)
|
||
|
|
||
|
func CreateEvent(event *Event, txnId string) (err error) {
|
||
|
sqlStmt := fmt.Sprintf(`INSERT INTO event
|
||
|
(id, roomId, txnId, eventType, content, parentId, depth)
|
||
|
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(event.Id, event.RoomId, txnId, event.EventType, event.Content, event.ParentId, event.Depth)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
tx.Commit()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func CreateEventsFromTransaction(txnId string, pdus map[string]*Event) (err error) {
|
||
|
sqlStmt := fmt.Sprintf(`INSERT INTO event
|
||
|
(id, roomId, txnId, eventType, content, parentId, depth)
|
||
|
VALUES
|
||
|
(?, ?, ?, ?, ?, ?, ?)`)
|
||
|
|
||
|
tx, err := database.DB.Begin()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
stmt, err := tx.Prepare(sqlStmt)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
defer stmt.Close()
|
||
|
|
||
|
for _, pdu := range pdus {
|
||
|
_, err = stmt.Exec(pdu.Id, pdu.RoomId, txnId, pdu.EventType, pdu.Content, pdu.ParentId, pdu.Depth)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
tx.Commit()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func ReadEvent(id string) (foundEvent *Event, err error) {
|
||
|
queryStmt := fmt.Sprintf(`SELECT id, roomId, eventType, content, parentId, depth
|
||
|
FROM event
|
||
|
WHERE id = '%s'`, id)
|
||
|
|
||
|
rows, err := database.DB.Query(queryStmt)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
defer rows.Close()
|
||
|
|
||
|
if rows.Next() {
|
||
|
foundEvent = &Event{}
|
||
|
err = rows.Scan(&foundEvent.Id,
|
||
|
&foundEvent.RoomId,
|
||
|
&foundEvent.EventType,
|
||
|
&foundEvent.Content,
|
||
|
&foundEvent.ParentId,
|
||
|
&foundEvent.Depth,
|
||
|
)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func ReadEventsFromRoom(roomId string) (events map[string]*Event, err error) {
|
||
|
queryStmt := fmt.Sprintf(`SELECT id, roomId, eventType, content, parentId, depth
|
||
|
FROM event
|
||
|
WHERE roomId = '%s'`, roomId)
|
||
|
|
||
|
rows, err := database.DB.Query(queryStmt)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
defer rows.Close()
|
||
|
|
||
|
events = make(map[string]*Event)
|
||
|
|
||
|
for rows.Next() {
|
||
|
foundEvent := &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
|
||
|
}
|
||
|
|
||
|
func ReadEventsFromTransaction(txnId string) (events map[string]*Event, err error) {
|
||
|
queryStmt := fmt.Sprintf(`SELECT id, roomId, eventType, content, parentId, depth
|
||
|
FROM event
|
||
|
WHERE txnId = '%s'`, txnId)
|
||
|
|
||
|
rows, err := database.DB.Query(queryStmt)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
defer rows.Close()
|
||
|
|
||
|
events = make(map[string]*Event)
|
||
|
|
||
|
for rows.Next() {
|
||
|
foundEvent := &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
|
||
|
}
|
||
|
|
||
|
func UpdateEvent(event *Event) (err error) {
|
||
|
sqlStmt := fmt.Sprintf(`UPDATE event SET
|
||
|
roomId = ?,
|
||
|
eventType = ?,
|
||
|
content = ?,
|
||
|
parentId = ?,
|
||
|
depth = ?
|
||
|
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(
|
||
|
event.RoomId,
|
||
|
event.EventType,
|
||
|
event.Content,
|
||
|
event.ParentId,
|
||
|
event.Depth,
|
||
|
event.Id,
|
||
|
)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
tx.Commit()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func DeleteEvent(id string) (err error) {
|
||
|
queryStmt := fmt.Sprintf(`DELETE FROM event
|
||
|
WHERE id = '%s'`, id)
|
||
|
|
||
|
tx, err := database.DB.Begin()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
_, err = database.DB.Exec(queryStmt)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
tx.Commit()
|
||
|
return
|
||
|
}
|