Search in sources :

Example 11 with DaoKeyHolder

use of bio.terra.common.DaoKeyHolder in project jade-data-repo by DataBiosphere.

the class SnapshotTableDao method createColumns.

private void createColumns(UUID tableId, Collection<Column> columns) {
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("table_id", tableId);
    DaoKeyHolder keyHolder = new DaoKeyHolder();
    for (Column column : columns) {
        params.addValue("name", column.getName());
        params.addValue("type", column.getType());
        params.addValue("array_of", column.isArrayOf());
        jdbcTemplate.update(sqlInsertColumn, params, keyHolder);
        UUID columnId = keyHolder.getId();
        column.id(columnId);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Column(bio.terra.common.Column) DaoKeyHolder(bio.terra.common.DaoKeyHolder) UUID(java.util.UUID)

Example 12 with DaoKeyHolder

use of bio.terra.common.DaoKeyHolder in project jade-data-repo by DataBiosphere.

the class GoogleResourceDao method createProject.

public UUID createProject(GoogleProjectResource project) {
    try {
        String sql = "INSERT INTO project_resource " + "(google_project_id, google_project_number, profile_id, service_ids) VALUES " + "(:google_project_id, :google_project_number, :profile_id, :service_ids)";
        MapSqlParameterSource params = new MapSqlParameterSource().addValue("google_project_id", project.getGoogleProjectId()).addValue("google_project_number", project.getGoogleProjectNumber()).addValue("profile_id", project.getProfileId()).addValue("service_ids", DaoUtils.createSqlStringArray(connection, project.getServiceIds()));
        DaoKeyHolder keyHolder = new DaoKeyHolder();
        jdbcTemplate.update(sql, params, keyHolder);
        return keyHolder.getId();
    } catch (SQLException e) {
        throw new GoogleResourceException("Can't save project resource: " + project.getGoogleProjectId(), e);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) SQLException(java.sql.SQLException) DaoKeyHolder(bio.terra.common.DaoKeyHolder) GoogleResourceException(bio.terra.service.resourcemanagement.exception.GoogleResourceException)

Example 13 with DaoKeyHolder

use of bio.terra.common.DaoKeyHolder in project jade-data-repo by DataBiosphere.

the class GoogleResourceDao method createAndLockBucket.

/**
 * Insert a new row into the bucket_resource metadata table and give the provided flight the lock by setting the
 * flightid column. If there already exists a row with this bucket name, return null instead of throwing an
 * exception.
 * @param bucketRequest
 * @param flightId
 * @return a reference to the bucket as a POJO GoogleBucketResource if the insert succeeded, null otherwise
 */
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
public GoogleBucketResource createAndLockBucket(GoogleBucketRequest bucketRequest, String flightId) {
    GoogleProjectResource projectResource = Optional.ofNullable(bucketRequest.getGoogleProjectResource()).orElseThrow(IllegalArgumentException::new);
    // Put an end to serialization errors here. We only come through here if we really need to create
    // the bucket, so this is not on the path of most bucket lookups.
    jdbcTemplate.getJdbcTemplate().execute("LOCK TABLE bucket_resource IN EXCLUSIVE MODE");
    String sql = "INSERT INTO bucket_resource (project_resource_id, name, flightid) VALUES " + "(:project_resource_id, :name, :flightid) " + "ON CONFLICT ON CONSTRAINT bucket_resource_name_key DO NOTHING";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("project_resource_id", projectResource.getRepositoryId()).addValue("name", bucketRequest.getBucketName()).addValue("flightid", flightId);
    DaoKeyHolder keyHolder = new DaoKeyHolder();
    // Lock the table to avoid serialization failures during bulk load
    jdbcTemplate.getJdbcTemplate().execute("LOCK TABLE bucket_resource IN EXCLUSIVE MODE");
    int numRowsUpdated = jdbcTemplate.update(sql, params, keyHolder);
    if (numRowsUpdated == 1) {
        return (new GoogleBucketResource(bucketRequest)).projectResource(projectResource).name(bucketRequest.getBucketName()).resourceId(keyHolder.getId()).flightId(flightId);
    } else {
        return null;
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DaoKeyHolder(bio.terra.common.DaoKeyHolder) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with DaoKeyHolder

use of bio.terra.common.DaoKeyHolder in project jade-data-repo by DataBiosphere.

the class ProfileDao method createBillingProfile.

public UUID createBillingProfile(BillingProfile billingProfile) {
    String sql = "INSERT INTO billing_profile (name, biller, billing_account_id) VALUES " + " (:name, :biller, :billing_account_id)";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("name", billingProfile.getName()).addValue("biller", billingProfile.getBiller()).addValue("billing_account_id", billingProfile.getBillingAccountId());
    DaoKeyHolder keyHolder = new DaoKeyHolder();
    jdbcTemplate.update(sql, params, keyHolder);
    return keyHolder.getId();
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DaoKeyHolder(bio.terra.common.DaoKeyHolder)

Example 15 with DaoKeyHolder

use of bio.terra.common.DaoKeyHolder in project jade-data-repo by DataBiosphere.

the class DatasetTableDao method createColumns.

private void createColumns(UUID tableId, Collection<Column> columns) {
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("table_id", tableId);
    DaoKeyHolder keyHolder = new DaoKeyHolder();
    for (Column column : columns) {
        params.addValue("name", column.getName());
        params.addValue("type", column.getType());
        params.addValue("array_of", column.isArrayOf());
        jdbcTemplate.update(sqlInsertColumn, params, keyHolder);
        UUID columnId = keyHolder.getId();
        column.id(columnId);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Column(bio.terra.common.Column) DaoKeyHolder(bio.terra.common.DaoKeyHolder) UUID(java.util.UUID)

Aggregations

DaoKeyHolder (bio.terra.common.DaoKeyHolder)20 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)20 UUID (java.util.UUID)14 Transactional (org.springframework.transaction.annotation.Transactional)5 Column (bio.terra.common.Column)2 SQLException (java.sql.SQLException)2 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)2 InvalidAssetException (bio.terra.service.dataset.exception.InvalidAssetException)1 InvalidDatasetException (bio.terra.service.dataset.exception.InvalidDatasetException)1 LoadLockedException (bio.terra.service.load.exception.LoadLockedException)1 GoogleResourceException (bio.terra.service.resourcemanagement.exception.GoogleResourceException)1 CorruptMetadataException (bio.terra.service.snapshot.exception.CorruptMetadataException)1 InvalidSnapshotException (bio.terra.service.snapshot.exception.InvalidSnapshotException)1 Array (java.sql.Array)1 Connection (java.sql.Connection)1