// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (c) 2012-2014 Monty Program Ab
// Copyright (c) 2015-2021 MariaDB Corporation Ab

package org.mariadb.jdbc.message.client;

import java.io.IOException;
import org.mariadb.jdbc.client.Context;
import org.mariadb.jdbc.client.socket.Writer;

public final class QueryPacket implements RedoableClientMessage {

  private final String sql;

  public QueryPacket(String sql) {
    this.sql = sql;
  }

  public int batchUpdateLength() {
    return 1;
  }

  @Override
  public int encode(Writer writer, Context context) throws IOException {
    writer.initPacket();
    writer.writeByte(0x03);
    writer.writeString(this.sql);
    writer.flush();
    return 1;
  }

  public boolean isCommit() {
    return "COMMIT".equalsIgnoreCase(sql);
  }

  public String description() {
    return sql;
  }
}
