-
-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract out
db.QuestionMarks
function (#6568)
We use this pattern in several places: there is a query that needs to have a variable number of placeholders (question marks) in it, depending on how many items we are inserting or querying for. For instance, when issuing a precertificate we add that precertificate's names to the "issuedNames" table. To make things more efficient, we do that in a single query, whether there is one name on the certificate or a hundred. That means interpolating into the query string with series of question marks that matches the number of names. We have a helper type MultiInserter that solves this problem for simple inserts, but it does not solve the problem for selects or more complex inserts, and we still have a number of places that generate their sequence of question marks manually. This change updates addIssuedNames to use MultiInserter. To enable that, it also narrows the interface required by MultiInserter.Insert, so it's easier to mock in tests. This change adds the new function db.QuestionMarks, which generates e.g. `?,?,?` depending on the input N. In a few places I had to rename a function parameter named `db` to avoid shadowing the `db` package.
- Loading branch information
Showing
8 changed files
with
111 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package db | ||
|
||
import "strings" | ||
|
||
// QuestionMarks returns a string consisting of N question marks, joined by | ||
// commas. If n is <= 0, panics. | ||
func QuestionMarks(n int) string { | ||
if n <= 0 { | ||
panic("db.QuestionMarks called with n <=0") | ||
} | ||
var qmarks strings.Builder | ||
qmarks.Grow(2 * n) | ||
for i := 0; i < n; i++ { | ||
if i == 0 { | ||
qmarks.WriteString("?") | ||
} else { | ||
qmarks.WriteString(",?") | ||
} | ||
} | ||
return qmarks.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package db | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/letsencrypt/boulder/test" | ||
) | ||
|
||
func TestQuestionMarks(t *testing.T) { | ||
test.AssertEquals(t, QuestionMarks(1), "?") | ||
test.AssertEquals(t, QuestionMarks(2), "?,?") | ||
test.AssertEquals(t, QuestionMarks(3), "?,?,?") | ||
} | ||
|
||
func TestQuestionMarksPanic(t *testing.T) { | ||
defer func() { recover() }() | ||
QuestionMarks(0) | ||
t.Errorf("calling QuestionMarks(0) did not panic as expected") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters