1 package org.apache.torque.adapter; 2 3 21 22 import java.sql.Connection ; 23 import java.sql.SQLException ; 24 import java.sql.Statement ; 25 import java.text.SimpleDateFormat ; 26 import java.util.Date ; 27 import java.util.HashSet ; 28 import java.util.ListIterator ; 29 import java.util.Set ; 30 31 import org.apache.torque.util.Query; 32 import org.apache.torque.util.UniqueList; 33 34 43 public class DBOracle extends AbstractDBAdapter 44 { 45 48 private static final long serialVersionUID = 8966976210230241194L; 49 50 51 private static final String DATE_FORMAT = "dd-MM-yyyy HH:mm:ss"; 52 53 56 protected DBOracle() 57 { 58 } 59 60 66 public String toUpperCase(String in) 67 { 68 return new StringBuffer ("UPPER(").append(in).append(")").toString(); 69 } 70 71 77 public String ignoreCase(String in) 78 { 79 return new StringBuffer ("UPPER(").append(in).append(")").toString(); 80 } 81 82 88 public String getDateString(Date date) 89 { 90 return "TO_DATE('" + new SimpleDateFormat (DATE_FORMAT).format(date) 91 + "', 'DD-MM-YYYY HH24:MI:SS')"; 92 } 93 94 97 public String getIDMethodType() 98 { 99 return SEQUENCE; 100 } 101 102 115 public String getIDMethodSQL(Object sequenceName) 116 { 117 return ("select " + sequenceName + ".nextval from dual"); 118 } 119 120 127 public void lockTable(Connection con, String table) throws SQLException 128 { 129 Statement statement = con.createStatement(); 130 131 StringBuffer stmt = new StringBuffer (); 132 stmt.append("SELECT next_id FROM ") 133 .append(table) 134 .append(" FOR UPDATE"); 135 136 statement.executeQuery(stmt.toString()); 137 } 138 139 146 public void unlockTable(Connection con, String table) throws SQLException 147 { 148 con.commit(); 151 } 152 153 160 public int getLimitStyle() 161 { 162 return DB.LIMIT_STYLE_ORACLE; 163 } 164 165 169 public boolean supportsNativeLimit() 170 { 171 return true; 172 } 173 174 178 public boolean supportsNativeOffset() 179 { 180 return true; 181 } 182 183 200 public void generateLimits(Query query, int offset, int limit) 201 { 202 StringBuffer preLimit = new StringBuffer () 203 .append("SELECT B.* FROM ( ") 204 .append("SELECT A.*, rownum AS TORQUE$ROWNUM FROM ( "); 205 206 StringBuffer postLimit = new StringBuffer () 207 .append(" ) A ") 208 .append(" ) B WHERE "); 209 210 if (offset > 0) 211 { 212 postLimit.append(" B.TORQUE$ROWNUM > ") 213 .append(offset); 214 215 if (limit >= 0) 216 { 217 postLimit.append(" AND B.TORQUE$ROWNUM <= ") 218 .append(offset + limit); 219 } 220 } 221 else 222 { 223 postLimit.append(" B.TORQUE$ROWNUM <= ") 224 .append(limit); 225 } 226 227 query.setPreLimit(preLimit.toString()); 228 query.setPostLimit(postLimit.toString()); 229 query.setLimit(null); 230 231 UniqueList selectColumns = query.getSelectClause(); 235 int replacementSuffix = 0; 236 Set columnNames = new HashSet (); 237 for (ListIterator columnIt = selectColumns.listIterator(); 242 columnIt.hasNext();) 243 { 244 String selectColumn = (String ) columnIt.next(); 245 246 if ((selectColumn.indexOf('(') != -1) 248 || (selectColumn.indexOf(')') != -1)) 249 { 250 continue; 252 } 253 254 int spacePos = selectColumn.lastIndexOf(' '); 256 if (spacePos == -1) 257 { 258 continue; 260 } 261 262 String aliasName = selectColumn.substring(spacePos + 1); 263 columnNames.add(aliasName); 264 } 265 266 for (ListIterator columnIt = selectColumns.listIterator(); 268 columnIt.hasNext();) 269 { 270 String selectColumn = (String ) columnIt.next(); 271 272 if ((selectColumn.indexOf('(') != -1) 274 || (selectColumn.indexOf(')') != -1)) 275 { 276 continue; 278 } 279 280 { 281 int spacePos = selectColumn.lastIndexOf(' '); 282 if (spacePos != -1) 283 { 284 continue; 286 } 287 } 288 String column; 290 { 291 int dotPos = selectColumn.lastIndexOf('.'); 292 if (dotPos != -1) 293 { 294 column = selectColumn.substring(dotPos + 1); 295 } 296 else 297 { 298 column = selectColumn; 299 } 300 } 301 if (columnNames.contains(column)) 302 { 303 String aliasName; 306 do 307 { 308 aliasName = "a" + replacementSuffix; 309 ++replacementSuffix; 310 } 311 while (columnNames.contains(aliasName)); 312 313 selectColumn = selectColumn + " " + aliasName; 314 columnIt.set(selectColumn); 315 columnNames.add(aliasName); 316 } 317 else 318 { 319 columnNames.add(column); 320 } 321 } 322 } 323 324 331 public boolean escapeText() 332 { 333 return false; 334 } 335 336 345 public boolean useEscapeClauseForLike() 346 { 347 return true; 348 } 349 } 350 | Popular Tags |