Пример #1
0
 public ValueAdaptor getAdaptor(MappingField ef) {
   Mirror<?> mirror = ef.getTypeMirror();
   // 为数字型枚举的特殊判断
   if (mirror.isEnum() && ColType.INT == ef.getColumnType()) return Jdbcs.Adaptor.asEnumInt;
   // 用普通逻辑返回适配器
   return Jdbcs.getAdaptor(mirror);
 }
Пример #2
0
  /**
   * 执行一个特殊的Chain(事实上普通Chain也能执行,但不建议使用)
   *
   * @see org.nutz.dao.Chain#addSpecial(String, Object)
   */
  @SuppressWarnings({"rawtypes"})
  public static void insertBySpecialChain(Dao dao, Entity en, String tableName, Chain chain) {
    if (en != null) tableName = en.getTableName();
    if (tableName == null) throw Lang.makeThrow(DaoException.class, "tableName and en is NULL !!");
    final StringBuilder sql = new StringBuilder("INSERT INTO ").append(tableName).append(" (");
    StringBuilder _value_places = new StringBuilder(" VALUES(");
    final List<Object> values = new ArrayList<Object>();
    final List<ValueAdaptor> adaptors = new ArrayList<ValueAdaptor>();
    Chain head = chain.head();
    while (head != null) {
      String colName = head.name();
      MappingField mf = null;
      if (en != null) {
        mf = en.getField(colName);
        if (mf != null) colName = mf.getColumnName();
      }
      sql.append(colName);

      if (head.special()) {
        _value_places.append(head.value());
      } else {
        if (en != null) mf = en.getField(head.name());
        _value_places.append("?");
        values.add(head.value());
        ValueAdaptor adaptor = Jdbcs.getAdaptorBy(head.value());
        if (mf != null && mf.getAdaptor() != null) adaptor = mf.getAdaptor();
        adaptors.add(adaptor);
      }

      head = head.next();
      if (head != null) {
        sql.append(", ");
        _value_places.append(", ");
      }
    }
    sql.append(")");
    _value_places.append(")");
    sql.append(_value_places);
    if (log.isDebugEnabled()) log.debug(sql);
    dao.run(
        new ConnCallback() {
          public void invoke(Connection conn) throws Exception {
            PreparedStatement ps = conn.prepareStatement(sql.toString());
            try {
              for (int i = 0; i < values.size(); i++) adaptors.get(i).set(ps, values.get(i), i + 1);
              ps.execute();
            } finally {
              Daos.safeClose(ps);
            }
          }
        });
  }
Пример #3
0
 public void setExpert(Object obj) throws Exception {
   if (obj == null) throw new NullPointerException("expert MUST NOT NULL!!");
   if (obj instanceof JdbcExpert) {
     this.expert = (JdbcExpert) obj;
   } else {
     String name = obj.toString();
     this.expert = Jdbcs.getExpert(name, "");
     if (this.expert == null) {
       if (name.contains(".")) {
         this.expert = (JdbcExpert) Lang.loadClass(name).newInstance();
       } else {
         throw new DaoException("not such expert=" + obj);
       }
     }
   }
   DataSource ds = this.dataSource;
   // 如果数据源比expert先设置,那么需要重新设置一次
   if (ds != null) {
     this.dataSource = null;
     setDataSource(ds);
   }
 }
Пример #4
0
 @Override
 public ValueAdaptor getAdaptor(MappingField ef) {
   if (ef.getTypeMirror().isOf(Blob.class)) return new BlobValueAdaptor2(Jdbcs.getFilePool());
   return super.getAdaptor(ef);
 }
Пример #5
0
 /**
  * 执行一个特殊的Chain(事实上普通Chain也能执行,但不建议使用)
  *
  * @see org.nutz.dao.Chain#addSpecial(String, Object)
  */
 @SuppressWarnings({"rawtypes"})
 public static int updateBySpecialChain(
     Dao dao, Entity en, String tableName, Chain chain, Condition cnd) {
   if (en != null) tableName = en.getTableName();
   if (tableName == null) throw Lang.makeThrow(DaoException.class, "tableName and en is NULL !!");
   final StringBuilder sql = new StringBuilder("UPDATE ").append(tableName).append(" SET ");
   Chain head = chain.head();
   final List<Object> values = new ArrayList<Object>();
   final List<ValueAdaptor> adaptors = new ArrayList<ValueAdaptor>();
   while (head != null) {
     MappingField mf = null;
     if (en != null) mf = en.getField(head.name());
     String colName = head.name();
     if (mf != null) colName = mf.getColumnName();
     sql.append(colName).append("=");
     if (head.special()) {
       if (head.value() != null && head.value() instanceof String) {
         String str = (String) head.value();
         if (str.length() > 0) {
           switch (str.charAt(0)) {
             case '+':
             case '-':
             case '*':
             case '/':
             case '%':
             case '&':
             case '^':
             case '|':
               sql.append(colName);
               break;
           }
         }
       }
       sql.append(head.value());
     } else {
       sql.append("?");
       values.add(head.value());
       ValueAdaptor adaptor = Jdbcs.getAdaptorBy(head.value());
       if (mf != null && mf.getAdaptor() != null) adaptor = mf.getAdaptor();
       adaptors.add(adaptor);
     }
     sql.append(" ");
     head = head.next();
     if (head != null) sql.append(", ");
   }
   if (cnd != null) sql.append(" ").append(cnd.toSql(en));
   if (log.isDebugEnabled()) log.debug(sql);
   final int[] ints = new int[1];
   dao.run(
       new ConnCallback() {
         public void invoke(Connection conn) throws Exception {
           PreparedStatement ps = conn.prepareStatement(sql.toString());
           try {
             for (int i = 0; i < values.size(); i++) adaptors.get(i).set(ps, values.get(i), i + 1);
             ints[0] = ps.executeUpdate();
           } finally {
             Daos.safeClose(ps);
           }
         }
       });
   return ints[0];
 }
Пример #6
0
  /** 根据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;
  }