Correctly set Channel.ID in DB.StoreChannel

This commit is contained in:
Simon Ser 2020-03-25 14:23:41 +01:00
parent 02c3bdde20
commit 04dd9d51f3
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

31
db.go
View File

@ -33,6 +33,13 @@ type Network struct {
SASL SASL SASL SASL
} }
func (net *Network) GetName() string {
if net.Name != "" {
return net.Name
}
return net.Addr
}
type Channel struct { type Channel struct {
ID int64 ID int64
Name string Name string
@ -225,8 +232,21 @@ func (db *DB) StoreChannel(networkID int64, ch *Channel) error {
defer db.lock.Unlock() defer db.lock.Unlock()
key := toStringPtr(ch.Key) key := toStringPtr(ch.Key)
_, err := db.db.Exec(`INSERT OR REPLACE INTO Channel(network, name, key)
VALUES (?, ?, ?)`, networkID, ch.Name, key) var err error
if ch.ID != 0 {
_, err = db.db.Exec(`UPDATE Channel
SET network = ?, name = ?, key = ?
WHERE id = ?`, networkID, ch.Name, key, ch.ID)
} else {
var res sql.Result
res, err = db.db.Exec(`INSERT INTO Channel(network, name, key)
VALUES (?, ?, ?)`, networkID, ch.Name, key)
if err != nil {
return err
}
ch.ID, err = res.LastInsertId()
}
return err return err
} }
@ -237,10 +257,3 @@ func (db *DB) DeleteChannel(networkID int64, name string) error {
_, err := db.db.Exec("DELETE FROM Channel WHERE network = ? AND name = ?", networkID, name) _, err := db.db.Exec("DELETE FROM Channel WHERE network = ? AND name = ?", networkID, name)
return err return err
} }
func (net *Network) GetName() string {
if net.Name != "" {
return net.Name
}
return net.Addr
}