Skip to content

Commit db2d7fb

Browse files
committed
PostgreSQL Compatibility
- 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
1 parent 9ed57c6 commit db2d7fb

28 files changed

Lines changed: 126 additions & 75 deletions

JavaSource/ChangeLog.hbm.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@
101101
<property
102102
name="detail"
103103
column="detail"
104-
type="org.unitime.commons.hibernate.blob.XmlBlobType"
105-
length="65535"/>
104+
type="byte[]"/>
106105

107106
</class>
108107
</hibernate-mapping>

JavaSource/Event.hbm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
type="java.lang.Long"
131131
not-null="false"
132132
lazy="true"
133-
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 ) "
133+
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 ) "
134134
/>
135135

136136
<set

JavaSource/ItypeDesc.hbm.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@
5353
<property
5454
name="basic"
5555
column="basic"
56-
type="java.lang.Integer"
57-
not-null="false"
58-
length="1"/>
56+
type="java.lang.Boolean"
57+
not-null="false"/>
5958

6059
<property
6160
name="organized"

JavaSource/PitClass.hbm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
name="controllingDept"
6464
class="Department"
6565
lazy="false"
66-
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 ) "/>
66+
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 ) "/>
6767

6868
<many-to-one
6969
name="clazz"

JavaSource/PitSchedulingSubpart.hbm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<property
7474
name="courseName"
7575
type="java.lang.String"
76-
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)"
76+
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)"
7777
update="false"
7878
insert="false"/>
7979

JavaSource/PreferenceGroup.hbm.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
<property
8888
name="courseName"
8989
type="java.lang.String"
90-
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)"
90+
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)"
9191
update="false"
9292
insert="false"/>
9393

@@ -102,7 +102,7 @@
102102
name="controllingCourseOffering"
103103
class="CourseOffering"
104104
lazy="proxy"
105-
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)"
105+
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)"
106106
/>
107107
-->
108108
<many-to-one
@@ -227,7 +227,7 @@
227227
name="controllingDept"
228228
class="Department"
229229
lazy="false"
230-
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 ) "/>
230+
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 ) "/>
231231

232232
<many-to-one
233233
name="managingDept"
@@ -356,7 +356,7 @@
356356
class="org.unitime.timetable.model.Assignment"
357357
not-null="false"
358358
lazy="proxy"
359-
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 ) "/>
359+
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 ) "/>
360360

361361
<!--
362362
<one-to-one name="sectioningInfo" class="SectioningInfo" property-ref="clazz" cascade="all"/>

JavaSource/StudentSectHistory.hbm.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@
4242
<property
4343
name="data"
4444
column="data"
45-
type="org.unitime.commons.hibernate.blob.XmlBlobType"
46-
not-null="true"
47-
length="65535"/>
45+
type="byte[]"
46+
not-null="true"/>
4847

4948
<property
5049
name="type"

JavaSource/StudentSectQueue.hbm.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
length="10"/>
5353

5454
<property
55-
name="message"
55+
name="data"
5656
column="message"
57-
type="org.unitime.commons.hibernate.blob.XmlClobType"
57+
type="java.lang.String"
5858
not-null="false"/>
5959

6060
</class>

JavaSource/org/unitime/commons/hibernate/util/HibernateUtil.java

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.hibernate.dialect.Dialect;
4040
import org.hibernate.dialect.MySQLDialect;
4141
import org.hibernate.dialect.Oracle8iDialect;
42+
import org.hibernate.dialect.PostgreSQL9Dialect;
4243
import org.hibernate.dialect.function.SQLFunctionTemplate;
4344
import org.hibernate.dialect.function.StandardSQLFunction;
4445
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
@@ -129,23 +130,32 @@ public static String getProperty(Properties properties, String name) {
129130
return value;
130131
}
131132

132-
public static void fixSchemaInFormulas(Configuration cfg) {
133+
public static void fixSchemaInFormulas(Configuration cfg) throws ClassNotFoundException {
133134
cfg.buildMappings();
134-
String schema = cfg.getProperty("default_schema");
135-
if (schema!=null) {
136-
for (Iterator i=cfg.getClassMappings();i.hasNext();) {
137-
PersistentClass pc = (PersistentClass)i.next();
138-
for (Iterator j=pc.getPropertyIterator();j.hasNext();) {
139-
Property p = (Property)j.next();
140-
for (Iterator k=p.getColumnIterator();k.hasNext();) {
141-
Selectable c = (Selectable)k.next();
142-
if (c instanceof Formula) {
143-
Formula f = (Formula)c;
144-
if (f.getFormula()!=null && f.getFormula().indexOf("%SCHEMA%")>=0) {
145-
f.setFormula(f.getFormula().replaceAll("%SCHEMA%", schema));
146-
sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
147-
}
135+
Class dialect = Class.forName(cfg.getProperty("dialect"));
136+
String schema = cfg.getProperty("default_schema");
137+
for (Iterator i=cfg.getClassMappings();i.hasNext();) {
138+
PersistentClass pc = (PersistentClass)i.next();
139+
for (Iterator j=pc.getPropertyIterator();j.hasNext();) {
140+
Property p = (Property)j.next();
141+
for (Iterator k=p.getColumnIterator();k.hasNext();) {
142+
Selectable c = (Selectable)k.next();
143+
if (c instanceof Formula) {
144+
Formula f = (Formula)c;
145+
boolean updated = false;
146+
if (schema != null && f.getFormula() != null && f.getFormula().indexOf("%SCHEMA%")>=0) {
147+
f.setFormula(f.getFormula().replaceAll("%SCHEMA%", schema));
148+
sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
148149
}
150+
if (f.getFormula()!=null && (f.getFormula().indexOf("%TRUE%")>=0 || f.getFormula().indexOf("%FALSE%")>=0)) {
151+
if (isPostgress(dialect)) {
152+
f.setFormula(f.getFormula().replaceAll("%TRUE%", "'t'").replaceAll("%FALSE%", "'f'"));
153+
} else {
154+
f.setFormula(f.getFormula().replaceAll("%TRUE%", "1").replaceAll("%FALSE%", "0"));
155+
}
156+
}
157+
if (updated)
158+
sLog.debug("Schema updated in "+pc.getClassName()+"."+p.getName()+" to "+f.getFormula());
149159
}
150160
}
151161
}
@@ -449,6 +459,14 @@ public static boolean isOracle() {
449459
return Oracle8iDialect.class.isAssignableFrom(getDialect());
450460
}
451461

462+
public static boolean isPostgress() {
463+
return PostgreSQL9Dialect.class.isAssignableFrom(getDialect());
464+
}
465+
466+
public static boolean isPostgress(Class dialect) {
467+
return PostgreSQL9Dialect.class.isAssignableFrom(dialect);
468+
}
469+
452470
public static String addDate(String dateSQL, String incrementSQL) {
453471
if (isMySQL())
454472
return "adddate("+dateSQL+","+incrementSQL+")";
@@ -459,12 +477,14 @@ public static String addDate(String dateSQL, String incrementSQL) {
459477
public static String dayOfWeek(String field) {
460478
if (isOracle())
461479
return "(trunc(" + field + ") - trunc(" + field + ", 'IW'))";
480+
else if (isPostgress())
481+
return "extract(isodow from " + field + ") - 1";
462482
else
463483
return "weekday(" + field + ")";
464484
}
465485

466486
public static String date(Date date) {
467-
if (isOracle())
487+
if (isOracle() || isPostgress())
468488
return "to_date('" + new SimpleDateFormat("yyyy-MM-dd").format(date) + "', 'YYYY-MM-DD')";
469489
else
470490
return "str_to_date('" + new SimpleDateFormat("yyyy-MM-dd").format(date) + "', '%Y-%m-%d')";
@@ -475,7 +495,9 @@ public static void addBitwiseOperationsToDialect() {
475495
Dialect dialect = hibSessionFactory.getDialect();
476496
if (!dialect.getFunctions().containsKey("bit_and")) {
477497
if (isOracle())
478-
dialect.getFunctions().put("bit_and", new StandardSQLFunction("bitand", IntegerType.INSTANCE));
498+
dialect.getFunctions().put("bit_and", new StandardSQLFunction("bitand", IntegerType.INSTANCE));
499+
else if (isPostgress())
500+
dialect.getFunctions().put("bit_and", new SQLFunctionTemplate(IntegerType.INSTANCE, "cast(?1 as int) & cast(?2 as int)"));
479501
else
480502
dialect.getFunctions().put("bit_and", new SQLFunctionTemplate(IntegerType.INSTANCE, "?1 & ?2"));
481503
}

JavaSource/org/unitime/timetable/action/DepartmentEditAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private void doDelete(DepartmentEditForm frm) throws Exception{
188188
hibSession.createQuery(
189189
"delete StudentClassEnrollment e where e.clazz.uniqueId in " +
190190
"(select c.uniqueId from Class_ c, CourseOffering co where " +
191-
"co.isControl=1 and " +
191+
"co.isControl=true and " +
192192
"c.schedulingSubpart.instrOfferingConfig.instructionalOffering=co.instructionalOffering and "+
193193
"co.subjectArea.department.uniqueId=:deptId)").
194194
setLong("deptId", department.getUniqueId()).

0 commit comments

Comments
 (0)