FEATURE: Add support for CREATE INDEX.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@981637 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adam Heath
2010-08-02 17:44:00 +00:00
parent 56c7c3391a
commit ca01d94d2a
5 changed files with 145 additions and 3 deletions
+30 -3
View File
@@ -109,6 +109,8 @@ TOKEN: {
| <DELETE: "DELETE">
| <UPDATE: "UPDATE">
| <INSERT: "INSERT">
| <INDEX: "INDEX">
| <UNIQUE: "UNIQUE">
| <RELATION: "RELATION">
| <EXCLUDE: "EXCLUDE">
| <TYPE: "TYPE">
@@ -210,7 +212,7 @@ public SQLStatement StandaloneStatement(): {
public SQLView ViewStatement(): {
SQLView sqlView;
} {
sqlView=View() ( <SEMI> )? <EOF>
<CREATE> <VIEW> sqlView=View() ( <SEMI> )? <EOF>
{ return sqlView; }
}
@@ -270,7 +272,18 @@ private SQLStatement Statement(): {
| statement=Delete() { return statement; }
| statement=Update() { return statement; }
| statement=Insert() { return statement; }
| statement=View() { return statement; }
| statement=Creates() { return statement; }
)
}
private SQLStatement Creates(): {
SQLStatement statement;
boolean isUnique = false;
} {
<CREATE> (
<VIEW> statement=View() { return statement; }
| ( <UNIQUE> { isUnique = true; } )?
<INDEX> statement=Index(isUnique) { return statement; }
)
}
@@ -278,7 +291,7 @@ private SQLView View(): {
String name;
SQLSelect sqlSelect;
} {
<CREATE> <VIEW> name=NamePart() <AS> sqlSelect=Select() { return new SQLView(name, sqlSelect); }
name=NamePart() <AS> sqlSelect=Select() { return new SQLView(name, sqlSelect); }
}
private SQLSelect Select(): {
@@ -365,6 +378,20 @@ private SQLDelete Delete(): {
{ return new SQLDelete(new Table(tableName, joined), whereCondition); }
}
private SQLIndex Index(boolean isUnique): {
String name, table, using = null;
ConstantValue value;
List<ConstantValue> values = FastList.newInstance();
} {
name=NamePart() <ON> table=NamePart()
( <USING> using=NamePart() )?
<OPEN_PAREN>
value=ConstantValue() { values.add(value); }
( <COMMA> value=ConstantValue() { values.add(value); } )*
<CLOSE_PAREN>
{ return new SQLIndex(isUnique, name, table, using, values); }
}
private SQLInsert Insert(): {
TableName tableName;
List<String> columns = FastList.newInstance();
@@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.ofbiz.sql;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import org.ofbiz.base.util.StringUtil;
public final class SQLIndex extends SQLStatement<SQLIndex> {
private final boolean isUnique;
private final String name;
private final String table;
private final String using;
private final List<ConstantValue> values;
public SQLIndex(boolean isUnique, String name, String table, String using, List<ConstantValue> values) {
this.isUnique = isUnique;
this.name = name;
this.table = table;
this.using = using;
this.values = values;
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
public boolean getIsUnique() {
return isUnique;
}
public String getName() {
return name;
}
public String getTable() {
return table;
}
public String getUsing() {
return using;
}
public List<ConstantValue> getValues() {
return values;
}
public boolean equals(Object o) {
if (o instanceof SQLIndex) {
SQLIndex other = (SQLIndex) o;
return isUnique == other.isUnique && name.equals(other.name) && table.equals(other.table) && equalsHelper(using, other.using) && values.equals(other.values);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append("CREATE");
if (isUnique) {
sb.append(" UNIQUE");
}
sb.append(" INDEX ");
sb.append(name);
sb.append(" ON ");
sb.append(table);
if (using != null) {
sb.append(" USING ").append(using);
}
sb.append(" (");
StringUtil.append(sb, values, null, null, ", ");
sb.append(')');
sb.append(';');
return sb;
}
}
@@ -21,6 +21,7 @@ package org.ofbiz.sql;
public abstract class SQLStatement<S extends SQLStatement<S>> extends Atom {
public interface Visitor {
void visit(SQLDelete statement);
void visit(SQLIndex statement);
void visit(SQLInsert statement);
void visit(SQLSelect statement);
void visit(SQLUpdate statement);
@@ -31,6 +32,9 @@ public abstract class SQLStatement<S extends SQLStatement<S>> extends Atom {
public void visit(SQLDelete statement) {
}
public void visit(SQLIndex statement) {
}
public void visit(SQLInsert statement) {
}
@@ -68,6 +68,7 @@ UPDATE Person SET lastName = ('auto-' || partyId), height = 6, width = 5, nickna
DELETE FROM Person WHERE partyId IN ('a', 'b');
DELETE FROM Party WHERE partyId IN ('a', 'b');
CREATE VIEW viewOne AS SELECT a.* FROM Party a;
CREATE INDEX testIndex ON Party USING btree (partyId);
/*
UPDATE Person SET firstName = partyId || '-auto' WHERE partyId IN ('a', 'b');
*/
@@ -29,6 +29,7 @@ import org.ofbiz.sql.BetweenCondition;
import org.ofbiz.sql.BooleanCondition;
import org.ofbiz.sql.Condition;
import org.ofbiz.sql.ConditionList;
import org.ofbiz.sql.ConstantValue;
import org.ofbiz.sql.CountFunction;
import org.ofbiz.sql.FieldAll;
import org.ofbiz.sql.FieldDef;
@@ -47,6 +48,7 @@ import org.ofbiz.sql.ParameterValue;
import org.ofbiz.sql.Parser;
import org.ofbiz.sql.Relation;
import org.ofbiz.sql.SQLDelete;
import org.ofbiz.sql.SQLIndex;
import org.ofbiz.sql.SQLInsert;
import org.ofbiz.sql.SQLSelect;
import org.ofbiz.sql.SQLStatement;
@@ -266,6 +268,19 @@ public class SQLTest extends GenericTestCaseBase {
assertEquals("firstView", view, stmt);
assertEquals("firstView:parse", parser(view).ViewStatement(), parser(stmt).ViewStatement());
}
{
SQLIndex index = new SQLIndex(
false,
"testIndex",
"Party",
"btree",
GenericTestCaseBase.<ConstantValue>list(
new FieldValue(null, "partyId")
)
);
SQLStatement stmt = stmtIt.next();
assertEquals("firstIndex", index, stmt);
}
assertFalse("has no more statements", stmtIt.hasNext());
}
/*