View Javadoc

1   package liquibase.snapshot.jvm;
2   
3   import liquibase.database.Database;
4   import liquibase.database.core.InformixDatabase;
5   import liquibase.database.structure.Column;
6   import liquibase.database.structure.ForeignKey;
7   import liquibase.database.structure.ForeignKeyInfo;
8   import liquibase.exception.DatabaseException;
9   
10  import java.sql.ResultSet;
11  import java.sql.SQLException;
12  import java.util.ArrayList;
13  import java.util.HashMap;
14  import java.util.List;
15  import java.util.Map;
16  
17  public class InformixDatabaseSnapshotGenerator extends JdbcDatabaseSnapshotGenerator {
18  
19      private static final Map<Integer, String> qualifiers = new HashMap<Integer, String>();
20  
21      static {
22          qualifiers.put(0, "YEAR");
23          qualifiers.put(2, "MONTH");
24          qualifiers.put(4, "DAY");
25          qualifiers.put(6, "HOUR");
26          qualifiers.put(8, "MINUTE");
27          qualifiers.put(10, "SECOND");
28          qualifiers.put(11, "FRACTION(1)");
29          qualifiers.put(12, "FRACTION(2)");
30          qualifiers.put(13, "FRACTION(3)");
31          qualifiers.put(14, "FRACTION(4)");
32          qualifiers.put(15, "FRACTION(5)");
33      }
34  
35      public boolean supports(Database database) {
36          return database instanceof InformixDatabase;
37      }
38  
39      public int getPriority(Database database) {
40          return PRIORITY_DATABASE;
41      }
42  
43      @Override
44      protected void getColumnTypeAndDefValue(Column columnInfo, ResultSet rs, Database database) throws SQLException,
45              DatabaseException {
46          // See http://publib.boulder.ibm.com/infocenter/idshelp/v115/topic/com.ibm.sqlr.doc/sqlr07.htm
47          String typeName = rs.getString("TYPE_NAME").toUpperCase();
48          if ("DATETIME".equals(typeName) || "INTERVAL".equals(typeName)) {
49              int collength = columnInfo.getColumnSize();
50              // int positions = collength / 256;
51              int firstQualifierType = (collength % 256) / 16;
52              int lastQualifierType = (collength % 256) % 16;
53              String type = "DATETIME".equals(typeName) ? "DATETIME" : "INTERVAL";
54              String firstQualifier = qualifiers.get(firstQualifierType);
55              String lastQualifier = qualifiers.get(lastQualifierType);
56              columnInfo.setTypeName(type + " " + firstQualifier + " TO " + lastQualifier);
57              columnInfo.setLengthSemantics(Column.LengthSemantics.BYTE);
58          } else {
59              super.getColumnTypeAndDefValue(columnInfo, rs, database);
60          }
61      }
62  
63      @Override
64      public List<ForeignKey> getForeignKeys(String schemaName, String foreignKeyTableName, Database database)
65              throws DatabaseException {
66          List<ForeignKey> fkList = new ArrayList<ForeignKey>();
67          try {
68              String dbCatalog = database.convertRequestedSchemaToCatalog(schemaName);
69              // Informix handles schema differently
70              String dbSchema = null;
71              ResultSet rs = getMetaData(database).getImportedKeys(dbCatalog, dbSchema,
72                      convertTableNameToDatabaseTableName(foreignKeyTableName));
73  
74              try {
75                  while (rs.next()) {
76                      ForeignKeyInfo fkInfo = fillForeignKeyInfo(rs);
77  
78                      fkList.add(generateForeignKey(fkInfo, database, fkList));
79                  }
80              } finally {
81                  rs.close();
82              }
83  
84              return fkList;
85  
86          } catch (Exception e) {
87              throw new DatabaseException(e);
88          }
89      }
90  }