001
002package gudusoft.gsqlparser.dlineage.dataflow.model.xml;
003
004import gudusoft.gsqlparser.dlineage.dataflow.model.json.Coordinate;
005import gudusoft.gsqlparser.util.SQLUtil;
006
007import javax.xml.bind.annotation.XmlAttribute;
008import javax.xml.bind.annotation.XmlTransient;
009import javax.xml.bind.annotation.XmlType;
010import java.util.Comparator;
011import java.util.Objects;
012import java.util.TreeSet;
013
014@XmlType(propOrder = { "id", "name", "displayName", "coordinate", "source", "qualifiedTable", "isFunction", "dataType", "primaryKey", "unqiueKey", "indexKey", "foreignKey" })
015public class column {
016        private gudusoft.gsqlparser.sqlenv.CanonKey identifierKey;
017
018        /** Producer-only identity; never serialized or reconstructed from display text. */
019        @XmlTransient
020        public gudusoft.gsqlparser.sqlenv.CanonKey getIdentifierKey() {
021                return "system".equals(source) ? null : identifierKey;
022        }
023
024        public void setIdentifierKey(gudusoft.gsqlparser.sqlenv.CanonKey identifierKey) {
025                this.identifierKey = identifierKey;
026        }
027
028        private Object fieldPathKey;
029
030        /**
031         * Producer-only: the structural identity of a STRUCT field path node, its declared column and its path, by which
032         * an output merge tells two field nodes apart whose displays collide ({@code s."a.b".c} and {@code s.a.b.c}) and
033         * keeps one node for two spellings of one field; null for any other column. Never serialized (plan section 4.7).
034         */
035        @XmlTransient
036        public Object getFieldPathKey() {
037                return fieldPathKey;
038        }
039
040        public void setFieldPathKey(Object fieldPathKey) {
041                this.fieldPathKey = fieldPathKey;
042        }
043
044        private String sqlSpelling;
045
046        /**
047         * Producer-only: the column's spelling as SQL when the producer knows it (the column a star link of an identity
048         * names), from which an output merge gives two columns of one table whose names collide names that tell them
049         * apart. Never serialized.
050         */
051        @XmlTransient
052        public String getSqlSpelling() {
053                return sqlSpelling;
054        }
055
056        public void setSqlSpelling(String sqlSpelling) {
057                this.sqlSpelling = sqlSpelling;
058        }
059
060        private String id;
061
062        private String name;
063
064        private String displayName;
065
066        @XmlTransient
067        private TreeSet<String> coordinateItems = new TreeSet<String>(new Comparator<String>() {
068
069                @Override
070                public int compare(String o1, String o2) {
071                        Coordinate[] c1 = Coordinate.parse(o1);
072                        Coordinate[] c2 = Coordinate.parse(o2);
073                        if(c1[0].getY() == -1 && c2[0].getY()!=-1){
074                                return 1;
075                        }
076                        if(c1[0].getY() != -1 && c2[0].getY()==-1){
077                                return -1;
078                        }
079                        return Long.compare(c1[0].getY(), c1[0].getY());
080                }
081        });
082
083        private StringBuffer coordinate = new StringBuffer();
084
085        private String source;
086
087        private String qualifiedTable;
088
089        private String isFunction;
090
091        private String dataType;
092
093        private Boolean primaryKey;
094
095        private Boolean unqiueKey;
096
097        private Boolean indexKey;
098
099        private Boolean foreignKey;
100
101        private Boolean nullable;
102
103        private Boolean identity;
104
105        private Boolean computed;
106
107        private Boolean persisted;
108
109        private String computedDefinition;
110
111        private String defaultValue;
112
113        private String defaultName;
114
115        private Boolean star;
116
117        /**
118         * Whether this column stands for a star ({@code *}, {@code t.*}, a whole-row pseudo column). Output version 1
119         * names a star {@code *}; a column NAMED {@code *} (SQL Server {@code [*]}, BigQuery {@code `*`}) is shown the
120         * same way, so the document says {@code star="false"} on it. Never decided from the name alone.
121         */
122        public boolean standsForStar() {
123                return "*".equals(name) && !Boolean.FALSE.equals(star);
124        }
125
126        /** The star marker as written: {@code false} on a column named {@code *} that is not a star, else absent. */
127        @XmlAttribute(name = "star", required = false)
128        public Boolean getStar() {
129                return star;
130        }
131
132        public void setStar(Boolean star) {
133                this.star = star;
134        }
135
136        @XmlAttribute(required = false)
137        public String getCoordinate() {
138                String result = coordinate.toString();
139                if(SQLUtil.isEmpty(result))
140                        return null;
141                return result;
142        }
143
144        public void appendCoordinate(String coordinate) {
145                if(SQLUtil.isEmpty(coordinate)){
146                        return;
147                }
148                if (!coordinateItems.contains(coordinate)) {
149                        coordinateItems.add(coordinate);
150                        this.coordinate.setLength(0);
151                        for(String item:coordinateItems){
152                                if (this.coordinate.length() > 0) {
153                                        if (item.indexOf("-1") != -1) {
154                                                continue;
155                                        }
156                                        this.coordinate.append(",");
157                                }
158                                this.coordinate.append(item);
159                        }
160                }
161        }
162
163        public void setCoordinate(String coordinate) {
164                if(SQLUtil.isEmpty(coordinate)){
165                        return;
166                }
167                this.coordinate.setLength(0);
168                this.coordinate.append(coordinate);
169        }
170        
171    public void clearCoordinate() {
172        this.coordinate = new StringBuffer();
173    }
174
175
176        @XmlAttribute(required = false)
177        public String getName() {
178                return name;
179        }
180
181        public void setName(String name) {
182                this.name = name;
183        }
184
185        @XmlAttribute(required = false)
186        public String getDisplayName() {
187                return displayName;
188        }
189
190        public void setDisplayName(String displayName) {
191                this.displayName = displayName;
192        }
193
194        @XmlAttribute(required = false)
195        public String getId() {
196                return id;
197        }
198
199        public void setId(String id) {
200                this.id = id;
201        }
202
203        @XmlAttribute(required = false)
204        public String getSource() {
205                return source;
206        }
207
208        public void setSource(String source) {
209                this.source = source;
210        }
211
212        @XmlAttribute(required = false)
213        public String getQualifiedTable() {
214                return qualifiedTable;
215        }
216
217        public void setQualifiedTable(String qualifiedTable) {
218                this.qualifiedTable = qualifiedTable;
219        }
220
221        @Override
222        public int hashCode() {
223                final int prime = 31;
224                int result = 1;
225                result = prime * result + ((coordinate == null) ? 0 : coordinate.toString().hashCode());
226                result = prime * result + ((id == null) ? 0 : id.hashCode());
227                result = prime * result + ((name == null) ? 0 : name.hashCode());
228                result = prime * result + ((qualifiedTable == null) ? 0 : qualifiedTable.hashCode());
229                result = prime * result + ((source == null) ? 0 : source.hashCode());
230                return result;
231        }
232
233        @Override
234        public boolean equals(Object obj) {
235                if (this == obj)
236                        return true;
237                if (obj == null)
238                        return false;
239                if (getClass() != obj.getClass())
240                        return false;
241                column other = (column) obj;
242                if (coordinate == null) {
243                        if (other.coordinate != null)
244                                return false;
245                } else if (!Objects.equals(coordinate.toString(),other.coordinate.toString()))
246                        return false;
247                if (id == null) {
248                        if (other.id != null)
249                                return false;
250                } else if (!id.equals(other.id))
251                        return false;
252                if (name == null) {
253                        if (other.name != null)
254                                return false;
255                } else if (!name.equals(other.name))
256                        return false;
257                if (qualifiedTable == null) {
258                        if (other.qualifiedTable != null)
259                                return false;
260                } else if (!qualifiedTable.equals(other.qualifiedTable))
261                        return false;
262                if (source == null) {
263                        if (other.source != null)
264                                return false;
265                } else if (!source.equals(other.source))
266                        return false;
267                return true;
268        }
269
270        @XmlAttribute(name = "isFunction", required = false)
271        public String getIsFunction() {
272                return isFunction;
273        }
274
275        public void setIsFunction(String isFunction) {
276                this.isFunction = isFunction;
277        }
278
279        public int getOccurrencesNumber() {
280                return PositionUtil.getOccurrencesNumber(coordinate.toString());
281        }
282
283        public Coordinate getStartPos(int index) {
284                return PositionUtil.getStartPos(coordinate.toString(), index);
285        }
286
287        public Coordinate getEndPos(int index) {
288                return PositionUtil.getEndPos(coordinate.toString(), index);
289        }
290
291        @XmlAttribute(name = "primaryKey", required = false)
292        public Boolean isPrimaryKey() {
293                return primaryKey;
294        }
295
296        public void setPrimaryKey(Boolean primaryKey) {
297                if(primaryKey == null || Boolean.FALSE.equals(primaryKey)){
298                        return;
299                }
300                this.primaryKey = primaryKey;
301        }
302
303        @XmlAttribute(name = "unqiueKey", required = false)
304        public Boolean isUnqiueKey() {
305                return unqiueKey;
306        }
307
308        public void setUnqiueKey(Boolean unqiueKey) {
309                if(unqiueKey == null || Boolean.FALSE.equals(unqiueKey)){
310                        return;
311                }
312                this.unqiueKey = unqiueKey;
313        }
314
315        @XmlAttribute(name = "indexKey", required = false)
316        public Boolean isIndexKey() {
317                return indexKey;
318        }
319
320        public void setIndexKey(Boolean indexKey) {
321                if(indexKey == null || Boolean.FALSE.equals(indexKey)){
322                        return;
323                }
324                this.indexKey = indexKey;
325        }
326
327        @XmlAttribute(name = "foreignKey", required = false)
328        public Boolean isForeignKey() {
329                return foreignKey;
330        }
331
332        public void setForeignKey(Boolean foreignKey) {
333                if(foreignKey == null || Boolean.FALSE.equals(foreignKey)){
334                        return;
335                }
336                this.foreignKey = foreignKey;
337        }
338
339        @XmlAttribute(name = "dataType", required = false)
340        public String getDataType() {
341                return dataType;
342        }
343
344        public void setDataType(String dataType) {
345                this.dataType = dataType;
346        }
347
348        @XmlAttribute(name = "nullable", required = false)
349        public Boolean isNullable() {
350                return nullable;
351        }
352
353        public void setNullable(Boolean nullable) {
354                this.nullable = nullable;
355        }
356
357        @XmlAttribute(name = "identity", required = false)
358        public Boolean isIdentity() {
359                return identity;
360        }
361
362        public void setIdentity(Boolean identity) {
363                this.identity = identity;
364        }
365
366        @XmlAttribute(name = "computed", required = false)
367        public Boolean isComputed() {
368                return computed;
369        }
370
371        public void setComputed(Boolean computed) {
372                this.computed = computed;
373        }
374
375        @XmlAttribute(name = "persisted", required = false)
376        public Boolean isPersisted() {
377                return persisted;
378        }
379
380        public void setPersisted(Boolean persisted) {
381                this.persisted = persisted;
382        }
383
384        @XmlAttribute(name = "computedDefinition", required = false)
385        public String getComputedDefinition() {
386                return computedDefinition;
387        }
388
389        public void setComputedDefinition(String computedDefinition) {
390                this.computedDefinition = computedDefinition;
391        }
392
393        @XmlAttribute(name = "defaultValue", required = false)
394        public String getDefaultValue() {
395                return defaultValue;
396        }
397
398        public void setDefaultValue(String defaultValue) {
399                this.defaultValue = defaultValue;
400        }
401
402        @XmlAttribute(name = "defaultName", required = false)
403        public String getDefaultName() {
404                return defaultName;
405        }
406
407        public void setDefaultName(String defaultName) {
408                this.defaultName = defaultName;
409        }
410}