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