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

com.samskivert.depot.expression.ColumnExp Maven / Gradle / Ivy

//
// Depot library - a Java relational persistence library
// https://github.com/threerings/depot/blob/master/LICENSE

package com.samskivert.depot.expression;

import java.util.Collection;

import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.clause.Join;
import com.samskivert.depot.impl.FragmentVisitor;

/**
 * An expression that unambiguously identifies a field of a class, for example
 * GameRecord.itemId.
 */
public class ColumnExp extends FluentExp
{
    /** The name of the column we reference. */
    public final String name;

    public ColumnExp (Class pClass, String field)
    {
        super();
        _pClass = pClass;
        this.name = field;
    }

    /**
     * Returns a column expression for the supplied persistent class with the same name as this
     * expression. This is useful for "casting" a column expression from a parent class to a
     * derived class.
     */
    public ColumnExp as (Class oClass)
    {
        return new ColumnExp(oClass, name);
    }

    /** Returns a {@link Join} on this column and the supplied target. */
    public Join join (ColumnExp join)
    {
        return new Join(this, join);
    }

    // from SQLExpression
    public Object accept (FragmentVisitor builder)
    {
        return builder.visit(this);
    }

    // from SQLExpression
    public void addClasses (Collection> classSet)
    {
        classSet.add(_pClass);
    }

    public Class getPersistentClass ()
    {
        return _pClass;
    }

    @Override // from Object
    public int hashCode ()
    {
        return _pClass.hashCode() ^ this.name.hashCode();
    }

    @Override // from Object
    public boolean equals (Object other)
    {
        return (other instanceof ColumnExp) &&
            ((ColumnExp)other)._pClass.equals(_pClass) &&
            ((ColumnExp)other).name.equals(this.name);
    }

    @Override // from Object
    public String toString ()
    {
        return "\"" + name + "\""; // TODO: qualify with record name and be uber verbose?
    }

    /** The table that hosts the column we reference, or null. */
    protected final Class _pClass;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy