Coverage Report - liquibase.snapshot.jvm.PostgresDatabaseSnapshotGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
PostgresDatabaseSnapshotGenerator
0%
0/67
0%
0/26
3.125
 
 1  
 package liquibase.snapshot.jvm;
 2  
 
 3  
 import java.sql.Array;
 4  
 import java.sql.DatabaseMetaData;
 5  
 import java.sql.PreparedStatement;
 6  
 import java.sql.ResultSet;
 7  
 import java.sql.SQLException;
 8  
 import java.util.ArrayList;
 9  
 import java.util.HashMap;
 10  
 import java.util.List;
 11  
 import java.util.StringTokenizer;
 12  
 
 13  
 import liquibase.database.Database;
 14  
 import liquibase.database.core.PostgresDatabase;
 15  
 import liquibase.database.jvm.JdbcConnection;
 16  
 import liquibase.database.structure.Table;
 17  
 import liquibase.database.structure.UniqueConstraint;
 18  
 import liquibase.exception.DatabaseException;
 19  
 import liquibase.snapshot.DatabaseSnapshot;
 20  
 import liquibase.util.StringUtils;
 21  
 
 22  0
 public class PostgresDatabaseSnapshotGenerator extends JdbcDatabaseSnapshotGenerator {
 23  
 
 24  
     @Override
 25  
     public boolean supports(Database database) {
 26  0
         return database instanceof PostgresDatabase;
 27  
     }
 28  
 
 29  
     @Override
 30  
     public int getPriority(Database database) {
 31  0
         return PRIORITY_DATABASE;
 32  
     }
 33  
 
 34  
     @Override
 35  
     protected String convertTableNameToDatabaseTableName(String tableName) {
 36  0
         return tableName;
 37  
     }
 38  
 
 39  
     @Override
 40  
     protected String convertColumnNameToDatabaseTableName(String columnName) {
 41  0
         return columnName;
 42  
     }
 43  
 
 44  
     @Override
 45  
     protected String convertPrimaryKeyName(String pkName) throws SQLException {
 46  0
         return pkName;
 47  
     }
 48  
 
 49  
     @Override
 50  
     protected String convertFromDatabaseName(String objectName) {
 51  0
         if (objectName == null) {
 52  0
             return null;
 53  
         }
 54  0
         return objectName.replaceAll("\"", "");
 55  
     }
 56  
 
 57  
     /**
 58  
      *
 59  
      */
 60  
     @Override
 61  
     protected void readUniqueConstraints(DatabaseSnapshot snapshot, String schema, DatabaseMetaData databaseMetaData)
 62  
             throws DatabaseException, SQLException {
 63  0
         Database database = snapshot.getDatabase();
 64  0
         updateListeners("Reading unique constraints for " + database.toString() + " ...");
 65  0
         List<UniqueConstraint> foundUC = new ArrayList<UniqueConstraint>();
 66  0
         PreparedStatement statement = null;
 67  0
         ResultSet rs = null;
 68  
         try {
 69  0
             statement = ((JdbcConnection) database.getConnection())
 70  
                     .getUnderlyingConnection()
 71  
                     .prepareStatement(
 72  
                             "select pgc.conname, pgc.conrelid, pgc.conkey, pgcl.relname from pg_constraint pgc inner join pg_class pgcl on pgcl.oid = pgc.conrelid and pgcl.relkind ='r' where contype = 'u'");
 73  0
             rs = statement.executeQuery();
 74  0
             while (rs.next()) {
 75  0
                 String constraintName = rs.getString("conname");
 76  0
                 long conrelid = rs.getLong("conrelid");
 77  0
                 Array keys = rs.getArray("conkey");
 78  0
                 String tableName = rs.getString("relname");
 79  0
                 UniqueConstraint constraintInformation = new UniqueConstraint();
 80  0
                 constraintInformation.setName(constraintName);
 81  0
                 if (!database.isSystemTable(null, schema, tableName) && !database.isLiquibaseTable(tableName)) {
 82  0
                     Table table = snapshot.getTable(tableName);
 83  0
                     if (table == null) {
 84  
                         // SKip it -- the query above pulls back more then the query for tables & views in the super
 85  
                         // class
 86  0
                         continue;
 87  
                     }
 88  0
                     constraintInformation.setTable(table);
 89  0
                     getColumnsForUniqueConstraint(database, conrelid, keys, constraintInformation);
 90  0
                     foundUC.add(constraintInformation);
 91  
                 }
 92  0
             }
 93  0
             snapshot.getUniqueConstraints().addAll(foundUC);
 94  
         } finally {
 95  0
             try {
 96  0
                 if (rs != null) {
 97  0
                     rs.close();
 98  
                 }
 99  0
             } catch (SQLException ignored) {
 100  0
             }
 101  0
             if (statement != null) {
 102  0
                 statement.close();
 103  
             }
 104  
 
 105  
         }
 106  0
     }
 107  
 
 108  
     protected void getColumnsForUniqueConstraint(Database database, long conrelid, Array keys,
 109  
             UniqueConstraint constraint) throws SQLException {
 110  0
         HashMap<Integer, String> columns_map = new HashMap<Integer, String>();
 111  0
         PreparedStatement stmt = null;
 112  0
         ResultSet rs = null;
 113  
         try {
 114  0
             String str = null;
 115  0
             Object arrays = keys.getArray();
 116  0
             if (arrays instanceof Integer[]) {
 117  0
                 str = StringUtils.join((Integer[]) arrays, ",");
 118  0
             } else if (arrays instanceof int[]) {
 119  0
                 str = StringUtils.join((int[]) arrays, ",");
 120  
             } else {
 121  0
                 throw new SQLException("Can't detect type of array " + arrays);
 122  
             }
 123  0
             stmt = ((JdbcConnection) database.getConnection()).getUnderlyingConnection().prepareStatement(
 124  
                     "select attname,attnum from pg_attribute where attrelid = ? and attnum in (" + str + ")");
 125  0
             stmt.setLong(1, conrelid);
 126  0
             rs = stmt.executeQuery();
 127  0
             while (rs.next()) {
 128  0
                 columns_map.put(rs.getInt("attnum"), rs.getString("attname"));
 129  
             }
 130  0
             StringTokenizer str_token = new StringTokenizer(keys.toString().replace("{", "").replace("}", ""), ",");
 131  0
             while (str_token.hasMoreTokens()) {
 132  0
                 Integer column_id = new Integer(str_token.nextToken());
 133  0
                 constraint.getColumns().add(columns_map.get(column_id));
 134  0
             }
 135  
         } finally {
 136  0
             if (rs != null) {
 137  
                 try {
 138  0
                     rs.close();
 139  0
                 } catch (SQLException ignored) {
 140  0
                 }
 141  
             }
 142  0
             if (stmt != null)
 143  0
                 stmt.close();
 144  
         }
 145  0
     }
 146  
 }