aeon.compiler.utils.SqliteUtils.java Source code

Java tutorial

Introduction

Here is the source code for aeon.compiler.utils.SqliteUtils.java

Source

/**
 * Copyright 2015 Appvengers
 *
 * Licensed 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 aeon.compiler.utils;

import com.google.common.base.CaseFormat;
import org.jetbrains.annotations.NotNull;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author Sven Jacobs
 */
public class SqliteUtils {

    static final String QUOTE = "\"";

    /**
     * Converts a camelCase class or field name into an entity name with underscore-notation for use as a SQLite
     * table or column name.
     *
     * @param name camelCase class or field name
     * @return Entity name in underscore-notation
     */
    @NotNull
    public static String entityName(@NotNull final String name) {
        checkNotNull(name);
        return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name);
    }

    /**
     * Converts a camelCase field name to an entity name.
     * Also handles the case where a field starts with "m" (for member).
     *
     * @param fieldName camelCase field name
     * @return Entity name in underscore-notation
     */
    @NotNull
    public static String fieldToEntityName(@NotNull final String fieldName) {
        checkNotNull(fieldName);
        return entityName(Utils.normalizeFieldName(fieldName));
    }

    /**
     * Escapes entity name with double quotes.
     *
     * @param name Entity name
     * @return Escaped entity name
     * @throws IllegalArgumentException if entity name contains double quote
     */
    @NotNull
    public static String escapeEntity(@NotNull final String name) throws IllegalArgumentException {
        checkNotNull(name);
        if (name.contains(QUOTE)) {
            throw new IllegalArgumentException("Entity name " + name + " contains double quote");
        }
        return QUOTE + name + QUOTE;
    }
}