Search in sources :

Example 6 with BeanProperty

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

the class Dialect method insert.

public String insert(SQLParams sqlParams) {
    StringBuilder sql = new StringBuilder();
    sql.append("INSERT INTO ").append(sqlParams.getTableName());
    StringBuilder columnNames = new StringBuilder();
    StringBuilder placeholder = new StringBuilder();
    JdbcBeanMetadata beanMetadata = new JdbcBeanMetadata(sqlParams.getModelClass());
    Object model = sqlParams.getModel();
    for (BeanProperty property : beanMetadata) {
        Object value = property.getValue(model);
        if (value != null) {
            columnNames.append(",").append(" ").append(property.getName());
            placeholder.append(", ?");
        }
    }
    if (columnNames.length() > 0 && placeholder.length() > 0) {
        sql.append("(").append(columnNames.substring(2)).append(")");
        sql.append(" VALUES (").append(placeholder.substring(2)).append(")");
    }
    return sql.toString();
}
Also used : JdbcBeanMetadata(cn.taketoday.jdbc.result.JdbcBeanMetadata) BeanProperty(cn.taketoday.beans.BeanProperty)

Example 7 with BeanProperty

use of cn.taketoday.beans.BeanProperty in project today-infrastructure 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 8 with BeanProperty

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

the class JmxUtilsTests method getAttributeNameWithoutStrictCasing.

@Test
void getAttributeNameWithoutStrictCasing() {
    BeanProperty pd = new BeanWrapperImpl(AttributeTestBean.class).getBeanProperty("name");
    String attributeName = JmxUtils.getAttributeName(pd, false);
    assertThat(attributeName).as("Incorrect casing on attribute name").isEqualTo("name");
}
Also used : BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) BeanProperty(cn.taketoday.beans.BeanProperty) Test(org.junit.jupiter.api.Test)

Example 9 with BeanProperty

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

the class JmxUtilsTests method getAttributeNameWithStrictCasing.

@Test
void getAttributeNameWithStrictCasing() {
    BeanProperty pd = new BeanWrapperImpl(AttributeTestBean.class).getBeanProperty("name");
    String attributeName = JmxUtils.getAttributeName(pd, true);
    assertThat(attributeName).as("Incorrect casing on attribute name").isEqualTo("Name");
}
Also used : BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) BeanProperty(cn.taketoday.beans.BeanProperty) Test(org.junit.jupiter.api.Test)

Example 10 with BeanProperty

use of cn.taketoday.beans.BeanProperty in project today-infrastructure 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)

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