@Override
  public void init(NutConfig config) {
    Ioc ioc = config.getIoc();
    Dao dao = ioc.get(Dao.class);

    // 若必要的数据表不存在,则初始化数据库
    if (!dao.exists(User.class)) {
      dao.create(User.class, true);
      dao.create(Role.class, true);
      dao.create(Permission.class, true);
      FileSqlManager fm = new FileSqlManager("init_system_h2.sql");
      List<Sql> sqlList = fm.createCombo(fm.keys());
      dao.execute(sqlList.toArray(new Sql[sqlList.size()]));
      // 初始化用户密码(全部都是123)及salt
      List<User> userList = dao.query(User.class, null);
      for (User user : userList) {
        RandomNumberGenerator rng = new SecureRandomNumberGenerator();
        String salt = rng.nextBytes().toBase64();
        String hashedPasswordBase64 = new Sha256Hash("123", salt, 1024).toBase64();
        user.setSalt(salt);
        user.setPassword(hashedPasswordBase64);
        dao.update(user);
      }
    }

    // 设置 Shiro 的 securityManager
    SecurityManager securityManager = ioc.get(SecurityManager.class);
    SecurityUtils.setSecurityManager(securityManager);
  }
Exemple #2
0
 protected void dropRelation(Dao dao, Entity<?> en) {
   final List<Sql> sqls = new ArrayList<Sql>(5);
   for (LinkField lf : en.visitManyMany(null, null, null)) {
     ManyManyLinkField mm = (ManyManyLinkField) lf;
     if (!dao.exists(mm.getRelationName())) continue;
     sqls.add(Sqls.create("DROP TABLE " + mm.getRelationName()));
   }
   dao.execute(sqls.toArray(new Sql[sqls.size()]));
 }
Exemple #3
0
 public void createRelation(Dao dao, Entity<?> en) {
   final List<Sql> sqls = new ArrayList<Sql>(5);
   for (LinkField lf : en.visitManyMany(null, null, null)) {
     ManyManyLinkField mm = (ManyManyLinkField) lf;
     if (dao.exists(mm.getRelationName())) continue;
     String sql = "CREATE TABLE " + mm.getRelationName() + "(";
     sql += mm.getFromColumnName() + " " + evalFieldType(mm.getHostField()) + ",";
     sql += mm.getToColumnName() + " " + evalFieldType(mm.getLinkedField());
     sql += ")";
     sqls.add(Sqls.create(sql));
   }
   dao.execute(sqls.toArray(new Sql[sqls.size()]));
 }
Exemple #4
0
  public boolean dropEntity(Dao dao, Entity<?> en) {
    String tableName = en.getTableName();
    String viewName = en.getViewName();

    try {
      dropRelation(dao, en);
      if (!tableName.equals(viewName) && dao.exists(viewName)) {
        dao.execute(Sqls.create("DROP VIEW " + viewName));
      }
      dao.execute(Sqls.create("DROP TABLE " + tableName));
    } catch (Exception e) {
      return false;
    }
    return true;
  }
Exemple #5
0
  /**
   * 为数据表自动增减字段
   *
   * @param dao Dao实例
   * @param klass 映射Pojo
   * @param add 是否允许添加
   * @param del 是否允许删除
   * @param tableName 动态表名上下文
   */
  public static void migration(
      Dao dao, final Class<?> klass, final boolean add, final boolean del, Object tableName) {
    final AbstractJdbcExpert expert = (AbstractJdbcExpert) ((NutDao) dao).getJdbcExpert();
    if (tableName != null && Strings.isNotBlank(tableName.toString())) {
      dao = ext(dao, tableName);
    }
    final Entity<?> en = dao.getEntity(klass);
    if (!dao.exists(klass)) return;
    final List<Sql> sqls = new ArrayList<Sql>();
    final Set<String> _indexs = new HashSet<String>();
    final boolean sqlAddNeedColumn = !dao.meta().isOracle();
    final boolean isCanComment = dao.meta().isMySql();
    dao.run(
        new ConnCallback() {
          public void invoke(Connection conn) throws Exception {
            expert.setupEntityField(conn, en);
            Statement stat = null;
            ResultSet rs = null;
            ResultSetMetaData meta = null;
            try {
              // 获取数据库元信息
              stat = conn.createStatement();
              rs = stat.executeQuery("select * from " + en.getTableName() + " where 1 != 1");
              meta = rs.getMetaData();

              Set<String> columnNames = new HashSet<String>();
              int columnCount = meta.getColumnCount();
              for (int i = 1; i <= columnCount; i++) {
                columnNames.add(meta.getColumnName(i).toLowerCase());
              }
              for (MappingField mf : en.getMappingFields()) {
                if (mf.isReadonly()) continue;
                String colName = mf.getColumnName();
                if (columnNames.contains(colName.toLowerCase())) {
                  columnNames.remove(colName.toLowerCase());
                  continue;
                }
                if (add) {
                  log.infof("add column[%s] to table[%s]", mf.getColumnName(), en.getTableName());
                  StringBuilder sb = new StringBuilder("ALTER TABLE ");
                  sb.append(meta.getTableName(1)).append(" ADD ");
                  if (sqlAddNeedColumn) sb.append("COLUMN ");
                  sb.append(colName).append(" ").append(expert.evalFieldType(mf));
                  if (mf.isUnsigned()) {
                    sb.append(" UNSIGNED");
                  }
                  if (mf.isNotNull()) {
                    sb.append(" NOT NULL");
                  }
                  if (mf.getColumnType() == ColType.TIMESTAMP) {
                    if (mf.hasDefaultValue()) {
                      sb.append(" ").append(mf.getDefaultValue(null).replaceAll("@", "@@"));
                    } else {
                      if (mf.isNotNull()) {
                        sb.append(" DEFAULT 0");
                      } else {
                        sb.append(" NULL DEFAULT NULL");
                      }
                    }
                  } else {
                    if (mf.hasDefaultValue()) expert.addDefaultValue(sb, mf);
                  }
                  if (mf.hasColumnComment() && isCanComment) {
                    sb.append(" COMMENT '").append(mf.getColumnComment()).append("'");
                  }
                  sb.append(';');
                  Sql sql = Sqls.create(sb.toString());
                  sqls.add(sql);
                }
              }
              if (del) {
                for (String colName : columnNames) {
                  log.infof("del column[%s] from table[%s]", colName, en.getTableName());
                  Sql sql = Sqls.create("ALTER table $table DROP column $name");
                  sql.vars().set("table", en.getTableName());
                  sql.vars().set("name", colName);
                  sqls.add(sql);
                }
              }
              // show index from mytable;
              String showIndexs = "show index from " + en.getTableName();
              PreparedStatement ppstat = conn.prepareStatement(showIndexs);
              ResultSet rest = ppstat.executeQuery();
              while (rest.next()) {
                String index = rest.getString(3);
                _indexs.add(index);
              }
            } catch (SQLException e) {
              if (log.isDebugEnabled())
                log.debugf("migration Table '%s' fail!", en.getTableName(), e);
            }
            // Close ResultSet and Statement
            finally {
              Daos.safeClose(stat, rs);
            }
          }
        });
    for (Sql sql : sqls) {
      dao.execute(sql);
    }
    // 创建索引
    List<Sql> indexsSql = createIndexs(dao, en, _indexs, tableName);
    if (!Lang.isEmpty(indexsSql)) {
      dao.execute(indexsSql.toArray(new Sql[0]));
    }
    // 创建关联表
    expert.createRelation(dao, en);
  }