-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/net/sf/jsqlparser/statement/create/index/CreateIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* #%L | ||
* JSQLParser library | ||
* %% | ||
* Copyright (C) 2004 - 2013 JSQLParser | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, either version 2.1 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Lesser Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Lesser Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
* #L% | ||
*/ | ||
package net.sf.jsqlparser.statement.create.index; | ||
|
||
import java.util.Iterator; | ||
|
||
import net.sf.jsqlparser.schema.Table; | ||
import net.sf.jsqlparser.statement.Statement; | ||
import net.sf.jsqlparser.statement.StatementVisitor; | ||
import net.sf.jsqlparser.statement.create.table.Index; | ||
|
||
/** | ||
* A "CREATE INDEX" statement | ||
* | ||
* @author Raymond Augé | ||
*/ | ||
public class CreateIndex implements Statement { | ||
|
||
private Table table; | ||
private Index index; | ||
|
||
public void accept(StatementVisitor statementVisitor) { | ||
statementVisitor.visit(this); | ||
} | ||
|
||
/** | ||
* The index to be created | ||
*/ | ||
public Index getIndex() { | ||
return index; | ||
} | ||
|
||
public void setIndex(Index index) { | ||
this.index = index; | ||
} | ||
|
||
/** | ||
* The table on which the index is to be created | ||
*/ | ||
public Table getTable() { | ||
return table; | ||
} | ||
|
||
public void setTable(Table table) { | ||
this.table = table; | ||
} | ||
|
||
public String toString() { | ||
StringBuffer buffer = new StringBuffer(); | ||
|
||
buffer.append("CREATE "); | ||
|
||
if (index.getType() != null) { | ||
buffer.append(index.getType()); | ||
buffer.append(" "); | ||
} | ||
|
||
buffer.append("INDEX "); | ||
buffer.append(index.getName()); | ||
buffer.append(" ON "); | ||
buffer.append(table.getWholeTableName()); | ||
|
||
if (index.getColumnsNames() != null) { | ||
buffer.append(" ("); | ||
|
||
for (Iterator iter = index.getColumnsNames().iterator(); iter.hasNext() ;) { | ||
String columnName = (String)iter.next(); | ||
|
||
buffer.append(columnName); | ||
|
||
if (iter.hasNext()) { | ||
buffer.append(", "); | ||
} | ||
} | ||
|
||
buffer.append(")"); | ||
} | ||
|
||
return buffer.toString(); | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/net/sf/jsqlparser/util/deparser/CreateIndexDeParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* #%L | ||
* JSQLParser library | ||
* %% | ||
* Copyright (C) 2004 - 2013 JSQLParser | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, either version 2.1 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Lesser Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Lesser Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
* #L% | ||
*/ | ||
package net.sf.jsqlparser.util.deparser; | ||
|
||
import java.util.Iterator; | ||
|
||
import net.sf.jsqlparser.statement.create.index.CreateIndex; | ||
import net.sf.jsqlparser.statement.create.table.Index; | ||
|
||
/** | ||
* A class to de-parse (that is, tranform from JSqlParser hierarchy into a string) | ||
* a {@link net.sf.jsqlparser.statement.create.index.CreateIndex} | ||
* | ||
* @author Raymond Augé | ||
*/ | ||
public class CreateIndexDeParser { | ||
protected StringBuilder buffer; | ||
|
||
/** | ||
* @param buffer the buffer that will be filled with the create | ||
*/ | ||
public CreateIndexDeParser(StringBuilder buffer) { | ||
this.buffer = buffer; | ||
} | ||
|
||
public void deParse(CreateIndex createIndex) { | ||
Index index = createIndex.getIndex(); | ||
|
||
buffer.append("CREATE "); | ||
|
||
if (index.getType() != null) { | ||
buffer.append(index.getType()); | ||
buffer.append(" "); | ||
} | ||
|
||
buffer.append("INDEX "); | ||
buffer.append(index.getName()); | ||
buffer.append(" ON "); | ||
buffer.append(createIndex.getTable().getWholeTableName()); | ||
|
||
if (index.getColumnsNames() != null) { | ||
buffer.append(" ("); | ||
for (Iterator iter = index.getColumnsNames().iterator(); iter.hasNext();) { | ||
String columnName = (String)iter.next(); | ||
buffer.append(columnName); | ||
|
||
if (iter.hasNext()) { | ||
buffer.append(", "); | ||
} | ||
} | ||
buffer.append(")"); | ||
} | ||
} | ||
|
||
public StringBuilder getBuffer() { | ||
return buffer; | ||
} | ||
|
||
public void setBuffer(StringBuilder buffer) { | ||
this.buffer = buffer; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/test/java/net/sf/jsqlparser/test/create/CreateIndexTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package net.sf.jsqlparser.test.create; | ||
|
||
import java.io.StringReader; | ||
|
||
import junit.framework.TestCase; | ||
import net.sf.jsqlparser.JSQLParserException; | ||
import net.sf.jsqlparser.parser.CCJSqlParserManager; | ||
import net.sf.jsqlparser.statement.create.index.CreateIndex; | ||
|
||
|
||
/** | ||
* @author Raymond Augé | ||
*/ | ||
public class CreateIndexTest extends TestCase { | ||
|
||
CCJSqlParserManager parserManager = new CCJSqlParserManager(); | ||
|
||
public CreateIndexTest(String arg0) { | ||
super(arg0); | ||
} | ||
|
||
public void testCreateIndex() throws JSQLParserException { | ||
String statement = | ||
"CREATE INDEX myindex ON mytab (mycol, mycol2)"; | ||
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement)); | ||
assertEquals(2, createIndex.getIndex().getColumnsNames().size()); | ||
assertEquals("myindex", createIndex.getIndex().getName()); | ||
assertNull(createIndex.getIndex().getType()); | ||
assertEquals("mytab", createIndex.getTable().getWholeTableName()); | ||
assertEquals("mycol", createIndex.getIndex().getColumnsNames().get(0)); | ||
assertEquals(statement, ""+createIndex); | ||
} | ||
|
||
public void testCreateIndex2() throws JSQLParserException { | ||
String statement = | ||
"CREATE mytype INDEX myindex ON mytab (mycol, mycol2)"; | ||
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement)); | ||
assertEquals(2, createIndex.getIndex().getColumnsNames().size()); | ||
assertEquals("myindex", createIndex.getIndex().getName()); | ||
assertEquals("mytype", createIndex.getIndex().getType()); | ||
assertEquals("mytab", createIndex.getTable().getWholeTableName()); | ||
assertEquals("mycol2", createIndex.getIndex().getColumnsNames().get(1)); | ||
assertEquals(statement, ""+createIndex); | ||
} | ||
|
||
public void testCreateIndex3() throws JSQLParserException { | ||
String statement = | ||
"CREATE mytype INDEX myindex ON mytab (mycol ASC, mycol2, mycol3)"; | ||
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement)); | ||
assertEquals(3, createIndex.getIndex().getColumnsNames().size()); | ||
assertEquals("myindex", createIndex.getIndex().getName()); | ||
assertEquals("mytype", createIndex.getIndex().getType()); | ||
assertEquals("mytab", createIndex.getTable().getWholeTableName()); | ||
assertEquals("mycol3", createIndex.getIndex().getColumnsNames().get(2)); | ||
} | ||
|
||
public void testCreateIndex4() throws JSQLParserException { | ||
String statement = | ||
"CREATE mytype INDEX myindex ON mytab (mycol ASC, mycol2 (75), mycol3)"; | ||
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement)); | ||
assertEquals(3, createIndex.getIndex().getColumnsNames().size()); | ||
assertEquals("myindex", createIndex.getIndex().getName()); | ||
assertEquals("mytype", createIndex.getIndex().getType()); | ||
assertEquals("mytab", createIndex.getTable().getWholeTableName()); | ||
assertEquals("mycol3", createIndex.getIndex().getColumnsNames().get(2)); | ||
} | ||
|
||
public void testCreateIndex5() throws JSQLParserException { | ||
String statement = | ||
"CREATE mytype INDEX myindex ON mytab (mycol ASC, mycol2 (75), mycol3) mymodifiers"; | ||
CreateIndex createIndex = (CreateIndex) parserManager.parse(new StringReader(statement)); | ||
assertEquals(3, createIndex.getIndex().getColumnsNames().size()); | ||
assertEquals("myindex", createIndex.getIndex().getName()); | ||
assertEquals("mytype", createIndex.getIndex().getType()); | ||
assertEquals("mytab", createIndex.getTable().getWholeTableName()); | ||
assertEquals("mycol3", createIndex.getIndex().getColumnsNames().get(2)); | ||
} | ||
|
||
} |