1 22 23 package org.xquark.extractor.mysql.sql; 24 25 import java.sql.Types ; 26 27 import org.xquark.extractor.common.Debug; 28 import org.xquark.extractor.sql.Context; 29 import org.xquark.extractor.sql.SqlExpression; 30 import org.xquark.extractor.sql.SqlSelect; 31 import org.xquark.jdbc.typing.DbType; 32 33 public class SqlConvert extends org.xquark.extractor.sql.SqlConvert { 34 private static final String RCSRevision = "$Revision: 1.7 $"; 35 private static final String RCSName = "$Name: $"; 36 37 public SqlConvert() { 38 super(); 39 } 40 41 public SqlConvert(SqlExpression expr, DbType originalType, DbType targetType) { 42 super(expr, originalType, targetType); 43 } 44 45 public String toSql(Context context) { 46 String retVal = null; 48 49 String expr = getExpression().toSql(context); 50 if (getExpression() instanceof SqlSelect) { 51 expr = '(' + expr + ')'; 52 } 53 54 int targetTypeCode = _targetType.getJDBCType(); 55 56 if (_targetType.isNumeric()) { 57 retVal = convertToNumber(expr); 58 } else if (_targetType.isString()) { 59 retVal = convertToString(expr); 60 } else if (Types.DATE == targetTypeCode) { 61 retVal = convertToDate(expr); 62 } else if (Types.TIME == targetTypeCode) { 63 retVal = convertToTime(expr); 64 } else if (Types.TIMESTAMP == targetTypeCode) { 65 retVal = convertToDateTime(expr); 66 } else { 67 Debug.assertTrue(false, "NYI!!"); 68 } 69 return retVal; 71 } 72 73 private String convertToString(String expr) { 74 StringBuffer retVal = new StringBuffer (); 76 77 int originalTypeCode = _originalType.getJDBCType(); 78 if (_originalType.isString()) { 79 retVal.append(expr); 80 } else if (_originalType.isNumeric()) { 81 retVal.append("CONCAT("); 82 retVal.append(expr); 84 retVal.append(")"); 85 } else 86 switch (originalTypeCode) { 87 case Types.DATE : 88 retVal.append("DATE_FORMAT("); 89 retVal.append(expr); 90 retVal.append(",'%Y-%m-%d')"); 91 break; 92 case Types.TIME : 93 retVal.append("TIME_FORMAT("); 94 retVal.append(expr); 95 retVal.append(",'%H:%i:%s')"); 96 break; 97 case Types.TIMESTAMP : 98 retVal.append("DATE_FORMAT("); 99 retVal.append(expr); 100 retVal.append(",'%Y-%m-%dT%H:%i:%s')"); 101 break; 102 case Types.NULL : 103 retVal.append("null"); 104 break; 105 default : 106 Debug.assertTrue(false, originalTypeCode + " type not expected."); 107 } 108 109 return retVal.toString(); 111 } 112 113 private String convertToDate(String expr) { 114 return expr; 118 } 119 120 private String convertToDateTime(String expr) { 121 return expr; 125 } 126 127 private String convertToTime(String expr) { 128 return expr; 132 } 133 134 private String convertToNumber(String expr) { 135 return expr; 139 } 140 } 141 | Popular Tags |