use of cn.hutool.db.sql.Query in project hutool by looly.
the class SqlConnRunner method find.
/**
* 查询<br>
* 此方法不会关闭Connection
*
* @param <T> 结果对象类型
* @param conn 数据库连接对象
* @param fields 返回的字段列表,null则返回所有字段
* @param where 条件实体类(包含表名)
* @param rsh 结果集处理对象
* @return 结果对象
* @throws SQLException SQL执行异常
*/
public <T> T find(Connection conn, Collection<String> fields, Entity where, RsHandler<T> rsh) throws SQLException {
final Query query = new Query(DbUtil.buildConditions(where), where.getTableName());
query.setFields(fields);
return find(conn, query, rsh);
}
use of cn.hutool.db.sql.Query in project hutool by looly.
the class SqlConnRunner method count.
/**
* 结果的条目数
* @param conn 数据库连接对象
* @param where 查询条件
* @return 复合条件的结果数
* @throws SQLException SQL执行异常
*/
public int count(Connection conn, Entity where) throws SQLException {
checkConn(conn);
final Query query = new Query(DbUtil.buildConditions(where), where.getTableName());
PreparedStatement ps = null;
try {
ps = dialect.psForCount(conn, query);
return SqlExecutor.query(ps, new NumberHandler()).intValue();
} catch (SQLException e) {
throw e;
} finally {
DbUtil.close(ps);
}
}
use of cn.hutool.db.sql.Query in project hutool by looly.
the class SqlConnRunner method update.
/**
* 更新数据<br>
* 此方法不会关闭Connection
* @param conn 数据库连接
* @param record 记录
* @param where 条件
* @return 影响行数
* @throws SQLException SQL执行异常
*/
public int update(Connection conn, Entity record, Entity where) throws SQLException {
checkConn(conn);
if (CollectionUtil.isEmpty(record)) {
throw new SQLException("Empty entity provided!");
}
if (CollectionUtil.isEmpty(where)) {
// 不允许做全表更新
throw new SQLException("Empty where provided!");
}
// 表名可以从被更新记录的Entity中获得,也可以从Where中获得
String tableName = record.getTableName();
if (StrUtil.isBlank(tableName)) {
tableName = where.getTableName();
record.setTableName(tableName);
}
final Query query = new Query(DbUtil.buildConditions(where), tableName);
PreparedStatement ps = null;
try {
ps = dialect.psForUpdate(conn, record, query);
return ps.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
DbUtil.close(ps);
}
}
use of cn.hutool.db.sql.Query in project hutool by looly.
the class SqlConnRunner method del.
/**
* 删除数据<br>
* 此方法不会关闭Connection
* @param conn 数据库连接
* @param where 条件
* @return 影响行数
* @throws SQLException SQL执行异常
*/
public int del(Connection conn, Entity where) throws SQLException {
checkConn(conn);
if (CollectionUtil.isEmpty(where)) {
// 不允许做全表删除
throw new SQLException("Empty entity provided!");
}
final Query query = new Query(DbUtil.buildConditions(where), where.getTableName());
PreparedStatement ps = null;
try {
ps = dialect.psForDelete(conn, query);
return ps.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
DbUtil.close(ps);
}
}
use of cn.hutool.db.sql.Query in project hutool by looly.
the class SqlConnRunner method page.
/**
* 分页查询<br>
* 此方法不会关闭Connection
*
* @param <T> 结果对象类型
* @param conn 数据库连接对象
* @param fields 返回的字段列表,null则返回所有字段
* @param where 条件实体类(包含表名)
* @param page 分页对象
* @param rsh 结果集处理对象
* @return 结果对象
* @throws SQLException SQL执行异常
*/
public <T> T page(Connection conn, Collection<String> fields, Entity where, Page page, RsHandler<T> rsh) throws SQLException {
checkConn(conn);
if (null == page) {
return this.find(conn, fields, where, rsh);
}
final Query query = new Query(DbUtil.buildConditions(where), where.getTableName());
query.setFields(fields);
query.setPage(page);
return SqlExecutor.queryAndClosePs(dialect.psForPage(conn, query), rsh);
}
Aggregations