1 18 package org.objectweb.jorm.facility.naming.rdbsequence; 19 20 import org.objectweb.jorm.api.PException; 21 import org.objectweb.jorm.api.PExceptionIO; 22 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter; 23 import org.objectweb.jorm.mapper.rdb.lib.RdbConnectionWrapper; 24 import org.objectweb.util.monolog.api.Logger; 25 26 import java.sql.Connection ; 27 import java.sql.Statement ; 28 import java.sql.SQLException ; 29 import java.sql.ResultSet ; 30 31 37 public class RdbSequenceHelper { 38 39 42 private String nextValRequest; 43 44 48 private RdbAdapter adapter; 49 50 53 private String seqName; 54 55 60 private Integer seqStart = null; 61 62 67 private Integer seqIncrement = null; 68 69 74 private Integer seqCache = null; 75 76 private boolean created; 77 78 private Logger logger; 79 80 public RdbSequenceHelper() { 81 } 82 83 88 public RdbSequenceHelper(RdbAdapter adapter, 89 String seqName, 90 boolean sequenceCreated) { 91 this(); 92 setAdapter(adapter); 93 setSequenceName(seqName); 94 setSequenceCreated(sequenceCreated); 95 } 96 97 public void setSequenceCreated(boolean sequencecreated) { 98 this.created = sequencecreated; 99 } 100 101 public boolean isSequenceCreated() { 102 return created; 103 } 104 105 public void setSequenceName(String seqName) { 106 this.seqName = seqName; 107 } 108 109 public String getSequenceName() { 110 return seqName; 111 } 112 113 public RdbAdapter getAdapter() { 114 return adapter; 115 } 116 117 public void setAdapter(RdbAdapter adapter) { 118 this.adapter = adapter; 119 } 120 121 public Integer getSequenceStart() { 122 return seqStart; 123 } 124 125 public void setSequenceStart(Integer seqStart) { 126 this.seqStart = seqStart; 127 } 128 129 public Integer getSequenceIncrement() { 130 return seqIncrement; 131 } 132 133 public void setSequenceIncrement(Integer seqIncrement) { 134 this.seqIncrement = seqIncrement; 135 } 136 137 public Integer getSequenceCache() { 138 return seqCache; 139 } 140 141 public void setSequenceCache(Integer seqCache) { 142 this.seqCache = seqCache; 143 } 144 145 public Logger getLogger() { 146 return logger; 147 } 148 149 public void setLogger(Logger logger) { 150 this.logger = logger; 151 } 152 153 160 public synchronized boolean createSequence(Object c) throws PException { 161 if (created) { 162 return false; 163 } 164 if (seqName == null) { 165 throw new PException("No sequence name specified"); 166 } 167 if (adapter == null) { 168 throw new PException("No RdbAdapter specified"); 169 } 170 Connection conn = RdbConnectionWrapper.narrow2SQL(c); 171 created = true; 172 Statement s = null; 173 try { 174 if (!adapter.existSequence(conn, seqName)) { 175 s = conn.createStatement(); 176 s.execute(adapter.getCreateSequence( 177 seqName, seqStart, seqIncrement, seqCache)); 178 return true; 179 } else { 180 return false; 181 } 182 } catch (SQLException e) { 183 created = false; 184 throw new PExceptionIO(e, "Impossible to create the sequence '" 185 + seqName + "'."); 186 } finally { 187 if (s != null) { 188 try { 189 s.close(); 190 } catch (SQLException e) { 191 } 192 } 193 } 194 } 195 196 public long allocateId(Object c) throws PException { 197 if (nextValRequest == null) { 198 if (seqName == null) { 199 throw new PException("No sequence name specified"); 200 } 201 if (adapter == null) { 202 throw new PException("No RdbAdapter specified"); 203 } 204 if (!created) { 205 throw new PException("Protocol Error: You have to ask the sequence creation before id allocation"); 206 } 207 nextValRequest = adapter.getNextValInSequence(seqName); 208 } 209 Connection conn = RdbConnectionWrapper.narrow2SQL(c); 210 Statement s = null; 211 ResultSet rs = null; 212 long res = -1; 213 try { 214 s = conn.createStatement(); 215 rs = s.executeQuery(nextValRequest); 216 if (rs.next()) { 217 res = rs.getLong(1); 218 } else { 219 throw new PExceptionIO("Strong case: The nextval operation does " + 220 "not return any value!"); 221 } 222 } catch (SQLException e) { 223 throw new PExceptionIO(e, "Impossible to allocate a new identifier" 224 + " on the sequence: " + seqName); 225 } finally { 226 try { 227 if (rs != null) { 228 rs.close(); 229 } 230 if (s != null) { 231 s.close(); 232 } 233 } catch (SQLException e) { 234 } 235 } 236 return res; 237 } 238 } 239 | Popular Tags |