1 22 23 package org.xquark.mapper.dbms.oracle; 24 25 import java.sql.*; 26 import java.util.*; 27 28 import org.xquark.jdbc.typing.DBMSInfo; 29 import org.xquark.mapper.RepositoryException; 30 import org.xquark.mapper.dbms.*; 31 import org.xquark.mapper.metadata.Repository; 32 import org.xquark.mapper.metadata.RepositoryConstants; 33 import org.xquark.mapper.util.LongList; 34 35 39 public class O8iConnection extends JDBC2Connection 40 { 41 private static final String RCSRevision = "$Revision: 1.3 $"; 42 private static final String RCSName = "$Name: $"; 43 44 private static final String SEQ_QUERY = "select SEQUENCE_NAME from USER_SEQUENCES where SEQUENCE_NAME LIKE('"; 45 46 47 public O8iConnection(Connection connection, short dbms, DBMSInfo dbmsInfo) 48 throws SQLException 49 { 50 super(connection, dbms, dbmsInfo); 51 } 52 53 public void dropTemporaryTable(String tableName) throws SQLException 57 { 58 Statement stmt = connection.createStatement(); 59 try 60 { 61 stmt.executeUpdate("TRUNCATE TABLE "+ tableName); 62 } 63 finally 64 { 65 stmt.close(); 66 } 67 dropTable(tableName); 68 } 69 70 public void createSequence(TableInfo table, short step) throws SQLException 71 { 72 StringBuffer sql = new StringBuffer (); 73 sql.append("CREATE SEQUENCE "); 74 sql.append(table.getName()); 75 sql.append(" START WITH "); 76 sql.append(RepositoryConstants.SEQUENCE_FIRST_VALUE); 77 sql.append(" INCREMENT BY "); 78 sql.append(step); 79 executeUpdate(sql.toString()); 80 } 81 82 public void dropSequence(String seqName) throws SQLException 83 { 84 executeUpdate("DROP SEQUENCE " + seqName); 85 } 86 87 public long nextSequenceValue(TableInfo table, short step) throws RepositoryException 88 { 89 long next = 1; 90 Statement stmt = null; 91 ResultSet rs = null; 92 StringBuffer sql = new StringBuffer (); 93 sql.append("SELECT "); 94 sql.append(table.getName()); 95 sql.append(".NEXTVAL FROM DUAL"); 96 try 97 { 98 stmt = connection.createStatement(); 99 rs = stmt.executeQuery(sql.toString()); 100 if (rs.next()) 101 next = rs.getLong(1); 102 } 103 catch (SQLException e) 104 { 105 throw new RepositoryException(RepositoryException.DB_ERROR, 106 "JDBC error while querying the " + table.getName() + " table."); 107 } 108 finally 109 { 110 try 111 { 112 if (rs != null) 113 { 114 rs.close(); 115 stmt.close(); 116 } 117 } 118 catch (SQLException e) 119 { 120 } 122 } 123 return next; 124 } 125 126 public List getUserSequenceNames(String seqPattern) throws SQLException 127 { 128 Statement stmt = null; 129 ResultSet rs = null; 130 ArrayList seqs = new ArrayList(); 131 132 StringBuffer sql = new StringBuffer (); 133 sql.append(SEQ_QUERY); 134 sql.append(seqPattern); 135 sql.append("')"); 136 try 137 { 138 stmt = connection.createStatement(); 139 rs = stmt.executeQuery(sql.toString()); 140 while (rs.next()) 141 seqs.add(rs.getString(1)); 142 } 143 finally 144 { 145 if (rs != null) 146 rs.close(); 147 if (stmt != null) 148 stmt.close(); 149 } 150 return seqs; 151 } 152 153 160 private StringBuffer hierarchicalQueryString(LongList path,String select, String tableName) 161 { 162 StringBuffer sql = new StringBuffer (); 163 164 int len = path.size(); 165 sql.append("SELECT "+select); 166 sql.append(" FROM "+ tableName); 167 sql.append(" START WITH label = '"+path.getValue(0)+"' "); 168 sql.append("CONNECT BY (PRIOR target = source) AND ("); 169 for(int i=1; i<len; i++) { 170 sql.append(" (level="+(i+1)+" AND label='"+path.getValue(i)+"') OR "); 171 } 172 if (len>1) sql.setLength(sql.length()-4); 173 else sql.setLength(sql.length()-8); 174 sql.append(") "); 175 return sql; 177 } 178 179 183 private StringBuffer reversedHierarchicalQueryString(LongList path, String select, String tableName) 184 { 185 StringBuffer sql = new StringBuffer (); 186 187 int len = path.size(); 188 sql.append("select "+select); 189 sql.append(" from " + tableName); 190 sql.append(" start with label = '"+path.getValue(len-1)+"' \n"); 191 sql.append("connect by (prior source = target) AND (\n"); 192 for(int i=len-2; i>=0; i--) { 193 sql.append(" (level="+(len-i)+" and label='"+path.getValue(i)+"') or \n"); 194 } 195 if(len>1) sql.setLength(sql.length()-4); 196 else sql.setLength(sql.length()-8); 197 sql.append(") \n"); 198 return sql; 199 } 200 201 public int getUniversalError(SQLException e) 202 { 203 switch (e.getErrorCode()) 204 { 205 case 955: 206 return OBJECT_ALREADY_EXISTS; 207 case 942: 208 return OBJECT_DOES_NOT_EXISTS; 209 default: 210 return super.getUniversalError(e); 211 } 212 } 213 214 public void updateStatistics(String tableName) throws SQLException 215 { 216 executeUpdate("ANALYZE TABLE " + tableName + " ESTIMATE STATISTICS"); 217 } 218 219 222 public void onInitRepository(Repository rep) throws SQLException 223 { 224 Statement stmt = null; 225 try 226 { 227 String dualTableName = rep.getTableInfo(TableSpec.TYPE_DUAL).getName(); 229 stmt = connection.createStatement(); 230 stmt.executeUpdate("INSERT INTO " + dualTableName + " VALUES('X')"); 231 stmt.executeUpdate("ANALYZE TABLE " + dualTableName + " COMPUTE STATISTICS"); 232 } 233 finally 234 { 235 if (stmt != null) 236 stmt.close(); 237 } 238 } 239 public void onDeleteRepository() throws SQLException 240 { 241 } 242 243 public boolean distinguishNullAndEmptyStrings() 244 { 245 return false; 246 } 247 248 public String getBitmapIndexClause() 252 { 253 return "BITMAP"; 254 } 255 } 256 | Popular Tags |