001package gudusoft.gsqlparser.dlineage.metadata;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import gudusoft.gsqlparser.EDbVendor;
007import gudusoft.gsqlparser.sqlenv.ESQLDataObjectType;
008import gudusoft.gsqlparser.util.SQLUtil;
009
010public class Database extends TreeNode {
011
012        private String name;
013        private List<Schema> schemas;
014        private List<Synonym> synonyms;
015        private List<Sequence> sequences;
016        private List<Table> tables;
017        private List<Table> views;
018        private List<Table> others;
019        private List<Package> packages;
020        private List<Procedure> procedures;
021        private List<Procedure> functions;
022        private List<Procedure> triggers;
023        private List<Process> processes;
024
025        public String getName() {
026                return name;
027        }
028
029        public void setName(String name) {
030                if (SQLUtil.parseNames(name).size() > 1) {
031                        name = "\"" + name + "\"";
032                }
033                this.name = name;
034        }
035
036        public List<Schema> getSchemas() {
037                return schemas;
038        }
039
040        public void setSchemas(List<Schema> schemas) {
041                this.schemas = schemas;
042        }
043
044        public List<Table> getTables() {
045                return tables;
046        }
047
048        public List<Table> getViews() {
049                return views;
050        }
051
052        public void appendTables(List<Table> tables) {
053                if (tables == null) {
054                        return;
055                }
056
057                List<Table> tableList = new ArrayList<Table>();
058                List<Table> viewList = new ArrayList<Table>();
059                List<Table> otherList = new ArrayList<Table>();
060                for (Table table : tables) {
061                        if (table.getType() == null) {
062                                continue;
063                        }
064                        if ("view".equalsIgnoreCase(table.getType()) || "materialized view".equalsIgnoreCase(table.getType())) {
065                                viewList.add(table);
066                        } else if ("table".equalsIgnoreCase(table.getType())) {
067                                tableList.add(table);
068                        } else {
069                                otherList.add(table);
070                        }
071                }
072
073                if (!tableList.isEmpty()) {
074                        if (this.tables != null) {
075                                this.tables.addAll(tableList);
076                        } else {
077                                this.tables = tableList;
078                        }
079                }
080
081                if (!viewList.isEmpty()) {
082                        if (this.views != null) {
083                                this.views.addAll(viewList);
084                        } else {
085                                this.views = viewList;
086                        }
087                }
088
089                if (!otherList.isEmpty()) {
090                        if (this.others != null) {
091                                this.others.addAll(otherList);
092                        } else {
093                                this.others = otherList;
094                        }
095                }
096        }
097
098        public void appendViews(List<Table> views) {
099                if (views == null) {
100                        return;
101                }
102
103                List<Table> viewList = new ArrayList<Table>();
104                for (Table table : views) {
105                        if (table.getType() == null) {
106                                continue;
107                        }
108                        if ("view".equalsIgnoreCase(table.getType()) || "materialized view".equalsIgnoreCase(table.getType())) {
109                                viewList.add(table);
110                        }
111                }
112
113                if (!viewList.isEmpty()) {
114                        if (this.views != null) {
115                                this.views.addAll(viewList);
116                        } else {
117                                this.views = viewList;
118                        }
119                }
120        }
121
122        public List<Synonym> getSynonyms() {
123                return synonyms;
124        }
125
126        public void setSynonyms(List<Synonym> synonyms) {
127                if (this.synonyms != null) {
128                        this.synonyms.addAll(synonyms);
129                } else {
130                        this.synonyms = synonyms;
131                }
132        }
133
134        public List<Sequence> getSequences() {
135                return sequences;
136        }
137
138        public void setSequences(List<Sequence> sequences) {
139                if (this.sequences != null) {
140                        this.sequences.addAll(sequences);
141                } else {
142                        this.sequences = sequences;
143                }
144        }
145
146        public List<Package> getPackages() {
147                return packages;
148        }
149
150        public void setPackages(List<Package> packages) {
151                if (this.packages != null) {
152                        this.packages.addAll(packages);
153                } else {
154                        this.packages = packages;
155                }
156        }
157
158        public List<Procedure> getProcedures() {
159                return procedures;
160        }
161
162        public void setProcedures(List<Procedure> procedures) {
163                if (this.procedures != null) {
164                        this.procedures.addAll(procedures);
165                } else {
166                        this.procedures = procedures;
167                }
168        }
169
170        public List<Procedure> getFunctions() {
171                return functions;
172        }
173
174        public void setFunctions(List<Procedure> functions) {
175                if (this.functions != null) {
176                        this.functions.addAll(functions);
177                } else {
178                        this.functions = functions;
179                }
180        }
181
182        public List<Procedure> getTriggers() {
183                return triggers;
184        }
185
186        public void setTriggers(List<Procedure> triggers) {
187                if (this.triggers != null) {
188                        this.triggers.addAll(triggers);
189                } else {
190                        this.triggers = triggers;
191                }
192        }
193
194        public Schema appendSchema(Server server, Schema schema) {
195                if (schema == null)
196                        return null;
197
198                if (this.schemas == null) {
199                        this.schemas = new ArrayList<Schema>();
200                }
201
202                for (Schema schemaItem : schemas) {
203                        if (SQLUtil.compareIdentifier(EDbVendor.valueOf(server.getDbVendor()), ESQLDataObjectType.dotSchema,
204                                        schemaItem.getName(), schema.getName())) {
205                                return schemaItem;
206                        }
207                }
208
209                this.schemas.add(schema);
210                return schema;
211        }
212
213        public void appendTable(Table table) {
214                if (table == null || table.getType() == null) {
215                        return;
216                }
217                if ("view".equalsIgnoreCase(table.getType()) || "materialized view".equalsIgnoreCase(table.getType())) {
218                        if (views == null) {
219                                views = new ArrayList<Table>();
220                        }
221                        views.add(table);
222                } else if ("table".equalsIgnoreCase(table.getType())) {
223                        if (tables == null) {
224                                tables = new ArrayList<Table>();
225                        }
226                        tables.add(table);
227                } else {
228                        if (others == null) {
229                                others = new ArrayList<Table>();
230                        }
231                        others.add(table);
232                }
233        }
234
235        public void appendProcedure(Procedure procedure) {
236                if (procedure == null || procedure.getType() == null) {
237                        return;
238                }
239                if (procedure.getType().toLowerCase().indexOf("function") != -1) {
240                        if (functions == null) {
241                                functions = new ArrayList<Procedure>();
242                        }
243                        procedure.setType("function");
244                        functions.add(procedure);
245                } else if (procedure.getType().toLowerCase().indexOf("trigger") != -1) {
246                        if (triggers == null) {
247                                triggers = new ArrayList<Procedure>();
248                        }
249                        procedure.setType("trigger");
250                        triggers.add(procedure);
251                } else {
252                        if (procedures == null) {
253                                procedures = new ArrayList<Procedure>();
254                        }
255                        procedure.setType("procedure");
256                        procedures.add(procedure);
257                }
258        }
259
260        public List<Table> getOthers() {
261                return others;
262        }
263
264        public void appendOthers(List<Table> others) {
265                if (!others.isEmpty()) {
266                        if (this.others != null) {
267                                this.others.addAll(others);
268                        } else {
269                                this.others = others;
270                        }
271                }
272        }
273
274        public void setTables(List<Table> tables) {
275                this.tables = tables;
276        }
277
278        public void setViews(List<Table> views) {
279                this.views = views;
280        }
281
282        public void setOthers(List<Table> others) {
283                this.others = others;
284        }
285
286        public List<Process> getProcesses() {
287                return processes;
288        }
289
290        public void setProcesses(List<Process> processes) {
291                this.processes = processes;
292        }
293
294        public void appendProcess(Process process) {
295                if (process == null) {
296                        return;
297                }
298                if (processes == null) {
299                        processes = new ArrayList<Process>();
300                }
301                processes.add(process);
302        }
303}