Matrix/utils/database/deviceDatabaseConnector.go

180 lines
3.2 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/device"
)
2020-10-01 15:45:57 +00:00
func initDeviceTable(db *sql.DB) (err error) {
log.Printf("Init Device Table")
statement, err := db.Prepare(`CREATE TABLE IF NOT EXISTS device (
id TEXT PRIMARY KEY,
name TEXT,
userId TEXT
)`)
if err != nil {
return
}
2020-09-30 08:28:49 +00:00
statement.Exec()
2020-10-01 15:45:57 +00:00
/*
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.Device, userId string) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO device
(id, name, userId)
VALUES
(?, ?, ?)`)
tx, err := db.Begin()
if err != nil {
return
}
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
2020-09-30 08:28:49 +00:00
}
2020-10-01 15:45:57 +00:00
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
}
defer rows.Close()
2020-09-30 08:28:49 +00:00
2020-10-01 15:45:57 +00:00
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
2020-09-30 08:28:49 +00:00
}
2020-10-01 15:45:57 +00:00
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
}
2020-09-30 08:28:49 +00:00
2020-10-01 15:45:57 +00:00
return
2020-09-30 08:28:49 +00:00
}
2020-10-01 15:45:57 +00:00
func UpdateDevice(db *sql.DB, device *device.Device) (err error) {
sqlStmt := fmt.Sprintf(`UPDATE device SET
name = ?
WHERE id = ?`)
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(device.Name, device.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 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
}
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
}