xtern commented on code in PR #4971: URL: https://github.com/apache/ignite-3/pull/4971#discussion_r1899586928
########## modules/api/src/main/java/org/apache/ignite/table/QualifiedName.java: ########## @@ -0,0 +1,344 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.table; + +import static org.apache.ignite.lang.util.IgniteNameUtils.identifierExtend; +import static org.apache.ignite.lang.util.IgniteNameUtils.identifierStart; +import static org.apache.ignite.lang.util.IgniteNameUtils.quote; + +import java.util.NoSuchElementException; +import java.util.Objects; +import org.jetbrains.annotations.Nullable; + +/** + * Class represents a catalog object name (table, index and etc.) and provides factory methods. + * + * <p>Qualified name is a pair of schema name and object name. Schema name is optional, when not set, then + * {@link QualifiedName#DEFAULT_SCHEMA_NAME} will be used. + * + * <p>Both schema name and object name can be case sensitive or insensitive respecting SQL-parser style quotation. + * Unquoted name will be cast to upper case. E.g. "tbl0" - is equivalent to "TBL0", "\"Tbl0\"" - "Tbl0", etc. + */ +public class QualifiedName { + /** Default schema name. */ + public static final String DEFAULT_SCHEMA_NAME = "PUBLIC"; + + /** Normalized schema name. */ + private final String schemaIdentifier; + + /** Normalized object name. */ + private final String objectIdentifier; + + /** + * Parses given simple or canonical name, and returns the object qualified name. + */ + public static QualifiedName parse(String simpleOrCanonicalName) { + verifyObjectIdentifier(simpleOrCanonicalName); + + Tokenizer tokenizer = new Tokenizer(simpleOrCanonicalName); + + String schemaName = DEFAULT_SCHEMA_NAME; + String objectName = tokenizer.nextToken(); + + if (tokenizer.hasNext()) { + // Canonical name + schemaName = objectName; + objectName = tokenizer.nextToken(); + } + + if (tokenizer.hasNext()) { + throw new IllegalArgumentException("Canonical name format mismatch: " + simpleOrCanonicalName); + } + + verifySchemaIdentifier(schemaName); + verifyObjectIdentifier(objectName); + + return new QualifiedName(schemaName, objectName); + } + + /** + * Resolves given simple name against default schema. + */ + public static QualifiedName fromSimple(String simpleName) { + return of(null, simpleName); + } + + /** + * Normalize schemaName and objectName, and returns the object qualified name. + * + * @param schemaName Schema name or {@code null} for default schema. + * @param objectName Object name. + */ + public static QualifiedName of(@Nullable String schemaName, String objectName) { + String schemaIdentifier = schemaName == null ? DEFAULT_SCHEMA_NAME : parseIdentifier(schemaName); + String objectIdentifier = parseIdentifier(objectName); + + verifySchemaIdentifier(schemaIdentifier); + verifyObjectIdentifier(objectIdentifier); + + return new QualifiedName(schemaIdentifier, objectIdentifier); + } + + /** + * Constructs a qualified name. + * + * @param schemaName Normalized schema name. + * @param objectName Normalized object name. + */ + private QualifiedName(String schemaName, String objectName) { + this.schemaIdentifier = schemaName; + this.objectIdentifier = objectName; + } + + /** Returns normalized schema name. */ + public String schemaName() { + return schemaIdentifier; + } + + /** Returns normalized object name. */ + public String objectName() { + return objectIdentifier; + } + + /** Returns qualified name in canonical form. */ + public String toCanonicalForm() { + // TODO https://issues.apache.org/jira/browse/IGNITE-24021 Extract method and move to IgniteNameUtils + return quoteIfNeeded(schemaIdentifier) + '.' + quoteIfNeeded(objectIdentifier); + } + + /** {@inheritDoc} */ + @Override + public boolean equals(Object object) { + if (this == object) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } + QualifiedName that = (QualifiedName) object; + return Objects.equals(schemaIdentifier, that.schemaIdentifier) && Objects.equals(objectIdentifier, that.objectIdentifier); + } + + /** {@inheritDoc} */ + @Override + public int hashCode() { + return Objects.hash(schemaIdentifier, objectIdentifier); + } + + /** {@inheritDoc} */ + @Override + public String toString() { + return "QualifiedName[" + + "schemaName='" + schemaIdentifier + '\'' + + ", objectName='" + objectIdentifier + '\'' + + ']'; + } + + private static void verifyObjectIdentifier(@Nullable String identifier) { + Objects.requireNonNull(identifier); + + if (identifier.isEmpty()) { + throw new IllegalArgumentException("Object identifier can't be empty."); + } + } + + private static void verifySchemaIdentifier(@Nullable String identifier) { + if (identifier != null && identifier.isEmpty()) { + throw new IllegalArgumentException("Schema identifier can't be empty."); + } + } + + /** + * Parse simple name: unquote name or cast to upper case not-quoted name. + * + * @param name String to parse object name. + * @return Unquoted name or name is cast to upper case. "tbl0" -> "TBL0", "\"Tbl0\"" -> "Tbl0". + */ + // TODO https://issues.apache.org/jira/browse/IGNITE-24021: Use QualifiedName instead. + // This method should be called from QualifiedName class only. Rename to parseIdentifier. + static String parseIdentifier(String name) { + if (name == null || name.isEmpty()) { + return name; + } + + var tokenizer = new Tokenizer(name); + + String parsedName = tokenizer.nextToken(); + + if (tokenizer.hasNext()) { + throw new IllegalArgumentException("Fully qualified name is not expected [name=" + name + "]"); + } + + return parsedName; + } + + /** + * Wraps the given name with double quotes if it not uppercased non-quoted name, e.g. "myColumn" -> "\"myColumn\"", "MYCOLUMN" -> + * "MYCOLUMN" + * + * @param identifier Object identifier. + * @return Quoted object name. + */ + public static String quoteIfNeeded(String identifier) { Review Comment: I prefer to keep it private until [IGNITE-24021](https://issues.apache.org/jira/browse/IGNITE-24021) will be implemented -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org