コード例 #1
0
ファイル: PsqlJdbcExpert.java プロジェクト: varlxj/nutz
  public boolean createEntity(Dao dao, Entity<?> en) {
    StringBuilder sb = new StringBuilder("CREATE TABLE " + en.getTableName() + "(");
    // 创建字段
    for (MappingField mf : en.getMappingFields()) {
      if (mf.isReadonly()) continue;
      sb.append('\n').append(mf.getColumnName());
      // 自增主键特殊形式关键字
      if (mf.isId() && mf.isAutoIncreasement()) {
        sb.append(" SERIAL");
      } else {
        sb.append(' ').append(evalFieldType(mf));
        // 非主键的 @Name,应该加入唯一性约束
        if (mf.isName() && en.getPkType() != PkType.NAME) {
          sb.append(" UNIQUE NOT NULL");
        }
        // 普通字段
        else {
          if (mf.isUnsigned()) sb.append(" UNSIGNED");
          if (mf.isNotNull()) sb.append(" NOT NULL");
          if (mf.isAutoIncreasement()) throw Lang.noImplement();
          if (mf.hasDefaultValue())
            sb.append(" DEFAULT '").append(getDefaultValue(mf)).append('\'');
        }
      }
      sb.append(',');
    }
    // 创建主键
    List<MappingField> pks = en.getPks();
    if (!pks.isEmpty()) {
      sb.append('\n');
      sb.append(
          String.format(
              "CONSTRAINT %s_pkey PRIMARY KEY (",
              en.getTableName().replace('.', '_').replace('"', '_')));
      for (MappingField pk : pks) {
        sb.append(pk.getColumnName()).append(',');
      }
      sb.setCharAt(sb.length() - 1, ')');
      sb.append("\n ");
    }

    // 结束表字段设置
    sb.setCharAt(sb.length() - 1, ')');

    // 执行创建语句
    dao.execute(Sqls.create(sb.toString()));

    // 创建索引
    dao.execute(createIndexs(en).toArray(new Sql[0]));

    // 创建关联表
    createRelation(dao, en);

    // 添加注释(表注释与字段注释)
    addComment(dao, en);

    return true;
  }
コード例 #2
0
  // TODO not tested!!
  public boolean createEntity(Dao dao, Entity<?> en) {
    StringBuilder sb = new StringBuilder("CREATE TABLE " + en.getTableName() + "(");
    // 创建字段
    for (MappingField mf : en.getMappingFields()) {
      sb.append('\n').append(mf.getColumnName());
      sb.append(' ').append(evalFieldType(mf));
      // 非主键的 @Name,应该加入唯一性约束
      if (mf.isName() && en.getPkType() != PkType.NAME) {
        sb.append(" UNIQUE NOT NULL");
      }
      // 普通字段
      else {
        if (mf.isNotNull() || mf.isPk()) sb.append(" NOT NULL");
        if (mf.hasDefaultValue()) sb.append(" DEFAULT '").append(getDefaultValue(mf)).append('\'');
        if (mf.isAutoIncreasement()) sb.append(" generated by default as identity ");
        if (mf.isPk() && en.getPks().size() == 1) {
          sb.append(" primary key ");
        }
      }
      sb.append(',');
    }
    // 结束表字段设置
    sb.setCharAt(sb.length() - 1, ')');

    // 执行创建语句
    dao.execute(Sqls.create(sb.toString()));

    // 创建联合主键
    if (en.getPks().size() > 1) {
      sb = new StringBuilder();
      sb.append("ALTER TABLE ").append(en.getTableName()).append(" ADD CONSTRAINT PK_");
      sb.append(makePksName(en));
      sb.append(" PRIMARY KEY (");
      for (MappingField mf : en.getPks()) {
        sb.append(mf.getColumnName()).append(",");
      }
      sb.setCharAt(sb.length() - 1, ')');
      dao.execute(Sqls.create(sb.toString()));
    }

    // 创建关联表
    createRelation(dao, en);
    // 创建索引
    dao.execute(createIndexs(en).toArray(new Sql[0]));
    // 添加注释(表注释与字段注释)
    addComment(dao, en);

    return true;
  }
コード例 #3
0
ファイル: Daos.java プロジェクト: GaoHuijian/nutz
  @SuppressWarnings({"unchecked", "rawtypes"})
  public static boolean filterFields(
      Object obj, FieldMatcher matcher, Dao dao, Callback2<MappingField, Object> callback) {
    if (obj == null) return false;
    obj = Lang.first(obj);
    if (obj == null) {
      return false;
    }
    if (obj.getClass() == Class.class) {
      throw Lang.impossible();
    }
    if (obj instanceof String || obj instanceof Number || obj instanceof Boolean) {
      throw Lang.impossible();
    }
    Entity en = dao.getEntity(obj.getClass());
    if (en == null) {
      throw Lang.impossible();
    }

    List<MappingField> mfs = en.getMappingFields();
    if (matcher != null) {
      Iterator<MappingField> it = mfs.iterator();
      while (it.hasNext()) {
        MappingField mf = it.next();
        if (!matcher.match(mf.getName())) it.remove();
      }
    }
    boolean flag = false;
    for (MappingField mf : mfs) {
      if (matcher.isIgnoreId() && mf.isId()) continue;
      if (matcher.isIgnoreName() && mf.isName()) continue;
      if (matcher.isIgnorePk() && mf.isCompositePk()) continue;
      Object val = mf.getValue(obj);
      if (val == null) {
        if (matcher.isIgnoreNull()) continue;
      }
      if (val instanceof Number && ((Number) val).doubleValue() == 0.0) {
        if (matcher.isIgnoreZero()) continue;
      }
      if (val instanceof Date) {
        if (matcher.isIgnoreDate()) continue;
      }
      callback.invoke(mf, val);
      flag = true;
    }
    return flag;
  }