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

Function encodeQuery

packages/sql/pg/src/PgConnection.ts:1411–1466  ·  view source on GitHub ↗
(
  sql: string,
  params: ReadonlyArray<unknown>,
  registry: PgTypes.Registry | undefined,
  encodeBind: BindEncoder,
  cache: PreparedCache | undefined,
  textResults: boolean
)

Source from the content-addressed store, hash-verified

1409 * rather than paying a round trip of their own.
1410 */
1411const encodeQuery = (
1412 sql: string,
1413 params: ReadonlyArray<unknown>,
1414 registry: PgTypes.Registry | undefined,
1415 encodeBind: BindEncoder,
1416 cache: PreparedCache | undefined,
1417 textResults: boolean
1418): Plan => {
1419 const { parameterTypes, parameters } = inferParameters(params, registry)
1420
1421 if (cache === undefined) {
1422 return encodeUnnamed(sql, parameters, parameterTypes, encodeBind, textResults)
1423 }
1424
1425 const prepared = cache.get(sql, parameterTypes, textResults)
1426 if (!prepared.ready && prepared.parsing) {
1427 // Another cycle is already on the wire carrying this name's `Parse`.
1428 // Naming it again would collide, and reusing it would mean binding to
1429 // columns nobody has seen yet, so this execution goes unnamed.
1430 return encodeUnnamed(sql, parameters, parameterTypes, encodeBind, textResults)
1431 }
1432 const bind = encodeBind({ portal: "", statement: prepared.name, parameters, textResults })
1433 if (EffectResult.isFailure(bind)) throw bind.failure
1434 const parse = prepared.ready ? undefined : PgProtocol.encodeParse({ name: prepared.name, query: sql, parameterTypes })
1435 if (parse !== undefined && EffectResult.isFailure(parse)) throw parse.failure
1436 const closeFrames = cache.takeCloses()
1437
1438 if (parse === undefined) {
1439 return {
1440 frame: closeFrames === undefined
1441 ? concat([bind.success, executeSync])
1442 : concat([closeFrames.frames, bind.success, executeSync]),
1443 closes: closeFrames?.count ?? 0,
1444 parses: false,
1445 describes: false,
1446 prepared,
1447 description: prepared.description,
1448 stale: false,
1449 statements: 1
1450 }
1451 }
1452
1453 prepared.parsing = true
1454 return {
1455 frame: closeFrames === undefined
1456 ? concat([parse.success, bind.success, describeExecuteSync])
1457 : concat([closeFrames.frames, parse.success, bind.success, describeExecuteSync]),
1458 closes: closeFrames?.count ?? 0,
1459 parses: true,
1460 describes: true,
1461 prepared,
1462 description: undefined,
1463 stale: false,
1464 statements: 1
1465 }
1466}
1467
1468/**

Callers 3

PgConnectionImplClass · 0.85
onEndpointFunction · 0.85
endpointBuilderFunction · 0.85

Calls 6

inferParametersFunction · 0.85
encodeUnnamedFunction · 0.85
encodeBindFunction · 0.85
takeClosesMethod · 0.80
concatFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected