package com.purui.prims.headless.api.pojo.request;


import com.purui.prims.headless.api.pojo.SqlVariable;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Data
public class SqlExecuteReq {
    public static final String LIMIT_WRAPPER = " SELECT * FROM ( %s ) a LIMIT %d ";

    @ApiModelProperty(value = "数据源ID")
    @NotNull(message = "DatabaseId can not be null")
    private Long id;

    @ApiModelProperty(value = "SQL语句")
    @NotBlank(message = "sql can not be blank")
    private String sql;

    @ApiModelProperty(value = "SQL变量")
    private List<SqlVariable> sqlVariables;

    @ApiModelProperty(value = "最大记录数")
    private Integer limit = 1000;

    public String getSql() {
        if (StringUtils.isNotBlank(sql) && sql.endsWith(";")) {
            sql = sql.substring(0, sql.length() - 1);
        }
        return String.format(LIMIT_WRAPPER, sql, limit);
    }

}
