1 21 22 package org.apache.derby.impl.tools.dblook; 23 24 import java.sql.Connection ; 25 import java.sql.Statement ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.sql.ResultSetMetaData ; 30 31 import java.util.HashMap ; 32 import java.util.Set ; 33 import java.util.Iterator ; 34 35 import org.apache.derby.tools.dblook; 36 37 public class DB_Table { 38 39 private static PreparedStatement getColumnInfoStmt; 42 private static PreparedStatement getColumnTypeStmt; 43 private static PreparedStatement getAutoIncStmt; 44 45 54 55 public static void doTables(Connection conn, HashMap tableIdToNameMap) 56 throws SQLException 57 { 58 59 61 getColumnInfoStmt = 62 conn.prepareStatement("SELECT C.COLUMNNAME, C.REFERENCEID, " + 63 "C.COLUMNNUMBER FROM SYS.SYSCOLUMNS C, SYS.SYSTABLES T WHERE T.TABLEID = ? " + 64 "AND T.TABLEID = C.REFERENCEID ORDER BY C.COLUMNNUMBER"); 65 66 getColumnTypeStmt = 67 conn.prepareStatement("SELECT COLUMNDATATYPE, COLUMNDEFAULT FROM SYS.SYSCOLUMNS " + 68 "WHERE REFERENCEID = ? AND COLUMNNAME = ?"); 69 70 getAutoIncStmt = 71 conn.prepareStatement("SELECT AUTOINCREMENTSTART, " + 72 "AUTOINCREMENTINC, COLUMNNAME, REFERENCEID, COLUMNDEFAULT FROM SYS.SYSCOLUMNS " + 73 "WHERE COLUMNNAME = ? AND REFERENCEID = ?"); 74 75 78 boolean firstTime = true; 79 Set tableIds = tableIdToNameMap.keySet(); 80 for (Iterator itr = tableIds.iterator(); itr.hasNext(); ) { 81 82 String tableId = (String )itr.next(); 83 String tableName = (String )(tableIdToNameMap.get(tableId)); 84 if (dblook.isExcludedTable(tableName)) 85 continue; 87 88 if (firstTime) { 89 Logs.reportString("----------------------------------------------"); 90 Logs.reportMessage("DBLOOK_TablesHeader"); 91 Logs.reportString("----------------------------------------------\n"); 92 } 93 94 Logs.writeToNewDDL("CREATE TABLE " + tableName + " ("); 95 96 boolean firstCol = true; 98 getColumnInfoStmt.setString(1, tableId); 99 ResultSet columnRS = getColumnInfoStmt.executeQuery(); 100 while (columnRS.next()) { 101 String colName = dblook.addQuotes(columnRS.getString(1)); 102 String createColString = createColumn(colName, columnRS.getString(2), 103 columnRS.getInt(3)); 104 if (!firstCol) 105 createColString = ", " + createColString; 106 107 Logs.writeToNewDDL(createColString); 108 firstCol = false; 109 } 110 111 columnRS.close(); 112 Logs.writeToNewDDL(")"); 113 Logs.writeStmtEndToNewDDL(); 114 Logs.writeNewlineToNewDDL(); 115 firstTime = false; 116 117 } 119 getColumnInfoStmt.close(); 120 getColumnTypeStmt.close(); 121 getAutoIncStmt.close(); 122 123 } 124 125 134 135 private static String createColumn(String colName, String tableId, 136 int colNum) throws SQLException 137 { 138 139 getColumnTypeStmt.setString(1, tableId); 140 getColumnTypeStmt.setString(2, dblook.stripQuotes(colName)); 141 142 ResultSet rs = getColumnTypeStmt.executeQuery(); 143 StringBuffer colDef = new StringBuffer (); 144 if (rs.next()) { 145 146 colDef.append(dblook.addQuotes(dblook.expandDoubleQuotes( 147 dblook.stripQuotes(colName)))); 148 colDef.append(" "); 149 colDef.append(rs.getString(1)); 150 if (!reinstateAutoIncrement(colName, tableId, colDef) && 151 rs.getString(2) != null) { 152 colDef.append(" DEFAULT "); 153 colDef.append(rs.getString(2)); 154 } 155 } 156 157 rs.close(); 158 return colDef.toString(); 159 160 } 161 162 170 171 public static boolean reinstateAutoIncrement(String colName, 172 String tableId, StringBuffer colDef) throws SQLException 173 { 174 175 getAutoIncStmt.setString(1, dblook.stripQuotes(colName)); 176 getAutoIncStmt.setString(2, tableId); 177 ResultSet autoIncCols = getAutoIncStmt.executeQuery(); 178 if (autoIncCols.next()) { 179 180 long start = autoIncCols.getLong(1); 181 if (!autoIncCols.wasNull()) { 182 colDef.append(" GENERATED "); 183 colDef.append(autoIncCols.getObject(5) == null ? 184 "ALWAYS ":"BY DEFAULT "); 185 colDef.append("AS IDENTITY (START WITH "); 186 colDef.append(autoIncCols.getLong(1)); 187 colDef.append(", INCREMENT BY "); 188 colDef.append(autoIncCols.getLong(2)); 189 colDef.append(")"); 190 return true; 191 } 192 } 193 194 return false; 195 196 } 197 198 } 199 200 | Popular Tags |