Skip to content

Comments

Add Len() method to *pgx.Batch#682

Merged
jackc merged 1 commit intojackc:masterfrom
WGH-:batch-len
Feb 22, 2020
Merged

Add Len() method to *pgx.Batch#682
jackc merged 1 commit intojackc:masterfrom
WGH-:batch-len

Conversation

@WGH-
Copy link
Contributor

@WGH- WGH- commented Feb 21, 2020

This makes the API slightly easier to use when number of calls to
Queue() cannot be trivially computed.

For example, if the program contains the loop like the following,
a separate variable counting the iterations is needed:

numHeaders := 0
for _, header := range prepareHeadersForInsert(*res.Headers) {
    headerBatch.Queue("INSERT ...", ...)
    numHeaders++
}

headerBatchResult := tx.SendBatch(ctx, headerBatch)

for i := 0; i < numHeaders; i++ {
    _, err := headerBatchResult.Exec()
    // ...
}

With method Len(), this extra variable can be eliminated.

This makes the API slightly easier to use when number of calls to
Queue() cannot be trivially computed.

For example, if the program contains the loop like the following,
a separate variable counting the iterations is needed:

    numHeaders := 0
    for _, header := range prepareHeadersForInsert(*res.Headers) {
        headerBatch.Queue("INSERT ...", ...)
        numHeaders++
    }

    headerBatchResult := tx.SendBatch(ctx, headerBatch)

    for i := 0; i < numHeaders; i++ {
        _, err := headerBatchResult.Exec()
        // ...
    }

With method Len(), this extra variable can be eliminated.
@jackc jackc merged commit 710ddf7 into jackc:master Feb 22, 2020
@jackc
Copy link
Owner

jackc commented Feb 22, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants