public static Object convertToSequence(
      ISelectable column, Object value, Parameters parameterSettings) {
    if (column.isAutoIncrement() && !(value instanceof ISequenceVal)) {
      // 处理自增
      if (value == null || value instanceof NullValue) {
        // 识别null值
        return buildSequenceVal(column);
      } else if (value instanceof IBindVal) {
        Object cvalue = ((IBindVal) value).copy();
        // 识别绑定变量
        if (parameterSettings != null && parameterSettings.isBatch()) {
          // boolean isAllNull = true;
          // boolean existNull = false;
          // for (int i = 0; i < parameterSettings.getBatchSize();
          // i++) {
          // Object bvalue = ((IBindVal)
          // value).assignment(parameterSettings.cloneByBatchIndex(i));
          // if (bvalue instanceof IBindVal) {
          // bvalue = ((IBindVal) bvalue).getValue();
          // }
          //
          // existNull |= (bvalue == null || bvalue instanceof
          // NullValue);
          // isAllNull &= (bvalue == null || bvalue instanceof
          // NullValue);
          // }
          //
          // if (existNull && !isAllNull) {// 部分出现null
          // throw new
          // OptimizerException("auto_increment only support all null values");
          // } else if (isAllNull) {
          // return buildSequenceVal(column);
          // }

          Object bvalue = ((IBindVal) cvalue).assignment(parameterSettings.cloneByBatchIndex(0));
          // 可能batch模式,还是返回bindVal对象
          if (bvalue instanceof IBindVal) {
            bvalue = ((IBindVal) bvalue).getValue();
          }

          if (bvalue == null || bvalue instanceof NullValue) {
            return buildSequenceVal(column);
          }
        } else {
          Object bvalue = ((IBindVal) cvalue).assignment(parameterSettings);
          // 可能batch模式,还是返回bindVal对象
          if (bvalue instanceof IBindVal) {
            bvalue = ((IBindVal) bvalue).getValue();
          }

          if (bvalue == null || bvalue instanceof NullValue) {
            return buildSequenceVal(column);
          }
        }
      }
    }

    return value;
  }
Example #2
0
  @Test
  public void test_batchNextVal() throws SqlParserException {
    String sql = "INSERT INTO TABLE1(ID,NAME,SCHOOL) VALUES(TABLE1.NEXTVAL,?,?)";

    Parameters parameterSettings = new Parameters();
    for (int i = 0; i < 10; i++) {
      ParameterContext p1 =
          new ParameterContext(ParameterMethod.setString, new Object[] {1, "ljh" + i});
      ParameterContext p2 =
          new ParameterContext(ParameterMethod.setString, new Object[] {2, "school" + i});
      parameterSettings.getCurrentParameter().put(1, p1);
      parameterSettings.getCurrentParameter().put(2, p2);

      parameterSettings.addBatch();
    }

    IMerge merge = (IMerge) optimizer.optimizeAndAssignment(sql, parameterSettings, extraCmd, true);
    System.out.println(merge);
  }
 private static void findSequenceVal(ISequenceVal s, Parameters parameters) {
   int index =
       parameters.getFirstParameter().size() + parameters.getSequenceSize().incrementAndGet();
   ((ISequenceVal) s).setOriginIndex(index);
 }