Skip to content

Commit

Permalink
directly insert arrays when snapshotting positions instead of concatt…
Browse files Browse the repository at this point in the history
…ing strings
  • Loading branch information
eli-d committed Sep 3, 2024
1 parent 5c00da7 commit fa12586
Showing 1 changed file with 2 additions and 30 deletions.
32 changes: 2 additions & 30 deletions cmd/snapshot.ethereum/database.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,10 @@
package main

import (
"fmt"
"log/slog"
"strconv"
"strings"

"gorm.io/gorm"
)

// storePositions in the database, by formatting a string to use to make
// the insertion. Thankfully we're protected by the datatype for this.
func storePositions(db *gorm.DB, pools []string, ids []int, amount0s, amount1s []string) error {
// Gorm lacks the support for inserting arrays (we think) so this
// is something we need to do. Ugly I know. Thankfully this isn't used super often.
idsS := make([]string, len(ids))
for i, id := range ids {
idsS[i] = strconv.Itoa(id)
}
var bu strings.Builder
for i, addr := range pools {
fmt.Fprintf(&bu, `'%s'`, addr)
if i != len(pools)-1 {
bu.WriteRune(',')
}
}
s := fmt.Sprintf(
"SELECT snapshot_create_positions_1(ARRAY[%s], ARRAY[%s], ARRAY[%s], ARRAY[%s])",
bu.String(), // pools
strings.Join(idsS, ","), // ids
strings.Join(amount0s, ","), // amount0s
strings.Join(amount1s, ","), // amount1s
)
slog.Debug("about to execute sql", "sql", s)
err := db.Exec(s).Error
return err
query := `SELECT snapshot_create_positions_1($1, $2, $3, $4)`
return db.Exec(query, pools, ids, amount0s, amount1s).Error
}

0 comments on commit fa12586

Please sign in to comment.