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

com.github.zhangxd1989.basetool.db.sql.Query Maven / Gradle / Ivy

There is a newer version: 1.0.16
Show newest version
package com.github.zhangxd1989.basetool.db.sql;

import com.github.zhangxd1989.basetool.collection.CollectionUtil;
import com.github.zhangxd1989.basetool.db.DbRuntimeException;
import com.github.zhangxd1989.basetool.db.Page;
import com.github.zhangxd1989.basetool.util.ArrayUtil;

import java.util.Collection;

/**
 * 查询对象,用于传递查询所需的字段值
* 查询对象根据表名(可以多个),多个条件 {@link Condition} 构建查询对象完成查询。
* 如果想自定义返回结果,则可在查询对象中自定义要查询的字段名,分页{@link Page}信息来自定义结果。 * * @author sheldon */ public class Query { /** * 查询的字段名列表 */ Collection fields; /** * 查询的表名 */ String[] tableNames; /** * 查询的条件语句 */ Condition[] where; /** * 分页对象 */ Page page; /** * 构造 * * @param tableNames 表名 */ public Query(String... tableNames) { this(null, tableNames); this.tableNames = tableNames; } /** * 构造 * * @param where 条件语句 * @param tableNames 表名 */ public Query(Condition[] where, String... tableNames) { this(where, null, tableNames); } /** * 构造 * * @param where 条件语句 * @param page 分页 * @param tableNames 表名 */ public Query(Condition[] where, Page page, String... tableNames) { this(null, tableNames, where, page); } /** * 构造 * * @param fields 字段 * @param tableNames 表名 * @param where 条件 * @param page 分页 */ public Query(Collection fields, String[] tableNames, Condition[] where, Page page) { this.fields = fields; this.tableNames = ArrayUtil.clone(tableNames); this.where = ArrayUtil.clone(where); this.page = page; } /** * 获得查询的字段名列表 * * @return 查询的字段名列表 */ public Collection getFields() { return fields; } /** * 设置查询的字段名列表 * * @param fields 查询的字段名列表 * @return this */ public Query setFields(Collection fields) { this.fields = fields; return this; } /** * 设置查询的字段名列表 * * @param fields 查询的字段名列表 * @return this */ public Query setFields(String... fields) { this.fields = CollectionUtil.newArrayList(fields); return this; } /** * 获得表名数组 * * @return 表名数组 */ public String[] getTableNames() { return ArrayUtil.clone(tableNames); } /** * 设置表名 * * @param tableNames 表名 * @return this */ public Query setTableNames(String... tableNames) { this.tableNames = tableNames; return this; } /** * 获得条件语句 * * @return 条件语句 */ public Condition[] getWhere() { return ArrayUtil.clone(where); } /** * 设置条件语句 * * @param where 条件语句 * @return this */ public Query setWhere(Condition... where) { this.where = where; return this; } /** * 获得分页对象 * * @return 分页对象 */ public Page getPage() { return page; } /** * 设置分页对象 * * @param page 分页对象 * @return this */ public Query setPage(Page page) { this.page = page; return this; } /** * 获得第一个表名 * * @return 表名 * @throws DbRuntimeException 没有表 */ public String getFirstTableName() throws DbRuntimeException { if (ArrayUtil.isEmpty(this.tableNames)) { throw new DbRuntimeException("No tableName!"); } return this.tableNames[0]; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy