package cn.hmsoft.jdbc.generator;

import cn.hmsoft.jdbc.entity.DateFormatType;

/**********************
 * sqllite数据库实现
 * 
 * @author zxq
 * @create 2019-05-05 20:17:53
 * @version 2.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
public class SqliteSQLGenerator extends AbstractSQLGenerator {

	@Override
	public String[] getNextSequence(String sequenceName) {
		// TODO Auto-generated method stub
		return null;
	}

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

	@Override
	public String queryTables(String databaseName) {
		return "SELECT name table_name FROM sqlite_master WHERE  type='table' ORDER BY name";
	}

	@Override
	public String queryColumns(String databaseName, String tableName) {
		return "PRAGMA table_info('" + tableName + "')";
	}

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

	@Override
	public String random(String sql, int rowCount) {
		return sql + " ORDER BY RANDOM() 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) {
		// TODO Auto-generated method stub
		return null;
	}

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

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