1 56 package org.objectstyle.cayenne.dba.mysql; 57 58 import java.sql.Connection ; 59 import java.sql.ResultSet ; 60 import java.sql.SQLException ; 61 import java.sql.Statement ; 62 import java.util.Collections ; 63 64 import org.objectstyle.cayenne.access.DataNode; 65 import org.objectstyle.cayenne.access.QueryLogger; 66 import org.objectstyle.cayenne.dba.JdbcPkGenerator; 67 import org.objectstyle.cayenne.map.DbEntity; 68 69 72 public class MySQLPkGenerator extends JdbcPkGenerator { 73 77 protected int pkFromDatabase(DataNode node, DbEntity ent) 78 throws Exception { 79 80 84 SQLException exception = null; 86 int pk = -1; 87 88 Connection con = node.getDataSource().getConnection(); 89 try { 90 Statement st = con.createStatement(); 91 92 try { 93 pk = getPrimaryKey(st, ent.getName()); 94 } catch (SQLException pkEx) { 95 exception = processSQLException(pkEx, exception); 96 } finally { 97 try { 100 String unlockString = "UNLOCK TABLES"; 101 QueryLogger.logQuery(QueryLogger.DEFAULT_LOG_LEVEL, unlockString, Collections.EMPTY_LIST); 102 st.execute(unlockString); 103 } catch (SQLException unlockEx) { 104 exception = processSQLException(unlockEx, exception); 105 } finally { 106 try { 108 st.close(); 109 } catch (SQLException stClosingEx) { 110 } 112 } 113 } 114 } catch (SQLException otherEx) { 115 exception = processSQLException(otherEx, exception); 116 } finally { 117 try { 118 con.close(); 119 } catch (SQLException closingEx) { 120 } 122 } 123 124 if (exception != null) { 126 throw exception; 127 } 128 129 return pk; 130 } 131 132 136 protected SQLException processSQLException(SQLException exception, SQLException parent) { 137 if(parent == null) { 138 return exception; 139 } 140 141 parent.setNextException(exception); 142 return parent; 143 } 144 145 protected String pkTableCreateString() { 146 StringBuffer buf = new StringBuffer (); 147 buf 148 .append("CREATE TABLE AUTO_PK_SUPPORT (") 149 .append(" TABLE_NAME CHAR(100) NOT NULL,") 150 .append(" NEXT_ID INTEGER NOT NULL, UNIQUE (TABLE_NAME)") 151 .append(")"); 152 153 return buf.toString(); 154 } 155 156 protected int getPrimaryKey(Statement statement, String entityName) 157 throws SQLException { 158 String lockString = "LOCK TABLES AUTO_PK_SUPPORT WRITE"; 160 QueryLogger.logQuery(QueryLogger.DEFAULT_LOG_LEVEL, lockString, Collections.EMPTY_LIST); 161 statement.execute(lockString); 162 163 int pk = -1; 165 166 String selectString = super.pkSelectString(entityName); 167 QueryLogger.logQuery(QueryLogger.DEFAULT_LOG_LEVEL, selectString, Collections.EMPTY_LIST); 168 ResultSet rs = statement.executeQuery(selectString); 169 try { 170 if(!rs.next()) { 171 throw new SQLException ("No rows for '" + entityName + "'"); 172 } 173 174 pk = rs.getInt(1); 175 176 if(rs.next()) { 177 throw new SQLException ("More than one row for '" + entityName + "'"); 178 } 179 } finally { 180 try { 181 rs.close(); 182 } catch (Exception ex) { 183 } 185 } 186 187 String updateString = super.pkUpdateString(entityName) + " AND NEXT_ID = " + pk; 189 QueryLogger.logQuery(QueryLogger.DEFAULT_LOG_LEVEL, updateString, Collections.EMPTY_LIST); 190 int updated = statement.executeUpdate(updateString); 191 if(updated != 1) { 193 throw new SQLException ("Error updating PK count '" + entityName + "': " + updated); 194 } 195 196 return pk; 197 } 198 } | Popular Tags |