1 22 23 package org.xquark.extractor.sybase.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.6 $"; 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 (Types.DATE == originalTypeCode) { 81 retVal.append("STUFF(STUFF(RTRIM(CONVERT(char,"); 82 retVal.append(expr); 83 retVal.append(", 102)), 5,1,\'-\'),8,1,\'-\')"); 84 } else if (Types.TIME == originalTypeCode) { 85 retVal.append("SUBSTRING(CONVERT(char,"); 86 retVal.append(expr); 87 retVal.append(",8),1,8)"); 88 } else if (Types.TIMESTAMP == originalTypeCode) { 89 retVal.append("STUFF(STUFF(RTRIM(CONVERT(char,"); 90 retVal.append(expr); 91 retVal.append(", 102)), 5,1,\'-\'),8,1,\'-\') + \'T\' + SUBSTRING(CONVERT(char, "); 92 retVal.append(expr); 93 retVal.append(",8),1,8)"); 94 } else if (_originalType.isNumeric()) { 95 retVal.append("RTRIM(CONVERT(varchar,"); 96 retVal.append(expr); 97 retVal.append("))"); 98 } else if (_originalType.isNull()) { 99 retVal.append("CONVERT(varchar,"); 100 retVal.append(expr); 101 retVal.append(")"); 102 } else { 103 Debug.assertTrue(false, "cas imprevu"); 104 } 105 106 return retVal.toString(); 108 } 109 110 private String convertToDate(String expr) { 111 StringBuffer retVal = new StringBuffer (); 113 114 int originalTypeCode = _originalType.getJDBCType(); 115 if (_originalType.isString() || _originalType.isNull()) { 116 retVal.append("CONVERT(DATETIME,"); 117 retVal.append(expr); 118 retVal.append(")"); 119 } else if (Types.DATE == originalTypeCode) { 120 retVal.append(expr); 121 } else { 122 Debug.assertTrue(false, "cas imprevu"); 123 } 124 125 return retVal.toString(); 127 } 128 129 private String convertToDateTime(String expr) { 130 StringBuffer retVal = new StringBuffer (); 132 133 int originalTypeCode = _originalType.getJDBCType(); 134 if (_originalType.isString() || _originalType.isNull()) { 135 retVal.append("CONVERT(DATETIME,"); 136 retVal.append(expr); 137 retVal.append(")"); 138 } else if (Types.TIMESTAMP == originalTypeCode) { 139 retVal.append(expr); 140 } else { 141 Debug.assertTrue(false, "cas imprevu"); 142 } 143 144 return retVal.toString(); 146 } 147 148 private String convertToTime(String expr) { 149 StringBuffer retVal = new StringBuffer (); 151 152 int originalTypeCode = _originalType.getJDBCType(); 153 if (_originalType.isString() || _originalType.isNull()) { 154 retVal.append("CONVERT(DATETIME,"); 155 retVal.append(expr); 156 retVal.append(")"); 157 } else if (Types.TIME == originalTypeCode) { 158 retVal.append(expr); 159 } else { 160 Debug.assertTrue(false, "cas imprevu"); 161 } 162 163 return retVal.toString(); 165 } 166 167 private String convertToNumber(String expr) { 168 StringBuffer retVal = new StringBuffer (); 170 171 int originalTypeCode = _originalType.getJDBCType(); 172 if (_originalType.isNull()) { 173 retVal.append(expr); 174 } else { 175 int targetTypeCode = _targetType.getJDBCType(); 176 switch (targetTypeCode) { 177 case Types.DECIMAL : 178 retVal.append("CONVERT(decimal, "); 179 break; 180 case Types.TINYINT : 181 retVal.append("CONVERT(tinyint, "); 182 break; 183 case Types.SMALLINT : 184 retVal.append("CONVERT(smallint, "); 185 break; 186 case Types.INTEGER : 187 retVal.append("CONVERT(integer, "); 188 break; 189 case Types.BIGINT : 190 retVal.append("CONVERT(bigint, "); 191 break; 192 case Types.REAL : 193 retVal.append("CONVERT(real, "); 194 break; 195 case Types.FLOAT : 196 retVal.append("CONVERT(float, "); 197 break; 198 case Types.DOUBLE : 199 retVal.append("CONVERT(float(16), "); 200 break; 201 default : 202 Debug.assertTrue(false, "Data covertion not implemented"); 203 break; 204 } 205 retVal.append(expr); 206 retVal.append(")"); 207 } 208 return retVal.toString(); 210 } 211 } 212 | Popular Tags |