Skip to content

Commit

Permalink
Support EQ(Column,null) to be represented as IS.NULL(Column), re #94
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed May 3, 2024
1 parent abaa7c9 commit 84adcbf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
20 changes: 16 additions & 4 deletions jsql/src/main/java/org/jaxdb/jsql/ComparisonPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static class Eq<V> extends LogicalPredicate<V> {

@Override
final StringBuilder compile(final Compiler compiler, final StringBuilder b, final boolean isForUpdateWhere) {
return b.append('=');
return b.append(this.b == null ? "IS" : "=");
}
}

Expand All @@ -85,7 +85,7 @@ static class Ne<V> extends LogicalPredicate<V> {

@Override
final StringBuilder compile(final Compiler compiler, final StringBuilder b, final boolean isForUpdateWhere) {
return b.append("!=");
return b.append(this.b == null ? "IS NOT" : "!=");
}
}

Expand All @@ -108,6 +108,7 @@ static class Gt<V> extends LogicalPredicate<V> {

@Override
final StringBuilder compile(final Compiler compiler, final StringBuilder b, final boolean isForUpdateWhere) {
super.compile(compiler, b, isForUpdateWhere);
return b.append('>');
}
}
Expand All @@ -131,6 +132,7 @@ static class Lt<V> extends LogicalPredicate<V> {

@Override
final StringBuilder compile(final Compiler compiler, final StringBuilder b, final boolean isForUpdateWhere) {
super.compile(compiler, b, isForUpdateWhere);
return b.append('<');
}
}
Expand All @@ -154,6 +156,7 @@ static class Gte<V> extends LogicalPredicate<V> {

@Override
final StringBuilder compile(final Compiler compiler, final StringBuilder b, final boolean isForUpdateWhere) {
super.compile(compiler, b, isForUpdateWhere);
return b.append(">=");
}
}
Expand All @@ -177,14 +180,15 @@ static class Lte<V> extends LogicalPredicate<V> {

@Override
final StringBuilder compile(final Compiler compiler, final StringBuilder b, final boolean isForUpdateWhere) {
super.compile(compiler, b, isForUpdateWhere);
return b.append("<=");
}
}

private ComparisonPredicate(final type.Column<?> a, final V b) {
super(((Subject)a).getTable());
this.a = (Subject)a;
this.b = org.jaxdb.jsql.data.wrap(b);
this.b = b == null ? null : org.jaxdb.jsql.data.wrap(b);
}

private ComparisonPredicate(final type.Column<?> a, final QuantifiedComparisonPredicate<?> b) {
Expand All @@ -195,7 +199,7 @@ private ComparisonPredicate(final type.Column<?> a, final QuantifiedComparisonPr

private ComparisonPredicate(final V a, final type.Column<?> b) {
super(((Subject)b).getTable());
this.a = org.jaxdb.jsql.data.wrap(a);
this.a = a == null ? null : org.jaxdb.jsql.data.wrap(a);
this.b = (Subject)b;
}

Expand All @@ -221,4 +225,12 @@ final void collectColumns(final ArrayList<data.Column<?>> list) {
collectColumn(list, a);
collectColumn(list, b);
}

@Override
StringBuilder compile(final Compiler compiler, final StringBuilder b, final boolean isForUpdateWhere) {
if (b == null)
throw new IllegalArgumentException("NULL is not allowed for " + this.getClass().getSimpleName());

return null;
}
}
6 changes: 6 additions & 0 deletions jsql/src/main/java/org/jaxdb/jsql/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,9 @@ void compilePredicate(final ComparisonPredicate<?> predicate, final Compilation
unwrapAlias(predicate.a).compile(compilation, true);
sql.append(')');
}
else if (predicate.a == null) {
throw new IllegalArgumentException("lhs is NULL");
}
else {
unwrapAlias(predicate.a).compile(compilation, true);
}
Expand All @@ -835,6 +838,9 @@ void compilePredicate(final ComparisonPredicate<?> predicate, final Compilation
unwrapAlias(predicate.b).compile(compilation, true);
sql.append(')');
}
else if (predicate.b == null) {
compilation.sql.append("NULL");
}
else {
unwrapAlias(predicate.b).compile(compilation, true);
}
Expand Down
6 changes: 5 additions & 1 deletion jsql/src/main/java/org/jaxdb/jsql/PostgreSQLCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,14 @@ private static void toChar(final data.ENUM<?> column, final Compilation compilat
final void compilePredicate(final ComparisonPredicate<?> predicate, final Compilation compilation) throws IOException, SQLException {
final Subject a = predicate.a;
final Subject b = predicate.b;
if (a.getClass() == b.getClass() || (!(a instanceof data.ENUM) && !(b instanceof data.ENUM))) {
if (!(a instanceof data.ENUM) && !(b instanceof data.ENUM) || (a != null && b != null && a.getClass() == b.getClass())) {
super.compilePredicate(predicate, compilation);
}
else {
if (a instanceof data.ENUM)
toChar((data.ENUM<?>)a, compilation);
else if (a == null)
compilation.sql.append("NULL");
else
a.compile(compilation, true);

Expand All @@ -285,6 +287,8 @@ final void compilePredicate(final ComparisonPredicate<?> predicate, final Compil
sql.append(' ');
if (b instanceof data.ENUM)
toChar((data.ENUM<?>)b, compilation);
else if (b == null)
compilation.sql.append("NULL");
else
b.compile(compilation, true);
}
Expand Down

0 comments on commit 84adcbf

Please sign in to comment.