1212import net .sf .jsqlparser .schema .Column ;
1313import net .sf .jsqlparser .schema .Table ;
1414import net .sf .jsqlparser .statement .imprt .ImportColumn ;
15+ import net .sf .jsqlparser .statement .create .table .TableElement ;
1516import net .sf .jsqlparser .statement .select .PlainSelect ;
1617import net .sf .jsqlparser .statement .select .SelectItem ;
1718
1819import java .io .Serializable ;
20+ import java .util .ArrayList ;
1921import java .util .List ;
2022
2123/**
22- * Exasol Like Clause
24+ * A LIKE clause used in table definitions and Exasol imports.
2325 *
2426 * @see <a href="https://docs.exasol.com/db/latest/sql/create_table.htm">Like Clause in CREATE
2527 * TABLE</a>
2628 * @see <a href="https://docs.exasol.com/db/latest/sql/import.htm">Like Clause in IMPORT</a>
2729 */
28- public class LikeClause implements ImportColumn , Serializable {
30+ public class LikeClause implements ImportColumn , TableElement , Serializable {
2931 private Table table ;
3032 private List <SelectItem <Column >> columnsList ;
3133
32- private Boolean includingDefaults ;
33- private Boolean includingIdentity ;
34- private Boolean includingComments ;
34+ private List <Option > options = new ArrayList <>();
35+
36+ public enum OptionKind {
37+ ALL , COMMENTS , COMPRESSION , CONSTRAINTS , DEFAULTS , GENERATED , IDENTITY , INDEXES , STATISTICS , STORAGE
38+ }
39+
40+ public static class Option implements Serializable {
41+ private final OptionKind kind ;
42+ private final boolean including ;
43+
44+ public Option (OptionKind kind , boolean including ) {
45+ this .kind = kind ;
46+ this .including = including ;
47+ }
48+
49+ public OptionKind getKind () {
50+ return kind ;
51+ }
52+
53+ public boolean isIncluding () {
54+ return including ;
55+ }
56+
57+ @ Override
58+ public String toString () {
59+ return (including ? "INCLUDING " : "EXCLUDING " ) + kind ;
60+ }
61+ }
62+
63+ /** Options in source order, including repeated options and ALL. */
64+ public List <Option > getOptions () {
65+ return options ;
66+ }
67+
68+ public void setOptions (List <Option > options ) {
69+ this .options = options == null ? new ArrayList <>() : new ArrayList <>(options );
70+ }
71+
72+ public void addOption (OptionKind kind , boolean including ) {
73+ options .add (new Option (kind , including ));
74+ }
75+
76+ /** Resolves ALL and explicit options in declaration order; omission defaults to exclusion. */
77+ public boolean isIncluding (OptionKind kind ) {
78+ boolean including = false ;
79+ for (Option option : options ) {
80+ if (option .getKind () == kind || option .getKind () == OptionKind .ALL ) {
81+ including = option .isIncluding ();
82+ }
83+ }
84+ return including ;
85+ }
86+
87+ private Boolean explicitIncluding (OptionKind kind ) {
88+ Boolean including = null ;
89+ for (Option option : options ) {
90+ if (option .getKind () == kind ) {
91+ including = option .isIncluding ();
92+ }
93+ }
94+ return including ;
95+ }
96+
97+ private void setIncluding (OptionKind kind , Boolean including ) {
98+ options .removeIf (option -> option .getKind () == kind );
99+ if (including != null ) {
100+ addOption (kind , including );
101+ }
102+ }
35103
36104 public Table getTable () {
37105 return table ;
@@ -50,51 +118,51 @@ public void setColumnsList(List<SelectItem<Column>> columnsList) {
50118 }
51119
52120 public Boolean isIncludingDefaults () {
53- return includingDefaults ;
121+ return explicitIncluding ( OptionKind . DEFAULTS ) ;
54122 }
55123
56124 public void setIncludingDefaults (Boolean includingDefaults ) {
57- this . includingDefaults = includingDefaults ;
125+ setIncluding ( OptionKind . DEFAULTS , includingDefaults ) ;
58126 }
59127
60128 public Boolean isExcludingDefaults () {
61- return includingDefaults == null ? null : !includingDefaults ;
129+ return isIncludingDefaults () == null ? null : !isIncludingDefaults () ;
62130 }
63131
64132 public void setExcludingDefaults (Boolean excludingDefaults ) {
65- this . includingDefaults = !excludingDefaults ;
133+ setIncludingDefaults ( excludingDefaults == null ? null : !excludingDefaults ) ;
66134 }
67135
68136 public Boolean isIncludingIdentity () {
69- return includingIdentity ;
137+ return explicitIncluding ( OptionKind . IDENTITY ) ;
70138 }
71139
72140 public void setIncludingIdentity (Boolean includingIdentity ) {
73- this . includingIdentity = includingIdentity ;
141+ setIncluding ( OptionKind . IDENTITY , includingIdentity ) ;
74142 }
75143
76144 public Boolean isExcludingIdentity () {
77- return includingIdentity == null ? null : !includingIdentity ;
145+ return isIncludingIdentity () == null ? null : !isIncludingIdentity () ;
78146 }
79147
80148 public void setExcludingIdentity (Boolean excludingIdentity ) {
81- this . includingIdentity = !excludingIdentity ;
149+ setIncludingIdentity ( excludingIdentity == null ? null : !excludingIdentity ) ;
82150 }
83151
84152 public Boolean isIncludingComments () {
85- return includingComments ;
153+ return explicitIncluding ( OptionKind . COMMENTS ) ;
86154 }
87155
88156 public void setIncludingComments (Boolean includingComments ) {
89- this . includingComments = includingComments ;
157+ setIncluding ( OptionKind . COMMENTS , includingComments ) ;
90158 }
91159
92160 public Boolean isExcludingComments () {
93- return includingComments == null ? null : !includingComments ;
161+ return isIncludingComments () == null ? null : !isIncludingComments () ;
94162 }
95163
96164 public void setExcludingComments (Boolean excludingComments ) {
97- this . includingComments = !excludingComments ;
165+ setIncludingComments ( excludingComments == null ? null : !excludingComments ) ;
98166 }
99167
100168 public StringBuilder appendTo (StringBuilder builder ) {
@@ -105,38 +173,15 @@ public StringBuilder appendTo(StringBuilder builder) {
105173 PlainSelect .appendStringListTo (builder , columnsList , true , true );
106174 }
107175
108- if (includingDefaults != null ) {
109- if (includingDefaults ) {
110- builder .append (" INCLUDING " );
111- } else {
112- builder .append (" EXCLUDING " );
113- }
114- builder .append (" DEFAULTS " );
115- }
116-
117- if (includingIdentity != null ) {
118- if (includingIdentity ) {
119- builder .append (" INCLUDING " );
120- } else {
121- builder .append (" EXCLUDING " );
122- }
123- builder .append (" IDENTITY " );
124- }
125-
126- if (includingComments != null ) {
127- if (includingComments ) {
128- builder .append (" INCLUDING " );
129- } else {
130- builder .append (" EXCLUDING " );
131- }
132- builder .append (" COMMENTS " );
176+ for (Option option : options ) {
177+ builder .append (' ' ).append (option );
133178 }
134179
135180 return builder ;
136181 }
137182
138183 @ Override
139184 public String toString () {
140- return appendTo (new StringBuilder ()).toString ();
185+ return appendTo (new StringBuilder ()).toString (). trim () ;
141186 }
142187}
0 commit comments