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

Function encodeBindUnsafe

packages/sql/pg/src/PgProtocol.ts:788–842  ·  view source on GitHub ↗
(options: Omit<Bind, "_tag">)

Source from the content-addressed store, hash-verified

786 * @since 4.0.0
787 */
788const encodeBindUnsafe = (options: Omit<Bind, "_tag">): Uint8Array => {
789 const writer = begin(0x42)
790 writer.cString(options.portal)
791 writer.cString(options.statement)
792 const parameters = options.parameters
793 const count = requireUint16Count(parameters.length, "Bind parameter")
794 // Sizing the rest of the frame up front turns every remaining write into a
795 // plain store: one bounds check for the message instead of one per field.
796 let size = 10 + count * 4
797 for (let index = 0; index < count; index++) {
798 const parameter = parameters[index]
799 if (parameter !== null) size += parameter.length
800 }
801 writer.reserve(size)
802 const bytes = writer.bytes
803 let offset = writer.offset
804 // One parameter format code, binary, for every parameter.
805 bytes[offset] = 0
806 bytes[offset + 1] = 1
807 bytes[offset + 2] = 0
808 bytes[offset + 3] = 1
809 bytes[offset + 4] = count >>> 8
810 bytes[offset + 5] = count
811 offset += 6
812 for (let index = 0; index < count; index++) {
813 const parameter = parameters[index]
814 if (parameter === null) {
815 bytes[offset] = 0xff
816 bytes[offset + 1] = 0xff
817 bytes[offset + 2] = 0xff
818 bytes[offset + 3] = 0xff
819 offset += 4
820 } else {
821 const length = parameter.length
822 bytes[offset] = length >>> 24
823 bytes[offset + 1] = length >>> 16
824 bytes[offset + 2] = length >>> 8
825 bytes[offset + 3] = length
826 offset += 4
827 if (length <= smallCopyLimit) {
828 for (let byte = 0; byte < length; byte++) bytes[offset + byte] = parameter[byte]
829 } else {
830 bytes.set(parameter, offset)
831 }
832 offset += length
833 }
834 }
835 // One result format code, binary, for every column.
836 bytes[offset] = 0
837 bytes[offset + 1] = 1
838 bytes[offset + 2] = 0
839 bytes[offset + 3] = 1
840 writer.offset = offset + 4
841 return end()
842}
843
844/**
845 * Encodes a `Bind` message.

Callers 1

encodeBindFunction · 0.85

Calls 6

requireUint16CountFunction · 0.85
beginFunction · 0.70
endFunction · 0.70
setMethod · 0.65
cStringMethod · 0.45
reserveMethod · 0.45

Tested by

no test coverage detected