Fix SQL error logged on JOIN

Closes: https://todo.sr.ht/~emersion/soju/40
This commit is contained in:
Simon Ser 2020-04-01 17:34:22 +02:00
parent bca0b2ad76
commit 7c10535bfd
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 30 additions and 6 deletions

19
db.go
View File

@ -46,6 +46,8 @@ type Channel struct {
Key string Key string
} }
var ErrNoSuchChannel = fmt.Errorf("soju: no such channel")
type DB struct { type DB struct {
lock sync.RWMutex lock sync.RWMutex
db *sql.DB db *sql.DB
@ -265,6 +267,23 @@ func (db *DB) ListChannels(networkID int64) ([]Channel, error) {
return channels, nil return channels, nil
} }
func (db *DB) GetChannel(networkID int64, name string) (*Channel, error) {
db.lock.RLock()
defer db.lock.RUnlock()
ch := &Channel{Name: name}
var key *string
row := db.db.QueryRow("SELECT id, key FROM Channel WHERE network = ? AND name = ?", networkID, name)
if err := row.Scan(&ch.ID, &key); err == sql.ErrNoRows {
return nil, ErrNoSuchChannel
} else if err != nil {
return nil, err
}
ch.Key = fromStringPtr(key)
return ch, nil
}
func (db *DB) StoreChannel(networkID int64, ch *Channel) error { func (db *DB) StoreChannel(networkID int64, ch *Channel) error {
db.lock.Lock() db.lock.Lock()
defer db.lock.Unlock() defer db.lock.Unlock()

View File

@ -896,12 +896,17 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
Params: params, Params: params,
}) })
err = dc.srv.db.StoreChannel(uc.network.ID, &Channel{ ch, err := dc.srv.db.GetChannel(uc.network.ID, upstreamName)
Name: upstreamName, if err == ErrNoSuchChannel {
Key: key, ch = &Channel{Name: upstreamName}
}) } else if err != nil {
if err != nil { return err
dc.logger.Printf("failed to create channel %q in DB: %v", upstreamName, err) }
ch.Key = key
if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
return err
} }
} }
case "PART": case "PART":