Author: doogie
Date: Mon Aug 2 17:44:00 2010 New Revision: 981637 URL: http://svn.apache.org/viewvc?rev=981637&view=rev Log: FEATURE: Add support for CREATE INDEX. Added: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLIndex.java Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLStatement.java ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj?rev=981637&r1=981636&r2=981637&view=diff ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj (original) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/Parser.jj Mon Aug 2 17:44:00 2010 @@ -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(); Added: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLIndex.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLIndex.java?rev=981637&view=auto ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLIndex.java (added) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLIndex.java Mon Aug 2 17:44:00 2010 @@ -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; + } +} Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLStatement.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLStatement.java?rev=981637&r1=981636&r2=981637&view=diff ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLStatement.java (original) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/SQLStatement.java Mon Aug 2 17:44:00 2010 @@ -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 ext public void visit(SQLDelete statement) { } + public void visit(SQLIndex statement) { + } + public void visit(SQLInsert statement) { } Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql?rev=981637&r1=981636&r2=981637&view=diff ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql (original) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/GoodParseAll.sql Mon Aug 2 17:44:00 2010 @@ -68,6 +68,7 @@ UPDATE Person SET lastName = ('auto-' || 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'); */ Modified: ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java?rev=981637&r1=981636&r2=981637&view=diff ============================================================================== --- ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java (original) +++ ofbiz/trunk/framework/sql/src/org/ofbiz/sql/test/SQLTest.java Mon Aug 2 17:44:00 2010 @@ -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 GenericTest 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()); } /* |
Free forum by Nabble | Edit this page |