|
27 | 27 | import net.sf.jsqlparser.statement.Statement; |
28 | 28 | import net.sf.jsqlparser.statement.StatementVisitor; |
29 | 29 | import net.sf.jsqlparser.statement.select.PlainSelect; |
| 30 | +import net.sf.jsqlparser.statement.select.SelectBody; |
30 | 31 |
|
31 | 32 | /** |
32 | 33 | * A "CREATE TABLE" statement |
33 | 34 | */ |
34 | 35 | public class CreateTable implements Statement { |
35 | 36 |
|
36 | | - private Table table; |
37 | | - private List<String> tableOptionsStrings; |
38 | | - private List<ColumnDefinition> columnDefinitions; |
39 | | - private List<Index> indexes; |
40 | | - |
41 | | - @Override |
42 | | - public void accept(StatementVisitor statementVisitor) { |
43 | | - statementVisitor.visit(this); |
44 | | - } |
45 | | - |
46 | | - /** |
47 | | - * The name of the table to be created |
48 | | - */ |
49 | | - public Table getTable() { |
50 | | - return table; |
51 | | - } |
52 | | - |
53 | | - public void setTable(Table table) { |
54 | | - this.table = table; |
55 | | - } |
56 | | - |
57 | | - /** |
58 | | - * A list of {@link ColumnDefinition}s of this table. |
59 | | - */ |
60 | | - public List<ColumnDefinition> getColumnDefinitions() { |
61 | | - return columnDefinitions; |
62 | | - } |
63 | | - |
64 | | - public void setColumnDefinitions(List<ColumnDefinition> list) { |
65 | | - columnDefinitions = list; |
66 | | - } |
67 | | - |
68 | | - /** |
69 | | - * A list of options (as simple strings) of this table definition, as |
70 | | - * ("TYPE", "=", "MYISAM") |
71 | | - */ |
72 | | - public List<?> getTableOptionsStrings() { |
73 | | - return tableOptionsStrings; |
74 | | - } |
75 | | - |
76 | | - public void setTableOptionsStrings(List<String> list) { |
77 | | - tableOptionsStrings = list; |
78 | | - } |
79 | | - |
80 | | - /** |
81 | | - * A list of {@link Index}es (for example "PRIMARY KEY") of this table.<br> |
82 | | - * Indexes created with column definitions (as in mycol INT PRIMARY KEY) are |
83 | | - * not inserted into this list. |
84 | | - */ |
85 | | - public List<Index> getIndexes() { |
86 | | - return indexes; |
87 | | - } |
88 | | - |
89 | | - public void setIndexes(List<Index> list) { |
90 | | - indexes = list; |
91 | | - } |
92 | | - |
93 | | - @Override |
94 | | - public String toString() { |
95 | | - String sql = ""; |
96 | | - |
97 | | - sql = "CREATE TABLE " + table + " ("; |
98 | | - |
99 | | - sql += PlainSelect.getStringList(columnDefinitions, true, false); |
100 | | - if (indexes != null && indexes.size() != 0) { |
101 | | - sql += ", "; |
102 | | - sql += PlainSelect.getStringList(indexes); |
103 | | - } |
104 | | - sql += ")"; |
105 | | - String options = PlainSelect.getStringList(tableOptionsStrings, false, false); |
106 | | - if (options != null && options.length() > 0) { |
107 | | - sql += " " + options; |
108 | | - } |
109 | | - |
110 | | - return sql; |
111 | | - } |
| 37 | + private Table table; |
| 38 | + private List<String> tableOptionsStrings; |
| 39 | + private List<ColumnDefinition> columnDefinitions; |
| 40 | + private List<Index> indexes; |
| 41 | + private SelectBody selectBody; |
| 42 | + |
| 43 | + @Override |
| 44 | + public void accept(StatementVisitor statementVisitor) { |
| 45 | + statementVisitor.visit(this); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * The name of the table to be created |
| 50 | + */ |
| 51 | + public Table getTable() { |
| 52 | + return table; |
| 53 | + } |
| 54 | + |
| 55 | + public void setTable(Table table) { |
| 56 | + this.table = table; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * A list of {@link ColumnDefinition}s of this table. |
| 61 | + */ |
| 62 | + public List<ColumnDefinition> getColumnDefinitions() { |
| 63 | + return columnDefinitions; |
| 64 | + } |
| 65 | + |
| 66 | + public void setColumnDefinitions(List<ColumnDefinition> list) { |
| 67 | + columnDefinitions = list; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * A list of options (as simple strings) of this table definition, as |
| 72 | + * ("TYPE", "=", "MYISAM") |
| 73 | + */ |
| 74 | + public List<?> getTableOptionsStrings() { |
| 75 | + return tableOptionsStrings; |
| 76 | + } |
| 77 | + |
| 78 | + public void setTableOptionsStrings(List<String> list) { |
| 79 | + tableOptionsStrings = list; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * A list of {@link Index}es (for example "PRIMARY KEY") of this table.<br> |
| 84 | + * Indexes created with column definitions (as in mycol INT PRIMARY KEY) are |
| 85 | + * not inserted into this list. |
| 86 | + */ |
| 87 | + public List<Index> getIndexes() { |
| 88 | + return indexes; |
| 89 | + } |
| 90 | + |
| 91 | + public void setIndexes(List<Index> list) { |
| 92 | + indexes = list; |
| 93 | + } |
| 94 | + |
| 95 | + public SelectBody getSelectBody() { |
| 96 | + return selectBody; |
| 97 | + } |
| 98 | + |
| 99 | + public void setSelectBody(SelectBody selectBody) { |
| 100 | + this.selectBody = selectBody; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public String toString() { |
| 105 | + String sql = ""; |
| 106 | + |
| 107 | + sql = "CREATE TABLE " + table; |
| 108 | + |
| 109 | + if (selectBody != null) { |
| 110 | + sql += " AS " + selectBody.toString(); |
| 111 | + } else { |
| 112 | + sql += " ("; |
| 113 | + |
| 114 | + sql += PlainSelect.getStringList(columnDefinitions, true, false); |
| 115 | + if (indexes != null && indexes.size() != 0) { |
| 116 | + sql += ", "; |
| 117 | + sql += PlainSelect.getStringList(indexes); |
| 118 | + } |
| 119 | + sql += ")"; |
| 120 | + String options = PlainSelect.getStringList(tableOptionsStrings, false, false); |
| 121 | + if (options != null && options.length() > 0) { |
| 122 | + sql += " " + options; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return sql; |
| 127 | + } |
112 | 128 | } |
0 commit comments