MCPcopy Create free account
hub / github.com/Effect-TS/effect / executeBatch

Function executeBatch

packages/effect/src/sql/Statement.ts:1699–1741  ·  view source on GitHub ↗
(
  statements: ReadonlyArray<Statement<unknown>>,
  acquirer: Acquirer,
  borrower: Borrower | undefined,
  spanAttributes: ReadonlyArray<readonly [string, unknown]>
)

Source from the content-addressed store, hash-verified

1697 * @internal
1698 */
1699export const executeBatch = (
1700 statements: ReadonlyArray<Statement<unknown>>,
1701 acquirer: Acquirer,
1702 borrower: Borrower | undefined,
1703 spanAttributes: ReadonlyArray<readonly [string, unknown]>
1704): Effect.Effect<any, SqlError> => {
1705 if (statements.length === 0) return Effect.succeed([])
1706 // OpenTelemetry names multi-statement batches `BATCH <target>`.
1707 const isBatch = statements.length > 1
1708 const target = spanTarget(spanAttributes)
1709 const name = !isBatch ? spanName(spanAttributes) : target === undefined ? "BATCH" : `BATCH ${target}`
1710 return Effect.useSpan(name, { kind: "client" }, (span) =>
1711 Effect.withFiber((fiber) => {
1712 const transform = fiber.getRef(CurrentTransformer)
1713 const impls = statements as ReadonlyArray<StatementImpl<unknown>>
1714 const resolved = transform === undefined
1715 ? Effect.succeed(impls)
1716 : Effect.forEach(impls, (statement) => applyTransformer(statement, transform, fiber, span))
1717 return Effect.flatMap(resolved, (statements) => {
1718 const compiled = statements.map((statement) => statement.compile())
1719 for (const [key, value] of spanAttributes) {
1720 span.attribute(key, value)
1721 }
1722 span.attribute(ATTR_SQL_METHOD, "executeBatch")
1723 if (isBatch) {
1724 span.attribute(ATTR_DB_OPERATION_NAME, "BATCH")
1725 span.attribute(ATTR_DB_OPERATION_BATCH_SIZE, statements.length)
1726 }
1727 span.attribute(ATTR_DB_QUERY_TEXT, compiled.map(([sql]) => sql).join("; "))
1728 return runOnConnection(fiber, span, acquirer, borrower, (connection) =>
1729 connection.executeBatch === undefined
1730 ? Effect.forEach(
1731 compiled,
1732 ([sql, params], index) => connection.execute(sql, params, statements[index].transformRows)
1733 )
1734 : Effect.map(connection.executeBatch(compiled), (results) =>
1735 results.map((rows, index) => {
1736 const transformRows = statements[index].transformRows
1737 return transformRows === undefined ? rows : transformRows(rows)
1738 })))
1739 })
1740 }))
1741}
1742
1743const isSegment = (u: unknown): u is Segment => hasProperty(u, SegmentTypeId)
1744

Callers

nothing calls this directly

Calls 13

spanTargetFunction · 0.85
applyTransformerFunction · 0.85
runOnConnectionFunction · 0.85
getRefMethod · 0.80
compileMethod · 0.80
spanNameFunction · 0.70
forEachMethod · 0.65
attributeMethod · 0.65
succeedMethod · 0.45
mapMethod · 0.45
joinMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected