001package gudusoft.gsqlparser.dlineage.dataflow.model.xml;
002
003import gudusoft.gsqlparser.dlineage.dataflow.model.ModelBindingManager;
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.XmlElement;
009import javax.xml.bind.annotation.XmlTransient;
010import javax.xml.bind.annotation.XmlType;
011import java.util.ArrayList;
012import java.util.Comparator;
013import java.util.List;
014import java.util.TreeSet;
015
016@XmlType(
017        propOrder = {"id", "server", "userName", "database", "schema", "name", "displayName", "alias", "uri", "type", "subType",
018                "processIds", "fileType", "fileFormat", "location", "namespace", "isTarget", "coordinate", "columns", "parent", "more", "fromDDL"}
019)
020public class table implements Cloneable {
021
022    private String id;
023
024    private String server;
025
026    private String userName;
027
028    private String database;
029
030    private String schema;
031
032    private String name;
033
034    private String displayName;
035
036    private String alias;
037
038    private String type;
039
040    private String subType;
041
042    private String uri;
043
044    private List<String> processIds;
045
046    private String isTarget;
047
048    private StringBuffer coordinate = new StringBuffer();
049
050    private List<column> columns;
051
052    private String parent;
053
054    private String fileType;
055
056    private String fileFormat;
057
058    private String location;
059
060    private String namespace;
061
062    @XmlTransient
063    private String starStmt;
064
065    private Boolean more;
066    
067    @XmlTransient
068    private String isDetermined;
069
070    private String fromDDL;
071
072    @XmlTransient
073    private TreeSet<String> coordinateItems = new TreeSet<String>(new Comparator<String>() {
074
075        @Override
076        public int compare(String o1, String o2) {
077            Coordinate[] c1 = Coordinate.parse(o1);
078            Coordinate[] c2 = Coordinate.parse(o2);
079            if (c1[0].getY() == -1 && c2[0].getY() != -1) {
080                return 1;
081            }
082            if (c1[0].getY() != -1 && c2[0].getY() == -1) {
083                return -1;
084            }
085            return Long.compare(c1[0].getY(), c1[0].getY());
086        }
087    });
088
089    @XmlAttribute(required = false)
090    public String getAlias() {
091        return alias;
092    }
093
094    public void setAlias(String alias) {
095        this.alias = alias;
096    }
097
098    @XmlElement(name = "column", required = false)
099    public List<column> getColumns() {
100        if (this.columns == null) {
101            this.columns = new ArrayList<column>();
102        }
103        return columns;
104    }
105
106    public void setColumns(List<column> columns) {
107        this.columns = columns;
108    }
109
110    @XmlAttribute(required = false)
111    public String getCoordinate() {
112        if (ModelBindingManager.getGlobalOption() != null && ModelBindingManager.getGlobalOption().isIgnoreCoordinate()) {
113            return null;
114        }
115        String result = coordinate.toString();
116        if (SQLUtil.isEmpty(result))
117            return null;
118        return result;
119    }
120
121    public void appendCoordinate(String coordinate) {
122        if (ModelBindingManager.getGlobalOption() != null && ModelBindingManager.getGlobalOption().isIgnoreCoordinate()) {
123            return;
124        }
125        if (!coordinateItems.contains(coordinate)) {
126            coordinateItems.add(coordinate);
127            this.coordinate.setLength(0);
128            for (String item : coordinateItems) {
129                if (this.coordinate.length() > 0) {
130                    if (item.indexOf("-1") != -1) {
131                        continue;
132                    }
133                    this.coordinate.append(",");
134                }
135                this.coordinate.append(item);
136            }
137        }
138    }
139
140    public void setCoordinate(String coordinate) {
141        if (ModelBindingManager.getGlobalOption() != null && ModelBindingManager.getGlobalOption().isIgnoreCoordinate()) {
142            return;
143        }
144        this.coordinate.setLength(0);
145        this.coordinate.append(coordinate);
146    }
147
148    @XmlAttribute(required = false)
149    public String getUserName() {
150        return userName;
151    }
152
153    public void setUserName(String userName) {
154        this.userName = userName;
155    }
156
157    @XmlAttribute(required = false)
158    public String getServer() {
159        return server;
160    }
161
162    public void setServer(String server) {
163        this.server = server;
164    }
165
166    @XmlAttribute(required = false)
167    public String getName() {
168        return name;
169    }
170
171    public void setName(String name) {
172        this.name = name;
173    }
174
175    @XmlAttribute(required = false)
176    public String getDisplayName() {
177        return displayName;
178    }
179
180    public void setDisplayName(String displayName) {
181        this.displayName = displayName;
182    }
183
184    @XmlAttribute(required = false)
185    public String getId() {
186        return id;
187    }
188
189    public void setId(String id) {
190        this.id = id;
191    }
192
193    @XmlAttribute(required = false)
194    public List<String> getProcessIds() {
195        return processIds;
196    }
197
198    public void setProcessIds(List<String> processIds) {
199        this.processIds = processIds;
200    }
201
202    @XmlAttribute(required = false)
203    public String getType() {
204        return type;
205    }
206
207    public void setType(String type) {
208        this.type = type;
209    }
210
211    @XmlAttribute(required = false)
212    public String getUri() {
213        return uri;
214    }
215
216    public void setUri(String uri) {
217        this.uri = uri;
218    }
219
220    @XmlAttribute(required = false)
221    public String getFileType() {
222        return fileType;
223    }
224
225    public void setFileType(String fileType) {
226        this.fileType = fileType;
227    }
228
229    @XmlAttribute(required = false)
230    public String getFileFormat() {
231        return fileFormat;
232    }
233
234    public void setFileFormat(String fileFormat) {
235        this.fileFormat = fileFormat;
236    }
237
238    @XmlAttribute(required = false)
239    public String getLocation() {
240        return location;
241    }
242
243    public void setLocation(String location) {
244        this.location = location;
245    }
246
247    @XmlAttribute(required = false)
248    public String getNamespace() {
249        return namespace;
250    }
251
252    public void setNamespace(String namespace) {
253        this.namespace = namespace;
254    }
255
256    @XmlTransient
257    public String getStarStmt() {
258        return starStmt;
259    }
260
261    public void setStarStmt(String starStmt) {
262        this.starStmt = starStmt;
263    }
264
265    @XmlTransient
266    public String getIsDetermined() {
267                return isDetermined;
268        }
269
270        public void setIsDetermined(String isDetermined) {
271                this.isDetermined = isDetermined;
272        }
273
274        public boolean isFunction() {
275        return "function".equals(type) || "function".equals(subType);
276    }
277
278    public boolean isView() {
279        return "view".equals(type);
280    }
281
282    public boolean isDatabaseType() {
283        return "database".equals(type);
284    }
285
286    public boolean isSchemaType() {
287        return "schema".equals(type);
288    }
289
290    public boolean isSequence() {
291        return "sequence".equals(type);
292    }
293
294    public boolean isStage() {
295        return "stage".equals(type);
296    }
297
298    public boolean isDataSource() {
299        return "dataSource".equals(type);
300    }
301
302    public boolean isStream() {
303        return "stream".equals(type);
304    }
305
306    public boolean isVariable() {
307        return "variable".equals(type);
308    }
309
310    public boolean isCursor() {
311        return "cursor".equals(type);
312    }
313
314    public boolean isFile() {
315        return "file".equals(type) || "path".equals(type);
316    }
317
318    public boolean isTable() {
319        return "table".equals(type) || "pseudoTable".equals(type) || "constantTable".equals(type);
320    }
321
322    public boolean isPseudoTable() {
323        return "pseudoTable".equals(type);
324    }
325
326    public boolean isConstantTable() {
327        return "pseudoTable".equals(type);
328    }
329
330    public boolean isResultSet() {
331        return type != null && !isView() && !isCursor() && !isTable() && !isStage() && !isSequence() && !isDataSource() && !isDatabaseType() && !isSchemaType() && !isStream() && !isVariable() && !isFile();
332    }
333
334    @XmlAttribute(name = "isTarget", required = false)
335    public String getIsTarget() {
336        return isTarget;
337    }
338
339    public boolean isTarget() {
340        return "true".equals(isTarget);
341    }
342
343    @XmlAttribute(required = false)
344    public String getParent() {
345        return parent;
346    }
347
348    public void setParent(String parent) {
349        this.parent = parent;
350    }
351
352    @XmlAttribute(required = false)
353    public String getDatabase() {
354        return database;
355    }
356
357    public void setDatabase(String database) {
358        if (SQLUtil.parseNames(database).size() > 1) {
359            database = "\"" + database + "\"";
360        }
361        this.database = database;
362    }
363
364    @XmlAttribute(required = false)
365    public String getSchema() {
366        return schema;
367    }
368
369    public void setSchema(String schema) {
370        if (SQLUtil.parseNames(schema).size() > 1) {
371            schema = "\"" + schema + "\"";
372        }
373        this.schema = schema;
374    }
375
376    @XmlAttribute(required = false)
377    public String getSubType() {
378        return subType;
379    }
380
381    public void setSubType(String subType) {
382        this.subType = subType;
383    }
384
385    public String getFullName() {
386        if (isDatabaseType()) {
387            return database;
388        }
389        StringBuilder fullName = new StringBuilder();
390        if (!SQLUtil.isEmpty(database)) {
391            fullName.append(database).append(".");
392        }
393        if (!SQLUtil.isEmpty(schema)) {
394            fullName.append(schema).append(".");
395        }
396        if (fullName.length() > 0) {
397            fullName.append(getTableNameOnly());
398        } else {
399            fullName.append(name);
400        }
401        return fullName.toString();
402    }
403
404    public String getTableNameOnly() {
405        if (name.indexOf("@") != -1 && SQLUtil.trimColumnStringQuote(name.substring(name.lastIndexOf("@") + 1).trim()).equals(SQLUtil.trimColumnStringQuote(database))) {
406            List<String> segments = SQLUtil.parseNames(name.substring(0, name.lastIndexOf("@")).trim());
407            if (segments.size() > 2) {
408                return SQLUtil.mergeSegments(segments, 2);
409            }
410            return segments.get(segments.size() - 1);
411        } else {
412            List<String> segments = SQLUtil.parseNames(name);
413            if (segments.size() > 2) {
414                return SQLUtil.mergeSegments(segments, 2);
415            }
416            return segments.get(segments.size() - 1);
417        }
418    }
419
420    public void setIsTarget(String isTarget) {
421        this.isTarget = isTarget;
422    }
423
424    public int getOccurrencesNumber() {
425        return PositionUtil.getOccurrencesNumber(coordinate.toString());
426    }
427
428    public Coordinate getStartPos(int index) {
429        return PositionUtil.getStartPos(coordinate.toString(), index);
430    }
431
432    public Coordinate getEndPos(int index) {
433        return PositionUtil.getEndPos(coordinate.toString(), index);
434    }
435
436    public Boolean getMore() {
437        return more;
438    }
439
440    public void setMore(Boolean more) {
441        this.more = more;
442    }
443
444    @XmlAttribute(required = false)
445    public String getFromDDL() {
446        return fromDDL;
447    }
448
449    public void setFromDDL(String fromDDL) {
450        this.fromDDL = fromDDL;
451    }
452
453    @Override
454    public Object clone() throws CloneNotSupportedException {
455        return super.clone();
456    }
457}