edu.uvm.ccts.common.sql.model.Field Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ccts-common Show documentation
Show all versions of ccts-common Show documentation
A library of useful generic objects and tools consolidated here to simplify all UVM CCTS projects
/*
* Copyright 2015 The University of Vermont and State
* Agricultural College. All rights reserved.
*
* Written by Matthew B. Storer
*
* This file is part of CCTS Common.
*
* CCTS Common is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CCTS Common is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CCTS Common. If not, see .
*/
package edu.uvm.ccts.common.sql.model;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* Created by mstorer on 11/1/13.
*/
public class Field implements Comparable {
public static final String NULL = "null";
private String table;
private String name;
private String alias;
public Field(String table, String name, String alias) {
this.table = table == null ? "" : table.toLowerCase();
this.name = name == null ? "" : name.toLowerCase();
this.alias = alias == null ? "" : alias.toLowerCase();
}
public Field(String table, String name) {
// todo : we shouldn't allow name to be null, ever
this.table = table == null ? "" : table.toLowerCase();
this.name = name == null ? "" : name.toLowerCase();
this.alias = table == null || table.isEmpty() ?
"" :
table.toLowerCase() + "_" + this.name.toLowerCase();
}
public String asResultSetLabel() {
// todo : is this function even necessary? 'alias' should never be empty -
// fields generated by SqlStatement never have empty aliases
return ! alias.isEmpty() ? alias : name;
}
public String asSelectField() {
StringBuilder sb = new StringBuilder();
if ( ! table.isEmpty() ) sb.append(table).append(".");
sb.append(name);
if ( ! alias.isEmpty() ) sb.append(" as ").append(alias);
return sb.toString();
}
public String asWhereClauseField() {
StringBuilder sb = new StringBuilder();
if ( ! table.isEmpty() ) sb.append(table).append(".");
sb.append(name);
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (o == this) return true;
if (o.getClass() != getClass()) return false;
Field f = (Field) o;
return new EqualsBuilder()
.append(table, f.table)
.append(name, f.name)
.append(alias, f.alias)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(487, 307)
.append(table)
.append(name)
.append(alias)
.toHashCode();
}
@Override
public int compareTo(Field f) {
if (this == f) return 0;
int x = table.compareTo(f.table);
if (x != 0) return x;
x = name.compareTo(f.name);
if (x != 0) return x;
x = alias.compareTo(f.alias);
if (x != 0) return x;
assert this.equals(f) : "compareTo inconsistent with equals.";
return 0;
}
public String getTable() {
return table;
}
public String getName() {
return name;
}
public String getAlias() {
return alias;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy