Add Database Functions

This commit is contained in:
hoernschen 2020-10-01 17:45:57 +02:00
parent 126070829c
commit 253b020733
22 changed files with 1076 additions and 119 deletions

17
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"env": {},
"args": []
}
]
}

View file

@ -3,5 +3,5 @@ package device
type Device struct { type Device struct {
Id string `json:"id,omitempty"` Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Keys map[string]string `json:"keys,omitempty"` Keys map[string]*Key `json:"keys,omitempty"`
} }

View file

@ -1,5 +1,5 @@
package device package device
func New() device *Device { func New() (device *Device) {
return
} }

7
entities/device/key.go Normal file
View file

@ -0,0 +1,7 @@
package device
type Key struct {
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Key string `json:"key,omitempty"`
}

View file

@ -1,5 +1,7 @@
package event package event
// TODO: Check if it can be deleted
type EDU struct { type EDU struct {
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
Content string `json:"content,omitempty"` Content string `json:"content,omitempty"`

View file

@ -5,6 +5,6 @@ type Event struct {
RoomId string `json:"roomId,omitempty"` RoomId string `json:"roomId,omitempty"`
EventType string `json:"eventType,omitempty"` EventType string `json:"eventType,omitempty"`
Content string `json:"content,omitempty"` Content string `json:"content,omitempty"`
Parent string `json:"parent,omitempty"` ParentId string `json:"parent,omitempty"`
Depth int `json:"depth,omitempty"` Depth int `json:"depth,omitempty"`
} }

View file

@ -1,5 +1,5 @@
package event package event
func New() event *Event { func New() (event *Event) {
return
} }

View file

@ -6,8 +6,8 @@ import (
type Room struct { type Room struct {
Id string `json:"id,omitempty"` Id string `json:"id,omitempty"`
Messages map[string]event.Event `json:"messages,omitempty"` Messages map[string]*event.Event `json:"messages,omitempty"`
State map[string]event.Event `json:"state,omitempty"` //State map[string]event.Event `json:"state,omitempty"`
Members []string `json:"members,omitempty"` Members []string `json:"members,omitempty"`
Version string `json:"version,omitempty"` Version string `json:"version,omitempty"`
} }

View file

@ -1,5 +1,5 @@
package room package room
func New() room *Room { func New() (room *Room) {
return
} }

View file

@ -6,6 +6,8 @@ import (
type Transaction struct { type Transaction struct {
Id string `json:"id,omitempty"` Id string `json:"id,omitempty"`
PDUS map[string]event.Event `json:"pdus,omitempty"` Origin string `json:"origin,omitempty"`
EDUS []event.EDU `json:"edus,omitempty"` Timestamp int `json:"timestamp,omitempty"`
PDUS map[string]*event.Event `json:"pdus,omitempty"`
//EDUS []event.EDU `json:"edus,omitempty"`
} }

View file

@ -1,5 +1,5 @@
package transaction package transaction
func New() transaction *Transaction { func New() (transaction *Transaction) {
return
} }

View file

@ -8,5 +8,5 @@ type User struct {
Id string `json:"id,omitempty"` Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
Devices map[string]device.Device `json:"devices,omitempty"` Devices map[string]*device.Device `json:"devices,omitempty"`
} }

View file

@ -1,5 +1,5 @@
package user package user
func New() user *User { func New() (user *User) {
return
} }

15
main.go
View file

@ -2,13 +2,11 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
"strconv"
"nutfactory.org/Matrix/utils" "nutfactory.org/Matrix/utils/database"
"nutfactory.org/Matrix/utils/router" "nutfactory.org/Matrix/utils/router"
) )
@ -75,18 +73,9 @@ func main() {
// TODO: Remove later // TODO: Remove later
os.Remove("sqlite.db") os.Remove("sqlite.db")
db := utils.InitDB("sqlite.db") db, _ := database.InitDB("sqlite.db")
defer db.Close() defer db.Close()
rows, _ := db.Query("SELECT id, firstname, lastname FROM people")
var id int
var firstname string
var lastname string
for rows.Next() {
rows.Scan(&id, &firstname, &lastname)
fmt.Println(strconv.Itoa(id) + ": " + firstname + " " + lastname)
}
router := router.NewRouter(routes) router := router.NewRouter(routes)
//router.PathPrefix("/").Handler(http.FileServer(http.Dir(htmlPath))) //router.PathPrefix("/").Handler(http.FileServer(http.Dir(htmlPath)))

BIN
sqlite.db Normal file

Binary file not shown.

View file

@ -2,14 +2,15 @@ package database
import ( import (
"database/sql" "database/sql"
"fmt"
"log" "log"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
func InitDB(filepath string) *sql.DB { func InitDB(filepath string) (db *sql.DB, err error) {
log.Printf("Init DB") log.Printf("Init DB")
db, err := sql.Open("sqlite3", filepath) db, err = sql.Open("sqlite3", filepath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -18,46 +19,17 @@ func InitDB(filepath string) *sql.DB {
} }
handleError(initDeviceTable(db)) handleError(initDeviceTable(db))
handleError(initKeyTable(db))
handleError(initEventTable(db)) handleError(initEventTable(db))
handleError(initRoomTable(db)) handleError(initRoomTable(db))
handleError(initTransactionTable(db)) handleError(initTransactionTable(db))
handleError(initUserTable(db)) handleError(initUserTable(db))
return db return
} }
func handleError(err error) { func handleError(err error) {
if err != nil { if err != nil {
// TODO: Add Error Message panic(fmt.Sprintf("Could not execute Database Query: %s", err))
panic("Could not execute Database Query")
} }
} }
/*
sqlStmt := fmt.Sprintf(`INSERT INTO data
(id, content)
VALUES
(?, ?)`)
tx, err := db.Begin()
if err != nil {
log.Panic(err)
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return err
}
defer stmt.Close()
for i := 1; i < 10; i++ {
id := fmt.Sprintf("%d", i)
content := fmt.Sprintf("content #%d %s", i, shortuuid.New())
log.Printf("Inserting %s: %s", id, content)
_, err := stmt.Exec(id, content)
if err != nil {
log.Panic(err)
}
}
tx.Commit()
*/

View file

@ -1,29 +1,179 @@
package device package database
import ( import (
"database/sql" "database/sql"
"fmt"
"log"
"nutfactory.org/Matrix/entities/device" "nutfactory.org/Matrix/entities/device"
) )
func initDeviceTable(db *sql.DB) err error { func initDeviceTable(db *sql.DB) (err error) {
// TODO: Change to correct Table-Structure log.Printf("Init Device Table")
statement, err := db.Prepare("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT)") statement, err := db.Prepare(`CREATE TABLE IF NOT EXISTS device (
id TEXT PRIMARY KEY,
name TEXT,
userId TEXT
)`)
if err != nil {
return
}
statement.Exec() statement.Exec()
/*
newDevice := &device.Device{Id: "test", Name: "TEST", Keys: nil}
err = CreateDevice(db, newDevice, "test")
if err != nil {
log.Printf("Error Create: %s", err)
return
}
newDevice.Name = "TEST2"
err = UpdateDevice(db, newDevice)
if err != nil {
log.Printf("Error Update: %s", err)
return
}
devices, err := ReadDevicesForUser(db, "test")
if err != nil {
log.Printf("Error Read User: %s", err)
return
}
log.Println(devices)
err = DeleteDevice(db, newDevice.Id)
if err != nil {
log.Printf("Error Delete: %s", err)
return
}
de, err := ReadDevice(db, "test")
if err != nil {
log.Printf("Error Read: %s", err)
return
}
if de != nil {
log.Printf("Device ID: %s Name: %s", de.Id, de.Name)
} else {
log.Printf("No Device found")
}
*/
return
} }
func createDevice(db *sql.DB, device *Device) err error { func CreateDevice(db *sql.DB, device *device.Device, userId string) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO device
(id, name, userId)
VALUES
(?, ?, ?)`)
tx, err := db.Begin()
if err != nil {
return
} }
func readDevice(db *sql.DB, id string) (device *Device, err error) { stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
_, err = stmt.Exec(device.Id, device.Name, userId)
if err != nil {
return
}
tx.Commit()
return
} }
func updateDevice(db *sql.DB, device *Device) err error { func ReadDevice(db *sql.DB, id string) (foundDevice *device.Device, err error) {
queryStmt := fmt.Sprintf(`SELECT id, name
FROM device
WHERE id = '%s'`, id)
rows, err := db.Query(queryStmt)
if err != nil {
return
} }
func deleteDevice(db *sql.DB, id string) err error { defer rows.Close()
if rows.Next() {
foundDevice = &device.Device{}
err = rows.Scan(&foundDevice.Id, &foundDevice.Name)
if err != nil {
return
}
foundDevice.Keys, err = ReadKeysForDevice(db, foundDevice.Id)
}
return
}
func ReadDevicesForUser(db *sql.DB, userId string) (devices map[string]*device.Device, err error) {
queryStmt := fmt.Sprintf(`SELECT id, name
FROM device
WHERE userId = '%s'`, userId)
rows, err := db.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
devices = make(map[string]*device.Device)
for rows.Next() {
foundDevice := &device.Device{}
err = rows.Scan(&foundDevice.Id, &foundDevice.Name)
if err != nil {
return
}
foundDevice.Keys, err = ReadKeysForDevice(db, foundDevice.Id)
devices[foundDevice.Id] = foundDevice
}
return
}
func UpdateDevice(db *sql.DB, device *device.Device) (err error) {
sqlStmt := fmt.Sprintf(`UPDATE device SET
name = ?
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(device.Name, device.Id)
if err != nil {
return
}
tx.Commit()
return
}
func DeleteDevice(db *sql.DB, id string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM device
WHERE id = '%s'`, id)
tx, err := db.Begin()
if err != nil {
return
}
_, err = db.Exec(queryStmt)
if err != nil {
return
}
tx.Commit()
return
} }

View file

@ -1,29 +1,278 @@
package event package database
import ( import (
"database/sql" "database/sql"
"fmt"
"log"
"nutfactory.org/Matrix/entities/event" "nutfactory.org/Matrix/entities/event"
"nutfactory.org/Matrix/entities/transaction"
) )
func initEventTable(db *sql.DB) err error { func initEventTable(db *sql.DB) (err error) {
// TODO: Change to correct Table-Structure log.Printf("Init Event Table")
statement, err := db.Prepare("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT)") 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() 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) err error { 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
} }
func readEvent(db *sql.DB, id string) (event *Event, err error) { 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 updateEvent(db *sql.DB, event *Event) err error { 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
} }
func deleteEvent(db *sql.DB, id string) err error { 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
} }

View file

@ -0,0 +1,177 @@
package database
import (
"database/sql"
"fmt"
"log"
"nutfactory.org/Matrix/entities/device"
)
func initKeyTable(db *sql.DB) (err error) {
log.Printf("Init Key Table")
statement, err := db.Prepare(`CREATE TABLE IF NOT EXISTS key (
id TEXT PRIMARY KEY,
type TEXT,
key TEXT,
deviceId TEXT
)`)
if err != nil {
return
}
statement.Exec()
/*
newKey := &device.Key{Id: "test", Type: "test", Key: "test"}
err = CreateKey(db, newKey, "test")
if err != nil {
log.Printf("Error Create: %s", err)
return
}
keys, err := ReadKeysForDevice(db, "test")
if err != nil {
log.Printf("Error Read Multiple: %s", err)
return
}
log.Println(keys)
newKey.Key = "TEST123"
err = UpdateKey(db, newKey)
if err != nil {
log.Printf("Error Update: %s", err)
return
}
err = DeleteKey(db, newKey.Id)
if err != nil {
log.Printf("Error Delete: %s", err)
return
}
readKey, err := ReadKey(db, "test")
if err != nil {
log.Printf("Error Read: %s", err)
return
}
if readKey != nil {
log.Printf("Key ID: %s Type: %s, Key: %s", readKey.Id, readKey.Type, readKey.Key)
} else {
log.Printf("No Key found")
}
*/
return
}
func CreateKey(db *sql.DB, key *device.Key, deviceId string) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO key
(id, type, key, deviceId)
VALUES
(?, ?, ?, ?)`)
tx, err := db.Begin()
if err != nil {
return
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
_, err = stmt.Exec(key.Id, key.Type, key.Key, deviceId)
if err != nil {
return
}
tx.Commit()
return
}
func ReadKey(db *sql.DB, id string) (foundKey *device.Key, err error) {
queryStmt := fmt.Sprintf(`SELECT id, type, key
FROM key
WHERE id = '%s'`, id)
rows, err := db.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
if rows.Next() {
foundKey = &device.Key{}
err = rows.Scan(&foundKey.Id, &foundKey.Type, &foundKey.Key)
if err != nil {
return
}
}
return
}
func ReadKeysForDevice(db *sql.DB, deviceId string) (keys map[string]*device.Key, err error) {
queryStmt := fmt.Sprintf(`SELECT id, type, key
FROM key
WHERE deviceId = '%s'`, deviceId)
rows, err := db.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
keys = make(map[string]*device.Key)
for rows.Next() {
foundKey := &device.Key{}
err = rows.Scan(&foundKey.Id, &foundKey.Type, &foundKey.Key)
if err != nil {
return
}
keys[foundKey.Id] = foundKey
}
return
}
func UpdateKey(db *sql.DB, key *device.Key) (err error) {
sqlStmt := fmt.Sprintf(`UPDATE key SET
type = ?,
key = ?
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(key.Type, key.Key, key.Id)
if err != nil {
return
}
tx.Commit()
return
}
func DeleteKey(db *sql.DB, id string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM key
WHERE id = '%s'`, id)
tx, err := db.Begin()
if err != nil {
return
}
_, err = db.Exec(queryStmt)
if err != nil {
return
}
tx.Commit()
return
}

View file

@ -1,29 +1,241 @@
package room package database
import ( import (
"database/sql" "database/sql"
"fmt"
"log"
"nutfactory.org/Matrix/entities/room" "nutfactory.org/Matrix/entities/room"
) )
func initRoomTable(db *sql.DB) err error { func initRoomTable(db *sql.DB) (err error) {
// TODO: Change to correct Table-Structure log.Printf("Init Room Table")
statement, err := db.Prepare("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT)") statement, err := db.Prepare(`CREATE TABLE IF NOT EXISTS room (
id TEXT PRIMARY KEY,
version TEXT
)`)
if err != nil {
return
}
statement.Exec() statement.Exec()
statement, err = db.Prepare(`CREATE TABLE IF NOT EXISTS roomMember (
userId TEXT,
roomId TEXT,
PRIMARY KEY (userId, roomId)
)`)
if err != nil {
return
}
statement.Exec()
//TODO: Test Queries
return
} }
func createRoom(db *sql.DB, room *Room) err error { func CreateRoom(db *sql.DB, room *room.Room, userId string) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO room
(id, version)
VALUES
(?, ?)`)
tx, err := db.Begin()
if err != nil {
return
} }
func readRoom(db *sql.DB, id string) (room *Room, err error) { stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
_, err = stmt.Exec(room.Id, room.Version)
if err != nil {
return
}
tx.Commit()
err = CreateRoomMember(db, room.Id, userId)
return
} }
func updateRoom(db *sql.DB, room *Room) err error { func CreateRoomMember(db *sql.DB, roomId string, userId string) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO roomMember
(roomId, userId)
VALUES
(?, ?)`)
tx, err := db.Begin()
if err != nil {
return
} }
func deleteRoom(db *sql.DB, id string) err error { stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
_, err = stmt.Exec(roomId, userId)
if err != nil {
return
}
tx.Commit()
return
}
func ReadRoom(db *sql.DB, id string) (foundRoom *room.Room, err error) {
queryStmt := fmt.Sprintf(`SELECT id, version
FROM room
WHERE id = '%s'`, id)
rows, err := db.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
if rows.Next() {
foundRoom = &room.Room{}
err = rows.Scan(&foundRoom.Id, &foundRoom.Version)
if err != nil {
return
}
foundRoom.Messages, err = ReadEventsFromRoom(db, foundRoom.Id)
if err != nil {
return
}
foundRoom.Members, err = ReadRoomMembers(db, foundRoom.Id)
if err != nil {
return
}
}
return
}
func ReadRoomMembers(db *sql.DB, roomId string) (roomMembers []string, err error) {
queryStmt := fmt.Sprintf(`SELECT userId
FROM roomMember
WHERE roomId = '%s'`, roomId)
rows, err := db.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
roomMembers = []string{}
for rows.Next() {
var foundUser string
err = rows.Scan(&foundUser)
if err != nil {
return
}
roomMembers = append(roomMembers, foundUser)
}
return
}
func UpdateRoom(db *sql.DB, room *room.Room) (err error) {
sqlStmt := fmt.Sprintf(`UPDATE room SET
version = ?
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(room.Version, room.Id)
if err != nil {
return
}
tx.Commit()
return
}
func DeleteRoom(db *sql.DB, id string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM room
WHERE id = '%s'`, id)
tx, err := db.Begin()
if err != nil {
return
}
_, err = db.Exec(queryStmt)
if err != nil {
return
}
err = DeleteAllRoomMemberForRoom(db, id)
tx.Commit()
return
}
func DeleteRoomMember(db *sql.DB, roomId string, userId string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
WHERE userId = '%s' AND roomId = '%s'`, userId, roomId)
tx, err := db.Begin()
if err != nil {
return
}
_, err = db.Exec(queryStmt)
if err != nil {
return
}
tx.Commit()
return
}
func DeleteAllRoomMemberForUser(db *sql.DB, userId string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
WHERE userId = '%s'`, userId)
tx, err := db.Begin()
if err != nil {
return
}
_, err = db.Exec(queryStmt)
if err != nil {
return
}
tx.Commit()
return
}
func DeleteAllRoomMemberForRoom(db *sql.DB, roomId string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM roomMember
WHERE roomId = '%s'`, roomId)
tx, err := db.Begin()
if err != nil {
return
}
_, err = db.Exec(queryStmt)
if err != nil {
return
}
tx.Commit()
return
} }

View file

@ -1,29 +1,119 @@
package transaction package database
import ( import (
"database/sql" "database/sql"
"fmt"
"log"
"nutfactory.org/Matrix/entities/transaction" "nutfactory.org/Matrix/entities/transaction"
) )
func initTransactionTable(db *sql.DB) err error { func initTransactionTable(db *sql.DB) (err error) {
// TODO: Change to correct Table-Structure log.Printf("Init Transaction Table")
statement, err := db.Prepare("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT)") statement, err := db.Prepare(`CREATE TABLE IF NOT EXISTS txn (
id TEXT PRIMARY KEY,
origin TEXT,
timestamp INTEGER
)`)
if err != nil {
return
}
statement.Exec() statement.Exec()
//TODO: Test Queries
return
} }
func createTransaction(db *sql.DB, transaction *Transaction) err error { 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
} }
func readTransaction(db *sql.DB, id string) (transaction *Transaction, err error) { 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 updateTransaction(db *sql.DB, transaction *Transaction) err error { 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
} }
func deleteTransaction(db *sql.DB, id string) err error { 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
} }

View file

@ -1,29 +1,119 @@
package user package database
import ( import (
"database/sql" "database/sql"
"fmt"
"log"
"nutfactory.org/Matrix/entities/user" "nutfactory.org/Matrix/entities/user"
) )
func initUserTable(db *sql.DB) err error { func initUserTable(db *sql.DB) (err error) {
// TODO: Change to correct Table-Structure log.Printf("Init User Table")
statement, err := db.Prepare("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT)") statement, err := db.Prepare(`CREATE TABLE IF NOT EXISTS user (
id TEXT PRIMARY KEY,
name TEXT,
password TEXT
)`)
if err != nil {
return
}
statement.Exec() statement.Exec()
//TODO: Test Queries
return
} }
func createUser(db *sql.DB, user *User) err error { func createUser(db *sql.DB, user *user.User) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO user
(id, name, password)
VALUES
(?, ?, ?)`)
tx, err := db.Begin()
if err != nil {
return
} }
func readUser(db *sql.DB id string) (user *User, err error) { stmt, err := tx.Prepare(sqlStmt)
if err != nil {
return
}
defer stmt.Close()
_, err = stmt.Exec(user.Id, user.Name, user.Password)
if err != nil {
return
}
tx.Commit()
return
} }
func updateUser(db *sql.DB, user *User) err error { func readUser(db *sql.DB, id string) (foundUser *user.User, err error) {
queryStmt := fmt.Sprintf(`SELECT id, name, password
FROM user
WHERE id = '%s'`, id)
rows, err := db.Query(queryStmt)
if err != nil {
return
} }
func deleteUser(db *sql.DB, id string) err error { defer rows.Close()
if rows.Next() {
foundUser = &user.User{}
err = rows.Scan(&foundUser.Id, &foundUser.Name, &foundUser.Password)
if err != nil {
return
}
foundUser.Devices, err = ReadDevicesForUser(db, foundUser.Id)
}
return
}
func updateUser(db *sql.DB, user *user.User) (err error) {
sqlStmt := fmt.Sprintf(`UPDATE user SET
name = ?,
password = ?
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(user.Name, user.Password, user.Id)
if err != nil {
return
}
tx.Commit()
return
}
func deleteUser(db *sql.DB, id string) (err error) {
queryStmt := fmt.Sprintf(`DELETE FROM user
WHERE id = '%s'`, id)
tx, err := db.Begin()
if err != nil {
return
}
_, err = db.Exec(queryStmt)
if err != nil {
return
}
tx.Commit()
return
} }