tledkov-gridgain commented on a change in pull request #563:
URL: https://github.com/apache/ignite-3/pull/563#discussion_r790526365



##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/IgniteObjectName.java
##########
@@ -0,0 +1,140 @@
+/*
+ * 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.internal.util;
+
+import org.apache.ignite.lang.IgniteInternalException;
+
+/**
+ * Utility methods used for cluster's named objects: schemas, tables, columns, 
indexes, etc.
+ */
+public class IgniteObjectName {
+    /** No instance methods. */
+    private IgniteObjectName() {
+    }
+
+    /**
+     * Parse database's object name: unquote name or cast to upper case 
not-quoted name.
+     *
+     * @param str String to parse object name.
+     * @return Unquoted name or name is cast to upper case. "tbl0" -> 
"TBL0", "\"Tbl0\"" -> "Tbl0".
+     */
+    public static String parse(String str) {
+        if (str == null || str.isEmpty()) {
+            return str;
+        }
+
+        if (str.startsWith("\"") && str.endsWith("\"")) {
+            if (str.length() == 1) {
+                throw new IgniteInternalException("Invalid identifier: single 
quota");
+            }
+
+            return str.substring(1, str.length() - 1);
+        } else {
+            return str.toUpperCase();
+        }
+    }
+
+    /**
+     * Parse canonical table [schemaName].[tableName], unquote identifiers and 
normalize case, e.g. "public.tbl0" -> "PUBLIC.TBL0",
+     * "PUBLIC.\"Tbl0\"" -> "PUBLIC.Tbl0", "\"MySchema\".\"Tbl0\"" -> 
"MySchema.Tbl0", etc.
+     *
+     * @param str String to parse canonical name.
+     * @return Unquote identifiers and normalize case.
+     */
+    public static String parseCanonicalName(String str) {
+        if (str == null || str.isEmpty()) {
+            throw new IgniteInternalException("Invalid identifier: empty 
string");
+        }
+
+        StringBuilder name = new StringBuilder();
+        boolean quoted = false;
+        int idBegin = 0;
+        int idEnd = 0;
+
+        for (int i = 0; i < str.length(); ++i) {
+            switch (str.charAt(i)) {
+                case '\"':
+                    if (quoted) {
+                        idEnd = i;
+                    } else {
+                        idBegin = i + 1;
+                        quoted = true;
+                    }
+
+                    break;
+
+                case '.':
+                    if (!quoted) {
+                        idEnd = i;
+                    }
+
+                    String id = str.substring(idBegin, idEnd);
+
+                    name.append(quoted ? id : id.toUpperCase()).append('.');
+
+                    quoted = false;
+                    idBegin = i + 1;
+
+                    break;
+
+                default:
+                    break;
+            }
+        }
+
+        if (!quoted) {
+            idEnd = str.length();
+        }
+
+        // append last name
+        String id = str.substring(idBegin, idEnd);
+
+        name.append(quoted ? id : id.toUpperCase());
+
+        return name.toString();
+    }
+
+    /**
+     * Unparse database's object name: quote name.
+     *
+     * @param str Object name.
+     * @return Quoted object name.
+     */
+    public static String unparseName(String str) {

Review comment:
       If the `unparseName` is renamed to `quote` what name is applicable for 
`unparseCanonicalName`?
   @AMashenkov 




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to