1 22 23 package org.xquark.extractor.oracle.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 if (DbType.ORACLE_ROWID == targetTypeCode) { 67 retVal = convertToRowid(expr); 68 } else { 69 Debug.assertTrue(false, "NYI!!"); 70 } 71 return retVal; 73 } 74 75 private String convertToString(String expr) { 76 StringBuffer retVal = new StringBuffer (); 78 79 int originalTypeCode = _originalType.getJDBCType(); 80 if (_originalType.isString()) { 81 retVal.append(expr); 82 } else if (DbType.ORACLE_ROWID == originalTypeCode) { 83 retVal.append("ROWIDTOCHAR("); 84 retVal.append(expr); 85 retVal.append(")"); 86 } else if (Types.DATE == originalTypeCode) { 87 retVal.append("LTRIM(TO_CHAR("); 88 retVal.append(expr); 89 retVal.append(",'SYYYY-MM-DD'))"); 90 } else if (Types.TIME == originalTypeCode) { 91 retVal.append("TO_CHAR("); 92 retVal.append(expr); 93 retVal.append(",'HH24:MI:SS')"); 94 } else if (Types.TIMESTAMP == originalTypeCode) { 95 retVal.append("LTRIM(TO_CHAR("); 96 retVal.append(expr); 97 retVal.append(",'SYYYY-MM-DD\"T\"HH24:MI:SS'))"); 98 } else if (_originalType.isNumeric()) { 99 retVal.append("TO_CHAR("); 100 retVal.append(expr); 101 retVal.append(")"); 102 } else if (Types.NULL == originalTypeCode) { 103 retVal.append("TO_CHAR("); 104 retVal.append(expr); 105 retVal.append(")"); 106 } else { 107 Debug.assertTrue(false, originalTypeCode + " type not expected."); 108 } 109 110 return retVal.toString(); 112 } 113 114 private String convertToDate(String expr) { 115 StringBuffer retVal = new StringBuffer (); 117 118 int originalTypeCode = _originalType.getJDBCType(); 119 if (_originalType.isString() || _originalType.isNull()) { 120 retVal.append("TO_DATE("); 121 retVal.append(expr); 122 retVal.append(", 'SYYYY-MM-DD')"); 123 } else if (Types.DATE == originalTypeCode) { 124 retVal.append(expr); 125 } else { 126 Debug.assertTrue(false, "cas imprevu"); 127 } 128 129 return retVal.toString(); 131 } 132 133 private String convertToDateTime(String expr) { 134 StringBuffer retVal = new StringBuffer (); 136 137 int originalTypeCode = _originalType.getJDBCType(); 138 if (_originalType.isString() || _originalType.isNull()) { 139 retVal.append("TO_DATE("); 140 retVal.append(expr); 141 retVal.append(", 'SYYYY-MM-DD\"T\"HH24:MI:SS')"); 142 } else if (Types.TIMESTAMP == originalTypeCode) { 143 retVal.append(expr); 144 } else { 145 Debug.assertTrue(false, "cas imprevu"); 146 } 147 148 return retVal.toString(); 150 } 151 152 private String convertToTime(String expr) { 153 StringBuffer retVal = new StringBuffer (); 155 156 int originalTypeCode = _originalType.getJDBCType(); 157 if (_originalType.isString() || _originalType.isNull()) { 158 retVal.append("TO_DATE("); 159 retVal.append(expr); 160 retVal.append(", 'HH24:MI:SS')"); 161 } else if (Types.TIME == originalTypeCode) { 162 retVal.append(expr); 163 } else { 164 Debug.assertTrue(false, "cas imprevu"); 165 } 166 167 return retVal.toString(); 169 } 170 171 private String convertToNumber(String expr) { 172 StringBuffer retVal = new StringBuffer (); 174 175 int originalTypeCode = _originalType.getJDBCType(); 176 if (_originalType.isNumeric()) { 177 retVal.append(expr); 178 } else if (_originalType.isNull()) { 179 retVal.append("TO_NUMBER("); 180 retVal.append(expr); 181 retVal.append(")"); 182 } else if (_originalType.isString()) { 183 retVal.append("TO_NUMBER("); 184 retVal.append(expr); 185 retVal.append(")"); 186 } else { 187 Debug.assertTrue(false, "cas imprevu"); 188 } 189 return retVal.toString(); 191 } 192 private String convertToRowid(String expr) { 193 StringBuffer retVal = new StringBuffer (); 195 196 int originalTypeCode = _originalType.getJDBCType(); 197 if (_originalType.isString() || _originalType.isNull()) { 198 retVal.append(" CHARTOROWID("); 199 retVal.append(expr); 200 retVal.append(")"); 201 } else { 202 Debug.assertTrue(false, "cas imprevu"); 203 } 204 205 return retVal.toString(); 207 } 208 209 } 210 | Popular Tags |