package database import ( "database/sql" "fmt" "log" "nutfactory.org/Matrix/entities/device" ) 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 } 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.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 } 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() 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 }