1 19 20 package org.apache.cayenne.dba.mysql; 21 22 import java.sql.Connection ; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.sql.Statement ; 26 import java.util.Collections ; 27 28 import org.apache.cayenne.access.DataNode; 29 import org.apache.cayenne.access.QueryLogger; 30 import org.apache.cayenne.dba.JdbcPkGenerator; 31 import org.apache.cayenne.map.DbEntity; 32 33 36 public class MySQLPkGenerator extends JdbcPkGenerator { 37 38 protected String dropAutoPkString() { 39 return "DROP TABLE IF EXISTS AUTO_PK_SUPPORT"; 40 } 41 42 46 protected int pkFromDatabase(DataNode node, DbEntity ent) throws Exception { 47 48 52 SQLException exception = null; 54 int pk = -1; 55 56 Connection con = node.getDataSource().getConnection(); 57 try { 58 59 if(con.getAutoCommit()) { 60 con.setAutoCommit(false); 61 } 62 63 Statement st = con.createStatement(); 64 65 try { 66 pk = getPrimaryKey(st, ent.getName()); 67 con.commit(); 68 } 69 catch (SQLException pkEx) { 70 71 try { 72 con.rollback(); 73 } 74 catch (SQLException e) { 75 76 } 77 78 exception = processSQLException(pkEx, exception); 79 } 80 finally { 81 try { 85 String unlockString = "UNLOCK TABLES"; 86 QueryLogger.logQuery(unlockString, Collections.EMPTY_LIST); 87 st.execute(unlockString); 88 } 89 catch (SQLException unlockEx) { 90 exception = processSQLException(unlockEx, exception); 91 } 92 finally { 93 try { 95 st.close(); 96 } 97 catch (SQLException stClosingEx) { 98 } 100 } 101 } 102 } 103 catch (SQLException otherEx) { 104 exception = processSQLException(otherEx, exception); 105 } 106 finally { 107 try { 108 con.close(); 109 } 110 catch (SQLException closingEx) { 111 } 113 } 114 115 if (exception != null) { 117 throw exception; 118 } 119 120 return pk; 121 } 122 123 127 protected SQLException processSQLException(SQLException exception, SQLException parent) { 128 if (parent == null) { 129 return exception; 130 } 131 132 parent.setNextException(exception); 133 return parent; 134 } 135 136 protected String pkTableCreateString() { 137 StringBuffer buf = new StringBuffer (); 138 buf.append("CREATE TABLE AUTO_PK_SUPPORT (").append( 139 " TABLE_NAME CHAR(100) NOT NULL,").append( 140 " NEXT_ID INTEGER NOT NULL, UNIQUE (TABLE_NAME)").append(")"); 141 142 return buf.toString(); 143 } 144 145 protected int getPrimaryKey(Statement statement, String entityName) 146 throws SQLException { 147 String lockString = "LOCK TABLES AUTO_PK_SUPPORT WRITE"; 149 QueryLogger.logQuery(lockString, Collections.EMPTY_LIST); 150 statement.execute(lockString); 151 152 int pk = -1; 154 155 String selectString = super.pkSelectString(entityName); 156 QueryLogger.logQuery(selectString, Collections.EMPTY_LIST); 157 ResultSet rs = statement.executeQuery(selectString); 158 try { 159 if (!rs.next()) { 160 throw new SQLException ("No rows for '" + entityName + "'"); 161 } 162 163 pk = rs.getInt(1); 164 165 if (rs.next()) { 166 throw new SQLException ("More than one row for '" + entityName + "'"); 167 } 168 } 169 finally { 170 try { 171 rs.close(); 172 } 173 catch (Exception ex) { 174 } 176 } 177 178 String updateString = super.pkUpdateString(entityName) + " AND NEXT_ID = " + pk; 180 QueryLogger.logQuery(updateString, Collections.EMPTY_LIST); 181 int updated = statement.executeUpdate(updateString); 182 if (updated != 1) { 184 throw new SQLException ("Error updating PK count '" 185 + entityName 186 + "': " 187 + updated); 188 } 189 190 return pk; 191 } 192 } 193 | Popular Tags |