Example #1
0
  /**
   * 获取查询sql <功能详细描述>
   *
   * @return [参数说明]
   * @return String [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public String updateSql(Object obj) {
    AssertUtils.notEmpty(obj, "update obj must not empty.");

    // 获取当前对象中有哪些属性
    MetaObject metaObject = MetaObjectUtils.forObject(obj);
    Set<String> keySet = new HashSet<String>();
    for (String getterName : metaObject.getGetterNames()) {
      keySet.add(getterName);
    }
    AssertUtils.isTrue(
        keySet.contains(this.pkName),
        "obj:{} must contains pk{}.",
        new Object[] {obj, this.pkName});

    // 构建query语句
    SqlBuilder.BEGIN();
    SqlBuilder.UPDATE(this.tableName);

    for (String propertyName : updateAblePropertyNames) {
      if (!keySet.contains(propertyName)) {
        continue;
      }
      // Object value = metaObject.getValue(propertyName);
      String columnName = getter2columnNameMapping.get(propertyName);
      SqlBuilder.SET(columnName + " = ?");
    }
    SqlBuilder.WHERE(this.getter2columnNameMapping.get(this.pkName) + " = ? ");
    String updateSql = SqlBuilder.SQL();
    SqlBuilder.RESET();
    return updateSql;
  }
Example #2
0
  /**
   * 获取查询条件的Setter对象 <功能详细描述>
   *
   * @param obj
   * @return [参数说明]
   * @return PreparedStatementSetter [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public PreparedStatementSetter getUpdateSetter(Object obj) {
    AssertUtils.notEmpty(obj, "update obj must not empty.");

    // 获取当前对象中有哪些属性
    final MetaObject metaObject = MetaObjectUtils.forObject(obj);
    final Set<String> keySet = new HashSet<String>();
    for (String getterName : metaObject.getGetterNames()) {
      keySet.add(getterName);
    }
    AssertUtils.isTrue(
        keySet.contains(this.pkName),
        "obj:{} must contains pk{}.",
        new Object[] {obj, this.pkName});
    final String finalPkName = this.pkName;
    PreparedStatementSetter res =
        new PreparedStatementSetter() {
          @Override
          public void setValues(PreparedStatement ps) throws SQLException {
            int i = 1;
            for (String propertyNameTemp : updateAblePropertyNames) {
              if (!keySet.contains(propertyNameTemp)) {
                continue;
              }
              Object valueObj = metaObject.getValue(propertyNameTemp);
              Class<?> setterType = getter2JavaTypeMapping.get(propertyNameTemp);
              JdbcUtils.setPreparedStatementValueForSimpleType(ps, i++, valueObj, setterType);
            }
            Class<?> setterType = getter2JavaTypeMapping.get(finalPkName);
            JdbcUtils.setPreparedStatementValueForSimpleType(
                ps, i, metaObject.getValue(finalPkName), setterType);
          }
        };
    return res;
  }
Example #3
0
  /**
   * 获取删除语句<br>
   * <功能详细描述>
   *
   * @return [参数说明]
   * @return String [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public String deleteSql() {
    AssertUtils.notEmpty(this.pkName, "pkName is empty.");
    AssertUtils.isTrue(
        this.getter2columnNameMapping.containsKey(this.pkName),
        "property2columnNameMapping not contains pkName:{}.",
        this.pkName);

    // 开始构建sql
    SqlBuilder.BEGIN();
    SqlBuilder.DELETE_FROM(this.tableName);
    SqlBuilder.WHERE(this.getter2columnNameMapping.get(this.pkName) + " = ? ");
    String deleteSql = SqlBuilder.SQL();
    SqlBuilder.RESET();

    return deleteSql;
  }
Example #4
0
  /**
   * 获取查询单条数据的sql <功能详细描述>
   *
   * @return [参数说明]
   * @return String [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public String findSql() {
    AssertUtils.notEmpty(this.pkName, "pkName is empty.");
    AssertUtils.isTrue(
        this.getter2columnNameMapping.containsKey(this.pkName),
        "property2columnNameMapping not contains pkName:{}.",
        this.pkName);

    SqlBuilder.BEGIN();
    for (Entry<String, String> entryTemp : getter2columnNameMapping.entrySet()) {
      SqlBuilder.SELECT(entryTemp.getValue());
    }
    SqlBuilder.FROM(this.tableName);
    SqlBuilder.WHERE(this.getter2columnNameMapping.get(this.pkName) + " = ? ");
    String findSql = SqlBuilder.SQL();
    SqlBuilder.RESET();
    return findSql;
  }
Example #5
0
  /**
   * 获取查询sql <功能详细描述>
   *
   * @return [参数说明]
   * @return String [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public String querySql(Object obj) {
    AssertUtils.notEmpty(this.pkName, "pkName is empty.");
    AssertUtils.isTrue(
        this.getter2columnNameMapping.containsKey(this.pkName),
        "property2columnNameMapping not contains pkName:{}.",
        this.pkName);

    // 构建query语句
    SqlBuilder.BEGIN();
    for (Entry<String, String> entryTemp : getter2columnNameMapping.entrySet()) {
      SqlBuilder.SELECT(entryTemp.getValue());
    }
    SqlBuilder.FROM(this.tableName);

    if (!ObjectUtils.isEmpty(obj)) {
      MetaObject metaObject = MetaObjectUtils.forObject(obj);
      for (Entry<String, String> entryTemp : queryConditionKey2SqlMapping.entrySet()) {
        String queryKeyName = entryTemp.getKey();
        Object valueObj = metaObject.getValue(queryKeyName);
        if (ObjectUtils.isEmpty(valueObj)) {
          continue;
        }
        SqlBuilder.WHERE(entryTemp.getValue());
      }
    }
    for (String conditionExpressionTemp : otherCondition) {
      SqlBuilder.WHERE(conditionExpressionTemp);
    }

    // 在不存在排序字段时默认使用主键对应字段作为排序字段<br/>
    if (CollectionUtils.isEmpty(orderList)) {
      SqlBuilder.ORDER_BY(this.getter2columnNameMapping.get(this.pkName));
    } else {
      for (String order : orderList) {
        SqlBuilder.ORDER_BY(order);
      }
    }

    String querySql = SqlBuilder.SQL();
    SqlBuilder.RESET();

    return querySql;
  }
  /**
   * @param newTableDef
   * @param sourceTableDef
   * @param ddlDialect
   * @return
   */
  @Override
  public AlterTableDDLBuilder newInstance(
      TableDef newTable, TableDef sourceTable, DDLDialect ddlDialect) {
    AssertUtils.notNull(ddlDialect, "ddlDialect is null.");
    AssertUtils.notNull(newTable, "newTable is null.");
    AssertUtils.notEmpty(newTable.getTableName(), "newTable.tableName is empty.");
    AssertUtils.notNull(sourceTable, "sourceTable is null.");
    AssertUtils.notEmpty(sourceTable.getTableName(), "sourceTable.tableName is empty.");
    AssertUtils.isTrue(
        newTable.getTableName().equalsIgnoreCase(sourceTable.getTableName()),
        "newTable.tableName:{} should equalsIgnoreCase sourceTable.tableName:{}",
        new Object[] {newTable.getTableName(), sourceTable.getTableName()});

    MysqlAlterTableDDLBuilder builder =
        new MysqlAlterTableDDLBuilder(newTable, sourceTable, ddlDialect);
    this.columns.addAll(newTable.getColumns());
    this.indexes.addAll(newTable.getIndexes());
    return builder;
  }
Example #7
0
  /**
   * 获取设置主键的setter对象<br>
   * <功能详细描述>
   *
   * @param obj
   * @return [参数说明]
   * @return PreparedStatementSetter [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public PreparedStatementSetter getPKSetter(final Object obj) {
    AssertUtils.notEmpty(this.pkName, "pkName is empty.");
    AssertUtils.isTrue(
        this.getter2columnNameMapping.containsKey(this.pkName),
        "property2columnNameMapping not contains pkName:{}.",
        this.pkName);

    final String finalPkName = this.pkName;
    final MetaObject metaObject = MetaObjectUtils.forObject(obj);
    PreparedStatementSetter res =
        new PreparedStatementSetter() {
          @Override
          public void setValues(PreparedStatement ps) throws SQLException {
            Class<?> setterType = getter2JavaTypeMapping.get(finalPkName);
            JdbcUtils.setPreparedStatementValueForSimpleType(
                ps, 1, metaObject.getValue(finalPkName), setterType);
          }
        };
    return res;
  }