Ejemplo n.º 1
0
  /**
   * Performs inserts to the table for the managed instance or joins.
   *
   * @param connection the connection to use
   * @param managedInstance the managed instance to perform insert for
   * @throws SQLException thrown in case of underlying SQLException
   * @since $version
   * @author hceylan
   */
  public void performInsert(ConnectionImpl connection, final ManagedInstance<?> managedInstance)
      throws SQLException {
    final EntityTypeImpl<?> entityType = managedInstance.getType();
    final Object instance = managedInstance.getInstance();

    // Do not inline, generation of the insert SQL will initialize the insertColumns!
    final String insertSql = this.getInsertSql(entityType);
    final AbstractColumn[] insertColumns = this.getInsertColumns(entityType);

    // prepare the parameters
    final Object[] params = new Object[insertColumns.length];
    for (int i = 0; i < insertColumns.length; i++) {
      final AbstractColumn column = insertColumns[i];
      if (column instanceof DiscriminatorColumn) {
        params[i] = managedInstance.getType().getDiscriminatorValue();
      } else {
        params[i] = column.getValue(instance);
      }
    }

    // execute the insert
    final QueryRunner runner = new QueryRunner(this.jdbcAdaptor.isPmdBroken());
    runner.update(connection, insertSql, params);

    // if there is an identity column, extract the identity and set it back to the instance
    if (this.identityColumn != null) {
      final String selectLastIdSql = this.jdbcAdaptor.getSelectLastIdentitySql(this.identityColumn);
      final Number id = runner.query(connection, selectLastIdSql, new SingleValueHandler<Number>());

      this.identityColumn.setValue(managedInstance.getInstance(), id);
    }
  }
Ejemplo n.º 2
0
  /**
   * Performs update to the table for the managed instance or joins. In addition checks if the table
   * participate in update.
   *
   * @param connection the connection to use
   * @param managedInstance the managed instance to perform update for
   * @return returns true if the table is updatable
   * @throws SQLException thrown in case of underlying SQLException
   * @since $version
   * @author hceylan
   */
  public boolean performUpdateWithUpdatability(
      ConnectionImpl connection, final ManagedInstance<?> managedInstance) throws SQLException {
    final EntityTypeImpl<?> entityType = managedInstance.getType();
    final Object instance = managedInstance.getInstance();

    // Do not inline, generation of the update SQL will initialize the insertColumns!
    final String updateSql = this.getUpdateSql(entityType, this.pkColumns);
    final AbstractColumn[] updateColumns = this.getUpdateColumns(entityType);

    // prepare the parameters
    final Object[] params = new Object[updateColumns.length];
    for (int i = 0; i < updateColumns.length; i++) {
      final AbstractColumn column = updateColumns[i];

      // if the first column is primary key then the table is not updatable
      if ((i == 0) && column.isPrimaryKey()) {
        return false;
      }

      params[i] = column.getValue(instance);
    }

    // execute the insert
    final QueryRunner runner = new QueryRunner(this.jdbcAdaptor.isPmdBroken());
    runner.update(connection, updateSql, params);

    return true;
  }
Ejemplo n.º 3
0
  /**
   * Performs removes from the table for the managed instance or joins.
   *
   * @param connection the connection to use
   * @param managedInstance the managed instance to perform remove for
   * @throws SQLException thrown in case of underlying SQLException
   * @since $version
   * @author hceylan
   */
  public void performRemove(ConnectionImpl connection, final ManagedInstance<?> managedInstance)
      throws SQLException {
    final String removeSql = this.getRemoveSql();

    // prepare the parameters
    final Object[] params = new Object[this.removeColumns.length];
    for (int i = 0; i < this.removeColumns.length; i++) {
      final AbstractColumn column = this.removeColumns[i];
      params[i] = column.getValue(managedInstance.getInstance());
    }

    final QueryRunner runner = new QueryRunner(this.jdbcAdaptor.isPmdBroken());
    runner.update(connection, removeSql, params);
  }
Ejemplo n.º 4
0
  /**
   * Performs version update to the table.
   *
   * @param connection the connection to use
   * @param managedInstance the managed instance to perform version update for
   * @throws SQLException thrown in case of underlying SQLException
   * @since $version
   * @author hceylan
   */
  public void performVersionUpdate(
      ConnectionImpl connection, final ManagedInstance<?> managedInstance) throws SQLException {
    final Object instance = managedInstance.getInstance();

    // Do not inline, generation of the update SQL will initialize the insertColumns!
    final String updateSql = this.getVersionUpdateSql(this.pkColumns);
    final AbstractColumn[] versionUpdateColumns = this.getVersionUpdateColumns();

    // prepare the parameters
    final Object[] params = new Object[versionUpdateColumns.length];
    for (int i = 0; i < versionUpdateColumns.length; i++) {
      final AbstractColumn column = versionUpdateColumns[i];
      params[i] = column.getValue(instance);
    }

    // execute the insert
    final QueryRunner runner = new QueryRunner(this.jdbcAdaptor.isPmdBroken());
    runner.update(connection, updateSql, params);
  }
Ejemplo n.º 5
0
  /**
   * Selects the version from the table.
   *
   * @param connection the connection to use
   * @param managedInstance the managed instance to perform select for
   * @return the version
   * @throws SQLException thrown in case of underlying SQLException
   * @since $version
   * @author hceylan
   */
  public Object performSelectVersion(
      ConnectionImpl connection, final ManagedInstance<?> managedInstance) throws SQLException {
    final Object instance = managedInstance.getInstance();

    // Do not inline, generation of the update SQL will initialize the insertColumns!
    final String updateSql = this.getSelectVersionSql(this.pkColumns);
    final AbstractColumn[] selectVersionColumns = this.getSelectVersionColumns();

    // prepare the parameters
    final Object[] params = new Object[selectVersionColumns.length];
    for (int i = 0; i < selectVersionColumns.length; i++) {
      final AbstractColumn column = selectVersionColumns[i];
      params[i] = column.getValue(instance);
    }

    // execute the insert
    final QueryRunner runner = new QueryRunner(this.jdbcAdaptor.isPmdBroken());

    return runner.query(connection, updateSql, new SingleValueHandler<Object>(), params);
  }