Skip to content

Commit

Permalink
HHH-6258: Performance enhancement to toFragmentString method to only …
Browse files Browse the repository at this point in the history
…append to the StringBuffer, and not do inserts into the beginning, which causes additional buffer allocations and copies that are unnecessary. Also, changed the StringBuffer to a StringBuilder.
  • Loading branch information
andrigtmiller authored and sebersole committed Jun 8, 2011
1 parent 02a866c commit bf066b8
Showing 1 changed file with 60 additions and 44 deletions.
104 changes: 60 additions & 44 deletions hibernate-core/src/main/java/org/hibernate/sql/InFragment.java
Expand Up @@ -68,49 +68,65 @@ public InFragment setFormula(String alias, String formulaTemplate) {
}

public String toFragmentString() {
if ( values.size()==0 ) return "1=2";
StringBuffer buf = new StringBuffer( values.size() * 5 );
buf.append(columnName);
//following doesn't handle (null, not null) but unnecessary
//since this would mean all rows
if ( values.size()>1 ) {
boolean allowNull = false;
buf.append(" in (");
Iterator iter = values.iterator();
while ( iter.hasNext() ) {
Object value = iter.next();
if ( NULL.equals(value) ) {
allowNull = true;
}
else if ( NOT_NULL.equals(value) ) {
throw new IllegalArgumentException("not null makes no sense for in expression");
}
else {
buf.append(value);
buf.append(", ");
}
}
buf.setLength( buf.length()-2 );
buf.append(')');
if (allowNull) {
buf.insert(0, " is null or ")
.insert(0, columnName)
.insert(0, '(')
.append(')');
}
}
else {
Object value = values.iterator().next();
if ( NULL.equals(value) ) {
buf.append(" is null");
}
else if ( NOT_NULL.equals(value) ) {
buf.append(" is not null");
}
else {
buf.append("=").append(value);
}
}
return buf.toString();

if (values.size() == 0) {
return "1=2";
}

StringBuilder buf = new StringBuilder(values.size() * 5);

if (values.size() == 1) {
Object value = values.get(0);
buf.append(columnName);

if (NULL.equals(value)) {
buf.append(" is null");
} else {
if (NOT_NULL.equals(value)) {
buf.append(" is not null");
} else {
buf.append('=').append(value);
}
}
return buf.toString();
}

boolean allowNull = false;

for (Object value : values) {
if (NULL.equals(value)) {
allowNull = true;
} else {
if (NOT_NULL.equals(value)) {
throw new IllegalArgumentException("not null makes no sense for in expression");
}
}
}

if (allowNull) {
buf.append('(').append(columnName).append(" is null or ").append(columnName).append(" in (");
} else {
buf.append(columnName).append(" in (");
}

for (Object value : values) {
if (NULL.equals(value)) {
;
} else {
buf.append(value);
buf.append(", ");
}
}

buf.setLength(buf.length() - 2);

if (allowNull) {
buf.append("))");
} else {
buf.append(')');
}

return buf.toString();

}
}

0 comments on commit bf066b8

Please sign in to comment.