001
002package gudusoft.gsqlparser.dlineage.dataflow.model;
003
004import gudusoft.gsqlparser.EExpressionType;
005import gudusoft.gsqlparser.TCustomSqlStatement;
006import gudusoft.gsqlparser.dlineage.util.DlineageUtil;
007import gudusoft.gsqlparser.nodes.*;
008import gudusoft.gsqlparser.stmt.TCreateViewSqlStatement;
009
010import java.util.Collection;
011import java.util.LinkedHashSet;
012import java.util.Set;
013
014public abstract class AbstractRelationship implements Relationship {
015
016    private long id;
017
018    private String function; 
019    
020    private EffectType effectType;
021    
022    private String partition;
023    
024    private boolean showStarRelation;
025
026    protected RelationshipElement<?> target;
027    
028    protected Set<RelationshipElement<?>> sources = new LinkedHashSet<RelationshipElement<?>>();
029    
030    private Process process;
031
032        private String sqlHash;
033        
034        private Long procedureId;
035        
036        private String sqlComment;
037
038    public AbstractRelationship() {
039        id = ++ModelBindingManager.get().RELATION_ID;
040    }
041
042    public long getId() {
043        return id;
044    }
045
046    public RelationshipElement<?> getTarget() {
047        return target;
048    }
049
050    public void setTarget(RelationshipElement<?> target) {
051        this.target = target;
052        if(target.getElement() instanceof RelationRows<?>){
053                ((RelationRows<?>)target.getElement()).holdRelation(this);
054                ((RelationRows<?>)target.getElement()).setHasRelation(true);
055        }
056
057        TCustomSqlStatement stmt = DlineageUtil.getTopStmt(ModelBindingManager.getGlobalStmtStack().peek());
058        
059                if(ModelBindingManager.getGlobalOption().isTraceSQL()) {
060                        this.sqlHash = ModelBindingManager.get().getSqlHash(stmt);
061                        try {
062                                this.sqlComment = stmt.getCommentBeforeNode();
063                        } catch (Exception e) {
064                        }
065                }
066                
067                if(ModelBindingManager.getGlobalOption().isTraceProcedure()) {
068                        Object model = ModelBindingManager.get().getModel(stmt);
069                        if(model instanceof Procedure) {
070                                this.procedureId = ((Procedure)model).getId();
071                        }
072                }
073    }
074
075    public Collection<RelationshipElement<?>> getSources() {
076        return sources;
077    }
078
079    public void addSource(RelationshipElement<?> source) {
080        addSource(source, true);
081    }
082    
083    public void addSource(RelationshipElement<?> source, boolean inferStar) {
084        if (source != null && !sources.contains(source)) {
085            sources.add(source);
086            
087            Object obj = source.getElement();
088                        if (obj instanceof ResultColumn) {
089                                ResultSet resultSet = ((ResultColumn) obj).getResultSet();
090                                if (resultSet.isTarget() && !(resultSet instanceof Function)) {
091                                        resultSet.setTarget(false);
092                                }
093                        }
094            
095            if(getTarget() instanceof ResultColumnRelationshipElement) {
096                ResultColumn column = ((ResultColumnRelationshipElement)(getTarget())).getElement();
097                                if (column.getTransform() != null) {
098                                        source.addTransform(column.getTransform().getType(), column.getTransform().getCode());
099                                } else if (column.getColumnObject() instanceof TResultColumn) {
100                                        TResultColumn columnObject = (TResultColumn) column.getColumnObject();
101                                        TExpression expression = columnObject.getExpr();
102                                        addExpressionTransform(source, expression);
103                                } else if (column instanceof FunctionResultColumn) {
104                                        FunctionResultColumn functionColumn = (FunctionResultColumn) column;
105                                        if (functionColumn.getFunction() instanceof TFunctionCall) {
106                                                source.addTransform(Transform.FUNCTION, functionColumn.getFunction());
107                                        }
108                                        if (functionColumn.getFunction() instanceof TCaseExpression) {
109                                                source.addTransform(Transform.CASE, functionColumn.getFunction());
110                                        }
111                                } else if (column.getColumnObject() instanceof TObjectName) {
112                                        source.addTransform(Transform.SIMPLE, (TObjectName) column.getColumnObject());
113                                }
114                                
115                                if(source.getElement() instanceof ResultColumn) {
116                                        ResultColumn sourceColumn = (ResultColumn)source.getElement();
117                                        if(sourceColumn.getName().equals("*") && column.getName().equals("*") && inferStar) {
118                                                column.appendStarSourceColumn(sourceColumn);
119                                        }
120                                }
121                                else if(source.getElement() instanceof TableColumn) {
122                                        TableColumn sourceColumn = (TableColumn)source.getElement();
123                                        if(sourceColumn.getName().equals("*") && column.getName().equals("*") && inferStar) {
124                                                column.appendStarSourceColumn(sourceColumn);
125                                        }
126                                }
127            }
128            else if(getTarget() instanceof ViewColumnRelationshipElement) {
129                TableColumn column = ((ViewColumnRelationshipElement)(getTarget())).getElement();
130                if(column.getTable()!=null && column.getTable().getSqlStatement() instanceof TCreateViewSqlStatement) {
131                        TCreateViewSqlStatement view = (TCreateViewSqlStatement)column.getTable().getSqlStatement();
132                                        if (view.getSubquery() != null) {
133                                                if (column.getColumnObject() != null) {
134                                                        source.addTransform(Transform.SIMPLE, column.getColumnObject());
135                                                }
136                                        }
137                }
138                
139                if(source.getElement() instanceof ResultColumn) {
140                                        ResultColumn sourceColumn = (ResultColumn)source.getElement();
141                                        if(sourceColumn.getName().equals("*") && column.getName().equals("*") && inferStar) {
142                                                column.appendStarSourceColumn(sourceColumn);
143                                        }
144                                }
145                                else if(source.getElement() instanceof TableColumn) {
146                                        TableColumn sourceColumn = (TableColumn)source.getElement();
147                                        if(sourceColumn.getName().equals("*") && column.getName().equals("*") && inferStar) {
148                                                column.appendStarSourceColumn(sourceColumn);
149                                        }
150                                }
151            } 
152                        else if (getTarget() instanceof TableColumnRelationshipElement) {
153                                if (source.getElement() instanceof ResultColumn) {
154                                        ResultColumn column = (ResultColumn) source.getElement();
155                                        if (column.getTransform() != null) {
156                                                source.addTransform(column.getTransform().getType(), column.getTransform().getCode());
157                                        }
158                                }
159                        } 
160            else if (getTarget() == null && source.getElement() instanceof TableColumn) {
161                                TableColumn column = (TableColumn) source.getElement();
162                                if (column.getTransform() != null) {
163                                        source.addTransform(column.getTransform().getType(), column.getTransform().getCode());
164                                }
165                        }
166            
167            if(source.getElement() instanceof RelationRows<?>){
168                ((RelationRows<?>)source.getElement()).setHasRelation(true);
169            }
170        }
171    }
172
173        protected void addExpressionTransform(RelationshipElement<?> source, TExpression expression) {
174                if (expression.getExpressionType() == EExpressionType.simple_object_name_t) {
175                        source.addTransform(Transform.SIMPLE, expression);
176                }
177                else if (expression.getExpressionType() == EExpressionType.function_t) {
178                        source.addTransform(Transform.FUNCTION, expression);
179                }
180                else if (expression.getExpressionType() == EExpressionType.simple_constant_t) {
181                        source.addTransform(Transform.FUNCTION, expression);
182                }
183                else if (expression.getExpressionType() == EExpressionType.case_t) {
184                        source.addTransform(Transform.CASE, expression);
185                }
186                else if (expression.getExpressionType() == EExpressionType.subquery_t) {
187                        source.addTransform(Transform.SUBQUERY, expression);
188                }
189                else if (expression.getExpressionType() == EExpressionType.parenthesis_t) {
190                        addExpressionTransform(source, expression.getLeftOperand());
191                }
192                else if (expression.getOperatorToken() != null) {
193                        source.addTransform(Transform.EXPRESSION, expression);
194                }
195        }
196
197    public EffectType getEffectType() {
198        return effectType;
199    }
200
201    public void setEffectType(EffectType effectType) {
202        this.effectType = effectType;
203    }
204
205        public String getFunction() {
206                return function;
207        }
208
209        public void setFunction(String function) {
210                this.function = function;
211        }
212
213        public boolean isShowStarRelation() {
214                return showStarRelation;
215        }
216
217        public void setShowStarRelation(boolean showStarRelation) {
218                this.showStarRelation = showStarRelation;
219        }
220
221        public Process getProcess() {
222                return process;
223        }
224
225        public void setProcess(Process process) {
226                this.process = process;
227        }
228
229
230        public String getSqlHash() {
231                return sqlHash;
232        }
233
234        public String getSqlComment() {
235                return sqlComment;
236        }
237
238        public String getPartition() {
239                return partition;
240        }
241
242        public void setPartition(String partition) {
243                this.partition = partition;
244        }
245
246        public Long getProcedureId() {
247                return procedureId;
248        }
249
250        public void setProcedureId(Long procedureId) {
251                this.procedureId = procedureId;
252        }
253        
254}