All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.db.sql.util.SqlUtils Maven / Gradle / Ivy

There is a newer version: 3.14.0
Show newest version
package com.scalar.db.sql.util;

import com.scalar.db.sql.TableRef;
import com.scalar.db.sql.common.SqlError;
import com.scalar.db.sql.metadata.Metadata;
import com.scalar.db.sql.metadata.TableMetadata;
import com.scalar.db.sql.statement.NamespaceNameOmittable;
import com.scalar.db.sql.statement.Statement;
import com.scalar.db.sql.statement.StatementUtils;
import com.scalar.db.util.ScalarDbUtils;
import java.util.function.Supplier;
import javax.annotation.Nullable;

public final class SqlUtils {

  private SqlUtils() {}

  public static TableMetadata getTableMetadata(Metadata metadata, TableRef table) {
    return getTableMetadata(metadata, table, null);
  }

  public static TableMetadata getTableMetadata(
      Metadata metadata, TableRef table, @Nullable String defaultNamespace) {
    String namespaceName = table.namespaceName != null ? table.namespaceName : defaultNamespace;
    if (namespaceName == null) {
      throw new IllegalArgumentException(SqlError.NAMESPACE_NAME_NOT_SPECIFIED.buildMessage());
    }

    Supplier exceptionSupplier =
        () ->
            new IllegalArgumentException(
                SqlError.TABLE_NOT_FOUND.buildMessage(
                    ScalarDbUtils.getFullTableName(namespaceName, table.tableName)));
    return metadata
        .getNamespace(namespaceName)
        .orElseThrow(exceptionSupplier)
        .getTable(table.tableName)
        .orElseThrow(exceptionSupplier);
  }

  // This is partially ported (only for standardConformingStrings:true) from
  // org.postgresql.core.Utils.escapeLiteral
  // (https://github.com/pgjdbc/pgjdbc/blob/5c78edb1412467636f996416ad37118f651db86e/pgjdbc/src/main/java/org/postgresql/core/Utils.java#L49-L104)
  public static String escapeString(String value) {
    StringBuilder sbuf = new StringBuilder();

    for (int i = 0; i < value.length(); ++i) {
      char ch = value.charAt(i);
      if (ch == '\0') {
        // `core` package doesn't have dependency to `java.sql` package. So,
        // IllegalArgumentException is thrown here not SQLException.
        throw new IllegalArgumentException(SqlError.ZERO_BYTES_IN_STRING.buildMessage());
      }
      if (ch == '\'') {
        sbuf.append('\'');
      }
      sbuf.append(ch);
    }

    return sbuf.toString();
  }

  @SuppressWarnings("unchecked")
  public static  T setNamespaceNameIfOmitted(
      T statement, @Nullable String defaultNamespaceName) {
    if (defaultNamespaceName != null && statement instanceof NamespaceNameOmittable) {
      return (T)
          ((NamespaceNameOmittable) statement).setNamespaceNameIfOmitted(defaultNamespaceName);
    }
    return statement;
  }

  public static void validateNamespaceName(NamespaceNameOmittable namespaceNameOmittable) {
    if (namespaceNameOmittable.namespaceNameOmitted()) {
      throw new IllegalArgumentException(SqlError.NAMESPACE_NAME_NOT_SPECIFIED.buildMessage());
    }
  }

  public static String toString(TableRef table) {
    StringBuilder builder = new StringBuilder();
    StatementUtils.appendTable(builder, table, false);
    return builder.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy