1 56 57 package org.objectstyle.cayenne.dba.sybase; 58 59 import java.sql.CallableStatement ; 60 import java.sql.Connection ; 61 import java.sql.ResultSet ; 62 import java.util.ArrayList ; 63 import java.util.List ; 64 65 import org.objectstyle.cayenne.CayenneRuntimeException; 66 import org.objectstyle.cayenne.access.DataNode; 67 import org.objectstyle.cayenne.dba.JdbcPkGenerator; 68 import org.objectstyle.cayenne.map.DbEntity; 69 70 77 public class SybasePkGenerator extends JdbcPkGenerator { 78 79 116 public void createAutoPk(DataNode node, List dbEntities) throws Exception { 117 super.createAutoPk(node, dbEntities); 118 super.runUpdate(node, safePkProcDrop()); 119 super.runUpdate(node, unsafePkProcCreate()); 120 } 121 122 123 public List createAutoPkStatements(List dbEntities) { 124 List list = super.createAutoPkStatements(dbEntities); 125 126 list.add(safePkProcDrop()); 128 129 list.add(unsafePkProcCreate()); 131 132 return list; 133 } 134 135 136 156 public void dropAutoPk(DataNode node, List dbEntities) throws Exception { 157 super.runUpdate(node, safePkProcDrop()); 158 super.runUpdate(node, safePkTableDrop()); 159 } 160 161 public List dropAutoPkStatements(List dbEntities) { 162 List list = new ArrayList (); 163 list.add(safePkProcDrop()); 164 list.add(safePkTableDrop()); 165 return list; 166 } 167 168 protected int pkFromDatabase(DataNode node, DbEntity ent) throws Exception { 169 Connection connection = node.getDataSource().getConnection(); 170 try { 171 CallableStatement statement = connection 172 .prepareCall("{call auto_pk_for_table(?, ?)}"); 173 try { 174 statement.setString(1, ent.getName()); 175 statement.setInt(2, super.getPkCacheSize()); 176 177 statement.execute(); 180 if (statement.getMoreResults()) { 181 ResultSet rs = statement.getResultSet(); 182 183 try { 184 if (rs.next()) { 185 return rs.getInt(1); 186 } 187 else { 188 throw new CayenneRuntimeException( 189 "Error generating pk for DbEntity " + ent.getName()); 190 } 191 } 192 finally { 193 rs.close(); 194 } 195 } 196 else { 197 throw new CayenneRuntimeException("Error generating pk for DbEntity " 198 + ent.getName() 199 + ", no result set from stored procedure."); 200 } 201 } 202 finally { 203 statement.close(); 204 } 205 } 206 finally { 207 connection.close(); 208 } 209 } 210 211 212 private String safePkTableDrop() { 213 StringBuffer buf = new StringBuffer (); 214 buf 215 .append("if exists (SELECT * FROM sysobjects WHERE name = 'AUTO_PK_SUPPORT')") 216 .append(" BEGIN ") 217 .append(" DROP TABLE AUTO_PK_SUPPORT") 218 .append(" END"); 219 220 return buf.toString(); 221 } 222 223 private String unsafePkProcCreate() { 224 StringBuffer buf = new StringBuffer (); 225 buf 226 .append(" CREATE PROCEDURE auto_pk_for_table @tname VARCHAR(32), @pkbatchsize INT AS") 227 .append(" BEGIN") 228 .append(" BEGIN TRANSACTION") 229 .append(" UPDATE AUTO_PK_SUPPORT set NEXT_ID = NEXT_ID + @pkbatchsize") 230 .append(" WHERE TABLE_NAME = @tname") 231 .append(" SELECT NEXT_ID FROM AUTO_PK_SUPPORT WHERE TABLE_NAME = @tname") 232 .append(" COMMIT") 233 .append(" END"); 234 return buf.toString(); 235 } 236 237 private String safePkProcDrop() { 238 StringBuffer buf = new StringBuffer (); 239 buf 240 .append("if exists (SELECT * FROM sysobjects WHERE name = 'auto_pk_for_table')") 241 .append(" BEGIN") 242 .append(" DROP PROCEDURE auto_pk_for_table") 243 .append(" END"); 244 return buf.toString(); 245 } 246 247 } | Popular Tags |