package cn.hmsoft.jdbc.generator;

import cn.hmsoft.exception.develop.MethodNotImplException;
import cn.hmsoft.jdbc.entity.DateFormatType;

/***********************
 * MySQL 自定义语句
 * 
 * @author zxq
 * @create 2019-05-05 19:41:40
 * @version 2.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
public class MySqlSQLGenerator extends AbstractSQLGenerator {

	@Override
	public String[] getNextSequence(String sequenceName) {
		throw new MethodNotImplException(this.getClass(), "getNextSequence");
	}

	@Override
	public String list(String sql, int start, int limit) {
		return sql + " limit " + start + "," + limit;
	}

	@Override
	public String queryTables(String databaseName) {
		return "select UPPER(table_name) table_name,TABLE_COMMENT,table_rows row_count from INFORMATION_SCHEMA.tables where upper(table_schema)=upper('"
				+ databaseName + "') order by table_name";
	}

	@Override
	public String queryColumns(String databaseName, String tableName) {
		return "select column_name,COLUMN_COMMENT,COLUMN_DEFAULT,is_nullable nullable,ORDINAL_POSITION column_id,data_type column_type,CHARACTER_MAXIMUM_LENGTH column_length,column_type full_column_type from INFORMATION_SCHEMA.COLUMNS as data_reocrd where upper(table_schema)=upper('"
				+ databaseName + "') and upper(table_name)=upper(?) order by ORDINAL_POSITION";
	}

	@Override
	public String queryCurrentTime() {
		return "select now() ldt01";
	}

	@Override
	public String random(String sql, int rowCount) {
		return "select t.* from (" + sql + " order by RAND()) t limit " + rowCount;
	}

	@Override
	public String isNull(String columnName) {
		return "ifnull(" + columnName + ",?)";
	}

	@Override
	public String isNull(String columnName, String otherColumn) {
		return " ifnull(" + columnName + "," + otherColumn + ") ";
	}

	@Override
	public String concatValues(String columnName) {
		return " group_concat(" + columnName + ") ";
	}

	@Override
	public String random() {
		return " rand() ";
	}

	@Override
	public String formatDate(String columnName, DateFormatType format) {
		if(format == DateFormatType.LongDate)
			return " DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') ";
		else if(format == DateFormatType.ShortDate)
			return " DATE_FORMAT(" + columnName + ",'%Y-%m-%d') ";
		else if(format == DateFormatType.LongTime)
			return " DATE_FORMAT(" + columnName + ",'%H:%i:%s') ";
		else if(format == DateFormatType.HourMinute)
			return " DATE_FORMAT(" + columnName + ",'%H:%i') ";
		else if(format == DateFormatType.MinuteSecond)
			return " DATE_FORMAT(" + columnName + ",'%i:%s') ";
		return " DATE_FORMAT(" + columnName + ",'%Y-%m-%d %H:%i:%s') ";
	}

}
