use of com.qiuyj.mybatis.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.
the class AbstractCommonSqlBuilder method selectList.
@Override
public Object selectList(SqlInfo sqlInfo, Object args) {
checkBeanType(sqlInfo.getBeanType(), args);
BeanExampleResolver resolver = new BeanExampleResolver(args, sqlInfo.getJavaProperties(), sqlInfo.getDatabaseColumns());
List<PropertyColumnMapping> exampleSelectList = resolver.selectExample();
if (exampleSelectList.isEmpty()) {
throw new IllegalStateException("Please specify at least one condition.");
} else {
SQL sql = new SQL().SELECT(sqlInfo.getAllColumnsWithAlias()).FROM(sqlInfo.getTableName());
List<ParameterMapping> parameterMappings = new ArrayList<>(exampleSelectList.size());
for (PropertyColumnMapping pcm : exampleSelectList) {
parameterMappings.add(new ParameterMapping.Builder(sqlInfo.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
sql.WHERE(pcm.getDatabaseColumnName() + " = ?");
}
return new StaticSqlSource(sqlInfo.getConfiguration(), sql.toString(), parameterMappings);
}
}
use of com.qiuyj.mybatis.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.
the class EnumTypeConditionChecker method doCheck.
@Override
@SuppressWarnings("unchecked")
public ReturnValue doCheck(Field field, SqlInfo sqlInfo, ReturnValue preRv) {
Enumerated enumerated = AnnotationUtils.findAnnotation(field, Enumerated.class);
if (Objects.isNull(enumerated)) {
if (Objects.isNull(preRv.fieldMethod)) {
try {
preRv.fieldMethod = ReflectionUtils.getDeclaredMethod(sqlInfo.getBeanType(), fieldToGetterName(field));
} catch (Exception e) {
// ignore
}
}
if (Objects.nonNull(preRv.fieldMethod)) {
enumerated = AnnotationUtils.findAnnotation(preRv.fieldMethod, Enumerated.class);
}
}
if (Objects.nonNull(enumerated)) {
Class<?> type = getFieldJavaType(field);
if (type.isEnum()) {
PropertyColumnMapping enumMapping = sqlInfo.getPropertyColumnMappingByPropertyName(field.getName());
TypeHandler enumTypeHandlerType = enumerated.type() == Enumerated.ValueType.ORDINAL ? new EnumOrdinalTypeHandler(type) : new EnumTypeHandler(type);
enumMapping.setTypeHandler(enumTypeHandlerType);
enumMapping.setJdbcType(enumerated.jdbcType());
sqlInfo.setHasEnumField();
}
}
preRv.intValue = ConditionChecker.CONTINUE_EXECUTION;
return preRv;
}
use of com.qiuyj.mybatis.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.
the class PrimaryKeyAnnotationChecker method doCheck.
@Override
public ReturnValue doCheck(Field field, SqlInfo sqlInfo, ReturnValue preRv) {
preRv.intValue = ConditionChecker.CONTINUE_EXECUTION;
if (!sqlInfo.hasPrimaryKey()) {
boolean hasPrimaryKey = AnnotationUtils.hasAnnotation(field, PrimaryKey.class);
if (!hasPrimaryKey) {
try {
if (Objects.isNull(preRv.fieldMethod)) {
preRv.fieldMethod = ReflectionUtils.getDeclaredMethod(sqlInfo.getBeanType(), fieldToGetterName(field));
}
hasPrimaryKey = AnnotationUtils.hasAnnotation(preRv.fieldMethod, PrimaryKey.class);
} catch (IllegalStateException e) {
// ignore
}
}
if (hasPrimaryKey) {
String columnName = null;
Column column = AnnotationUtils.findAnnotation(field, Column.class);
if (Objects.nonNull(column)) {
columnName = column.value();
} else {
if (Objects.isNull(preRv.fieldMethod)) {
try {
preRv.fieldMethod = ReflectionUtils.getDeclaredMethod(sqlInfo.getBeanType(), fieldToGetterName(field));
} catch (IllegalStateException e) {
// ignore
}
if (Objects.nonNull(preRv.fieldMethod)) {
column = AnnotationUtils.findAnnotation(preRv.fieldMethod, Column.class);
if (Objects.nonNull(column)) {
columnName = column.value();
}
}
}
}
if (StringUtils.isBlank(columnName)) {
columnName = StringUtils.camelCaseToUnderscore(field.getName());
}
Class<?> fieldType = getFieldJavaType(field);
if (Objects.isNull(sqlInfo.getConfiguration())) {
sqlInfo.setPrimaryKey(new PropertyColumnMapping(field.getName(), columnName, fieldType));
} else {
sqlInfo.setPrimaryKey(new PropertyColumnMapping(field.getName(), columnName, sqlInfo.getConfiguration().getTypeHandlerRegistry().getTypeHandler(fieldType)));
}
// 解析@Sequence注解
Sequence sequence = AnnotationUtils.findAnnotation(field, Sequence.class);
if (Objects.isNull(sequence)) {
if (Objects.isNull(preRv.fieldMethod)) {
try {
preRv.fieldMethod = ReflectionUtils.getDeclaredMethod(sqlInfo.getBeanType(), fieldToGetterName(field));
} catch (IllegalStateException e) {
// ignore
}
if (Objects.nonNull(preRv.fieldMethod)) {
sequence = AnnotationUtils.findAnnotation(preRv.fieldMethod, Sequence.class);
}
}
}
if (Objects.nonNull(sequence)) {
String sequenceName = sequence.name();
if (StringUtils.isBlank(sequenceName)) {
throw new IllegalStateException("Sequence name can not be empty");
} else {
sqlInfo.setSequenceName(sequenceName);
}
}
preRv.intValue = ConditionChecker.SKIP_ONE;
}
}
return preRv;
}
use of com.qiuyj.mybatis.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.
the class MysqlBatchInsertParameterObjectResolver method resolveParameterObject.
@Override
public List<ParameterMapping> resolveParameterObject(Configuration config, SqlInfo sqlInfo, Object paramObj, SqlNode sqlNode) {
List list = (List) ParameterResolver.resolveParameter(paramObj).getParameterValues()[0];
if (list.isEmpty()) {
throw new IllegalArgumentException("Method batchInsert parameter can not be an empty list");
} else {
this.sqlInfo = sqlInfo;
this.config = config;
StringJoiner grammaJoiner = new StringJoiner(",");
StringJoiner valueJoiner = new StringJoiner(",", "(", ")");
List<ParameterMapping> singleInstanceParameterMapping = new ArrayList<>(sqlInfo.getFieldCount());
ParameterMapping canonic = new ParameterMapping.Builder(config, "list", batchInsertTypeHandler).build();
for (PropertyColumnMapping propertyColumnMapping : sqlInfo.getPropertyColumnMappings()) {
valueJoiner.add(SqlProvider.PREPARE_FLAG);
singleInstanceParameterMapping.add(canonic);
}
List<ParameterMapping> allParameterMappings = new ArrayList<>(sqlInfo.getFieldCount() * list.size());
String value = valueJoiner.toString();
for (int i = 0; i < list.size(); i++) {
grammaJoiner.add(value);
allParameterMappings.addAll(singleInstanceParameterMapping);
}
// 重新设置StaticTextSqlNode的text属性的值
resetStaticSqlNode((StaticTextSqlNode) sqlNode, grammaJoiner.toString());
return allParameterMappings;
}
}
use of com.qiuyj.mybatis.PropertyColumnMapping in project qiuyj-code by qiuyuanjun.
the class ColumnAnnotationChecker method doCheck.
@Override
public ReturnValue doCheck(Field field, SqlInfo sqlInfo, ReturnValue preRv) {
String columnName = null;
Column column = AnnotationUtils.findAnnotation(field, Column.class);
if (Objects.nonNull(column)) {
columnName = column.value();
} else {
// 查找对应的getter方法
try {
if (Objects.isNull(preRv.fieldMethod)) {
preRv.fieldMethod = ReflectionUtils.getDeclaredMethod(sqlInfo.getBeanType(), fieldToGetterName(field));
}
column = AnnotationUtils.findAnnotation(preRv.fieldMethod, Column.class);
if (Objects.nonNull(column)) {
columnName = column.value();
}
} catch (IllegalStateException e) {
// ingore
}
}
if (StringUtils.isBlank(columnName)) {
columnName = StringUtils.camelCaseToUnderscore(field.getName());
}
Class<?> fieldType = getFieldJavaType(field);
if (Objects.isNull(sqlInfo.getConfiguration())) {
sqlInfo.addPropertyColumn(new PropertyColumnMapping(field.getName(), columnName, fieldType));
} else {
sqlInfo.addPropertyColumn(new PropertyColumnMapping(field.getName(), columnName, sqlInfo.getConfiguration().getTypeHandlerRegistry().getTypeHandler(fieldType)));
}
preRv.intValue = ConditionChecker.CONTINUE_EXECUTION;
return preRv;
}
Aggregations