/** Deletes a single row in the projects table. */
  public void delete(ProjectsPk pk) throws ProjectsDaoException {
    long t1 = System.currentTimeMillis();
    // declare variables
    final boolean isConnSupplied = (userConn != null);
    Connection conn = null;
    PreparedStatement stmt = null;

    try {
      // get the user-specified connection or get a connection from the ResourceManager
      conn = isConnSupplied ? userConn : ResourceManager.getConnection();

      System.out.println("Executing " + SQL_DELETE + " with PK: " + pk);
      stmt = conn.prepareStatement(SQL_DELETE);
      stmt.setInt(1, pk.getIdprojects());
      int rows = stmt.executeUpdate();
      long t2 = System.currentTimeMillis();
      System.out.println(rows + " rows affected (" + (t2 - t1) + " ms)");
    } catch (Exception _e) {
      _e.printStackTrace();
      throw new ProjectsDaoException("Exception: " + _e.getMessage(), _e);
    } finally {
      ResourceManager.close(stmt);
      if (!isConnSupplied) {
        ResourceManager.close(conn);
      }
    }
  }
  /** Inserts a new row in the project_phases table. */
  public ProjectPhasesPk insert(ProjectPhases dto) throws ProjectPhasesDaoException {
    long t1 = System.currentTimeMillis();
    // declare variables
    final boolean isConnSupplied = (userConn != null);
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try {
      // get the user-specified connection or get a connection from the ResourceManager
      conn = isConnSupplied ? userConn : ResourceManager.getConnection();

      stmt = conn.prepareStatement(SQL_INSERT, Statement.RETURN_GENERATED_KEYS);
      int index = 1;
      stmt.setInt(index++, dto.getIdprojectPhases());
      stmt.setString(index++, dto.getName());
      stmt.setString(index++, dto.getDescription());
      stmt.setDate(
          index++,
          dto.getStartDate() == null ? null : new java.sql.Date(dto.getStartDate().getTime()));
      stmt.setDate(
          index++, dto.getEndDate() == null ? null : new java.sql.Date(dto.getEndDate().getTime()));
      if (dto.isStatusNull()) {
        stmt.setNull(index++, java.sql.Types.INTEGER);
      } else {
        stmt.setInt(index++, dto.getStatus());
      }

      if (dto.isProjectIdNull()) {
        stmt.setNull(index++, java.sql.Types.INTEGER);
      } else {
        stmt.setInt(index++, dto.getProjectId());
      }

      System.out.println("Executing " + SQL_INSERT + " with DTO: " + dto);
      int rows = stmt.executeUpdate();
      long t2 = System.currentTimeMillis();
      System.out.println(rows + " rows affected (" + (t2 - t1) + " ms)");

      // retrieve values from auto-increment columns
      rs = stmt.getGeneratedKeys();
      if (rs != null && rs.next()) {
        dto.setIdprojectPhases(rs.getInt(1));
      }

      reset(dto);
      return dto.createPk();
    } catch (Exception _e) {
      _e.printStackTrace();
      throw new ProjectPhasesDaoException("Exception: " + _e.getMessage(), _e);
    } finally {
      ResourceManager.close(stmt);
      if (!isConnSupplied) {
        ResourceManager.close(conn);
      }
    }
  }
  /** Updates a single row in the project_phases table. */
  public void update(ProjectPhasesPk pk, ProjectPhases dto) throws ProjectPhasesDaoException {
    long t1 = System.currentTimeMillis();
    // declare variables
    final boolean isConnSupplied = (userConn != null);
    Connection conn = null;
    PreparedStatement stmt = null;

    try {
      // get the user-specified connection or get a connection from the ResourceManager
      conn = isConnSupplied ? userConn : ResourceManager.getConnection();

      System.out.println("Executing " + SQL_UPDATE + " with DTO: " + dto);
      stmt = conn.prepareStatement(SQL_UPDATE);
      int index = 1;
      stmt.setInt(index++, dto.getIdprojectPhases());
      stmt.setString(index++, dto.getName());
      stmt.setString(index++, dto.getDescription());
      stmt.setDate(
          index++,
          dto.getStartDate() == null ? null : new java.sql.Date(dto.getStartDate().getTime()));
      stmt.setDate(
          index++, dto.getEndDate() == null ? null : new java.sql.Date(dto.getEndDate().getTime()));
      if (dto.isStatusNull()) {
        stmt.setNull(index++, java.sql.Types.INTEGER);
      } else {
        stmt.setInt(index++, dto.getStatus());
      }

      if (dto.isProjectIdNull()) {
        stmt.setNull(index++, java.sql.Types.INTEGER);
      } else {
        stmt.setInt(index++, dto.getProjectId());
      }

      stmt.setInt(8, pk.getIdprojectPhases());
      int rows = stmt.executeUpdate();
      reset(dto);
      long t2 = System.currentTimeMillis();
      System.out.println(rows + " rows affected (" + (t2 - t1) + " ms)");
    } catch (Exception _e) {
      _e.printStackTrace();
      throw new ProjectPhasesDaoException("Exception: " + _e.getMessage(), _e);
    } finally {
      ResourceManager.close(stmt);
      if (!isConnSupplied) {
        ResourceManager.close(conn);
      }
    }
  }
  /** Returns all rows from the projects table that match the specified arbitrary SQL statement */
  public Projects[] findByDynamicSelect(String sql, Object[] sqlParams)
      throws ProjectsDaoException {
    // declare variables
    final boolean isConnSupplied = (userConn != null);
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try {
      // get the user-specified connection or get a connection from the ResourceManager
      conn = isConnSupplied ? userConn : ResourceManager.getConnection();

      // construct the SQL statement
      final String SQL = sql;

      System.out.println("Executing " + SQL);
      // prepare statement
      stmt = conn.prepareStatement(SQL);
      stmt.setMaxRows(maxRows);

      // bind parameters
      for (int i = 0; sqlParams != null && i < sqlParams.length; i++) {
        stmt.setObject(i + 1, sqlParams[i]);
      }

      rs = stmt.executeQuery();

      // fetch the results
      return fetchMultiResults(rs);
    } catch (Exception _e) {
      _e.printStackTrace();
      throw new ProjectsDaoException("Exception: " + _e.getMessage(), _e);
    } finally {
      ResourceManager.close(rs);
      ResourceManager.close(stmt);
      if (!isConnSupplied) {
        ResourceManager.close(conn);
      }
    }
  }