/** * Adds a feature to the PrimaryKey attribute of the DatabaseSchemaUtils object * * @param db The feature to be added to the PrimaryKey attribute * @param primaryKey The feature to be added to the PrimaryKey attribute * @param dbTable The feature to be added to the PrimaryKey attribute * @exception SQLException Description of the Exception */ public static void addPrimaryKey(Connection db, DatabaseTable dbTable, String primaryKey) throws SQLException { boolean commit = db.getAutoCommit(); try { if (commit) { db.setAutoCommit(false); } // Create a clone 'y' of the table 'x' specified DatabaseTable clone = new DatabaseTable(dbTable); clone.setTableName(dbTable.getTableName() + "_clone"); clone.setSequenceName(""); clone.create(db); System.out.println("Inserting into clone"); // Move data from 'x' into its clone 'y' dbTable.selectInto(db, clone); // Delete data from 'x' and drop table 'x'. No need to drop the sequence since the table has // no primary key System.out.println("Dropping dbTable"); dbTable.drop(db, false); System.out.println("Creating dbTable"); // Create a new 'x' with a primary key column DatabaseColumn column = new DatabaseColumn(primaryKey, java.sql.Types.INTEGER); column.setIsPrimaryKey(true); dbTable.addColumn(0, column); dbTable.create(db); System.out.println("Inserting back into dbTable"); // Move data from 'y' into the new 'x' clone.selectInto(db, dbTable); // Delete data from 'y' and drop clone 'y'. No need to drop the sequence since the clone has // no primary key System.out.println("Dropping clone"); clone.drop(db, false); if (commit) { db.commit(); } } catch (SQLException e) { e.printStackTrace(System.out); if (commit) { db.rollback(); } throw new SQLException(e.getMessage()); } finally { if (commit) { db.setAutoCommit(true); } } }