schema: add Network.name

This commit is contained in:
delthas 2020-03-18 03:13:26 +01:00 committed by Simon Ser
parent e3d97bb164
commit 1c41576988
3 changed files with 20 additions and 8 deletions

26
db.go
View File

@ -23,6 +23,7 @@ type SASL struct {
type Network struct { type Network struct {
ID int64 ID int64
Name string
Addr string Addr string
Nick string Nick string
Username string Username string
@ -109,7 +110,7 @@ func (db *DB) ListNetworks(username string) ([]Network, error) {
db.lock.RLock() db.lock.RLock()
defer db.lock.RUnlock() defer db.lock.RUnlock()
rows, err := db.db.Query(`SELECT id, addr, nick, username, realname, pass, rows, err := db.db.Query(`SELECT id, name, addr, nick, username, realname, pass,
sasl_mechanism, sasl_plain_username, sasl_plain_password sasl_mechanism, sasl_plain_username, sasl_plain_password
FROM Network FROM Network
WHERE user = ?`, WHERE user = ?`,
@ -122,13 +123,14 @@ func (db *DB) ListNetworks(username string) ([]Network, error) {
var networks []Network var networks []Network
for rows.Next() { for rows.Next() {
var net Network var net Network
var username, realname, pass *string var name, username, realname, pass *string
var saslMechanism, saslPlainUsername, saslPlainPassword *string var saslMechanism, saslPlainUsername, saslPlainPassword *string
err := rows.Scan(&net.ID, &net.Addr, &net.Nick, &username, &realname, err := rows.Scan(&net.ID, &name, &net.Addr, &net.Nick, &username, &realname,
&pass, &saslMechanism, &saslPlainUsername, &saslPlainPassword) &pass, &saslMechanism, &saslPlainUsername, &saslPlainPassword)
if err != nil { if err != nil {
return nil, err return nil, err
} }
net.Name = fromStringPtr(name)
net.Username = fromStringPtr(username) net.Username = fromStringPtr(username)
net.Realname = fromStringPtr(realname) net.Realname = fromStringPtr(realname)
net.Pass = fromStringPtr(pass) net.Pass = fromStringPtr(pass)
@ -148,6 +150,7 @@ func (db *DB) StoreNetwork(username string, network *Network) error {
db.lock.Lock() db.lock.Lock()
defer db.lock.Unlock() defer db.lock.Unlock()
netName := toStringPtr(network.Name)
netUsername := toStringPtr(network.Username) netUsername := toStringPtr(network.Username)
realname := toStringPtr(network.Realname) realname := toStringPtr(network.Realname)
pass := toStringPtr(network.Pass) pass := toStringPtr(network.Pass)
@ -165,18 +168,18 @@ func (db *DB) StoreNetwork(username string, network *Network) error {
var err error var err error
if network.ID != 0 { if network.ID != 0 {
_, err = db.db.Exec(`UPDATE Network _, err = db.db.Exec(`UPDATE Network
SET addr = ?, nick = ?, username = ?, realname = ?, pass = ?, SET name = ?, addr = ?, nick = ?, username = ?, realname = ?, pass = ?,
sasl_mechanism = ?, sasl_plain_username = ?, sasl_plain_password = ? sasl_mechanism = ?, sasl_plain_username = ?, sasl_plain_password = ?
WHERE id = ?`, WHERE id = ?`,
network.Addr, network.Nick, netUsername, realname, pass, netName, network.Addr, network.Nick, netUsername, realname, pass,
saslMechanism, saslPlainUsername, saslPlainPassword, network.ID) saslMechanism, saslPlainUsername, saslPlainPassword, network.ID)
} else { } else {
var res sql.Result var res sql.Result
res, err = db.db.Exec(`INSERT INTO Network(user, addr, nick, username, res, err = db.db.Exec(`INSERT INTO Network(user, name, addr, nick, username,
realname, pass, sasl_mechanism, sasl_plain_username, realname, pass, sasl_mechanism, sasl_plain_username,
sasl_plain_password) sasl_plain_password)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
username, network.Addr, network.Nick, netUsername, realname, pass, username, netName, network.Addr, network.Nick, netUsername, realname, pass,
saslMechanism, saslPlainUsername, saslPlainPassword) saslMechanism, saslPlainUsername, saslPlainPassword)
if err != nil { if err != nil {
return err return err
@ -226,3 +229,10 @@ 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
}

View File

@ -5,6 +5,7 @@ CREATE TABLE User (
CREATE TABLE Network ( CREATE TABLE Network (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name VARCHAR(255),
user VARCHAR(255) NOT NULL, user VARCHAR(255) NOT NULL,
addr VARCHAR(255) NOT NULL, addr VARCHAR(255) NOT NULL,
nick VARCHAR(255) NOT NULL, nick VARCHAR(255) NOT NULL,

View File

@ -160,6 +160,7 @@ func (u *user) run() {
func (u *user) createNetwork(addr, nick string) (*network, error) { func (u *user) createNetwork(addr, nick string) (*network, error) {
network := newNetwork(u, &Network{ network := newNetwork(u, &Network{
Name: addr,
Addr: addr, Addr: addr,
Nick: nick, Nick: nick,
}) })