|
1 | | -package net.sf.jsqlparser.test.create; |
2 | | - |
3 | | -import java.io.BufferedReader; |
4 | | -import java.io.InputStreamReader; |
5 | | -import java.io.StringReader; |
6 | | -import java.util.ArrayList; |
7 | | -import java.util.Iterator; |
8 | | -import java.util.List; |
9 | | -import java.util.StringTokenizer; |
10 | | - |
11 | | -import junit.framework.TestCase; |
12 | | -import net.sf.jsqlparser.JSQLParserException; |
13 | | -import net.sf.jsqlparser.parser.CCJSqlParserManager; |
14 | | -import net.sf.jsqlparser.statement.Statement; |
15 | | -import net.sf.jsqlparser.statement.create.table.ColumnDefinition; |
16 | | -import net.sf.jsqlparser.statement.create.table.CreateTable; |
17 | | -import net.sf.jsqlparser.statement.create.table.Index; |
18 | | -import net.sf.jsqlparser.test.TestException; |
19 | | -import net.sf.jsqlparser.test.tablesfinder.TablesNamesFinder; |
20 | | -import net.sf.jsqlparser.util.deparser.StatementDeParser; |
21 | | - |
22 | | -public class CreateTableTest extends TestCase { |
23 | | - CCJSqlParserManager parserManager = new CCJSqlParserManager(); |
24 | | - |
25 | | - public CreateTableTest(String arg0) { |
26 | | - super(arg0); |
27 | | - } |
28 | | - |
29 | | - public void testCreateTable2() throws JSQLParserException { |
30 | | - String statement = "CREATE TABLE testtab (\"test\" varchar (255) )"; |
31 | | - assertSqlCanBeParsedAndDeparsed(statement); |
32 | | - } |
33 | | - |
34 | | - public void testCreateTable3() throws JSQLParserException { |
35 | | - String statement = "CREATE TABLE testtab (\"test\" varchar (255) , \"test\" varchar (255) )"; |
36 | | - assertSqlCanBeParsedAndDeparsed(statement); |
37 | | - } |
38 | | - |
39 | | - public void testCreateTable() throws JSQLParserException { |
40 | | - String statement = "CREATE TABLE mytab (mycol a (10, 20) c nm g, mycol2 mypar1 mypar2 (23,323,3) asdf ('23','123') dasd, " |
41 | | - + "PRIMARY KEY (mycol2, mycol)) type = myisam"; |
42 | | - CreateTable createTable = (CreateTable) parserManager.parse(new StringReader(statement)); |
43 | | - assertEquals(2, createTable.getColumnDefinitions().size()); |
44 | | - assertEquals("mycol", ((ColumnDefinition) createTable.getColumnDefinitions().get(0)).getColumnName()); |
45 | | - assertEquals("mycol2", ((ColumnDefinition) createTable.getColumnDefinitions().get(1)).getColumnName()); |
46 | | - assertEquals("PRIMARY KEY", ((Index) createTable.getIndexes().get(0)).getType()); |
47 | | - assertEquals("mycol", ((Index) createTable.getIndexes().get(0)).getColumnsNames().get(1)); |
48 | | - assertEquals(statement, "" + createTable); |
49 | | - } |
50 | | - |
51 | | - public void testRUBiSCreateList() throws Exception { |
52 | | - BufferedReader in = new BufferedReader( new InputStreamReader( CreateTableTest.class.getResourceAsStream( "/RUBiS-create-requests.txt" ) ) ); |
53 | | - TablesNamesFinder tablesNamesFinder = new TablesNamesFinder(); |
54 | | - |
55 | | - try { |
56 | | - int numSt = 1; |
57 | | - while (true) { |
58 | | - String line = getLine(in); |
59 | | - if (line == null) { |
60 | | - break; |
61 | | - } |
62 | | - |
63 | | - if (!line.equals("#begin")) |
64 | | - break; |
65 | | - line = getLine(in); |
66 | | - StringBuilder buf = new StringBuilder(line); |
67 | | - while (true) { |
68 | | - line = getLine(in); |
69 | | - if (line.equals("#end")) { |
70 | | - break; |
71 | | - } |
72 | | - buf.append("\n"); |
73 | | - buf.append(line); |
74 | | - } |
75 | | - |
76 | | - String query = buf.toString(); |
77 | | - if (!getLine(in).equals("true")) { |
78 | | - continue; |
79 | | - } |
80 | | - |
81 | | - String tableName = getLine(in); |
82 | | - String cols = getLine(in); |
83 | | - try { |
84 | | - CreateTable createTable = (CreateTable) parserManager.parse(new StringReader(query)); |
85 | | - String[] colsList = null; |
86 | | - if (cols.equals("null")) { |
87 | | - colsList = new String[0]; |
88 | | - } else { |
89 | | - StringTokenizer tokenizer = new StringTokenizer(cols, " "); |
90 | | - |
91 | | - List colsListList = new ArrayList(); |
92 | | - while (tokenizer.hasMoreTokens()) { |
93 | | - colsListList.add(tokenizer.nextToken()); |
94 | | - } |
95 | | - |
96 | | - colsList = (String[]) colsListList.toArray(new String[colsListList.size()]); |
97 | | - |
98 | | - } |
99 | | - List colsFound = new ArrayList(); |
100 | | - if (createTable.getColumnDefinitions() != null) { |
101 | | - for (Iterator iter = createTable.getColumnDefinitions().iterator(); iter.hasNext();) { |
102 | | - ColumnDefinition columnDefinition = (ColumnDefinition) iter.next(); |
103 | | - String colName = columnDefinition.getColumnName(); |
104 | | - boolean unique = false; |
105 | | - if (createTable.getIndexes() != null) { |
106 | | - for (Iterator iterator = createTable.getIndexes().iterator(); iterator.hasNext();) { |
107 | | - Index index = (Index) iterator.next(); |
108 | | - if (index.getType().equals("PRIMARY KEY") && index.getColumnsNames().size() == 1 |
109 | | - && index.getColumnsNames().get(0).equals(colName)) { |
110 | | - unique = true; |
111 | | - } |
112 | | - |
113 | | - } |
114 | | - } |
115 | | - |
116 | | - if (!unique) { |
117 | | - if (columnDefinition.getColumnSpecStrings() != null) { |
118 | | - for (Iterator iterator = columnDefinition.getColumnSpecStrings().iterator(); iterator |
119 | | - .hasNext();) { |
120 | | - String par = (String) iterator.next(); |
121 | | - if (par.equals("UNIQUE")) { |
122 | | - unique = true; |
123 | | - } else if (par.equals("PRIMARY") && iterator.hasNext() |
124 | | - && iterator.next().equals("KEY")) { |
125 | | - unique = true; |
126 | | - } |
127 | | - } |
128 | | - } |
129 | | - } |
130 | | - if (unique) { |
131 | | - colName += ".unique"; |
132 | | - } |
133 | | - colsFound.add(colName.toLowerCase()); |
134 | | - } |
135 | | - } |
136 | | - |
137 | | - assertEquals("stm:" + query, colsList.length, colsFound.size()); |
138 | | - |
139 | | - for (int i = 0; i < colsList.length; i++) { |
140 | | - assertEquals("stm:" + query, colsList[i], colsFound.get(i)); |
141 | | - |
142 | | - } |
143 | | - } catch (Exception e) { |
144 | | - throw new TestException("error at stm num: " + numSt, e); |
145 | | - } |
146 | | - numSt++; |
147 | | - |
148 | | - } |
149 | | - } finally { |
150 | | - if (in != null) |
151 | | - in.close(); |
152 | | - } |
153 | | - } |
154 | | - |
155 | | - private String getLine(BufferedReader in) throws Exception { |
156 | | - String line = null; |
157 | | - while (true) { |
158 | | - line = in.readLine(); |
159 | | - if (line != null) { |
160 | | - line.trim(); |
161 | | - if ((line.length() != 0) |
162 | | - && ((line.length() < 2) || (line.length() >= 2) |
163 | | - && !(line.charAt(0) == '/' && line.charAt(1) == '/'))) |
164 | | - break; |
165 | | - } else { |
166 | | - break; |
167 | | - } |
168 | | - |
169 | | - } |
170 | | - |
171 | | - return line; |
172 | | - } |
173 | | - |
174 | | - private void assertSqlCanBeParsedAndDeparsed(String statement) throws JSQLParserException { |
175 | | - Statement parsed = parserManager.parse(new StringReader(statement)); |
176 | | - assertStatementCanBeDeparsedAs(parsed, statement); |
177 | | - } |
178 | | - |
179 | | - private void assertStatementCanBeDeparsedAs(Statement parsed, String statement) { |
180 | | - assertEquals(statement, parsed.toString()); |
181 | | - |
182 | | - StatementDeParser deParser = new StatementDeParser(new StringBuilder()); |
183 | | - parsed.accept(deParser); |
184 | | - assertEquals(statement, deParser.getBuffer().toString()); |
185 | | - } |
186 | | -} |
| 1 | +package net.sf.jsqlparser.test.create; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.io.StringReader; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Iterator; |
| 8 | +import java.util.List; |
| 9 | +import java.util.StringTokenizer; |
| 10 | + |
| 11 | +import junit.framework.TestCase; |
| 12 | +import net.sf.jsqlparser.JSQLParserException; |
| 13 | +import net.sf.jsqlparser.parser.CCJSqlParserManager; |
| 14 | +import net.sf.jsqlparser.statement.Statement; |
| 15 | +import net.sf.jsqlparser.statement.create.table.ColumnDefinition; |
| 16 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 17 | +import net.sf.jsqlparser.statement.create.table.Index; |
| 18 | +import net.sf.jsqlparser.test.TestException; |
| 19 | +import net.sf.jsqlparser.util.TablesNamesFinder; |
| 20 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 21 | + |
| 22 | +public class CreateTableTest extends TestCase { |
| 23 | + CCJSqlParserManager parserManager = new CCJSqlParserManager(); |
| 24 | + |
| 25 | + public CreateTableTest(String arg0) { |
| 26 | + super(arg0); |
| 27 | + } |
| 28 | + |
| 29 | + public void testCreateTable2() throws JSQLParserException { |
| 30 | + String statement = "CREATE TABLE testtab (\"test\" varchar (255) )"; |
| 31 | + assertSqlCanBeParsedAndDeparsed(statement); |
| 32 | + } |
| 33 | + |
| 34 | + public void testCreateTable3() throws JSQLParserException { |
| 35 | + String statement = "CREATE TABLE testtab (\"test\" varchar (255) , \"test\" varchar (255) )"; |
| 36 | + assertSqlCanBeParsedAndDeparsed(statement); |
| 37 | + } |
| 38 | + |
| 39 | + public void testCreateTable() throws JSQLParserException { |
| 40 | + String statement = "CREATE TABLE mytab (mycol a (10, 20) c nm g, mycol2 mypar1 mypar2 (23,323,3) asdf ('23','123') dasd, " |
| 41 | + + "PRIMARY KEY (mycol2, mycol)) type = myisam"; |
| 42 | + CreateTable createTable = (CreateTable) parserManager.parse(new StringReader(statement)); |
| 43 | + assertEquals(2, createTable.getColumnDefinitions().size()); |
| 44 | + assertEquals("mycol", ((ColumnDefinition) createTable.getColumnDefinitions().get(0)).getColumnName()); |
| 45 | + assertEquals("mycol2", ((ColumnDefinition) createTable.getColumnDefinitions().get(1)).getColumnName()); |
| 46 | + assertEquals("PRIMARY KEY", ((Index) createTable.getIndexes().get(0)).getType()); |
| 47 | + assertEquals("mycol", ((Index) createTable.getIndexes().get(0)).getColumnsNames().get(1)); |
| 48 | + assertEquals(statement, "" + createTable); |
| 49 | + } |
| 50 | + |
| 51 | + public void testRUBiSCreateList() throws Exception { |
| 52 | + BufferedReader in = new BufferedReader( new InputStreamReader( CreateTableTest.class.getResourceAsStream( "/RUBiS-create-requests.txt" ) ) ); |
| 53 | + TablesNamesFinder tablesNamesFinder = new TablesNamesFinder(); |
| 54 | + |
| 55 | + try { |
| 56 | + int numSt = 1; |
| 57 | + while (true) { |
| 58 | + String line = getLine(in); |
| 59 | + if (line == null) { |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + if (!line.equals("#begin")) |
| 64 | + break; |
| 65 | + line = getLine(in); |
| 66 | + StringBuilder buf = new StringBuilder(line); |
| 67 | + while (true) { |
| 68 | + line = getLine(in); |
| 69 | + if (line.equals("#end")) { |
| 70 | + break; |
| 71 | + } |
| 72 | + buf.append("\n"); |
| 73 | + buf.append(line); |
| 74 | + } |
| 75 | + |
| 76 | + String query = buf.toString(); |
| 77 | + if (!getLine(in).equals("true")) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + String tableName = getLine(in); |
| 82 | + String cols = getLine(in); |
| 83 | + try { |
| 84 | + CreateTable createTable = (CreateTable) parserManager.parse(new StringReader(query)); |
| 85 | + String[] colsList = null; |
| 86 | + if (cols.equals("null")) { |
| 87 | + colsList = new String[0]; |
| 88 | + } else { |
| 89 | + StringTokenizer tokenizer = new StringTokenizer(cols, " "); |
| 90 | + |
| 91 | + List colsListList = new ArrayList(); |
| 92 | + while (tokenizer.hasMoreTokens()) { |
| 93 | + colsListList.add(tokenizer.nextToken()); |
| 94 | + } |
| 95 | + |
| 96 | + colsList = (String[]) colsListList.toArray(new String[colsListList.size()]); |
| 97 | + |
| 98 | + } |
| 99 | + List colsFound = new ArrayList(); |
| 100 | + if (createTable.getColumnDefinitions() != null) { |
| 101 | + for (Iterator iter = createTable.getColumnDefinitions().iterator(); iter.hasNext();) { |
| 102 | + ColumnDefinition columnDefinition = (ColumnDefinition) iter.next(); |
| 103 | + String colName = columnDefinition.getColumnName(); |
| 104 | + boolean unique = false; |
| 105 | + if (createTable.getIndexes() != null) { |
| 106 | + for (Iterator iterator = createTable.getIndexes().iterator(); iterator.hasNext();) { |
| 107 | + Index index = (Index) iterator.next(); |
| 108 | + if (index.getType().equals("PRIMARY KEY") && index.getColumnsNames().size() == 1 |
| 109 | + && index.getColumnsNames().get(0).equals(colName)) { |
| 110 | + unique = true; |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (!unique) { |
| 117 | + if (columnDefinition.getColumnSpecStrings() != null) { |
| 118 | + for (Iterator iterator = columnDefinition.getColumnSpecStrings().iterator(); iterator |
| 119 | + .hasNext();) { |
| 120 | + String par = (String) iterator.next(); |
| 121 | + if (par.equals("UNIQUE")) { |
| 122 | + unique = true; |
| 123 | + } else if (par.equals("PRIMARY") && iterator.hasNext() |
| 124 | + && iterator.next().equals("KEY")) { |
| 125 | + unique = true; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + if (unique) { |
| 131 | + colName += ".unique"; |
| 132 | + } |
| 133 | + colsFound.add(colName.toLowerCase()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + assertEquals("stm:" + query, colsList.length, colsFound.size()); |
| 138 | + |
| 139 | + for (int i = 0; i < colsList.length; i++) { |
| 140 | + assertEquals("stm:" + query, colsList[i], colsFound.get(i)); |
| 141 | + |
| 142 | + } |
| 143 | + } catch (Exception e) { |
| 144 | + throw new TestException("error at stm num: " + numSt, e); |
| 145 | + } |
| 146 | + numSt++; |
| 147 | + |
| 148 | + } |
| 149 | + } finally { |
| 150 | + if (in != null) |
| 151 | + in.close(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private String getLine(BufferedReader in) throws Exception { |
| 156 | + String line = null; |
| 157 | + while (true) { |
| 158 | + line = in.readLine(); |
| 159 | + if (line != null) { |
| 160 | + line.trim(); |
| 161 | + if ((line.length() != 0) |
| 162 | + && ((line.length() < 2) || (line.length() >= 2) |
| 163 | + && !(line.charAt(0) == '/' && line.charAt(1) == '/'))) |
| 164 | + break; |
| 165 | + } else { |
| 166 | + break; |
| 167 | + } |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + return line; |
| 172 | + } |
| 173 | + |
| 174 | + private void assertSqlCanBeParsedAndDeparsed(String statement) throws JSQLParserException { |
| 175 | + Statement parsed = parserManager.parse(new StringReader(statement)); |
| 176 | + assertStatementCanBeDeparsedAs(parsed, statement); |
| 177 | + } |
| 178 | + |
| 179 | + private void assertStatementCanBeDeparsedAs(Statement parsed, String statement) { |
| 180 | + assertEquals(statement, parsed.toString()); |
| 181 | + |
| 182 | + StatementDeParser deParser = new StatementDeParser(new StringBuilder()); |
| 183 | + parsed.accept(deParser); |
| 184 | + assertEquals(statement, deParser.getBuffer().toString()); |
| 185 | + } |
| 186 | +} |
0 commit comments