db_sqlite: switch to sql.Named

This allows us to avoid mixing up arguments.
This commit is contained in:
Simon Ser 2021-09-27 16:54:06 +02:00
parent 6b9c99821a
commit fce86b0f0d

View File

@ -259,17 +259,19 @@ func (db *SqliteDB) StoreUser(user *User) error {
db.lock.Lock() db.lock.Lock()
defer db.lock.Unlock() defer db.lock.Unlock()
password := toNullString(user.Password) args := []interface{}{
realname := toNullString(user.Realname) sql.Named("username", user.Username),
sql.Named("password", toNullString(user.Password)),
sql.Named("admin", user.Admin),
sql.Named("realname", toNullString(user.Realname)),
}
var err error var err error
if user.ID != 0 { if user.ID != 0 {
_, err = db.db.Exec("UPDATE User SET password = ?, admin = ?, realname = ? WHERE username = ?", _, err = db.db.Exec("UPDATE User SET password = :password, admin = :admin, realname = :realname WHERE username = :username", args...)
password, user.Admin, realname, user.Username)
} else { } else {
var res sql.Result var res sql.Result
res, err = db.db.Exec("INSERT INTO User(username, password, admin, realname) VALUES (?, ?, ?, ?)", res, err = db.db.Exec("INSERT INTO User(username, password, admin, realname) VALUES (:username, :password, :admin, :realname)", args...)
user.Username, password, user.Admin, realname)
if err != nil { if err != nil {
return err return err
} }
@ -373,12 +375,6 @@ func (db *SqliteDB) StoreNetwork(userID int64, network *Network) error {
db.lock.Lock() db.lock.Lock()
defer db.lock.Unlock() defer db.lock.Unlock()
netName := toNullString(network.Name)
netUsername := toNullString(network.Username)
realname := toNullString(network.Realname)
pass := toNullString(network.Pass)
connectCommands := toNullString(strings.Join(network.ConnectCommands, "\r\n"))
var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString
if network.SASL.Mechanism != "" { if network.SASL.Mechanism != "" {
saslMechanism = toNullString(network.SASL.Mechanism) saslMechanism = toNullString(network.SASL.Mechanism)
@ -395,26 +391,45 @@ func (db *SqliteDB) StoreNetwork(userID int64, network *Network) error {
} }
} }
args := []interface{}{
sql.Named("name", toNullString(network.Name)),
sql.Named("addr", network.Addr),
sql.Named("nick", network.Nick),
sql.Named("username", toNullString(network.Username)),
sql.Named("realname", toNullString(network.Realname)),
sql.Named("pass", toNullString(network.Pass)),
sql.Named("connect_commands", toNullString(strings.Join(network.ConnectCommands, "\r\n"))),
sql.Named("sasl_mechanism", saslMechanism),
sql.Named("sasl_plain_username", saslPlainUsername),
sql.Named("sasl_plain_password", saslPlainPassword),
sql.Named("sasl_external_cert", network.SASL.External.CertBlob),
sql.Named("sasl_external_key", network.SASL.External.PrivKeyBlob),
sql.Named("enabled", network.Enabled),
sql.Named("id", network.ID), // only for UPDATE
sql.Named("user", userID), // only for INSERT
}
var err error var err error
if network.ID != 0 { if network.ID != 0 {
_, err = db.db.Exec(`UPDATE Network _, err = db.db.Exec(`
SET name = ?, addr = ?, nick = ?, username = ?, realname = ?, pass = ?, connect_commands = ?, UPDATE Network
sasl_mechanism = ?, sasl_plain_username = ?, sasl_plain_password = ?, SET name = :name, addr = :addr, nick = :nick, username = :username,
sasl_external_cert = ?, sasl_external_key = ?, enabled = ? realname = :realname, pass = :pass, connect_commands = :connect_commands,
WHERE id = ?`, sasl_mechanism = :sasl_mechanism, sasl_plain_username = :sasl_plain_username, sasl_plain_password = :sasl_plain_password,
netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands, sasl_external_cert = :sasl_external_cert, sasl_external_key = :sasl_external_key,
saslMechanism, saslPlainUsername, saslPlainPassword, enabled = :enabled
network.SASL.External.CertBlob, network.SASL.External.PrivKeyBlob, network.Enabled, WHERE id = :id`, args...)
network.ID)
} else { } else {
var res sql.Result var res sql.Result
res, err = db.db.Exec(`INSERT INTO Network(user, name, addr, nick, username, res, err = db.db.Exec(`
realname, pass, connect_commands, sasl_mechanism, sasl_plain_username, INSERT INTO Network(user, name, addr, nick, username, realname, pass,
connect_commands, sasl_mechanism, sasl_plain_username,
sasl_plain_password, sasl_external_cert, sasl_external_key, enabled) sasl_plain_password, sasl_external_cert, sasl_external_key, enabled)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, VALUES (:user, :name, :addr, :nick, :username, :realname, :pass,
userID, netName, network.Addr, network.Nick, netUsername, realname, pass, connectCommands, :connect_commands, :sasl_mechanism, :sasl_plain_username,
saslMechanism, saslPlainUsername, saslPlainPassword, network.SASL.External.CertBlob, :sasl_plain_password, :sasl_external_cert, :sasl_external_key, :enabled)`,
network.SASL.External.PrivKeyBlob, network.Enabled) args...)
if err != nil { if err != nil {
return err return err
} }
@ -489,20 +504,31 @@ func (db *SqliteDB) StoreChannel(networkID int64, ch *Channel) error {
db.lock.Lock() db.lock.Lock()
defer db.lock.Unlock() defer db.lock.Unlock()
key := toNullString(ch.Key) args := []interface{}{
detachAfter := int64(math.Ceil(ch.DetachAfter.Seconds())) sql.Named("network", networkID),
sql.Named("name", ch.Name),
sql.Named("key", toNullString(ch.Key)),
sql.Named("detached", ch.Detached),
sql.Named("detached_internal_msgid", toNullString(ch.DetachedInternalMsgID)),
sql.Named("relay_detached", ch.RelayDetached),
sql.Named("reattach_on", ch.ReattachOn),
sql.Named("detach_after", int64(math.Ceil(ch.DetachAfter.Seconds()))),
sql.Named("detach_on", ch.DetachOn),
sql.Named("id", ch.ID), // only for UPDATE
}
var err error var err error
if ch.ID != 0 { if ch.ID != 0 {
_, err = db.db.Exec(`UPDATE Channel _, err = db.db.Exec(`UPDATE Channel
SET network = ?, name = ?, key = ?, detached = ?, detached_internal_msgid = ?, relay_detached = ?, reattach_on = ?, detach_after = ?, detach_on = ? SET network = :network, name = :name, key = :key, detached = :detached,
WHERE id = ?`, detached_internal_msgid = :detached_internal_msgid, relay_detached = :relay_detached,
networkID, ch.Name, key, ch.Detached, toNullString(ch.DetachedInternalMsgID), ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn, ch.ID) reattach_on = :reattach_on, detach_after = :detach_after, detach_on = :detach_on
WHERE id = :id`, args...)
} else { } else {
var res sql.Result var res sql.Result
res, err = db.db.Exec(`INSERT INTO Channel(network, name, key, detached, detached_internal_msgid, relay_detached, reattach_on, detach_after, detach_on) res, err = db.db.Exec(`INSERT INTO Channel(network, name, key, detached, detached_internal_msgid, relay_detached, reattach_on, detach_after, detach_on)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, VALUES (:network, :name, :key, :detached, :detached_internal_msgid, :relay_detached, :reattach_on, :detach_after, :detach_on)`, args...)
networkID, ch.Name, key, ch.Detached, toNullString(ch.DetachedInternalMsgID), ch.RelayDetached, ch.ReattachOn, detachAfter, ch.DetachOn)
if err != nil { if err != nil {
return err return err
} }
@ -567,8 +593,12 @@ func (db *SqliteDB) StoreClientDeliveryReceipts(networkID int64, client string,
for i := range receipts { for i := range receipts {
rcpt := &receipts[i] rcpt := &receipts[i]
res, err := tx.Exec("INSERT INTO DeliveryReceipt(network, target, client, internal_msgid) VALUES (?, ?, ?, ?)", res, err := tx.Exec(`INSERT INTO DeliveryReceipt(network, target, client, internal_msgid)
networkID, rcpt.Target, toNullString(client), rcpt.InternalMsgID) VALUES (:network, :target, :client, :internal_msgid)`,
sql.Named("network", networkID),
sql.Named("target", rcpt.Target),
sql.Named("client", toNullString(client)),
sql.Named("internal_msgid", rcpt.InternalMsgID))
if err != nil { if err != nil {
return err return err
} }