Search in sources :

Example 21 with BeanProperty

use of cn.taketoday.beans.BeanProperty in project today-framework by TAKETODAY.

the class TypeDescriptorTests method upCast.

@Test
public void upCast() throws Exception {
    final BeanProperty property = BeanProperty.valueOf(getClass(), "property");
    TypeDescriptor typeDescriptor = property.getTypeDescriptor();
    TypeDescriptor upCast = typeDescriptor.upcast(Object.class);
    assertThat(upCast.getAnnotation(MethodAnnotation3.class) != null).isTrue();
}
Also used : BeanProperty(cn.taketoday.beans.BeanProperty) Test(org.junit.jupiter.api.Test)

Example 22 with BeanProperty

use of cn.taketoday.beans.BeanProperty in project today-framework by TAKETODAY.

the class BeanPropertyRowMapper method mapRow.

/**
 * Extract the values for all columns in the current row.
 * <p>Utilizes public setters and result set meta-data.
 *
 * @see ResultSetMetaData
 */
@Override
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
    BeanWrapperImpl beanWrapper = this.beanWrapper;
    T mappedObject = constructMappedInstance(rs, beanWrapper);
    beanWrapper.setBeanInstance(mappedObject);
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    Set<String> populatedProperties = isCheckFullyPopulated() ? new HashSet<>() : null;
    HashMap<String, BeanProperty> mappedFields = this.mappedFields;
    for (int index = 1; index <= columnCount; index++) {
        String column = JdbcUtils.lookupColumnName(rsmd, index);
        String field = lowerCaseName(StringUtils.delete(column, " "));
        BeanProperty property = mappedFields != null ? mappedFields.get(field) : null;
        if (property != null) {
            try {
                // TODO using TypeHandler
                Object value = getColumnValue(rs, index, property);
                if (rowNumber == 0 && debugEnabled) {
                    log.debug("Mapping column '{}' to property '{}' of type '{}'", column, property.getName(), ClassUtils.getQualifiedName(property.getType()));
                }
                try {
                    beanWrapper.setPropertyValue(property.getName(), value);
                } catch (TypeMismatchException ex) {
                    if (value == null && this.primitivesDefaultedForNullValue) {
                        if (debugEnabled) {
                            log.debug("Intercepted TypeMismatchException for row {} and column '{}'" + " with null value when setting property '{}' of type '{}' on object: {}", rowNumber, column, property.getName(), ClassUtils.getQualifiedName(property.getType()), mappedObject, ex);
                        }
                    } else {
                        throw ex;
                    }
                }
                if (populatedProperties != null) {
                    populatedProperties.add(property.getName());
                }
            } catch (NotWritablePropertyException ex) {
                throw new DataRetrievalFailureException("Unable to map column '" + column + "' to property '" + property.getName() + "'", ex);
            }
        } else {
            // No BeanProperty found
            if (rowNumber == 0 && debugEnabled) {
                log.debug("No property found for column '{}' mapped to field '{}'", column, field);
            }
        }
    }
    if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
        throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of " + this.mappedClass + ": " + this.mappedProperties);
    }
    return mappedObject;
}
Also used : BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) BeanProperty(cn.taketoday.beans.BeanProperty) ResultSetMetaData(java.sql.ResultSetMetaData) NotWritablePropertyException(cn.taketoday.beans.NotWritablePropertyException) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) DataRetrievalFailureException(cn.taketoday.dao.DataRetrievalFailureException)

Example 23 with BeanProperty

use of cn.taketoday.beans.BeanProperty in project today-framework by TAKETODAY.

the class BeanPropertySqlParameterSource method getReadablePropertyNames.

/**
 * Provide access to the property names of the wrapped bean.
 * Uses support provided in the {@link PropertyAccessor} interface.
 *
 * @return an array containing all the known property names
 */
public String[] getReadablePropertyNames() {
    if (this.propertyNames == null) {
        ArrayList<String> names = new ArrayList<>();
        for (BeanProperty property : beanWrapper.getBeanProperties()) {
            if (property.isReadable()) {
                names.add(property.getName());
            }
        }
        this.propertyNames = StringUtils.toStringArray(names);
    }
    return this.propertyNames;
}
Also used : ArrayList(java.util.ArrayList) BeanProperty(cn.taketoday.beans.BeanProperty)

Example 24 with BeanProperty

use of cn.taketoday.beans.BeanProperty in project today-framework by TAKETODAY.

the class DefaultResultSetHandlerFactory method getAccessor.

private JdbcPropertyAccessor getAccessor(String propertyName, JdbcBeanMetadata metadata) {
    int index = propertyName.indexOf('.');
    if (index <= 0) {
        // Simple path - fast way
        BeanProperty beanProperty = metadata.getBeanProperty(propertyName);
        // behavior change: do not throw if POJO contains less properties
        if (beanProperty == null) {
            return null;
        }
        TypeHandler<?> typeHandler = registry.getTypeHandler(beanProperty.getType());
        return new TypeHandlerPropertyAccessor(typeHandler, beanProperty);
    }
    // dot path - long way
    // i'm too lazy now to rewrite this case so I just call old unoptimized code...
    TypeHandler<?> typeHandler = registry.getTypeHandler(metadata.getType());
    class PropertyPathPropertyAccessor extends JdbcPropertyAccessor {

        @Override
        public Object get(Object obj) {
            return metadata.getProperty(obj, propertyName);
        }

        @Override
        public void set(Object obj, ResultSet resultSet, int columnIndex) throws SQLException {
            Object result = typeHandler.getResult(resultSet, columnIndex);
            metadata.setProperty(obj, propertyName, result);
        }
    }
    return new PropertyPathPropertyAccessor();
}
Also used : ResultSet(java.sql.ResultSet) BeanProperty(cn.taketoday.beans.BeanProperty)

Example 25 with BeanProperty

use of cn.taketoday.beans.BeanProperty in project today-framework by TAKETODAY.

the class Dialect method buildColumns.

public static String buildColumns(List<String> excludedColumns, Class<?> model) {
    StringBuilder sql = new StringBuilder();
    JdbcBeanMetadata beanMetadata = new JdbcBeanMetadata(model);
    for (BeanProperty property : beanMetadata) {
        String alias = property.getName();
        if (excludedColumns.contains(alias)) {
            sql.append(alias).append(',');
        }
    }
    if (sql.length() > 0) {
        return sql.substring(0, sql.length() - 1);
    }
    return "*";
}
Also used : JdbcBeanMetadata(cn.taketoday.jdbc.result.JdbcBeanMetadata) BeanProperty(cn.taketoday.beans.BeanProperty)

Aggregations

BeanProperty (cn.taketoday.beans.BeanProperty)42 Test (org.junit.jupiter.api.Test)22 JdbcBeanMetadata (cn.taketoday.jdbc.result.JdbcBeanMetadata)8 BeanWrapperImpl (cn.taketoday.beans.BeanWrapperImpl)6 ArrayList (java.util.ArrayList)6 List (java.util.List)4 BeanMetadata (cn.taketoday.beans.BeanMetadata)2 NotWritablePropertyException (cn.taketoday.beans.NotWritablePropertyException)2 SimpleTypeConverter (cn.taketoday.beans.SimpleTypeConverter)2 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)2 PropertyAccessor (cn.taketoday.core.reflect.PropertyAccessor)2 DataRetrievalFailureException (cn.taketoday.dao.DataRetrievalFailureException)2 InvalidDataAccessApiUsageException (cn.taketoday.dao.InvalidDataAccessApiUsageException)2 Method (java.lang.reflect.Method)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2