Skip to content

Commit

Permalink
PostgreSQL Compatibility
Browse files Browse the repository at this point in the history
- made a few changes to make UniTime work on PostgreSQL, namely

- booleans cannot be compared with numbers (most of the changes)
  - in HQL queries all the boolean comparisons must use boolean constantes
    (e.g., instead of CourseOffering.isControl = 1, use CourseOffering.isControl = true)
  - in .hbm.xml formulas, use %TRUE% and %FALSE% that can be replaced based on the dialect
- custom XML types do not work
  - the remaining two cases replaced by generic types with the XML reading/writing done in the model
- strings cannot be compared with numbers
  - when matching on unqiue ids, parameters need to be Longs (there was one case when strings were provided)
- select distinct cannot have order by unless it is ordered by the same expression (one case corrected)
- HibernateUtil extended to allow for PostgreSQL9Dialect
  - mostly custom functions for adding dates, binary and, and the day of week
  • Loading branch information
tomas-muller committed Oct 24, 2019
1 parent 9ed57c6 commit 9ed26f6
Show file tree
Hide file tree
Showing 29 changed files with 138 additions and 76 deletions.
3 changes: 1 addition & 2 deletions JavaSource/ChangeLog.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@
<property
name="detail"
column="detail"
type="org.unitime.commons.hibernate.blob.XmlBlobType"
length="65535"/>
type="byte[]"/>

</class>
</hibernate-mapping>
2 changes: 1 addition & 1 deletion JavaSource/Event.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
type="java.lang.Long"
not-null="false"
lazy="true"
formula=" ( select sa.department_uniqueid from %SCHEMA%.class_ c, %SCHEMA%.scheduling_subpart ss, %SCHEMA%.instr_offering_config ioc, %SCHEMA%.instructional_offering io, %SCHEMA%.course_offering co, %SCHEMA%.subject_area sa where c.uniqueid = class_id and ss.uniqueid = c.subpart_id and ioc.uniqueid = ss.config_id and io.uniqueid = ioc.instr_offr_id and co.instr_offr_id = io.uniqueid and co.is_control = 1 and sa.uniqueid = co.subject_area_id ) "
formula=" ( select sa.department_uniqueid from %SCHEMA%.class_ c, %SCHEMA%.scheduling_subpart ss, %SCHEMA%.instr_offering_config ioc, %SCHEMA%.instructional_offering io, %SCHEMA%.course_offering co, %SCHEMA%.subject_area sa where c.uniqueid = class_id and ss.uniqueid = c.subpart_id and ioc.uniqueid = ss.config_id and io.uniqueid = ioc.instr_offr_id and co.instr_offr_id = io.uniqueid and co.is_control = %TRUE% and sa.uniqueid = co.subject_area_id ) "
/>

<set
Expand Down
5 changes: 2 additions & 3 deletions JavaSource/ItypeDesc.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@
<property
name="basic"
column="basic"
type="java.lang.Integer"
not-null="false"
length="1"/>
type="java.lang.Boolean"
not-null="false"/>

<property
name="organized"
Expand Down
2 changes: 1 addition & 1 deletion JavaSource/PitClass.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
name="controllingDept"
class="Department"
lazy="false"
formula=" ( select sa.department_uniqueid from %SCHEMA%.pit_sched_subpart ss, %SCHEMA%.pit_instr_offer_config ioc, %SCHEMA%.pit_instr_offering io, %SCHEMA%.pit_course_offering co, %SCHEMA%.subject_area sa where ss.uniqueid = pit_subpart_id and ioc.uniqueid = ss.pit_config_id and io.uniqueid = ioc.pit_instr_offr_id and co.pit_instr_offr_id = io.uniqueid and co.is_control = 1 and sa.uniqueid = co.subject_area_id ) "/>
formula=" ( select sa.department_uniqueid from %SCHEMA%.pit_sched_subpart ss, %SCHEMA%.pit_instr_offer_config ioc, %SCHEMA%.pit_instr_offering io, %SCHEMA%.pit_course_offering co, %SCHEMA%.subject_area sa where ss.uniqueid = pit_subpart_id and ioc.uniqueid = ss.pit_config_id and io.uniqueid = ioc.pit_instr_offr_id and co.pit_instr_offr_id = io.uniqueid and co.is_control = %TRUE% and sa.uniqueid = co.subject_area_id ) "/>

<many-to-one
name="clazz"
Expand Down
2 changes: 1 addition & 1 deletion JavaSource/PitSchedulingSubpart.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<property
name="courseName"
type="java.lang.String"
formula="(select concat( concat( sa.subject_area_abbreviation , ' ') , co.course_nbr) from %SCHEMA%.pit_sched_subpart s, %SCHEMA%.pit_instr_offer_config c, %SCHEMA%.pit_instr_offering io, %SCHEMA%.pit_course_offering co, %SCHEMA%.subject_area sa where s.uniqueid=uniqueid and s.pit_config_id=c.uniqueid and c.pit_instr_offr_id=io.uniqueid and co.is_control=1 and co.pit_instr_offr_id=io.uniqueid and co.subject_area_id=sa.uniqueid)"
formula="(select concat( concat( sa.subject_area_abbreviation , ' ') , co.course_nbr) from %SCHEMA%.pit_sched_subpart s, %SCHEMA%.pit_instr_offer_config c, %SCHEMA%.pit_instr_offering io, %SCHEMA%.pit_course_offering co, %SCHEMA%.subject_area sa where s.uniqueid=uniqueid and s.pit_config_id=c.uniqueid and c.pit_instr_offr_id=io.uniqueid and co.is_control = %TRUE% and co.pit_instr_offr_id=io.uniqueid and co.subject_area_id=sa.uniqueid)"
update="false"
insert="false"/>

Expand Down
8 changes: 4 additions & 4 deletions JavaSource/PreferenceGroup.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<property
name="courseName"
type="java.lang.String"
formula="(select concat( concat( sa.subject_area_abbreviation , ' ') , co.course_nbr) from %SCHEMA%.scheduling_subpart s, %SCHEMA%.instr_offering_config c, %SCHEMA%.instructional_offering io, %SCHEMA%.course_offering co, %SCHEMA%.subject_area sa where s.uniqueid=uniqueid and s.config_id=c.uniqueid and c.instr_offr_id=io.uniqueid and co.is_control=1 and co.instr_offr_id=io.uniqueid and co.subject_area_id=sa.uniqueid)"
formula="(select concat( concat( sa.subject_area_abbreviation , ' ') , co.course_nbr) from %SCHEMA%.scheduling_subpart s, %SCHEMA%.instr_offering_config c, %SCHEMA%.instructional_offering io, %SCHEMA%.course_offering co, %SCHEMA%.subject_area sa where s.uniqueid=uniqueid and s.config_id=c.uniqueid and c.instr_offr_id=io.uniqueid and co.is_control = %TRUE% and co.instr_offr_id=io.uniqueid and co.subject_area_id=sa.uniqueid)"
update="false"
insert="false"/>

Expand All @@ -102,7 +102,7 @@
name="controllingCourseOffering"
class="CourseOffering"
lazy="proxy"
formula="(select co.uniqueid from %SCHEMA%.scheduling_subpart s, %SCHEMA%.instr_offering_config c, %SCHEMA%.instructional_offering io, %SCHEMA%.course_offering co where s.uniqueid=UNIQUEID and s.config_id=c.uniqueid and c.instr_offr_id=io.uniqueid and co.is_control=1 and co.instr_offr_id=io.uniqueid)"
formula="(select co.uniqueid from %SCHEMA%.scheduling_subpart s, %SCHEMA%.instr_offering_config c, %SCHEMA%.instructional_offering io, %SCHEMA%.course_offering co where s.uniqueid=UNIQUEID and s.config_id=c.uniqueid and c.instr_offr_id=io.uniqueid and co.is_control = %TRUE% and co.instr_offr_id=io.uniqueid)"
/>
-->
<many-to-one
Expand Down Expand Up @@ -227,7 +227,7 @@
name="controllingDept"
class="Department"
lazy="false"
formula=" ( select sa.department_uniqueid from %SCHEMA%.scheduling_subpart ss, %SCHEMA%.instr_offering_config ioc, %SCHEMA%.instructional_offering io, %SCHEMA%.course_offering co, %SCHEMA%.subject_area sa where ss.uniqueid = subpart_id and ioc.uniqueid = ss.config_id and io.uniqueid = ioc.instr_offr_id and co.instr_offr_id = io.uniqueid and co.is_control = 1 and sa.uniqueid = co.subject_area_id ) "/>
formula=" ( select sa.department_uniqueid from %SCHEMA%.scheduling_subpart ss, %SCHEMA%.instr_offering_config ioc, %SCHEMA%.instructional_offering io, %SCHEMA%.course_offering co, %SCHEMA%.subject_area sa where ss.uniqueid = subpart_id and ioc.uniqueid = ss.config_id and io.uniqueid = ioc.instr_offr_id and co.instr_offr_id = io.uniqueid and co.is_control = %TRUE% and sa.uniqueid = co.subject_area_id ) "/>

<many-to-one
name="managingDept"
Expand Down Expand Up @@ -356,7 +356,7 @@
class="org.unitime.timetable.model.Assignment"
not-null="false"
lazy="proxy"
formula=" ( select a.uniqueid from %SCHEMA%.assignment a, %SCHEMA%.solution s, %SCHEMA%.department d, %SCHEMA%.solver_group g where a.class_id=uniqueid and a.solution_id=s.uniqueid and s.commited=1 and d.uniqueid=managing_dept and s.owner_id=g.uniqueid and d.solver_group_id=g.uniqueid ) "/>
formula=" ( select a.uniqueid from %SCHEMA%.assignment a, %SCHEMA%.solution s, %SCHEMA%.department d, %SCHEMA%.solver_group g where a.class_id=uniqueid and a.solution_id=s.uniqueid and s.commited = %TRUE% and d.uniqueid=managing_dept and s.owner_id=g.uniqueid and d.solver_group_id=g.uniqueid ) "/>

<!--
<one-to-one name="sectioningInfo" class="SectioningInfo" property-ref="clazz" cascade="all"/>
Expand Down
5 changes: 2 additions & 3 deletions JavaSource/StudentSectHistory.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@
<property
name="data"
column="data"
type="org.unitime.commons.hibernate.blob.XmlBlobType"
not-null="true"
length="65535"/>
type="byte[]"
not-null="true"/>

<property
name="type"
Expand Down
4 changes: 2 additions & 2 deletions JavaSource/StudentSectQueue.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
length="10"/>

<property
name="message"
name="data"
column="message"
type="org.unitime.commons.hibernate.blob.XmlClobType"
type="java.lang.String"
not-null="false"/>

</class>
Expand Down
68 changes: 50 additions & 18 deletions JavaSource/org/unitime/commons/hibernate/util/HibernateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.PostgreSQL9Dialect;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
Expand Down Expand Up @@ -129,23 +130,32 @@ public static String getProperty(Properties properties, String name) {
return value;
}

public static void fixSchemaInFormulas(Configuration cfg) {
public static void fixSchemaInFormulas(Configuration cfg) throws ClassNotFoundException {
cfg.buildMappings();
String schema = cfg.getProperty("default_schema");
if (schema!=null) {
for (Iterator i=cfg.getClassMappings();i.hasNext();) {
PersistentClass pc = (PersistentClass)i.next();
for (Iterator j=pc.getPropertyIterator();j.hasNext();) {
Property p = (Property)j.next();
for (Iterator k=p.getColumnIterator();k.hasNext();) {
Selectable c = (Selectable)k.next();
if (c instanceof Formula) {
Formula f = (Formula)c;
if (f.getFormula()!=null && f.getFormula().indexOf("%SCHEMA%")>=0) {
f.setFormula(f.getFormula().replaceAll("%SCHEMA%", schema));
sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
}
Class dialect = Class.forName(cfg.getProperty("dialect"));
String schema = cfg.getProperty("default_schema");
for (Iterator i=cfg.getClassMappings();i.hasNext();) {
PersistentClass pc = (PersistentClass)i.next();
for (Iterator j=pc.getPropertyIterator();j.hasNext();) {
Property p = (Property)j.next();
for (Iterator k=p.getColumnIterator();k.hasNext();) {
Selectable c = (Selectable)k.next();
if (c instanceof Formula) {
Formula f = (Formula)c;
boolean updated = false;
if (schema != null && f.getFormula() != null && f.getFormula().indexOf("%SCHEMA%")>=0) {
f.setFormula(f.getFormula().replaceAll("%SCHEMA%", schema));
sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
}
if (f.getFormula()!=null && (f.getFormula().indexOf("%TRUE%")>=0 || f.getFormula().indexOf("%FALSE%")>=0)) {
if (isPostgress(dialect)) {
f.setFormula(f.getFormula().replaceAll("%TRUE%", "'t'").replaceAll("%FALSE%", "'f'"));
} else {
f.setFormula(f.getFormula().replaceAll("%TRUE%", "1").replaceAll("%FALSE%", "0"));
}
}
if (updated)
sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
}
}
}
Expand Down Expand Up @@ -271,6 +281,8 @@ void setSF(SessionFactory fact) {
addBitwiseOperationsToDialect();
sLog.debug(" -- bitwise operation added to the dialect if needed");

addAddDateToDialect();

DatabaseUpdate.update();
}

Expand Down Expand Up @@ -449,8 +461,16 @@ public static boolean isOracle() {
return Oracle8iDialect.class.isAssignableFrom(getDialect());
}

public static boolean isPostgress() {
return PostgreSQL9Dialect.class.isAssignableFrom(getDialect());
}

public static boolean isPostgress(Class dialect) {
return PostgreSQL9Dialect.class.isAssignableFrom(dialect);
}

public static String addDate(String dateSQL, String incrementSQL) {
if (isMySQL())
if (isMySQL() || isPostgress())
return "adddate("+dateSQL+","+incrementSQL+")";
else
return dateSQL+(incrementSQL.startsWith("+")||incrementSQL.startsWith("-")?"":"+")+incrementSQL;
Expand All @@ -459,12 +479,14 @@ public static String addDate(String dateSQL, String incrementSQL) {
public static String dayOfWeek(String field) {
if (isOracle())
return "(trunc(" + field + ") - trunc(" + field + ", 'IW'))";
else if (isPostgress())
return "extract(isodow from " + field + ") - 1";
else
return "weekday(" + field + ")";
}

public static String date(Date date) {
if (isOracle())
if (isOracle() || isPostgress())
return "to_date('" + new SimpleDateFormat("yyyy-MM-dd").format(date) + "', 'YYYY-MM-DD')";
else
return "str_to_date('" + new SimpleDateFormat("yyyy-MM-dd").format(date) + "', '%Y-%m-%d')";
Expand All @@ -475,9 +497,19 @@ public static void addBitwiseOperationsToDialect() {
Dialect dialect = hibSessionFactory.getDialect();
if (!dialect.getFunctions().containsKey("bit_and")) {
if (isOracle())
dialect.getFunctions().put("bit_and", new StandardSQLFunction("bitand", IntegerType.INSTANCE));
dialect.getFunctions().put("bit_and", new StandardSQLFunction("bitand", IntegerType.INSTANCE));
else if (isPostgress())
dialect.getFunctions().put("bit_and", new SQLFunctionTemplate(IntegerType.INSTANCE, "cast(?1 as int) & cast(?2 as int)"));
else
dialect.getFunctions().put("bit_and", new SQLFunctionTemplate(IntegerType.INSTANCE, "?1 & ?2"));
}
}

public static void addAddDateToDialect() {
SessionFactoryImplementor hibSessionFactory = (SessionFactoryImplementor)new _RootDAO().getSession().getSessionFactory();
Dialect dialect = hibSessionFactory.getDialect();
if (isPostgress() && !dialect.getFunctions().containsKey("adddate")) {
dialect.getFunctions().put("adddate", new SQLFunctionTemplate(IntegerType.INSTANCE, "?1 + (?2) * interval '1 day'"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private void doDelete(DepartmentEditForm frm) throws Exception{
hibSession.createQuery(
"delete StudentClassEnrollment e where e.clazz.uniqueId in " +
"(select c.uniqueId from Class_ c, CourseOffering co where " +
"co.isControl=1 and " +
"co.isControl=true and " +
"c.schedulingSubpart.instrOfferingConfig.instructionalOffering=co.instructionalOffering and "+
"co.subjectArea.department.uniqueId=:deptId)").
setLong("deptId", department.getUniqueId()).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private void doLoad(
label += " [" + ioc.getName() + "]";
frm.setInstructionalTypeLabel(label);
frm.setUnlimitedEnroll(ioc.isUnlimitedEnrollment());
frm.setItypeBasic(ss.getItype()==null || ss.getItype().getBasic()==1);
frm.setItypeBasic(ss.getItype()==null || ss.getItype().getBasic());
if (!frm.getItypeBasic())
LookupTables.setupItypes(request, false);
frm.setSubjectArea(co.getSubjectAreaAbbv());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ private void doLoad(
label += " [" + ioc.getName() + "]";
frm.setInstructionalTypeLabel(label);
frm.setUnlimitedEnroll(ioc.isUnlimitedEnrollment());
frm.setItypeBasic(ss.getItype()==null || ss.getItype().getBasic()==1);
frm.setItypeBasic(ss.getItype()==null || ss.getItype().getBasic());
if (!frm.getItypeBasic())
LookupTables.setupItypes(request, false);
frm.setSubjectArea(co.getSubjectAreaAbbv());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,12 +597,13 @@ private Class_ findClassFromExternalId(String externalId, String year, String te
}

return((Class_) getHibSession().
createQuery("select c from Class_ as c inner join c.schedulingSubpart.instrOfferingConfig.instructionalOffering.session as s where s.academicInitiative = :academicInititive and s.academicYear = :aYear and s.academicTerm = :aTerm and c.externalUniqueId = :anExternalId and rownum = 1").
createQuery("select c from Class_ as c inner join c.schedulingSubpart.instrOfferingConfig.instructionalOffering.session as s where s.academicInitiative = :academicInititive and s.academicYear = :aYear and s.academicTerm = :aTerm and c.externalUniqueId = :anExternalId").
setString("academicInititive", academicInitiative).
setString("aYear", year).
setString("aTerm", term).
setString("anExternalId", externalId).
setCacheable(true).
setMaxResults(1).
uniqueResult());

}
Expand All @@ -613,12 +614,13 @@ private InstructionalOffering findInstructionalOfferingFromExternalId(String ext
}

return((InstructionalOffering) getHibSession().
createQuery("select io from InstructionalOffering as io inner join io.session as s where s.academicInitiative = :academicInititive and s.academicYear = :aYear and s.academicTerm = :aTerm and io.externalUniqueId = :anExternalId and rownum = 1").
createQuery("select io from InstructionalOffering as io inner join io.session as s where s.academicInitiative = :academicInititive and s.academicYear = :aYear and s.academicTerm = :aTerm and io.externalUniqueId = :anExternalId").
setString("academicInititive", academicInitiative).
setString("aYear", year).
setString("aTerm", term).
setString("anExternalId", externalId).
setCacheable(true).
setMaxResults(1).
uniqueResult());

}
Expand All @@ -629,12 +631,13 @@ private CourseOffering findCourseOfferingFromExternalId(String externalId, Strin
}

return((CourseOffering) getHibSession().
createQuery("select co from CourseOffering as co inner join co.instructionalOffering.session as s where s.academicInitiative = :academicInititive and s.academicYear = :aYear and s.academicTerm = :aTerm and co.externalUniqueId = :anExternalId and rownum = 1").
createQuery("select co from CourseOffering as co inner join co.instructionalOffering.session as s where s.academicInitiative = :academicInititive and s.academicYear = :aYear and s.academicTerm = :aTerm and co.externalUniqueId = :anExternalId").
setString("academicInititive", academicInitiative).
setString("aYear", year).
setString("aTerm", term).
setString("anExternalId", externalId).
setCacheable(true).
setMaxResults(1).
uniqueResult());

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ else if (!allRooms)
int id = 0;
for (String s: ids) {
list += (list.isEmpty() ? "" : ", ") + ":Xi" + id;
query.addParameter("id", "Xi" + id, s);
query.addParameter("id", "Xi" + id, Long.valueOf(s));
id++;
}
query.addWhere("id", "l.uniqueId in (" + list + ")");
Expand Down
4 changes: 2 additions & 2 deletions JavaSource/org/unitime/timetable/form/ItypeDescEditForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void load(ItypeDesc itype) {
setAbbreviation(itype.getAbbv());
setName(itype.getDesc());
setReference(itype.getSis_ref());
setBasicType(itype.getBasic());
setBasicType(itype.getBasic() ? 1 : 0);
setParent(itype.getParent()==null?null:itype.getParent().getItype());
setOrganized(itype.isOrganized());
}
Expand All @@ -139,7 +139,7 @@ public void saveOrUpdate(org.hibernate.Session hibSession) throws Exception {
itype.setAbbv(getAbbreviation());
itype.setDesc(getName());
itype.setSis_ref(getReference());
itype.setBasic(getBasicType());
itype.setBasic(getBasicType() == 1);
itype.setParent(getParent()==null?null:new ItypeDescDAO().get(getParent()));
itype.setOrganized(getOrganized());
hibSession.saveOrUpdate(itype);
Expand Down
Loading

0 comments on commit 9ed26f6

Please sign in to comment.