Fix column indices in hsqldb. Like mysql, it can't handle parenthesis around its index column expressions.
This commit is contained in:
@@ -98,6 +98,8 @@ import com.samskivert.depot.impl.operator.IsNull;
|
||||
import com.samskivert.depot.impl.operator.MultiOperator;
|
||||
import com.samskivert.depot.impl.operator.Not;
|
||||
|
||||
import static com.samskivert.Log.log;
|
||||
|
||||
/**
|
||||
* Implements the base functionality of the SQL-building pass of {@link SQLBuilder}. Dialectal
|
||||
* subclasses of this should be created and returned from {@link SQLBuilder#getBuildVisitor()}.
|
||||
@@ -499,6 +501,15 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
|
||||
|
||||
public Void visit (CreateIndexClause createIndexClause)
|
||||
{
|
||||
if (!_allowComplexIndices) {
|
||||
for (Tuple<SQLExpression, Order> field : createIndexClause.getFields()) {
|
||||
if (!(field.left instanceof ColumnExp)) {
|
||||
log.warning("This database can't handle complex indexes. Aborting creation.",
|
||||
"ixName", createIndexClause.getName());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
_builder.append("create ");
|
||||
if (createIndexClause.isUnique()) {
|
||||
_builder.append("unique ");
|
||||
@@ -518,7 +529,16 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
|
||||
}
|
||||
comma = true;
|
||||
|
||||
appendIndexComponent(field.left);
|
||||
// If the index can't be complex, it doesn't need to be wrapped in parenthesis. Some
|
||||
// databases can't handle parenthesis around their index expressions either, so if a
|
||||
// complex expression is already disallowed, don't wrap it in parenthesis.
|
||||
if (_allowComplexIndices) {
|
||||
_builder.append("(");
|
||||
}
|
||||
field.left.accept(this);
|
||||
if (_allowComplexIndices) {
|
||||
_builder.append(")");
|
||||
}
|
||||
if (field.right == Order.DESC) {
|
||||
// ascending is default, print nothing unless explicitly descending
|
||||
_builder.append(" desc");
|
||||
@@ -876,15 +896,6 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
|
||||
}
|
||||
}
|
||||
|
||||
// output one of potentially many fields in an index expression
|
||||
protected void appendIndexComponent (SQLExpression expression)
|
||||
{
|
||||
// the standard builder wraps each field in its own parens
|
||||
_builder.append("(");
|
||||
expression.accept(this);
|
||||
_builder.append(")");
|
||||
}
|
||||
|
||||
// output the column names and values for an insert
|
||||
protected void appendInsertColumns (InsertClause insertClause)
|
||||
{
|
||||
@@ -922,9 +933,10 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
|
||||
_builder.append(")");
|
||||
}
|
||||
|
||||
protected BuildVisitor (DepotTypes types)
|
||||
protected BuildVisitor (DepotTypes types, boolean allowComplexIndices)
|
||||
{
|
||||
_types = types;
|
||||
_allowComplexIndices = allowComplexIndices;
|
||||
}
|
||||
|
||||
protected static interface Bindable
|
||||
@@ -984,4 +996,7 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
|
||||
protected boolean _innerClause = false;
|
||||
protected boolean _enableOverrides = false;
|
||||
protected boolean _enableAliasing = false;
|
||||
|
||||
/** If this database allows complex expressions in its indices. */
|
||||
protected final boolean _allowComplexIndices;
|
||||
}
|
||||
|
||||
@@ -20,8 +20,6 @@
|
||||
|
||||
package com.samskivert.depot.impl;
|
||||
|
||||
import static com.samskivert.Log.log;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@@ -32,17 +30,13 @@ import com.google.common.collect.Lists;
|
||||
|
||||
import com.samskivert.jdbc.ColumnDefinition;
|
||||
import com.samskivert.util.ArrayUtil;
|
||||
import com.samskivert.util.Tuple;
|
||||
|
||||
import com.samskivert.depot.Ops;
|
||||
import com.samskivert.depot.PersistentRecord;
|
||||
import com.samskivert.depot.annotation.FullTextIndex;
|
||||
import com.samskivert.depot.annotation.GeneratedValue;
|
||||
import com.samskivert.depot.clause.OrderBy.Order;
|
||||
import com.samskivert.depot.expression.*;
|
||||
import com.samskivert.depot.operator.FullText;
|
||||
|
||||
import com.samskivert.depot.impl.clause.CreateIndexClause;
|
||||
import com.samskivert.depot.impl.expression.DateFun.DatePart.Part;
|
||||
import com.samskivert.depot.impl.expression.DateFun.DatePart;
|
||||
import com.samskivert.depot.impl.expression.DateFun.DateTruncate;
|
||||
@@ -128,23 +122,9 @@ public class HSQLBuilder
|
||||
throw new IllegalArgumentException("HSQL does not have built-in date truncation");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit (CreateIndexClause createIndexClause)
|
||||
{
|
||||
for (Tuple<SQLExpression, Order> field : createIndexClause.getFields()) {
|
||||
if (!(field.left instanceof ColumnExp)) {
|
||||
log.warning("This database can't handle complex indexes. Aborting creation.",
|
||||
"ixName", createIndexClause.getName());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
super.visit(createIndexClause);
|
||||
return null;
|
||||
}
|
||||
|
||||
protected HBuildVisitor (DepotTypes types)
|
||||
{
|
||||
super(types);
|
||||
super(types, false);
|
||||
}
|
||||
|
||||
protected String getDateFunction (Part part)
|
||||
|
||||
@@ -24,16 +24,11 @@ import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Set;
|
||||
|
||||
import com.samskivert.util.Tuple;
|
||||
|
||||
import com.samskivert.depot.PersistentRecord;
|
||||
import com.samskivert.depot.annotation.FullTextIndex;
|
||||
import com.samskivert.depot.clause.OrderBy.Order;
|
||||
import com.samskivert.depot.expression.ColumnExp;
|
||||
import com.samskivert.depot.expression.SQLExpression;
|
||||
import com.samskivert.depot.operator.FullText;
|
||||
|
||||
import com.samskivert.depot.impl.clause.CreateIndexClause;
|
||||
import com.samskivert.depot.impl.clause.DeleteClause;
|
||||
import com.samskivert.depot.impl.clause.DropIndexClause;
|
||||
import com.samskivert.depot.impl.expression.DateFun.DatePart;
|
||||
@@ -123,20 +118,6 @@ public class MySQLBuilder
|
||||
throw new IllegalArgumentException("Unknown date part: " + part);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit (CreateIndexClause createIndexClause)
|
||||
{
|
||||
for (Tuple<SQLExpression, Order> field : createIndexClause.getFields()) {
|
||||
if (!(field.left instanceof ColumnExp)) {
|
||||
log.warning("This database can't handle complex indexes. Aborting creation.",
|
||||
"ixName", createIndexClause.getName());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
super.visit(createIndexClause);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit (DropIndexClause dropIndexClause)
|
||||
{
|
||||
@@ -151,7 +132,7 @@ public class MySQLBuilder
|
||||
|
||||
protected MSBuildVisitor (DepotTypes types)
|
||||
{
|
||||
super(types);
|
||||
super(types, false);
|
||||
}
|
||||
|
||||
@Override protected void appendTableName (Class<? extends PersistentRecord> type)
|
||||
@@ -169,13 +150,6 @@ public class MySQLBuilder
|
||||
_builder.append(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void appendIndexComponent (SQLExpression expression)
|
||||
{
|
||||
// MySQL is never given complex expressions and hates parens, so just recurse
|
||||
expression.accept(this);
|
||||
}
|
||||
|
||||
protected void renderMatch (FullText fullText)
|
||||
{
|
||||
_builder.append("match(");
|
||||
|
||||
@@ -151,7 +151,7 @@ public class PostgreSQLBuilder
|
||||
|
||||
protected PGBuildVisitor (DepotTypes types)
|
||||
{
|
||||
super(types);
|
||||
super(types, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.samskivert.util.StringUtil;
|
||||
|
||||
import com.samskivert.depot.annotation.Entity;
|
||||
import com.samskivert.depot.annotation.Id;
|
||||
import com.samskivert.depot.annotation.Index;
|
||||
import com.samskivert.depot.expression.ColumnExp;
|
||||
|
||||
/**
|
||||
@@ -57,7 +58,7 @@ public class TestRecord extends PersistentRecord
|
||||
|
||||
public String homeTown;
|
||||
|
||||
// @Index // TODO: this horks HSQLDB
|
||||
@Index
|
||||
public Date created;
|
||||
|
||||
public Timestamp lastModified;
|
||||
|
||||
Reference in New Issue
Block a user