001package gudusoft.gsqlparser.sqlenv;
002
003import java.util.Objects;
004
005/**
006 * Canonical key for indexing schema objects.
007 * All fields must be canonicalized before constructing (vendor/type aware).
008 */
009public final class NameKey {
010
011    public final ESQLDataObjectType objectType;
012    public final String server;   // never null
013    public final String catalog;  // never null
014    public final String schema;   // never null
015    public final String object;   // never null
016
017    public NameKey(ESQLDataObjectType objectType, String server, String catalog, String schema, String object) {
018        this.objectType = Objects.requireNonNull(objectType, "objectType");
019        this.server = server == null ? TSQLEnv.DEFAULT_SERVER_NAME : server;
020        this.catalog = catalog == null ? TSQLEnv.DEFAULT_DB_NAME : catalog;
021        this.schema = schema == null ? TSQLEnv.DEFAULT_SCHEMA_NAME : schema;
022        this.object = Objects.requireNonNull(object, "object");
023    }
024
025    @Override
026    public boolean equals(Object o) {
027        if (this == o) return true;
028        if (!(o instanceof NameKey)) return false;
029        NameKey nameKey = (NameKey) o;
030        return objectType == nameKey.objectType
031                && server.equals(nameKey.server)
032                && catalog.equals(nameKey.catalog)
033                && schema.equals(nameKey.schema)
034                && object.equals(nameKey.object);
035    }
036
037    @Override
038    public int hashCode() {
039        return Objects.hash(objectType, server, catalog, schema, object);
040    }
041
042    @Override
043    public String toString() {
044        return "NameKey{" +
045                "type=" + objectType +
046                ", server='" + server + '\'' +
047                ", catalog='" + catalog + '\'' +
048                ", schema='" + schema + '\'' +
049                ", object='" + object + '\'' +
050                '}';
051    }
052}
053
054