public T addForeignKey ( final Connection _con , final String _tableName , final String _foreignKeyName , final String _key , final String _reference , final boolean _cascade ) throws InstallationException { final StringBuilder cmd = new StringBuilder ( ) . append ( "alter table " ) . append ( _tableName ) . append ( " " ) . append ( "add constraint " ) . append ( _foreignKeyName ) . append ( " " ) . append ( "foreign key(" ) . append ( _key ) . append ( ") " ) . append ( "references " ) . append ( _reference ) ; if ( _cascade ) { cmd . append ( " on delete cascade" ) ; } AbstractDatabase . LOG . debug ( " ..SQL> " + cmd . toString ( ) ) ; try { final Statement stmt = _con . createStatement ( ) ; try { stmt . execute ( cmd . toString ( ) ) ; } finally { stmt . close ( ) ; } } catch ( final SQLException e ) { throw new InstallationException ( "Foreign key could not be created. SQL statement was:\n" + cmd . toString ( ) , e ) ; } @ SuppressWarnings ( "unchecked" ) final T ret = ( T ) this ; return ret ; }
Adds a foreign key to given SQL table .