Search in sources :

Example 1 with StaticTextSqlNode

use of org.apache.ibatis.scripting.xmltags.StaticTextSqlNode in project qiuyj-code by qiuyuanjun.

the class SqlProvider method update.

public ReturnValueWrapper update(MappedStatement ms, SqlInfo sqlInfo, Object args) {
    checkPrimaryKey(sqlInfo);
    checkBeanType(sqlInfo.getBeanType(), args);
    BeanExampleResolver exampleResolver = new BeanExampleResolver(args, sqlInfo.getJavaProperties(), sqlInfo.getDatabaseColumns());
    if (!exampleResolver.hasPrimaryKeyAndNotDefault()) {
        throw new NoPrimaryKeyException("primary key is default value");
    } else {
        List<PropertyColumnMapping> nonNullColumns = exampleResolver.getWithoutPrimaryKey();
        if (nonNullColumns.isEmpty()) {
            throw new IllegalStateException("Please update at least one column");
        } else {
            List<ParameterMapping> parameterMappings = new ArrayList<>(nonNullColumns.size() + 1);
            SQL sql = new SQL();
            sql.UPDATE(sqlInfo.getTableName());
            for (PropertyColumnMapping pcm : nonNullColumns) {
                sql.SET(pcm.getDatabaseColumnName() + " = ?");
                parameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
            }
            sql.WHERE(sqlInfo.getPrimaryKey().getDatabaseColumnName() + " = ?");
            parameterMappings.add(new ParameterMapping.Builder(ms.getConfiguration(), sqlInfo.getPrimaryKey().getJavaClassPropertyName(), sqlInfo.getPrimaryKey().getTypeHandler()).build());
            return new ReturnValueWrapper(new StaticTextSqlNode(sql.toString()), parameterMappings);
        }
    }
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) BeanExampleResolver(com.qiuyj.mybatis.BeanExampleResolver) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode) SQL(org.apache.ibatis.jdbc.SQL)

Example 2 with StaticTextSqlNode

use of org.apache.ibatis.scripting.xmltags.StaticTextSqlNode in project qiuyj-code by qiuyuanjun.

the class SqlProvider method insert.

public ReturnValueWrapper insert(MappedStatement ms, SqlInfo sqlInfo) {
    // 不建议以下这种方式创建SQL,这样会增加编译之后的字节码文件数量
    // 因为这样会创建一个匿名内部类
    /*SQL sql = new SQL() {
      {
        INSERT_INTO(sqlInfo.getTableName());
        INTO_COLUMNS(sqlInfo.getAllColumnsWithoutAlias());
        String[] prepareColumnValues = new String[sqlInfo.getFiledCount()];
        Arrays.fill(prepareColumnValues, PREPARE_FLAG);
        INTO_VALUES(prepareColumnValues);
      }
    };*/
    String[] prepareColumnValues = new String[sqlInfo.getFieldCount()];
    Arrays.fill(prepareColumnValues, PREPARE_FLAG);
    String sql = new SQL().INSERT_INTO(sqlInfo.getTableName()).INTO_COLUMNS(sqlInfo.getAllColumnsWithoutAlias()).INTO_VALUES(prepareColumnValues).toString();
    List<ParameterMapping> parameterMappings = new ArrayList<>(sqlInfo.getFieldCount());
    TypeHandlerRegistry reg = ms.getConfiguration().getTypeHandlerRegistry();
    ParameterMapping.Builder parameterBuilder;
    for (PropertyColumnMapping mapping : sqlInfo.getPropertyColumnMappings()) {
        parameterBuilder = new ParameterMapping.Builder(ms.getConfiguration(), mapping.getJavaClassPropertyName(), mapping.getTypeHandler());
        parameterMappings.add(parameterBuilder.build());
    }
    return new ReturnValueWrapper(new StaticTextSqlNode(sql), parameterMappings);
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode) SQL(org.apache.ibatis.jdbc.SQL)

Example 3 with StaticTextSqlNode

use of org.apache.ibatis.scripting.xmltags.StaticTextSqlNode in project qiuyj-code by qiuyuanjun.

the class SqlProvider method selectList.

public ReturnValueWrapper selectList(MappedStatement ms, 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(ms.getConfiguration(), pcm.getJavaClassPropertyName(), pcm.getValue().getClass()).build());
            sql.WHERE(pcm.getDatabaseColumnName() + " = ?");
        }
        return new ReturnValueWrapper(new StaticTextSqlNode(sql.toString()), parameterMappings);
    }
}
Also used : PropertyColumnMapping(com.qiuyj.mybatis.PropertyColumnMapping) BeanExampleResolver(com.qiuyj.mybatis.BeanExampleResolver) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode) SQL(org.apache.ibatis.jdbc.SQL)

Example 4 with StaticTextSqlNode

use of org.apache.ibatis.scripting.xmltags.StaticTextSqlNode in project qiuyj-code by qiuyuanjun.

the class MySqlDialect method batchInsert.

@Override
public ReturnValueWrapper batchInsert(Configuration configuration, SqlInfo sqlInfo) {
    // 由于批量增加的sql可能会很长,所以这里给一个较大的初始化值
    StringBuilder sqlBuilder = new StringBuilder(256);
    sqlBuilder.append("INSERT INTO ").append(sqlInfo.getTableName());
    StringJoiner joiner = new StringJoiner(",", "(", ")");
    for (String column : sqlInfo.getAllColumnsWithoutAlias()) {
        joiner.add(column);
    }
    sqlBuilder.append(joiner.toString());
    sqlBuilder.append(" VALUES ");
    return new ReturnValueWrapper(new StaticTextSqlNode(sqlBuilder.toString()), MysqlBatchInsertParameterObjectResolver.getInstance());
}
Also used : ReturnValueWrapper(com.qiuyj.mybatis.sqlbuild.ReturnValueWrapper) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode) StringJoiner(java.util.StringJoiner)

Example 5 with StaticTextSqlNode

use of org.apache.ibatis.scripting.xmltags.StaticTextSqlNode in project qiuyj-code by qiuyuanjun.

the class SqlProvider method primaryKeyResolver.

/**
 * 生成以主键作为条件的sql的返回值
 */
private ReturnValueWrapper primaryKeyResolver(PropertyColumnMapping primaryKey, Configuration config, String sql) {
    List<ParameterMapping> parameterMappings = new ArrayList<>(1);
    parameterMappings.add(new ParameterMapping.Builder(config, primaryKey.getJavaClassPropertyName(), primaryKey.getTypeHandler()).build());
    return new ReturnValueWrapper(new StaticTextSqlNode(sql), parameterMappings);
}
Also used : ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ArrayList(java.util.ArrayList) StaticTextSqlNode(org.apache.ibatis.scripting.xmltags.StaticTextSqlNode)

Aggregations

StaticTextSqlNode (org.apache.ibatis.scripting.xmltags.StaticTextSqlNode)6 ArrayList (java.util.ArrayList)4 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)4 PropertyColumnMapping (com.qiuyj.mybatis.PropertyColumnMapping)3 SQL (org.apache.ibatis.jdbc.SQL)3 BeanExampleResolver (com.qiuyj.mybatis.BeanExampleResolver)2 ReturnValueWrapper (com.qiuyj.mybatis.sqlbuild.ReturnValueWrapper)1 StringJoiner (java.util.StringJoiner)1 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)1