use of indi.mybatis.flying.exception.BuildSqlException in project mybatis.flying by limeng32.
the class SqlBuilder method buildDeleteSql.
/**
* 由传入的对象生成delete sql语句
*
* @param object
* pojo
* @return sql
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws RuntimeException
*/
public static String buildDeleteSql(Object object) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (null == object) {
throw new BuildSqlException(BuildSqlExceptionEnum.nullObject);
}
Map<?, ?> dtoFieldMap = PropertyUtils.describe(object);
TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));
String tableName = tableMapper.getTableName();
StringBuffer sql = new StringBuffer();
sql.append(DELETE_FROM_).append(tableName).append(WHERE_);
for (FieldMapper fieldMapper : tableMapper.getUniqueKeyNames()) {
sql.append(fieldMapper.getDbFieldName());
Object value = dtoFieldMap.get(fieldMapper.getFieldName());
if (value == null) {
throw new BuildSqlException(new StringBuffer(BuildSqlExceptionEnum.deleteUniqueKeyIsNull.toString()).append(fieldMapper.getDbFieldName()).toString());
}
sql.append(EQUAL_POUND_OPENBRACE).append(fieldMapper.getFieldName()).append(COMMA).append(JDBCTYPE_EQUAL).append(fieldMapper.getJdbcType().toString()).append(CLOSEBRACE_AND_);
}
for (FieldMapper f : tableMapper.getOpVersionLocks()) {
sql.append(f.getDbFieldName()).append(EQUAL_POUND_OPENBRACE).append(f.getFieldName()).append(CLOSEBRACE_AND_);
}
sql.delete(sql.lastIndexOf(AND), sql.lastIndexOf(AND) + 3);
return sql.toString();
}
use of indi.mybatis.flying.exception.BuildSqlException in project mybatis.flying by limeng32.
the class SqlBuilder method buildCountSql.
/**
* 由传入的对象生成count sql语句
*
* @param object
* pojo
* @return sql
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws RuntimeException
*/
public static String buildCountSql(Object object) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (null == object) {
throw new BuildSqlException(BuildSqlExceptionEnum.nullObject);
}
TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));
ai = new AtomicInteger(0);
TableName tableName = new TableName(tableMapper, 0, null);
selectSql = new StringBuffer();
selectSql.append(SELECT_COUNT_OPENPAREN).append(tableName.sqlWhere());
/*
* 如果有且只有一个主键,采用select count("主键")的方式;如果无主键或有多个主键(联合主键),采用select
* count(*)的方式。
*/
if (tableMapper.getUniqueKeyNames().length == 1) {
selectSql.append(tableMapper.getUniqueKeyNames()[0].getDbFieldName());
} else {
selectSql.append(ASTERISK);
}
selectSql.append(CLOSEPAREN);
fromSql = new StringBuffer(FROM);
whereSql = new StringBuffer(WHERE_);
dealMapperAnnotationIterationForCount(object, fromSql, whereSql, null, null, null, ai, tableName);
if (selectSql.indexOf(COMMA) > -1) {
selectSql.delete(selectSql.lastIndexOf(COMMA), selectSql.lastIndexOf(COMMA) + 1);
}
if (WHERE_.equals(whereSql.toString())) {
whereSql = new StringBuffer();
} else if (whereSql.indexOf(AND) > -1) {
whereSql.delete(whereSql.lastIndexOf(AND), whereSql.lastIndexOf(AND) + 3);
}
return selectSql.append(fromSql).append(whereSql).toString();
}
Aggregations