Пример #1
0
  public PColumn newColumn(int position, ColumnDef def, PrimaryKeyConstraint pkConstraint)
      throws SQLException {
    try {
      Set<String> pkColumnNames =
          pkConstraint == null ? Collections.<String>emptySet() : pkConstraint.getColumnNames();
      String columnName = def.getColumnDefName().getColumnName().getName();
      PName familyName = null;
      if (def.isPK() && !pkColumnNames.isEmpty()) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.PRIMARY_KEY_ALREADY_EXISTS)
            .setColumnName(columnName)
            .build()
            .buildException();
      }
      boolean isPK = def.isPK() || pkColumnNames.contains(columnName);
      if (def.getColumnDefName().getFamilyName() != null) {
        String family = def.getColumnDefName().getFamilyName().getName();
        if (isPK) {
          throw new SQLExceptionInfo.Builder(SQLExceptionCode.PRIMARY_KEY_WITH_FAMILY_NAME)
              .setColumnName(columnName)
              .setFamilyName(family)
              .build()
              .buildException();
        } else if (!def.isNull()) {
          throw new SQLExceptionInfo.Builder(SQLExceptionCode.KEY_VALUE_NOT_NULL)
              .setColumnName(columnName)
              .setFamilyName(family)
              .build()
              .buildException();
        }
        familyName = new PNameImpl(family);
      } else if (!isPK) {
        familyName = QueryConstants.DEFAULT_COLUMN_FAMILY_NAME;
      }

      ColumnModifier columnModifier = def.getColumnModifier();
      if (pkConstraint != null && pkConstraint.getColumnModifier(columnName) != null) {
        columnModifier = pkConstraint.getColumnModifier(columnName);
      }

      PColumn column =
          new PColumnImpl(
              new PNameImpl(columnName),
              familyName,
              def.getDataType(),
              def.getMaxLength(),
              def.getScale(),
              def.isNull(),
              position,
              columnModifier);
      return column;
    } catch (IllegalArgumentException e) { // Based on precondition check in constructor
      throw new SQLException(e);
    }
  }