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

org.fingertip.simpledao.query.Query Maven / Gradle / Ivy

The newest version!
package org.fingertip.simpledao.query;

import org.fingertip.simpledao.bean.Direction;
import org.fingertip.simpledao.bean.Order;
import org.fingertip.simpledao.enums.JoinType;

import java.util.Collections;
import java.util.List;

public class Query extends AbstractQuery{

	public Query() {
		super();
	}
	
	public Query(String table) {
		super(table);
	}
	public Query(Class entityClass) {
		super(entityClass);
	}

	@Override
	public Query eq(String field, Object value) {
		ICriteria eq = new Criteria().eq(field, value);
		queries.add(eq);
		return this;
	}

	@Override
	public Query ne(String field, Object value) {
		ICriteria ne = new Criteria().ne(field, value);
		queries.add(ne);
		return this;
	}

	@Override
	public Query gt(String field, Object value) {
		ICriteria gt = new Criteria().gt(field, value);
		queries.add(gt);
		return this;
	}

	@Override
	public Query gte(String field, Object value) {
		ICriteria gte = new Criteria().gte(field, value);
		queries.add(gte);
		return this;
	}

	@Override
	public Query lt(String field, Object value) {
		ICriteria lt = new Criteria().lt(field, value);
		queries.add(lt);
		return this;
	}

	@Override
	public Query lte(String field, Object value) {
		ICriteria lte = new Criteria().lte(field, value);
		queries.add(lte);
		return this;
	}

	@Override
	public Query like(String field, Object value) {
		ICriteria like = new Criteria().like(field, value);
		queries.add(like);
		return this;
	}

	@Override
	public Query in(String field, List values) {
		ICriteria in = new Criteria().in(field, values);
		queries.add(in);
		return this;
	}

	@Override
	public Query in(String field, Object... values) {
		ICriteria in = new Criteria().in(field, values);
		queries.add(in);
		return this;
	}

	@Override
	public Query nin(String field, List values) {
		ICriteria nin = new Criteria().nin(field, values);
		queries.add(nin);
		return this;
	}

	@Override
	public Query nin(String field, Object... values) {
		ICriteria nin = new Criteria().nin(field, values);
		queries.add(nin);
		return this;
	}

	@Override
	public Query between(String field, Object start, Object end) {
		ICriteria between = new Criteria().between(field, start, end);
		queries.add(between);
		return this;
	}

	@Override
	public Query and(IQuery... queries) {
		ICriteria and = new Criteria().and(queries);
		this.queries.add(and);
		return this;
	}

	@Override
	public Query or(IQuery... queries) {
		ICriteria or = new Criteria().or(queries);
		this.queries.add(or);
		return this;
	}

    @Override
    public Query or(IQuery query) {
        ICriteria or = new Criteria().or(query);
        queries.add(or);
        return null;
    }

	@Override
	public Query isNull(String field) {
		ICriteria isNull = new Criteria().isNull(field);
		queries.add(isNull);
		return this;
	}

	@Override
	public Query isNotNull(String field) {
		ICriteria isNotNull = new Criteria().isNotNull(field);
		queries.add(isNotNull);
		return this;
	}

	@Override
	public Query group(String... fields) {
        Collections.addAll(this.groups, fields);
		return this;
	}
	
	@Override
	public Query limit(int size) {
		this.limit = new Criteria().limit(size);
		return this;
	}
	
	@Override
	public Query limit(int from, int size) {
		this.limit = new Criteria().limit(from,size);
		return this;
	}

	@Override
	public Query fields(String... fields) {
		Collections.addAll(this.fields, fields);
		return this;
	}

	@Override
	public Query order(String field, Direction direction) {
		Order order = Order.by(field);
		order.setDirection(direction);
		this.orders.add(order);
		return this;
	}

	@Override
	public Query sort(Order... orders) {
		Collections.addAll(this.orders, orders);
		return this;
	}

	@Override
	public Query sort(List orders) {
		this.orders.addAll(orders);
		return this;
	}

    @Override
    public Query having(ICriteria... queries) {
	    Collections.addAll(this.havings,queries);
        return this;
    }

    @Override
    public Query from(String tableName) {
	    this.tableName = tableName;
        return this;
    }

    @Override
    public Query tables(String... tables) {
        Collections.addAll(this.tables,tables);
        return this;
    }

    @Override
    public Query leftJoin(String table, ICriteria... queries) {
        JoinQuery joinQuery = new JoinQuery(JoinType.LEFT, table, queries);
        this.joins.add(joinQuery);
        return this;
    }

    @Override
    public Query rightJoin(String table, ICriteria... queries) {
        JoinQuery joinQuery = new JoinQuery(JoinType.RIGHT, table, queries);
        this.joins.add(joinQuery);
        return this;
    }

    @Override
    public Query innerJoin(String table, ICriteria... queries) {
        JoinQuery joinQuery = new JoinQuery(JoinType.INNER, table, queries);
        this.joins.add(joinQuery);
        return this;
    }

    @Override
    public Query leftJoin(String table,JoinQuery joinQuery, ICriteria... queries) {
        JoinQuery leftJoin = new JoinQuery(JoinType.LEFT, table, queries);
        leftJoin.setJoinQuery(joinQuery);
        this.joins.add(leftJoin);
        return this;
    }

    @Override
    public Query rightJoin(String table,JoinQuery joinQuery, ICriteria... queries) {
        JoinQuery rightJoin = new JoinQuery(JoinType.RIGHT, table, queries);
        rightJoin.setJoinQuery(joinQuery);
        this.joins.add(rightJoin);
        return this;
    }

    @Override
    public Query innerJoin(String table,JoinQuery joinQuery, ICriteria... queries) {
        JoinQuery innerJoin = new JoinQuery(JoinType.INNER, table, queries);
        innerJoin.setJoinQuery(joinQuery);
        this.joins.add(innerJoin);
        return this;
    }
}