예제 #1
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;
  }
예제 #2
0
파일: Daos.java 프로젝트: GaoHuijian/nutz
  /** 根据Pojo生成数据字典,zdoc格式 */
  public static StringBuilder dataDict(DataSource ds, String... packages) {
    StringBuilder sb = new StringBuilder();
    List<Class<?>> ks = new ArrayList<Class<?>>();
    for (String packageName : packages) {
      ks.addAll(Scans.me().scanPackage(packageName));
    }
    Iterator<Class<?>> it = ks.iterator();
    while (it.hasNext()) {
      Class<?> klass = it.next();
      if (klass.getAnnotation(Table.class) == null) it.remove();
    }
    // log.infof("Found %d table class", ks.size());

    JdbcExpert exp = Jdbcs.getExpert(ds);
    NutDao dao = new NutDao(ds);

    Method evalFieldType;
    try {
      evalFieldType = exp.getClass().getDeclaredMethod("evalFieldType", MappingField.class);
    } catch (Throwable e) {
      throw Lang.wrapThrow(e);
    }
    evalFieldType.setAccessible(true);
    Entity<?> entity = null;
    String line = "-------------------------------------------------------------------\n";
    sb.append("#title:数据字典\n");
    sb.append("#author:wendal\n");
    sb.append("#index:0,1\n").append(line);
    for (Class<?> klass : ks) {
      sb.append(line);
      entity = dao.getEntity(klass);
      sb.append("表名 ").append(entity.getTableName()).append("\n\n");
      if (!Strings.isBlank(entity.getTableComment()))
        sb.append("表注释: ").append(entity.getTableComment());
      sb.append("\t").append("Java类名 ").append(klass.getName()).append("\n\n");
      sb.append("\t||序号||列名||数据类型||主键||非空||默认值||java属性名||java类型||注释||\n");
      int index = 1;
      for (MappingField field : entity.getMappingFields()) {
        String dataType;
        try {
          dataType = (String) evalFieldType.invoke(exp, field);
        } catch (Throwable e) {
          throw Lang.wrapThrow(e); // 不可能发生的
        }
        sb.append("\t||")
            .append(index++)
            .append("||")
            .append(field.getColumnName())
            .append("||")
            .append(dataType)
            .append("||")
            .append(field.isPk())
            .append("||")
            .append(field.isNotNull())
            .append("||")
            .append(field.getDefaultValue(null) == null ? " " : field.getDefaultValue(null))
            .append("||")
            .append(field.getName())
            .append("||")
            .append(field.getTypeClass().getName())
            .append("||")
            .append(field.getColumnComment() == null ? " " : field.getColumnComment())
            .append("||\n");
      }
    }
    return sb;
  }