public NativePacketPayload buildComQuery()

in src/main/protocol-impl/java/com/mysql/cj/protocol/a/NativeMessageBuilder.java [111:205]


    public NativePacketPayload buildComQuery(NativePacketPayload sharedPacket, Session sess, String query, Query callingQuery, String characterEncoding) {
        String statementComment = sess.getQueryComment();
        byte[] commentAsBytes = StringUtils.getBytes(statementComment, characterEncoding);

        QueryAttributesBindings queryAttributesBindings = null;
        if (!this.supportsQueryAttributes && callingQuery != null && callingQuery.getQueryAttributesBindings().getCount() > 0) {
            sess.getLog().logWarn(Messages.getString("QueryAttributes.SetButNotSupported"));
        }
        if (this.supportsQueryAttributes && callingQuery != null) {
            queryAttributesBindings = callingQuery.getQueryAttributesBindings();
        } else {
            queryAttributesBindings = new NativeQueryAttributesBindings(sess); // Required for telemetry context propagation.
        }

        boolean contextPropagationAttributeWasInjected = false;
        final NativePacketPayload sendPacket;
        if (sharedPacket != null) {
            sendPacket = sharedPacket;
        } else {
            // Compute packet length. It's not possible to know exactly how many bytes will be obtained from the query, but UTF-8 max encoding length is 4, so
            // pad it (4 * query) + space for headers
            int packLength = 1 /* COM_QUERY */ + query.length() * 4 + 2;

            if (commentAsBytes.length > 0) {
                packLength += commentAsBytes.length;
                packLength += 6; // for "/*[space]" + "[space]*/"
            }

            if (this.supportsQueryAttributes) {
                if (!queryAttributesBindings.containsAttribute(sess.getTelemetryHandler().getContextPropagationKey())) {
                    sess.getTelemetryHandler().propagateContext(queryAttributesBindings::setAttribute);
                    contextPropagationAttributeWasInjected = true;
                }
                if (queryAttributesBindings.getCount() > 0) {
                    packLength += 9 /* parameter_count */ + 1 /* parameter_set_count */;
                    packLength += (queryAttributesBindings.getCount() + 7) / 8 /* null_bitmap */ + 1 /* new_params_bind_flag */;
                    for (int i = 0; i < queryAttributesBindings.getCount(); i++) {
                        BindValue queryAttribute = queryAttributesBindings.getAttributeValue(i);
                        packLength += 2 /* parameter_type */ + queryAttribute.getName().length() /* parameter_name */ + queryAttribute.getBinaryLength();
                    }
                } else {
                    packLength += 1 /* parameter_count */ + 1 /* parameter_set_count */;
                }
            }

            sendPacket = new NativePacketPayload(packLength);
        }

        sendPacket.setPosition(0);
        sendPacket.writeInteger(IntegerDataType.INT1, NativeConstants.COM_QUERY);

        if (this.supportsQueryAttributes) {
            if (queryAttributesBindings != null && queryAttributesBindings.getCount() > 0) {
                sendPacket.writeInteger(IntegerDataType.INT_LENENC, queryAttributesBindings.getCount());
                sendPacket.writeInteger(IntegerDataType.INT_LENENC, 1); // parameter_set_count (always 1)
                byte[] nullBitsBuffer = new byte[(queryAttributesBindings.getCount() + 7) / 8];
                for (int i = 0; i < queryAttributesBindings.getCount(); i++) {
                    if (queryAttributesBindings.getAttributeValue(i).isNull()) {
                        nullBitsBuffer[i >>> 3] |= 1 << (i & 7);
                    }
                }
                sendPacket.writeBytes(StringLengthDataType.STRING_VAR, nullBitsBuffer);
                sendPacket.writeInteger(IntegerDataType.INT1, 1); // new_params_bind_flag (always 1)
                queryAttributesBindings.runThroughAll(a -> {
                    sendPacket.writeInteger(IntegerDataType.INT2, a.getFieldType());
                    sendPacket.writeBytes(StringSelfDataType.STRING_LENENC, a.getName().getBytes());
                });
                queryAttributesBindings.runThroughAll(a -> {
                    if (!a.isNull()) {
                        a.writeAsQueryAttribute(sendPacket);
                    }
                });
            } else {
                sendPacket.writeInteger(IntegerDataType.INT_LENENC, 0);
                sendPacket.writeInteger(IntegerDataType.INT_LENENC, 1); // parameter_set_count (always 1)
            }
            if (contextPropagationAttributeWasInjected) {
                queryAttributesBindings.removeAttribute(sess.getTelemetryHandler().getContextPropagationKey());
            }
        }
        sendPacket.setTag("QUERY");

        if (commentAsBytes.length > 0) {
            sendPacket.writeBytes(StringLengthDataType.STRING_FIXED, Constants.SLASH_STAR_SPACE_AS_BYTES);
            sendPacket.writeBytes(StringLengthDataType.STRING_FIXED, commentAsBytes);
            sendPacket.writeBytes(StringLengthDataType.STRING_FIXED, Constants.SPACE_STAR_SLASH_SPACE_AS_BYTES);
        }

        if (!sess.getServerSession().getCharsetSettings().doesPlatformDbCharsetMatches() && StringUtils.startsWithIgnoreCaseAndWs(query, "LOAD DATA")) {
            sendPacket.writeBytes(StringLengthDataType.STRING_FIXED, StringUtils.getBytes(query));
        } else {
            sendPacket.writeBytes(StringLengthDataType.STRING_FIXED, StringUtils.getBytes(query, characterEncoding));
        }
        return sendPacket;
    }