178 lines
3.1 KiB
Go
178 lines
3.1 KiB
Go
|
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
|
||
|
}
|