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

com.reinaldoarrosi.android.querybuilder.sqlite.from.JoinFrom Maven / Gradle / Ivy

The newest version!
package com.reinaldoarrosi.android.querybuilder.sqlite.from;

import java.util.ArrayList;
import java.util.List;

import com.reinaldoarrosi.android.querybuilder.sqlite.criteria.Criteria;
import com.reinaldoarrosi.android.querybuilder.sqlite.projection.Projection;

public class JoinFrom extends From {
	private From left;
	private From right;
	private String joinType;
	private Criteria criteria;
	
	public JoinFrom(From left, From right, String joinType, Criteria criteria) {
		this.left = left;
		this.right = right;
		this.joinType = joinType;
		this.criteria = criteria;
	}
	
	public JoinFrom onOr(String leftColumn, String rightColumn) {
		return onOr(Criteria.equals(Projection.column(leftColumn), Projection.column(rightColumn)));
	}
	
	public JoinFrom onAnd(String leftColumn, String rightColumn) {
		return onAnd(Criteria.equals(Projection.column(leftColumn), Projection.column(rightColumn)));
	}
	
	public JoinFrom onAnd(Criteria criteria) {
		this.criteria = (this.criteria != null ? this.criteria.and(criteria) : criteria);		
		return this;
	}
	
	public JoinFrom onOr(Criteria criteria) {
		this.criteria = (this.criteria != null ? this.criteria.or(criteria) : criteria);		
		return this;
	}

	@Override
	public String build() {
		String leftSide = (left != null ? left.build() : "");
		String rightSide = (right != null ? right.build() : "");
		String joinCriteria = (criteria != null ? criteria.build() : "");
		
		return "(" + leftSide + " " + joinType + " " + rightSide + " ON " + joinCriteria + ")";
	}

	@Override
	public List buildParameters() {
		List ret = new ArrayList();
		
		if(left != null)
			ret.addAll(left.buildParameters());
		
		if(right != null)
			ret.addAll(right.buildParameters());
		
		if(criteria != null)
			ret.addAll(criteria.buildParameters());
		
		return ret;
	}
}