Search in sources :

Example 16 with BeanProperty

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

the class Dialect method update.

public String update(SQLParams sqlParams) {
    StringBuilder sql = new StringBuilder();
    sql.append("UPDATE ").append(sqlParams.getTableName()).append(" SET ");
    StringBuilder setSQL = new StringBuilder();
    Map<String, Object> updateColumns = sqlParams.getUpdateColumns();
    if (CollectionUtils.isNotEmpty(updateColumns)) {
        updateColumns.forEach((key, value) -> setSQL.append(key).append(" = ?, "));
    } else {
        Object model = sqlParams.getModel();
        if (model != null) {
            JdbcBeanMetadata beanMetadata = new JdbcBeanMetadata(sqlParams.getModelClass());
            for (BeanProperty property : beanMetadata) {
                Object value = property.getValue(model);
                if (value == null) {
                    continue;
                }
                setSQL.append(property.getName()).append(" = ?, ");
            }
        }
    }
    sql.append(setSQL.substring(0, setSQL.length() - 2));
    if (sqlParams.getConditionSQL().length() > 0) {
        sql.append(" WHERE ").append(sqlParams.getConditionSQL().substring(5));
    }
    return sql.toString();
}
Also used : JdbcBeanMetadata(cn.taketoday.jdbc.result.JdbcBeanMetadata) BeanProperty(cn.taketoday.beans.BeanProperty)

Example 17 with BeanProperty

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

the class TypeDescriptorTests method propertyGenericClassList.

@Test
void propertyGenericClassList() throws Exception {
    IntegerClass genericBean = new IntegerClass();
    BeanProperty property = BeanProperty.valueOf(genericBean.getClass().getMethod("getListProperty"), genericBean.getClass().getMethod("setListProperty", List.class), IntegerClass.class);
    TypeDescriptor desc = property.getTypeDescriptor();
    assertThat(desc.getType()).isEqualTo(List.class);
    assertThat(desc.getElementDescriptor().getType()).isEqualTo(Integer.class);
    assertThat(desc.getAnnotation(MethodAnnotation1.class)).isNotNull();
    assertThat(desc.hasAnnotation(MethodAnnotation1.class)).isTrue();
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) BeanProperty(cn.taketoday.beans.BeanProperty) Test(org.junit.jupiter.api.Test)

Example 18 with BeanProperty

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

the class TypeDescriptorTests method propertyTypeCovariance.

@Test
void propertyTypeCovariance() throws Exception {
    GenericType<Number> genericBean = new NumberType();
    BeanProperty property = BeanProperty.valueOf(genericBean.getClass().getMethod("getProperty"), genericBean.getClass().getMethod("setProperty", Number.class));
    TypeDescriptor desc = property.getTypeDescriptor();
    assertThat(desc.getType()).isEqualTo(Integer.class);
}
Also used : BeanProperty(cn.taketoday.beans.BeanProperty) Test(org.junit.jupiter.api.Test)

Example 19 with BeanProperty

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

the class TypeDescriptorTests method propertyGenericType.

@Test
void propertyGenericType() throws Exception {
    GenericType<Integer> genericBean = new IntegerType();
    BeanProperty property = BeanProperty.valueOf(genericBean.getClass().getMethod("getProperty"), genericBean.getClass().getMethod("setProperty", Integer.class));
    TypeDescriptor desc = property.getTypeDescriptor();
    assertThat(desc.getType()).isEqualTo(Integer.class);
}
Also used : BeanProperty(cn.taketoday.beans.BeanProperty) Test(org.junit.jupiter.api.Test)

Example 20 with BeanProperty

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

the class TypeDescriptorTests method property.

@Test
public void property() throws Exception {
    final BeanProperty property = BeanProperty.valueOf(getClass(), "property");
    TypeDescriptor desc = property.getTypeDescriptor();
    assertThat(desc.getType()).isEqualTo(Map.class);
    assertThat(desc.getMapKeyDescriptor().getElementDescriptor().getType()).isEqualTo(Integer.class);
    assertThat(desc.getMapValueDescriptor().getElementDescriptor().getType()).isEqualTo(Long.class);
    PropertyAccessor propertyAccessor = property.obtainAccessor();
    Method writeMethod = propertyAccessor.getWriteMethod();
    Method readMethod = propertyAccessor.getReadMethod();
    assertThat(writeMethod).isNotNull();
    assertThat(readMethod).isNotNull();
    assertThat(readMethod.getAnnotation(MethodAnnotation1.class)).isNotNull();
    assertThat(writeMethod.getAnnotation(MethodAnnotation2.class)).isNotNull();
    assertThat(desc.getAnnotation(MethodAnnotation3.class)).isNotNull();
}
Also used : PropertyAccessor(cn.taketoday.core.reflect.PropertyAccessor) Method(java.lang.reflect.Method) BeanProperty(cn.taketoday.beans.BeanProperty) Test(org.junit.jupiter.api.Test)

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