package database import ( "database/sql" "fmt" "log" "nutfactory.org/Matrix/entities/event" "nutfactory.org/Matrix/entities/transaction" ) 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 } statement.Exec() /* 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 } 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 (?, ?, ?, ?, ?, ?, ?)`) 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 } 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() 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 } 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() _, err = stmt.Exec( event.RoomId, event.EventType, event.Content, event.ParentId, event.Depth, event.Id, ) if err != nil { return } tx.Commit() return } 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 } tx.Commit() return }