Skip to content

Commit b9790bb

Browse files
author
Hayim.Shaul@ibm.com
committed
simplify query build
Signed-off-by: Hayim.Shaul@ibm.com <hayimsha@fhe3.haifa.ibm.com>
1 parent 005f781 commit b9790bb

File tree

1 file changed

+13
-8
lines changed
  • platform/view/services/storage/driver/sql/common

1 file changed

+13
-8
lines changed

platform/view/services/storage/driver/sql/common/binding.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,24 @@ func (db *BindingStore) PutBindings(ctx context.Context, longTerm view.Identity,
111111
}
112112

113113
// Build single INSERT with multiple VALUES
114-
query := fmt.Sprintf(`INSERT INTO %s (ephemeral_hash, long_term_id) VALUES `, db.table)
114+
// prepare query placeholder and arguments
115+
placeholders := make([]string, len(ephemerals)+1)
116+
args := make([]any, 0, (len(ephemerals)+1)*2)
115117

116-
var args = []interface{}{}
117-
argsReferences := []string{"($1, $2)"}
118+
// first item it the longTerm itself
119+
i := 0
120+
placeholders[i] = fmt.Sprintf("($%d,$%d)", i*2+1, i*2+2)
118121
args = append(args, longTerm.UniqueID(), longTerm)
119-
for i, eph := range ephemerals {
122+
123+
// next we go through our ephemerals
124+
for _, eph := range ephemerals {
125+
i++
126+
placeholders[i] = fmt.Sprintf("($%d,$%d)", i*2+1, i*2+2)
120127
args = append(args, eph.UniqueID(), longTerm)
121-
oneArgRef := fmt.Sprintf("($%d, $%d)", i*2+3, i*2+4)
122-
argsReferences = append(argsReferences, oneArgRef)
123128
}
124129

125-
query += strings.Join(argsReferences, ", ")
126-
query += " ON CONFLICT DO NOTHING;"
130+
query := fmt.Sprintf(`INSERT INTO %s (ephemeral_hash, long_term_id) VALUES %s ON CONFLICT DO NOTHING;`,
131+
db.table, strings.Join(placeholders, ","))
127132

128133
logger.DebugfContext(ctx, "executing bulk insert: %s", query)
129134

0 commit comments

Comments
 (0)