|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2020 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.schema; |
| 11 | + |
| 12 | +import net.sf.jsqlparser.parser.ASTNodeAccessImpl; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +public class Synonym extends ASTNodeAccessImpl implements MultiPartName { |
| 19 | + |
| 20 | + private static final int NAME_IDX = 0; |
| 21 | + private static final int SCHEMA_IDX = 1; |
| 22 | + private static final int DATABASE_IDX = 2; |
| 23 | + private static final int SERVER_IDX = 3; |
| 24 | + private List<String> partItems = new ArrayList<>(); |
| 25 | + |
| 26 | + public Synonym() { |
| 27 | + } |
| 28 | + |
| 29 | + public Synonym(List<String> partItems) { |
| 30 | + this.partItems = new ArrayList<>(partItems); |
| 31 | + Collections.reverse(this.partItems); |
| 32 | + } |
| 33 | + |
| 34 | + public Database getDatabase() { |
| 35 | + return new Database(getIndex(DATABASE_IDX)); |
| 36 | + } |
| 37 | + |
| 38 | + public void setDatabase(Database database) { |
| 39 | + setIndex(DATABASE_IDX, database.getDatabaseName()); |
| 40 | + if (database.getServer() != null) { |
| 41 | + setIndex(SERVER_IDX, database.getServer().getFullyQualifiedName()); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public Synonym withDatabase(Database database) { |
| 46 | + setDatabase(database); |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public String getSchemaName() { |
| 51 | + return getIndex(SCHEMA_IDX); |
| 52 | + } |
| 53 | + |
| 54 | + public void setSchemaName(String string) { |
| 55 | + setIndex(SCHEMA_IDX, string); |
| 56 | + } |
| 57 | + |
| 58 | + public Synonym withSchemaName(String string) { |
| 59 | + setSchemaName(string); |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + public String getName() { |
| 64 | + return getIndex(NAME_IDX); |
| 65 | + } |
| 66 | + |
| 67 | + public void setName(String string) { |
| 68 | + setIndex(NAME_IDX, string); |
| 69 | + } |
| 70 | + |
| 71 | + public Synonym withName(String string) { |
| 72 | + setName(string); |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + private void setIndex(int idx, String value) { |
| 77 | + int size = partItems.size(); |
| 78 | + for (int i = 0; i < idx - size + 1; i++) { |
| 79 | + partItems.add(null); |
| 80 | + } |
| 81 | + partItems.set(idx, value); |
| 82 | + } |
| 83 | + |
| 84 | + private String getIndex(int idx) { |
| 85 | + if (idx < partItems.size()) { |
| 86 | + return partItems.get(idx); |
| 87 | + } else { |
| 88 | + return null; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String getFullyQualifiedName() { |
| 94 | + StringBuilder fqn = new StringBuilder(); |
| 95 | + |
| 96 | + for (int i = partItems.size() - 1; i >= 0; i--) { |
| 97 | + String part = partItems.get(i); |
| 98 | + if (part == null) { |
| 99 | + part = ""; |
| 100 | + } |
| 101 | + fqn.append(part); |
| 102 | + if (i != 0) { |
| 103 | + fqn.append("."); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return fqn.toString(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String toString() { |
| 112 | + StringBuilder sql = new StringBuilder(getFullyQualifiedName()); |
| 113 | + return sql.toString(); |
| 114 | + } |
| 115 | +} |
0 commit comments