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

org.vesalainen.parsers.sql.JoinComparison Maven / Gradle / Ivy

/*
 * Copyright (C) 2012 Timo Vesalainen
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see .
 */

package org.vesalainen.parsers.sql;

import org.vesalainen.parsers.sql.util.ArrayMap;

/**
 * @author Timo Vesalainen
 */
public class JoinComparison extends BaseComparison implements JoinCondition
{
    protected JoinComparison pair;
    protected ColumnReference columnReference2;

    public JoinComparison(ColumnReference columnReference, Relation relation, ColumnReference columnReference2)
    {
        super(columnReference, relation);
        this.columnReference2 = columnReference2;
        pair = new JoinComparison<>(this);
    }

    private JoinComparison(JoinComparison pair)
    {
        super(pair.columnReference2, pair.relation);
        this.columnReference2 = pair.columnReference;
        this.pair = pair;
    }
    
    @Override
    public TruthValue matches(SQLConverter selector, R row)
    {
        C col1 = columnReference.getValue(selector, row);
        C col2 = columnReference2.getValue(selector, row);
        if (col1 == null || col2 == null)
        {
            return TruthValue.UNKNOWN;
        }
        if (matches(selector, col1, col2))
        {
            return TruthValue.TRUE;
        }
        else
        {
            return TruthValue.FALSE;
        }
    }

    @Override
    public TruthValue matches(SQLConverter selector, ArrayMap,R> rowCandidate)
    {
        C col1 = columnReference.getValue(selector, rowCandidate);
        C col2 = columnReference2.getValue(selector, rowCandidate);
        if (col1 == null || col2 == null)
        {
            return TruthValue.UNKNOWN;
        }
        if (matches(selector, col1, col2))
        {
            return TruthValue.TRUE;
        }
        else
        {
            return TruthValue.FALSE;
        }
    }
    @Override
    public void associateCondition(SelectStatement select, boolean andPath)
    {
        columnReference.associateCondition(this, andPath);
        columnReference2.associateCondition(pair, andPath);
    }

    @Override
    public Range narrow(SQLConverter selector, Range range, Table fromTable, String column, C lower, C upper)
    {
        range = narrow(columnReference, selector, range, fromTable, column, lower, upper);
        range = narrow(columnReference2, selector, range, fromTable, column, lower, upper);
        return range;
    }

    private Range narrow(ColumnReference cf, SQLConverter selector, Range range, Table fromTable, String column, C lower, C upper)
    {
        if (fromTable.equals(cf.getTable()) && column.equals(cf.getColumn()))
        {
            if (range == null)
            {
                range = new Range(selector.getComparator());
            }
            range.narrow(lower, upper);
        }
        return range;
    }
    
    @Override
    public void narrow(SQLConverter selector, Range range)
    {
    }

    @Override
    public void walk(ConditionVisitor visitor, boolean andPath)
    {
        visitor.visit(this, andPath);
    }

    @Override
    public ColumnReference getColumnReference2()
    {
        return columnReference2;
    }

    @Override
    public String getColumn()
    {
        return columnReference.getColumn();
    }

    @Override
    public ColumnReference getColumnReference()
    {
        return columnReference;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy