add query context with timeline

Also change the row column types to sql.NullString to handle cases where
null values were causing errors in row scanning.
absences
William Perron 2 years ago
parent 4dcbbaa226
commit 5793278523

@ -12,6 +12,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
"time"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
@ -378,7 +379,9 @@ func main() {
} }
q := i.ApplicationCommandData().Options[0].StringValue() q := i.ApplicationCommandData().Options[0].StringValue()
rows, err := roDB.Query(q) deadlined, cancelDeadline := context.WithTimeout(ctx, 15*time.Second)
defer cancelDeadline()
rows, err := roDB.QueryContext(deadlined, q)
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to exec user-provided query") log.Error().Err(err).Msg("failed to exec user-provided query")
return return
@ -389,7 +392,10 @@ func main() {
log.Error().Err(err).Msg("failed to format rows") log.Error().Err(err).Msg("failed to format rows")
} }
table := fmt.Sprintf("```\n%s\n```", fmtd[:min(len(fmtd), 1990)]) // TODO(wperron) find a better way to cutover // 2000 is a magic number here, it's the character limit for a discord
// message, we're cutting slightly under that to allow the backticks
// for the monospaced block.
table := fmt.Sprintf("```\n%s\n```", fmtd[:min(len(fmtd), 1990)])
if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
@ -429,6 +435,7 @@ func main() {
}() }()
<-ctx.Done() <-ctx.Done()
log.Info().Msg("context cancelled, exiting")
for _, c := range registeredCommands { for _, c := range registeredCommands {
err = discord.ApplicationCommandDelete(appId, guildId, c.ID) err = discord.ApplicationCommandDelete(appId, guildId, c.ID)

@ -29,7 +29,7 @@ func FormatRows(rows *sql.Rows) (string, error) {
for rows.Next() { for rows.Next() {
row := make([]interface{}, len(cols)) row := make([]interface{}, len(cols))
for i := range row { for i := range row {
row[i] = new(string) row[i] = new(sql.NullString)
} }
if err := rows.Scan(row...); err != nil { if err := rows.Scan(row...); err != nil {
return "", fmt.Errorf("failed to scan next row: %w", err) return "", fmt.Errorf("failed to scan next row: %w", err)
@ -37,9 +37,9 @@ func FormatRows(rows *sql.Rows) (string, error) {
scanned = append(scanned, row) // keep track of row for later scanned = append(scanned, row) // keep track of row for later
for i, a := range row { for i, a := range row {
s := a.(*string) s := a.(*sql.NullString)
if len(*s) > lengths[i] { if len(s.String) > lengths[i] {
lengths[i] = len(*s) lengths[i] = len(s.String)
} }
} }
} }
@ -62,7 +62,7 @@ func FormatRows(rows *sql.Rows) (string, error) {
for _, r := range scanned { for _, r := range scanned {
curr = curr[:0] // empty slice but preserve capacity curr = curr[:0] // empty slice but preserve capacity
for i := range lengths { for i := range lengths {
s := r[i].(*string) s := r[i].(*sql.NullString)
curr = append(curr, lengths[i], *s) curr = append(curr, lengths[i], *s)
} }
sb.WriteString(fmt.Sprintf(pattern, curr...)) sb.WriteString(fmt.Sprintf(pattern, curr...))

Loading…
Cancel
Save