본문 바로가기

카테고리 없음

[jdbc] mariadb - AbstractQueryProtocol

쿼리를 보내는 프로토콜에 대한 구현 부분

 

executeQuery 의 부분은 거의 항상 수행되었다.

package org.mariadb.jdbc.internal.protocol;

public class AbstractQueryProtocol extends AbstractConnectProtocol implements Protocol {

  // L.219
  public void executeQuery(
      boolean mustExecuteOnMaster,
      Results results,
      final ClientPrepareResult clientPrepareResult,
      ParameterHolder[] parameters)
      throws SQLException {
    cmdPrologue();
    try {

      if (clientPrepareResult.getParamCount() == 0
          && !clientPrepareResult.isQueryMultiValuesRewritable()) {
        if (clientPrepareResult.getQueryParts().size() == 1) {
          ComQuery.sendDirect(writer, clientPrepareResult.getQueryParts().get(0));
        } else {
          ComQuery.sendMultiDirect(writer, clientPrepareResult.getQueryParts());
        }
      } else {
        writer.startPacket(0);
        ComQuery.sendSubCmd(writer, clientPrepareResult, parameters, -1);
        writer.flush();
      }
      getResult(results);

    } catch (SQLException queryException) {
      throw logQuery.exceptionWithQuery(parameters, queryException, clientPrepareResult);
    } catch (IOException e) {
      throw handleIoException(e);
    }
  }

내부적으로는 ComQuery라는 정적 메서드가 호출된다.

  • sendDirect
  • sendMultiDirect
  • sendSubCmd
package org.mariadb.jdbc.internal.com.send;

public class ComQuery {

  public static void sendDirect(final PacketOutputStream pos, byte[] sqlBytes) throws IOException {
    pos.startPacket(0);
    pos.write(Packet.COM_QUERY);
    pos.write(sqlBytes);
    pos.flush();
  }
  
  public static void sendMultiDirect(final PacketOutputStream pos, List<byte[]> sqlBytes)
      throws IOException {
    pos.startPacket(0);
    pos.write(Packet.COM_QUERY);
    for (byte[] bytes : sqlBytes) {
      pos.write(bytes);
    }
    pos.flush();
  }
  
  public static void sendSubCmd(
      final PacketOutputStream out,
      final ClientPrepareResult clientPrepareResult,
      ParameterHolder[] parameters,
      int queryTimeout)
      throws IOException {
    out.write(Packet.COM_QUERY);
    if (queryTimeout > 0) {
      out.write(("SET STATEMENT max_statement_time=" + queryTimeout + " FOR ").getBytes());
    }
    if (clientPrepareResult.isRewriteType()) {

      out.write(clientPrepareResult.getQueryParts().get(0));
      out.write(clientPrepareResult.getQueryParts().get(1));
      for (int i = 0; i < clientPrepareResult.getParamCount(); i++) {
        parameters[i].writeTo(out);
        out.write(clientPrepareResult.getQueryParts().get(i + 2));
      }
      out.write(clientPrepareResult.getQueryParts().get(clientPrepareResult.getParamCount() + 2));

    } else {

      out.write(clientPrepareResult.getQueryParts().get(0));
      for (int i = 0; i < clientPrepareResult.getParamCount(); i++) {
        parameters[i].writeTo(out);
        out.write(clientPrepareResult.getQueryParts().get(i + 1));
      }
    }
  }