示例#1
0
  /**
   * 获取插入的sql语句
   *
   * @return
   */
  public static SqlInfo buildInsertSql(Object entity) {

    List<KeyValue> keyValueList = getSaveKeyValueListByEntity(entity);

    StringBuffer strSQL = new StringBuffer();
    SqlInfo sqlInfo = null;
    if (keyValueList != null && keyValueList.size() > 0) {

      sqlInfo = new SqlInfo();

      strSQL.append("INSERT INTO ");
      strSQL.append(TableInfo.get(entity.getClass()).getTableName());
      strSQL.append(" (");
      for (KeyValue kv : keyValueList) {
        strSQL.append(kv.getKey()).append(",");
        sqlInfo.addValue(kv.getValue());
      }
      strSQL.deleteCharAt(strSQL.length() - 1);
      strSQL.append(") VALUES ( ");

      int length = keyValueList.size();
      for (int i = 0; i < length; i++) {
        strSQL.append("?,");
      }
      strSQL.deleteCharAt(strSQL.length() - 1);
      strSQL.append(")");

      sqlInfo.setSql(strSQL.toString());
    }

    return sqlInfo;
  }
示例#2
0
  public static SqlInfo getUpdateSqlAsSqlInfo(Object entity) {

    TableInfo table = TableInfo.get(entity.getClass());
    Object idvalue = table.getId().getValue(entity);

    if (null == idvalue) { // 主键值不能为null,否则不能更新
      throw new DbException("this entity[" + entity.getClass() + "]'s id value is null");
    }

    List<KeyValue> keyValueList = new ArrayList<KeyValue>();
    // 添加属性
    Collection<Property> propertys = table.propertyMap.values();
    for (Property property : propertys) {
      KeyValue kv = property2KeyValue(property, entity);
      if (kv != null) keyValueList.add(kv);
    }

    if (keyValueList == null || keyValueList.size() == 0) return null;

    SqlInfo sqlInfo = new SqlInfo();
    StringBuffer strSQL = new StringBuffer("UPDATE ");
    strSQL.append(table.getTableName());
    strSQL.append(" SET ");
    for (KeyValue kv : keyValueList) {
      strSQL.append(kv.getKey()).append("=?,");
      sqlInfo.addValue(kv.getValue());
    }
    strSQL.deleteCharAt(strSQL.length() - 1);
    strSQL.append(" WHERE ").append(table.getId().getColumn()).append("=?");
    sqlInfo.addValue(idvalue);
    sqlInfo.setSql(strSQL.toString());
    return sqlInfo;
  }
示例#3
0
  public static SqlInfo getUpdateSqlAsSqlInfo(Object entity, String strWhere) {

    TableInfo table = TableInfo.get(entity.getClass());

    List<KeyValue> keyValueList = new ArrayList<KeyValue>();

    // 添加属性
    Collection<Property> propertys = table.propertyMap.values();
    for (Property property : propertys) {
      KeyValue kv = property2KeyValue(property, entity);
      if (kv != null) keyValueList.add(kv);
    }

    if (keyValueList == null || keyValueList.size() == 0) {
      throw new DbException("this entity[" + entity.getClass() + "] has no property");
    }

    SqlInfo sqlInfo = new SqlInfo();
    StringBuffer strSQL = new StringBuffer("UPDATE ");
    strSQL.append(table.getTableName());
    strSQL.append(" SET ");
    for (KeyValue kv : keyValueList) {
      strSQL.append(kv.getKey()).append("=?,");
      sqlInfo.addValue(kv.getValue());
    }
    strSQL.deleteCharAt(strSQL.length() - 1);
    if (!TextUtils.isEmpty(strWhere)) {
      strSQL.append(" WHERE ").append(strWhere);
    }
    sqlInfo.setSql(strSQL.toString());
    return sqlInfo;
  }
示例#4
0
 /**
  * 把List<KeyValue>数据存储到ContentValues
  *
  * @param list
  * @param cv
  */
 private void insertContentValues(List<KeyValue> list, ContentValues cv) {
   if (list != null && cv != null) {
     for (KeyValue kv : list) {
       cv.put(kv.getKey(), kv.getValue().toString());
     }
   } else {
     Log.w(TAG, "insertContentValues: List<KeyValue> is empty or ContentValues is empty!");
   }
 }