1 19 20 21 package org.apache.cayenne.dba.sybase; 22 23 import java.sql.CallableStatement ; 24 import java.sql.Connection ; 25 import java.sql.ResultSet ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 29 import org.apache.cayenne.CayenneRuntimeException; 30 import org.apache.cayenne.access.DataNode; 31 import org.apache.cayenne.access.Transaction; 32 import org.apache.cayenne.dba.JdbcPkGenerator; 33 import org.apache.cayenne.map.DbEntity; 34 35 42 public class SybasePkGenerator extends JdbcPkGenerator { 43 44 81 public void createAutoPk(DataNode node, List dbEntities) throws Exception { 82 super.createAutoPk(node, dbEntities); 83 super.runUpdate(node, safePkProcDrop()); 84 super.runUpdate(node, unsafePkProcCreate()); 85 } 86 87 88 public List createAutoPkStatements(List dbEntities) { 89 List list = super.createAutoPkStatements(dbEntities); 90 91 list.add(safePkProcDrop()); 93 94 list.add(unsafePkProcCreate()); 96 97 return list; 98 } 99 100 101 121 public void dropAutoPk(DataNode node, List dbEntities) throws Exception { 122 super.runUpdate(node, safePkProcDrop()); 123 super.runUpdate(node, safePkTableDrop()); 124 } 125 126 public List dropAutoPkStatements(List dbEntities) { 127 List list = new ArrayList (); 128 list.add(safePkProcDrop()); 129 list.add(safePkTableDrop()); 130 return list; 131 } 132 133 protected int pkFromDatabase(DataNode node, DbEntity ent) throws Exception { 134 137 142 Transaction transaction = Transaction.getThreadTransaction(); 143 Transaction.bindThreadTransaction(null); 144 145 try { 146 147 Connection connection = node.getDataSource().getConnection(); 148 try { 149 CallableStatement statement = connection 150 .prepareCall("{call auto_pk_for_table(?, ?)}"); 151 try { 152 statement.setString(1, ent.getName()); 153 statement.setInt(2, super.getPkCacheSize()); 154 155 statement.execute(); 158 if (statement.getMoreResults()) { 159 ResultSet rs = statement.getResultSet(); 160 161 try { 162 if (rs.next()) { 163 return rs.getInt(1); 164 } 165 else { 166 throw new CayenneRuntimeException( 167 "Error generating pk for DbEntity " 168 + ent.getName()); 169 } 170 } 171 finally { 172 rs.close(); 173 } 174 } 175 else { 176 throw new CayenneRuntimeException( 177 "Error generating pk for DbEntity " 178 + ent.getName() 179 + ", no result set from stored procedure."); 180 } 181 } 182 finally { 183 statement.close(); 184 } 185 } 186 finally { 187 connection.close(); 188 } 189 } 190 finally { 191 Transaction.bindThreadTransaction(transaction); 192 } 193 } 194 195 196 private String safePkTableDrop() { 197 StringBuffer buf = new StringBuffer (); 198 buf 199 .append("if exists (SELECT * FROM sysobjects WHERE name = 'AUTO_PK_SUPPORT')") 200 .append(" BEGIN ") 201 .append(" DROP TABLE AUTO_PK_SUPPORT") 202 .append(" END"); 203 204 return buf.toString(); 205 } 206 207 private String unsafePkProcCreate() { 208 StringBuffer buf = new StringBuffer (); 209 buf 210 .append(" CREATE PROCEDURE auto_pk_for_table @tname VARCHAR(32), @pkbatchsize INT AS") 211 .append(" BEGIN") 212 .append(" BEGIN TRANSACTION") 213 .append(" UPDATE AUTO_PK_SUPPORT set NEXT_ID = NEXT_ID + @pkbatchsize") 214 .append(" WHERE TABLE_NAME = @tname") 215 .append(" SELECT NEXT_ID FROM AUTO_PK_SUPPORT WHERE TABLE_NAME = @tname") 216 .append(" COMMIT") 217 .append(" END"); 218 return buf.toString(); 219 } 220 221 private String safePkProcDrop() { 222 StringBuffer buf = new StringBuffer (); 223 buf 224 .append("if exists (SELECT * FROM sysobjects WHERE name = 'auto_pk_for_table')") 225 .append(" BEGIN") 226 .append(" DROP PROCEDURE auto_pk_for_table") 227 .append(" END"); 228 return buf.toString(); 229 } 230 231 } 232 | Popular Tags |