Search in sources :

Example 1 with JdbcConnection

use of com.fathomdb.jdbc.JdbcConnection in project platformlayer by platformlayer.

the class JdbcServiceAuthorizationRepository method createAuthorization.

@Override
@JdbcTransaction
public ServiceAuthorization createAuthorization(ProjectId project, ServiceAuthorization authorization) throws RepositoryException {
    try {
        ServiceType serviceType = new ServiceType(authorization.serviceType);
        JdbcConnection connection = connectionProvider.get();
        int serviceId = JdbcRepositoryHelpers.getServiceKey(connection, serviceType);
        int projectId = JdbcRepositoryHelpers.getProjectKey(connection, project);
        final String sql = "INSERT INTO service_authorizations (service, project, data) VALUES (?, ?, ?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = null;
        try {
            ps.setInt(1, serviceId);
            ps.setInt(2, projectId);
            ps.setString(3, authorization.data);
            int updateCount = ps.executeUpdate();
            if (updateCount != 1) {
                throw new IllegalStateException("Unexpected number of rows inserted");
            }
        } finally {
            JdbcUtils.safeClose(rs);
            JdbcUtils.safeClose(ps);
        }
        return authorization;
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    }
}
Also used : SQLException(java.sql.SQLException) ServiceType(org.platformlayer.ids.ServiceType) ResultSet(java.sql.ResultSet) JdbcConnection(com.fathomdb.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) RepositoryException(org.platformlayer.RepositoryException) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 2 with JdbcConnection

use of com.fathomdb.jdbc.JdbcConnection in project platformlayer by platformlayer.

the class JdbcServiceAuthorizationRepository method findServiceAuthorization.

@Override
@JdbcTransaction
public ServiceAuthorization findServiceAuthorization(ServiceType serviceType, ProjectId project) throws RepositoryException {
    try {
        JdbcConnection connection = connectionProvider.get();
        int serviceId = JdbcRepositoryHelpers.getServiceKey(connection, serviceType);
        int projectId = JdbcRepositoryHelpers.getProjectKey(connection, project);
        String sql = "SELECT data FROM service_authorizations WHERE service=? and project=?";
        List<ServiceAuthorization> items = Lists.newArrayList();
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = null;
        try {
            ps.setInt(1, serviceId);
            ps.setInt(2, projectId);
            rs = ps.executeQuery();
            while (rs.next()) {
                items.add(mapRow(serviceType, rs));
            }
        } finally {
            JdbcUtils.safeClose(rs);
            JdbcUtils.safeClose(ps);
        }
        if (items.size() == 0) {
            return null;
        }
        if (items.size() != 1) {
            throw new IllegalStateException("Found duplicate results for primary key: " + serviceType + ":" + project);
        }
        return items.get(0);
    } catch (SQLException e) {
        throw new RepositoryException("Error running query", e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) JdbcConnection(com.fathomdb.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) RepositoryException(org.platformlayer.RepositoryException) ServiceAuthorization(org.platformlayer.xaas.model.ServiceAuthorization) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Aggregations

JdbcConnection (com.fathomdb.jdbc.JdbcConnection)2 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 RepositoryException (org.platformlayer.RepositoryException)2 ServiceType (org.platformlayer.ids.ServiceType)1 ServiceAuthorization (org.platformlayer.xaas.model.ServiceAuthorization)1