Skip to content

Commit

Permalink
fixes #141 only truncate sequence names if on Oracle < 12.2
Browse files Browse the repository at this point in the history
  • Loading branch information
brianbolt committed Feb 18, 2018
1 parent fe8cfe7 commit d7b8d6d
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/main/java/com/labsynch/labseer/domain/LabelSequence.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.labsynch.labseer.domain;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -22,7 +23,6 @@

import org.hibernate.Session;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.engine.jdbc.dialect.internal.StandardDialectResolver;
Expand Down Expand Up @@ -95,21 +95,34 @@ public LabelSequence save() {
}
}
}
EntityManager em = LabelSequence.entityManager();
if (this.getDbSequence() == null) {
//set to temp sequence first so we can persist, get and id and then use the id to create a unique sequence name
this.setDbSequence("tempseq");
this.persist();
String dbSequence = "labelseq_"+this.getId()+"_"+this.getLabelPrefix()+"_"+this.getLabelTypeAndKind()+"_"+this.getThingTypeAndKind();
dbSequence = dbSequence.replaceAll("[^a-zA-Z0-9_]+", "_");
//Limit to 30 characters to be compatible with Oracle Version <= 12.1
dbSequence = dbSequence.substring(0, 30);
int MAX_CHAR = 30;
int maxLength = (dbSequence.length() < MAX_CHAR)?dbSequence.length():MAX_CHAR;
dbSequence = dbSequence.substring(0, maxLength);
this.setDbSequence(dbSequence);
//Limit to 30 characters to be compatible with Oracle Version <= 12.1 if applicable
String databaseType = null;
Float databaseVersion = 0.0f;
try{
org.hibernate.engine.spi.SessionImplementor sessionImp =
(org.hibernate.engine.spi.SessionImplementor) em.getDelegate();
DatabaseMetaData metadata = sessionImp.connection().getMetaData();
databaseType = metadata.getDatabaseProductName();
databaseVersion = Float.parseFloat(metadata.getDatabaseMajorVersion()+"."+metadata.getDatabaseMinorVersion());
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(databaseType.equalsIgnoreCase("Oracle") && (databaseVersion < 12.2f)) {
int MAX_CHAR = 30;
int maxLength = (dbSequence.length() < MAX_CHAR)?dbSequence.length():MAX_CHAR;
dbSequence = dbSequence.substring(0, maxLength);
this.setDbSequence(dbSequence);
}
}
if (this.getStartingNumber() < 1L) this.setStartingNumber(1L);
EntityManager em = LabelSequence.entityManager();
Query q = em.createNativeQuery("CREATE SEQUENCE "+this.dbSequence+" START WITH "+this.getStartingNumber());
q.executeUpdate();
this.merge();
Expand Down

0 comments on commit d7b8d6d

Please sign in to comment.