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

Function streamRows

packages/sql/pg/src/PgConnection.ts:2083–2296  ·  view source on GitHub ↗
(
  conn: PgConnectionImpl,
  pin: Effect.Effect<PinnedPgConnection, never, Scope.Scope>,
  sql: string,
  params: ReadonlyArray<unknown>
)

Source from the content-addressed store, hash-verified

2081}
2082
2083const streamRows = (
2084 conn: PgConnectionImpl,
2085 pin: Effect.Effect<PinnedPgConnection, never, Scope.Scope>,
2086 sql: string,
2087 params: ReadonlyArray<unknown>
2088): Stream.Stream<Row, SqlError> =>
2089 Stream.fromChannel(Channel.fromTransform(Effect.fnUntraced(function*(_, scope) {
2090 const pinned = yield* Scope.provide(pin, scope)
2091 yield* Scope.provide(conn.wire.takeScoped(1), scope)
2092 if (pinned.released) return yield* pinReleasedError()
2093 // Streams stay on the unnamed path: a stream pays its setup once over the
2094 // whole result, so naming the statement buys little and would need the
2095 // stale-plan retry to unwind rows already delivered.
2096 const plan = yield* Effect.try({
2097 try: () => conn.encodeQuery(sql, params, undefined),
2098 catch: (cause) => queryError(cause, "PgConnection: Failed to encode query")
2099 })
2100 const frame = plan.frame
2101
2102 const socket = conn.session.socket
2103 const parser = conn.session.parser
2104 let phase: QueryPhase = "parse"
2105 let fieldCount = 0
2106 let rowBuilder: RowBuilder | undefined
2107 let buffer: Array<Row> = []
2108 let failure: SqlError | undefined
2109 let finished = false
2110 let done = false
2111 let aborted = false
2112 let paused = false
2113 let pending:
2114 | ((effect: Effect.Effect<Arr.NonEmptyReadonlyArray<Row>, SqlError | Cause.Done>) => void)
2115 | undefined
2116 let drainDone: (() => void) | undefined
2117
2118 const setPaused = (value: boolean): void => {
2119 if (paused === value) return
2120 paused = value
2121 if (value) socket.pause()
2122 else socket.resume()
2123 }
2124
2125 const deliver = (): void => {
2126 if (pending === undefined) return
2127 const resume = pending
2128 if (buffer.length > 0) {
2129 const chunk = buffer as Arr.NonEmptyArray<Row>
2130 buffer = []
2131 pending = undefined
2132 resume(Effect.succeed(chunk))
2133 } else if (finished) {
2134 pending = undefined
2135 resume(failure !== undefined ? Effect.fail(failure) : Cause.done())
2136 }
2137 }
2138
2139 const onFatal = (error: SqlError): void => {
2140 if (done) return

Callers 2

PgConnectionImplClass · 0.85
PinnedPgConnectionClass · 0.85

Calls 12

pinReleasedErrorFunction · 0.85
queryErrorFunction · 0.85
drainAbortedFunction · 0.85
setPausedFunction · 0.85
failFatalFunction · 0.85
connectionQueryErrorFunction · 0.85
addFinalizerMethod · 0.80
deliverFunction · 0.70
takeScopedMethod · 0.65
writeMethod · 0.65
provideMethod · 0.45
syncMethod · 0.45

Tested by

no test coverage detected