/**
 * 
 */
package org.springblade.etl.enums;

import java.util.regex.Pattern;

import com.baomidou.mybatisplus.annotation.EnumValue;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @author wookvn
 *
 */
@Getter
@AllArgsConstructor
public enum SqlScriptType {

	INSERT(0, "DML:插入数据"),
	
	SELECT(1, "DML:查询数据"),
	
	CREATE(2, "DDL:创建表"),
	
	UPDATE(3, "DML:更新数据"),
	
	DELETE(4, "DML:删除数据");
	
	@EnumValue
	private final Integer value;
	private final String label;
	
	//该SQL关键字，需要跟Enum类对照。Enum支持的，这里面要去掉
	final static String SQL_DML_DDL = "drop|alter|truncate|declare|exec|master|execute";
	final static Pattern SQL_INJECT_PATTERN = Pattern.compile("(/\\*(?:.|[\\n\\r])*?\\*/)|(\\b("+SQL_DML_DDL+")\\b)", Pattern.CASE_INSENSITIVE);
	/**
	 * 校验是否Sql注入
	 * @param sql
	 * @return
	 */
	public static boolean checkSqlInject(String sql) {
		return SQL_INJECT_PATTERN.matcher(sql).find();
	}
}
