1 22 23 package org.xquark.extractor.microsoft.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 34 public class SqlConvert extends org.xquark.extractor.sql.SqlConvert{ 35 private static final String RCSRevision = "$Revision: 1.6 $"; 36 private static final String RCSName = "$Name: $"; 37 38 public SqlConvert() { 39 super(); 40 } 41 42 public SqlConvert(SqlExpression expr, DbType originalType, DbType targetType) 43 { 44 super(expr, originalType, targetType); 45 } 46 47 public String toSql (Context context) { 48 String retVal = null; 50 51 String expr = getExpression().toSql(context); 52 if (getExpression() instanceof SqlSelect) { 53 expr = '('+ expr +')'; 54 } 55 56 int targetTypeCode = _targetType.getJDBCType(); 57 58 if (_targetType.isNumeric()) { 59 retVal = convertToNumber(expr); 60 } 61 else if (_targetType.isString()) { 62 retVal = convertToString(expr); 63 } 64 else if (Types.DATE == targetTypeCode) { 65 retVal = convertToDate(expr); 66 } 67 else if (Types.TIME == targetTypeCode) { 68 retVal = convertToTime(expr); 69 } 70 else if (Types.TIMESTAMP == targetTypeCode) { 71 retVal = convertToDateTime(expr); 72 } 73 else { 74 Debug.assertTrue (false, "NYI!!"); 75 } 76 return retVal; 78 } 79 80 private String convertToString(String expr) { 81 StringBuffer retVal = new StringBuffer (); 83 84 int originalTypeCode = _originalType.getJDBCType(); 85 if (_originalType.isString()) { 86 retVal.append(expr); 87 } 88 else if (Types.DATE == originalTypeCode) { 89 retVal.append("STUFF(STUFF(RTRIM(CONVERT(char,"); 90 retVal.append(expr); 91 retVal.append(", 102)), 5,1,\'-\'),8,1,\'-\')"); 92 } 93 else if (Types.TIME == originalTypeCode) { 94 retVal.append("SUBSTRING(CONVERT(char,"); 95 retVal.append(expr); 96 retVal.append(",8),1,8)"); 97 } 98 else if (Types.TIMESTAMP == originalTypeCode) { 99 retVal.append("STUFF(STUFF(RTRIM(CONVERT(char,"); 100 retVal.append(expr); 101 retVal.append(", 102)), 5,1,\'-\'),8,1,\'-\') + \'T\' + SUBSTRING(CONVERT(char, "); 102 retVal.append(expr); 103 retVal.append(",8),1,8)"); 104 } 105 else if (_originalType.isNumeric() ){ 106 retVal.append("RTRIM(CONVERT(varchar,"); 107 retVal.append(expr); 108 retVal.append("))"); 109 } 110 else if (_originalType.isNull() ){ 111 retVal.append("CONVERT(varchar,"); 112 retVal.append(expr); 113 retVal.append(")"); 114 } 115 else { 116 Debug.assertTrue(false, "cas imprevu"); 117 } 118 119 return retVal.toString(); 121 } 122 123 private String convertToDate(String expr) { 124 StringBuffer retVal = new StringBuffer (); 126 127 int originalTypeCode = _originalType.getJDBCType(); 128 if (_originalType.isString() || _originalType.isNull()) { 129 retVal.append("CONVERT(DATETIME,"); 130 retVal.append(expr); 131 retVal.append(")"); 132 } 133 else if (Types.DATE ==originalTypeCode) { 134 retVal.append(expr); 135 } 136 else { 137 Debug.assertTrue(false, "cas imprevu"); 138 } 139 140 return retVal.toString(); 142 } 143 144 private String convertToDateTime(String expr) { 145 StringBuffer retVal = new StringBuffer (); 147 148 int originalTypeCode = _originalType.getJDBCType(); 149 if (_originalType.isString() || _originalType.isNull()) { 150 retVal.append("CONVERT(DATETIME,"); 151 retVal.append(expr); 152 retVal.append(")"); 153 } 154 else if (Types.TIMESTAMP ==originalTypeCode) { 155 retVal.append(expr); 156 } 157 else { 158 Debug.assertTrue(false, "cas imprevu"); 159 } 160 161 return retVal.toString(); 163 } 164 165 private String convertToTime(String expr) { 166 StringBuffer retVal = new StringBuffer (); 168 169 int originalTypeCode = _originalType.getJDBCType(); 170 if (_originalType.isString() || _originalType.isNull()) { 171 retVal.append("CONVERT(DATETIME,"); 172 retVal.append(expr); 173 retVal.append(")"); 174 } 175 else if (Types.TIME ==originalTypeCode) { 176 retVal.append(expr); 177 } 178 else { 179 Debug.assertTrue(false, "cas imprevu"); 180 } 181 182 return retVal.toString(); 184 } 185 186 private String convertToNumber(String expr) { 187 StringBuffer retVal = new StringBuffer (); 189 190 int originalTypeCode = _originalType.getJDBCType(); 191 if (_originalType.isNull()) { 192 retVal.append(expr); 193 } 194 else { 195 int targetTypeCode = _targetType.getJDBCType(); 196 switch (targetTypeCode) { 197 case Types.DECIMAL: 198 retVal.append("CONVERT(decimal, "); 199 break; 200 case Types.TINYINT: 201 retVal.append("CONVERT(tinyint, "); 202 break; 203 case Types.SMALLINT: 204 retVal.append("CONVERT(smallint, "); 205 break; 206 case Types.INTEGER: 207 retVal.append("CONVERT(integer, "); 208 break; 209 case Types.BIGINT: 210 retVal.append("CONVERT(bigint, "); 211 break; 212 case Types.REAL: 213 retVal.append("CONVERT(real, "); 214 break; 215 case Types.FLOAT: 216 retVal.append("CONVERT(float, "); 217 break; 218 case Types.DOUBLE: 219 retVal.append("CONVERT(float(16), "); 220 break; 221 default: 222 Debug.assertTrue(false, "Data covertion not implemented"); 223 break; 224 } 225 retVal.append(expr); 226 retVal.append(")"); 227 } 228 return retVal.toString(); 230 } 231 } 232 | Popular Tags |