package foundation.persist.adapter;
|
|
import java.text.DateFormat;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
|
import foundation.server.config.DBaseType;
|
import foundation.util.Util;
|
|
public class DateAdapter implements IDBAdapter{
|
|
private Date date;
|
private boolean includeTime;
|
|
private static String unlimtedDate = "9999-12-31";
|
|
|
public DateAdapter() {
|
date = new Date();
|
includeTime = false;
|
}
|
|
public DateAdapter(Date date) {
|
this.date = date;
|
includeTime = false;
|
}
|
|
public DateAdapter(Date date, boolean includeTime) {
|
this.date = date;
|
this.includeTime = includeTime;
|
}
|
|
@Override
|
public String toSqlString(DBaseType dbaseType) {
|
if (date == null) {
|
return "null";
|
}
|
|
String result = null;
|
String formatter = this.includeTime ? "yyyy-MM-dd HH:mm:ss" : "yyyy-MM-dd";
|
DateFormat dateFormat = new SimpleDateFormat(formatter);
|
String value = dateFormat.format(this.date);
|
|
if (dbaseType.isOracle()) {
|
result = "to_date('" + value + "','yyyy-mm-dd hh24:mi:ss')";
|
}
|
else if (dbaseType.isSQLServer()) {
|
result = "'" + value + "'";
|
}
|
else if (dbaseType.isMySQL()) {
|
result = "'" + value + "'";
|
}
|
else {
|
result = "'" + value + "'";
|
}
|
|
return result;
|
}
|
|
@Override
|
public String getFunciton(DBaseType dbaseType, String functionName) {
|
if (functionName.equalsIgnoreCase("dateAddFunction")) {
|
return getDayAddFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("currentTime")) {
|
return getCurrentTimeFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("currentDate")) {
|
return getCurrentDateFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("unlimitedDate")) {
|
return getUnlimitedDateFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("toDate")) {
|
return getToDateFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("timeFormat")) {
|
return getTimeFormatFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("dateFormat")) {
|
return getDateFormatFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("yearFormat")) {
|
return getYearFormatFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("monthFormat")) {
|
return getMonthFormatFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("dayFormat")) {
|
return getDayFormatFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("hourFormat")) {
|
return getHourFormatFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("minuteFormat")) {
|
return getMinuteFormatFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("secondFormat")) {
|
return getSecondFormatFunciton(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("getYear(")) {
|
return getYear(dbaseType);
|
}
|
|
if (functionName.equalsIgnoreCase("getMonth(")) {
|
return getMonth(dbaseType);
|
}
|
|
return null;
|
}
|
|
public String getDayAddFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "ADD_DAYS";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "ADDDATE";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "DATE_ADD";
|
}
|
|
return "ADD_DAYS";
|
}
|
|
public String getCurrentTimeFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "SYSDATE";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "now()";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "GETDATE()";
|
}
|
|
return "SYSDATE";
|
}
|
|
public String getCurrentDateFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "SYSDATE";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "now()";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "GETDATE()";
|
}
|
|
return "SYSDATE";
|
}
|
|
private String getUnlimitedDateFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "to_date(" + Util.quotedStr(unlimtedDate) + ", 'yyyy-mm-dd')";
|
}
|
else if (dbaseType.isMySQL()) {
|
return Util.quotedStr(unlimtedDate);
|
}
|
else if (dbaseType.isSQLServer()) {
|
return Util.quotedStr(unlimtedDate);
|
}
|
|
return "to_date(" + Util.quotedStr(unlimtedDate) + ", 'yyyy-mm-dd')";
|
}
|
|
private String getTimeFormatFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "yyyy-mm-dd hh24:mi:ss";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "%y-%M-%d %H:%m:%s";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "yyyy-MM-dd HH:mm:ss";
|
}
|
|
return "yyyy-MM-dd hh24:mi:ss";
|
}
|
|
private String getDateFormatFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "yyyy-mm-dd";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "%y-%M-%d";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "yyyy-MM-dd";
|
}
|
|
return "yyyy-MM-dd";
|
}
|
|
private String getYearFormatFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "yyyy";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "%y";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "yyyy";
|
}
|
|
return "yyyy";
|
}
|
|
private String getMonthFormatFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "mm";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "%M";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "MM";
|
}
|
|
return "MM";
|
}
|
|
private String getDayFormatFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "dd";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "%d";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "dd";
|
}
|
|
return "dd";
|
}
|
|
private String getHourFormatFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "hh24";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "%H";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "HH";
|
}
|
|
return "hh24";
|
}
|
|
private String getMinuteFormatFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "mi";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "%m";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "mm";
|
}
|
|
return "mi";
|
}
|
|
private String getSecondFormatFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "ss";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "%s";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "ss";
|
}
|
|
return "ss";
|
}
|
|
private String getToDateFunciton(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "to_date";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "date_format";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "format()";
|
}
|
|
return "SYSDATE";
|
}
|
|
public String getYear(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "EXTRACT(YEAR FROM ";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "YEAR(";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "YEAR(";
|
}
|
|
return "EXTRACT(YEAR FROM ";
|
}
|
|
public String getMonth(DBaseType dbaseType) {
|
if (dbaseType.isOracle()) {
|
return "EXTRACT(MONTH FROM ";
|
}
|
else if (dbaseType.isMySQL()) {
|
return "MONTH(";
|
}
|
else if (dbaseType.isSQLServer()) {
|
return "MONTH(";
|
}
|
|
return "EXTRACT(MONTH FROM ";
|
}
|
}
|