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

br.com.objectos.way.sql.ColumnInfo Maven / Gradle / Ivy

/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.way.sql;

import br.com.objectos.way.code.ImportInfoSet;
import br.com.objectos.way.core.auto.AutoFunctional;
import br.com.objectos.way.core.testing.Testable;
import br.com.objectos.way.core.tmpl.mustache.IsMustacheSerializable;
import br.com.objectos.way.core.tmpl.mustache.MustacheObject;
import br.com.objectos.way.core.tmpl.mustache.Mustaches;
import br.com.objectos.way.core.tmpl.mustache.ToMustacheHelper;

import com.google.common.base.Objects;
import com.google.common.base.Optional;

/**
 * @author [email protected] (Marcio Endo)
 */
public abstract class ColumnInfo
    implements
    CanDecorateSqlBuilder,
    IsMustacheSerializable,
    Testable {

  abstract TableName tableName();
  @AutoFunctional
  abstract String name();
  abstract boolean nullable();

  ColumnInfo() {
  }

  @Override
  public MustacheObject toMustache() {
    return toMustacheHelper()
        .toMustache();
  }

  public ToMustacheHelper toMustacheHelper() {
    return Mustaches.toMustacheHelper()
        .add("identifier", identifier())
        .add("name", name())
        .add("schemaName", tableName().schemaName())
        .add("tableName", tableName().name())
        .add("type", typeName());
  }

  public PlaceholderInfo toPlaceholderInfo(int index) {
    throw new UnsupportedOperationException();
  }

  @AutoFunctional
  public abstract QualifiedColumnInfo toQualifiedColumnInfo(TableInfo tableInfo);

  public void addTo(ImportInfoSet set) {
    set.add(importInfoType());
    set.add(importInfoTypeQualified());
  }

  @AutoFunctional
  public boolean autoIncrement() {
    return false;
  }

  @Override
  public SqlBuilder decorate(SqlBuilder sql) {
    return sql.escape(name());
  }

  @Override
  public final int hashCode() {
    return Objects.hashCode(tableName(), name(), nullable());
  }

  @Override
  public final boolean equals(final Object obj) {
    if (obj == this) {
      return true;
    }
    if (obj instanceof ColumnInfo) {
      final ColumnInfo that = (ColumnInfo) obj;
      return tableName().equals(that.tableName())
          && name().equals(that.name())
          && nullable() == that.nullable();
    } else {
      return false;
    }
  }

  abstract Class importInfoType();
  abstract Class importInfoTypeQualified();

  abstract String typeName();

  String identifier() {
    String res = name();

    String tableName = tableName().name();
    if (res.equals(tableName)) {
      res = res + "_";
    }

    return res;
  }

  boolean primaryKey(TableInfo tableInfo) {
    boolean pk = false;

    Optional maybePrimaryKeyInfo = tableInfo.primaryKeyInfo();
    if (maybePrimaryKeyInfo.isPresent()) {
      PrimaryKeyInfo primaryKeyInfo = maybePrimaryKeyInfo.get();
      pk = primaryKeyInfo.matches(this);
    }

    return pk;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy