Switch to sql.NullString

Not really better than what we had before, however new contributors will
maybe be familiar with it.
This commit is contained in:
Simon Ser 2020-10-24 15:47:53 +02:00
parent fa16337d97
commit af1e578936
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

71
db.go
View File

@ -215,18 +215,11 @@ func (db *DB) upgrade() error {
return tx.Commit() return tx.Commit()
} }
func fromStringPtr(ptr *string) string { func toNullString(s string) sql.NullString {
if ptr == nil { return sql.NullString{
return "" String: s,
Valid: s != "",
} }
return *ptr
}
func toStringPtr(s string) *string {
if s == "" {
return nil
}
return &s
} }
func (db *DB) ListUsers() ([]User, error) { func (db *DB) ListUsers() ([]User, error) {
@ -242,11 +235,11 @@ func (db *DB) ListUsers() ([]User, error) {
var users []User var users []User
for rows.Next() { for rows.Next() {
var user User var user User
var password *string var password sql.NullString
if err := rows.Scan(&user.ID, &user.Username, &password, &user.Admin); err != nil { if err := rows.Scan(&user.ID, &user.Username, &password, &user.Admin); err != nil {
return nil, err return nil, err
} }
user.Password = fromStringPtr(password) user.Password = password.String
users = append(users, user) users = append(users, user)
} }
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {
@ -262,12 +255,12 @@ func (db *DB) GetUser(username string) (*User, error) {
user := &User{Username: username} user := &User{Username: username}
var password *string var password sql.NullString
row := db.db.QueryRow("SELECT id, password, admin FROM User WHERE username = ?", username) row := db.db.QueryRow("SELECT id, password, admin FROM User WHERE username = ?", username)
if err := row.Scan(&user.ID, &password, &user.Admin); err != nil { if err := row.Scan(&user.ID, &password, &user.Admin); err != nil {
return nil, err return nil, err
} }
user.Password = fromStringPtr(password) user.Password = password.String
return user, nil return user, nil
} }
@ -275,7 +268,7 @@ func (db *DB) StoreUser(user *User) error {
db.lock.Lock() db.lock.Lock()
defer db.lock.Unlock() defer db.lock.Unlock()
password := toStringPtr(user.Password) password := toNullString(user.Password)
var err error var err error
if user.ID != 0 { if user.ID != 0 {
@ -346,24 +339,24 @@ func (db *DB) ListNetworks(userID int64) ([]Network, error) {
var networks []Network var networks []Network
for rows.Next() { for rows.Next() {
var net Network var net Network
var name, username, realname, pass, connectCommands *string var name, username, realname, pass, connectCommands sql.NullString
var saslMechanism, saslPlainUsername, saslPlainPassword *string var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString
err := rows.Scan(&net.ID, &name, &net.Addr, &net.Nick, &username, &realname, err := rows.Scan(&net.ID, &name, &net.Addr, &net.Nick, &username, &realname,
&pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword, &pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword,
&net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob) &net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob)
if err != nil { if err != nil {
return nil, err return nil, err
} }
net.Name = fromStringPtr(name) net.Name = name.String
net.Username = fromStringPtr(username) net.Username = username.String
net.Realname = fromStringPtr(realname) net.Realname = realname.String
net.Pass = fromStringPtr(pass) net.Pass = pass.String
if connectCommands != nil { if connectCommands.Valid {
net.ConnectCommands = strings.Split(*connectCommands, "\r\n") net.ConnectCommands = strings.Split(connectCommands.String, "\r\n")
} }
net.SASL.Mechanism = fromStringPtr(saslMechanism) net.SASL.Mechanism = saslMechanism.String
net.SASL.Plain.Username = fromStringPtr(saslPlainUsername) net.SASL.Plain.Username = saslPlainUsername.String
net.SASL.Plain.Password = fromStringPtr(saslPlainPassword) net.SASL.Plain.Password = saslPlainPassword.String
networks = append(networks, net) networks = append(networks, net)
} }
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {
@ -377,19 +370,19 @@ func (db *DB) StoreNetwork(userID int64, network *Network) error {
db.lock.Lock() db.lock.Lock()
defer db.lock.Unlock() defer db.lock.Unlock()
netName := toStringPtr(network.Name) netName := toNullString(network.Name)
netUsername := toStringPtr(network.Username) netUsername := toNullString(network.Username)
realname := toStringPtr(network.Realname) realname := toNullString(network.Realname)
pass := toStringPtr(network.Pass) pass := toNullString(network.Pass)
connectCommands := toStringPtr(strings.Join(network.ConnectCommands, "\r\n")) connectCommands := toNullString(strings.Join(network.ConnectCommands, "\r\n"))
var saslMechanism, saslPlainUsername, saslPlainPassword *string var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString
if network.SASL.Mechanism != "" { if network.SASL.Mechanism != "" {
saslMechanism = &network.SASL.Mechanism saslMechanism = toNullString(network.SASL.Mechanism)
switch network.SASL.Mechanism { switch network.SASL.Mechanism {
case "PLAIN": case "PLAIN":
saslPlainUsername = toStringPtr(network.SASL.Plain.Username) saslPlainUsername = toNullString(network.SASL.Plain.Username)
saslPlainPassword = toStringPtr(network.SASL.Plain.Password) saslPlainPassword = toNullString(network.SASL.Plain.Password)
network.SASL.External.CertBlob = nil network.SASL.External.CertBlob = nil
network.SASL.External.PrivKeyBlob = nil network.SASL.External.PrivKeyBlob = nil
case "EXTERNAL": case "EXTERNAL":
@ -465,11 +458,11 @@ func (db *DB) ListChannels(networkID int64) ([]Channel, error) {
var channels []Channel var channels []Channel
for rows.Next() { for rows.Next() {
var ch Channel var ch Channel
var key *string var key sql.NullString
if err := rows.Scan(&ch.ID, &ch.Name, &key, &ch.Detached); err != nil { if err := rows.Scan(&ch.ID, &ch.Name, &key, &ch.Detached); err != nil {
return nil, err return nil, err
} }
ch.Key = fromStringPtr(key) ch.Key = key.String
channels = append(channels, ch) channels = append(channels, ch)
} }
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {
@ -483,7 +476,7 @@ func (db *DB) StoreChannel(networkID int64, ch *Channel) error {
db.lock.Lock() db.lock.Lock()
defer db.lock.Unlock() defer db.lock.Unlock()
key := toStringPtr(ch.Key) key := toNullString(ch.Key)
var err error var err error
if ch.ID != 0 { if ch.ID != 0 {