Example #1
0
 /**
  * Returns an array of EOSQLExpressions for deleting this column.
  *
  * @return an array of EOSQLExpressions for deleting this column
  */
 @SuppressWarnings("unchecked")
 public NSArray<EOSQLExpression> _deleteExpressions() {
   EOSchemaSynchronization schemaSynchronization = _table.database().synchronizationFactory();
   NSArray<EOSQLExpression> expressions =
       schemaSynchronization.statementsToDeleteColumnNamed(
           name(), _table.name(), (NSDictionary<String, String>) NSDictionary.EmptyDictionary);
   ERXMigrationDatabase._ensureNotEmpty(expressions, "delete column", true);
   return expressions;
 }
Example #2
0
  /**
   * Returns an EOAttribute with all of its fields filled in based on the properties of this
   * ERXMigrationColumn. The attribute is attached to the given entity.
   *
   * @param entity the entity to add the attribute to
   * @return an EOAttribute with all of its fields filled in
   */
  @SuppressWarnings("unchecked")
  public EOAttribute _newAttribute(EOEntity entity) {
    EOAdaptor eoAdaptor = _table.database().adaptor();
    // MS: Hack to make Memory adaptor migrations "work"
    if (!(eoAdaptor instanceof JDBCAdaptor)) {
      EOAttribute nonJdbcAttribute = new EOAttribute();
      nonJdbcAttribute.setName(_name);
      nonJdbcAttribute.setColumnName(_name);
      nonJdbcAttribute.setExternalType("nonJdbcAttribute");
      entity.addAttribute(nonJdbcAttribute);
      return nonJdbcAttribute;
    }

    JDBCAdaptor adaptor = (JDBCAdaptor) _table.database().adaptor();
    ERXSQLHelper sqlHelper = ERXSQLHelper.newSQLHelper(adaptor);
    String externalType = sqlHelper.externalTypeForJDBCType(adaptor, _jdbcType);
    if (externalType == null) {
      externalType = "IF_YOU_ARE_SEEING_THIS_SOMETHING_WENT_WRONG_WITH_EXTERNAL_TYPES";
    }
    EOAttribute attribute =
        adaptor.createAttribute(
            _name, _name, _jdbcType, externalType, _precision, _scale, _allowsNull ? 1 : 0);
    if (_width > 0) {
      attribute.setWidth(_width);
    }
    if (_defaultValue != null) {
      NSDictionary userInfo = attribute.userInfo();
      NSMutableDictionary mutableUserInfo;
      if (userInfo == null) {
        mutableUserInfo = new NSMutableDictionary();
      } else {
        mutableUserInfo = userInfo.mutableClone();
      }
      mutableUserInfo.setObjectForKey(_defaultValue, "default");
      attribute.setUserInfo(mutableUserInfo);
    }

    if (_overrideValueType != null) {
      if (ERXMigrationColumn.NULL_VALUE_TYPE.equals(_overrideValueType)) {
        attribute.setValueType(null);
      } else {
        attribute.setValueType(_overrideValueType);
      }
      if (sqlHelper.reassignExternalTypeForValueTypeOverride(attribute)) {
        adaptor.assignExternalTypeForAttribute(attribute);
      }
    }

    if (_overrideExternalType != null) {
      attribute.setExternalType(_overrideExternalType);
    }

    entity.addAttribute(attribute);
    return attribute;
  }
Example #3
0
 /**
  * Changes the "allows null" state of this column.
  *
  * @param allowsNull if true, this column allows nulls
  * @throws SQLException if the change fails
  */
 @SuppressWarnings("unchecked")
 public void setAllowsNull(boolean allowsNull) throws SQLException {
   EOSchemaSynchronization schemaSynchronization = _table.database().synchronizationFactory();
   NSArray<EOSQLExpression> expressions =
       schemaSynchronization.statementsToModifyColumnNullRule(
           name(),
           _table.name(),
           allowsNull,
           (NSDictionary<String, String>) NSDictionary.EmptyDictionary);
   ERXMigrationDatabase._ensureNotEmpty(expressions, "modify allows null", true);
   ERXJDBCUtilities.executeUpdateScript(
       _table.database().adaptorChannel(),
       ERXMigrationDatabase._stringsForExpressions(expressions));
 }
Example #4
0
 /**
  * Returns an array of EOSQLExpressions for creating this column.
  *
  * @return an array of EOSQLExpressions for creating this column
  */
 @SuppressWarnings("unchecked")
 public NSArray<EOSQLExpression> _createExpressions() {
   EOSchemaSynchronization schemaSynchronization = _table.database().synchronizationFactory();
   NSArray<EOSQLExpression> expressions =
       schemaSynchronization.statementsToInsertColumnForAttribute(
           _newAttribute(), (NSDictionary<String, String>) NSDictionary.EmptyDictionary);
   ERXMigrationDatabase._ensureNotEmpty(expressions, "add column", true);
   return expressions;
 }
Example #5
0
 /**
  * Executes the SQL operations to create this column.
  *
  * @throws SQLException if the creation fails
  */
 public void create() throws SQLException {
   if (_new) {
     ERXJDBCUtilities.executeUpdateScript(
         _table.database().adaptorChannel(),
         ERXMigrationDatabase._stringsForExpressions(_createExpressions()));
     _new = false;
   } else {
     ERXMigrationDatabase.log.warn(
         "You called .create() on the column '" + _name + "', but it was already created.");
   }
 }
Example #6
0
 /**
  * Changes the data type of this column.
  *
  * @param jdbcType the new JDBC type of the column (see java.sql.Types)
  * @param scale the new scale
  * @param precision the new precision
  * @param width the new width
  * @param options the options to use for conversion (or null)
  * @throws SQLException if the change fails
  */
 @SuppressWarnings("unchecked")
 public void setDataType(int jdbcType, int scale, int precision, int width, NSDictionary options)
     throws SQLException {
   JDBCAdaptor adaptor = (JDBCAdaptor) _table.database().adaptor();
   String externalType =
       ERXSQLHelper.newSQLHelper(adaptor).externalTypeForJDBCType(adaptor, jdbcType);
   EOSchemaSynchronization schemaSynchronization = _table.database().synchronizationFactory();
   NSArray<EOSQLExpression> expressions =
       schemaSynchronization.statementsToConvertColumnType(
           _name,
           _table.name(),
           null,
           new _ColumnType(externalType, scale, precision, width),
           options);
   ERXMigrationDatabase._ensureNotEmpty(expressions, "convert column type", true);
   ERXJDBCUtilities.executeUpdateScript(
       _table.database().adaptorChannel(),
       ERXMigrationDatabase._stringsForExpressions(expressions));
   _jdbcType = jdbcType;
   _scale = scale;
   _precision = precision;
   _width = width;
 }
Example #7
0
 /**
  * Executes the SQL operations to rename this column.
  *
  * @param newName the new name of this column
  * @throws SQLException if the rename fails
  */
 public void renameTo(String newName) throws SQLException {
   ERXJDBCUtilities.executeUpdateScript(
       _table.database().adaptorChannel(),
       ERXMigrationDatabase._stringsForExpressions(_renameToExpressions(newName)));
 }
Example #8
0
 /**
  * Executes the SQL operations to delete this column.
  *
  * @throws SQLException if the delete fails
  */
 public void delete() throws SQLException {
   ERXJDBCUtilities.executeUpdateScript(
       _table.database().adaptorChannel(),
       ERXMigrationDatabase._stringsForExpressions(_deleteExpressions()));
   _table._columnDeleted(this);
 }
Example #9
0
 /**
  * Returns an EOAttribute with all of its fields filled in based on the properties of this
  * ERXMigrationColumn. The attribute is attached to a table._blankEntity().
  *
  * @return an EOAttribute with all of its fields filled in
  */
 public EOAttribute _newAttribute() {
   return _newAttribute(_table._blankEntity());
 }