Search in sources :

Example 1 with JdbcBeanMetadata

use of cn.taketoday.jdbc.result.JdbcBeanMetadata 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 2 with JdbcBeanMetadata

use of cn.taketoday.jdbc.result.JdbcBeanMetadata in project today-framework 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 3 with JdbcBeanMetadata

use of cn.taketoday.jdbc.result.JdbcBeanMetadata in project today-framework by TAKETODAY.

the class Dialect method delete.

public String delete(SQLParams sqlParams) {
    StringBuilder sql = new StringBuilder();
    sql.append("DELETE FROM ").append(sqlParams.getTableName());
    if (sqlParams.getConditionSQL().length() > 0) {
        sql.append(" WHERE ").append(sqlParams.getConditionSQL().substring(5));
    } else {
        if (null != sqlParams.getModel()) {
            StringBuilder columnNames = new StringBuilder();
            JdbcBeanMetadata beanMetadata = new JdbcBeanMetadata(sqlParams.getModelClass());
            for (BeanProperty property : beanMetadata) {
                Object value = property.getValue(sqlParams.getModel());
                if (value == null) {
                    continue;
                }
                columnNames.append(property.getName()).append(" = ? and ");
            }
            if (columnNames.length() > 0) {
                sql.append(" WHERE ").append(columnNames.substring(0, columnNames.length() - 5));
            }
        }
    }
    return sql.toString();
}
Also used : JdbcBeanMetadata(cn.taketoday.jdbc.result.JdbcBeanMetadata) BeanProperty(cn.taketoday.beans.BeanProperty)

Example 4 with JdbcBeanMetadata

use of cn.taketoday.jdbc.result.JdbcBeanMetadata 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 5 with JdbcBeanMetadata

use of cn.taketoday.jdbc.result.JdbcBeanMetadata 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)8 JdbcBeanMetadata (cn.taketoday.jdbc.result.JdbcBeanMetadata)8