InsertIdentityOperation.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2004, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */
  21. package org.dbunit.ext.mssql;

  22. import java.sql.Connection;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;

  25. import org.dbunit.DatabaseUnitException;
  26. import org.dbunit.database.DatabaseConfig;
  27. import org.dbunit.database.IDatabaseConnection;
  28. import org.dbunit.dataset.Column;
  29. import org.dbunit.dataset.DataSetException;
  30. import org.dbunit.dataset.DefaultDataSet;
  31. import org.dbunit.dataset.IDataSet;
  32. import org.dbunit.dataset.ITable;
  33. import org.dbunit.dataset.ITableIterator;
  34. import org.dbunit.dataset.ITableMetaData;
  35. import org.dbunit.dataset.Column.AutoIncrement;
  36. import org.dbunit.dataset.filter.IColumnFilter;
  37. import org.dbunit.operation.AbstractOperation;
  38. import org.dbunit.operation.CompositeOperation;
  39. import org.dbunit.operation.DatabaseOperation;
  40. import org.slf4j.Logger;
  41. import org.slf4j.LoggerFactory;

  42. /**
  43.  * This class disable the MS SQL Server automatic identifier generation for
  44.  * the execution of inserts.
  45.  * <p>
  46.  * If you are using the Microsoft driver (i.e.
  47.  * <code>com.microsoft.jdbc.sqlserver.SQLServerDriver</code>), you'll need to
  48.  * use the <code>SelectMethod=cursor</code> parameter in the JDBC connection
  49.  * string. Your databaseUrl would look something like the following:
  50.  * <p>
  51.  * <code>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb;SelectMethod=cursor</code>
  52.  * <p>
  53.  * Thanks to Jeremy Stein who has submitted multiple patches.
  54.  *
  55.  * @author Manuel Laflamme
  56.  * @author Eric Pugh
  57.  * @author Last changed by: $Author$
  58.  * @version $Revision$ $Date$
  59.  * @since 1.4 (Apr 9, 2002)
  60.  */
  61. public class InsertIdentityOperation extends AbstractOperation
  62. {

  63.     /**
  64.      * Logger for this class
  65.      */
  66.     private static final Logger logger = LoggerFactory.getLogger(InsertIdentityOperation.class);

  67.     public static final DatabaseOperation INSERT =
  68.             new InsertIdentityOperation(DatabaseOperation.INSERT);

  69.     public static final DatabaseOperation CLEAN_INSERT =
  70.             new CompositeOperation(DatabaseOperation.DELETE_ALL,
  71.                     new InsertIdentityOperation(DatabaseOperation.INSERT));

  72.     public static final DatabaseOperation REFRESH =
  73.             new InsertIdentityOperation(DatabaseOperation.REFRESH);

  74.     private static final IColumnFilter DEFAULT_IDENTITY_FILTER = new IColumnFilter()
  75.     {
  76.         public boolean accept(String tableName, Column column)
  77.         {
  78.             return column.getSqlTypeName().endsWith("identity");
  79.         }
  80.     };

  81.    
  82.     /**
  83.      * Accepts columns that have one of the remarks
  84.      * <ul><li>GENERATED BY DEFAULT AS IDENTITY</li>
  85.      * <li>GENERATED ALWAYS AS IDENTITY</li></ul>
  86.      * set which is the SQL standard syntax to describe auto-generated key columns.
  87.      * Also accepts columns that have the auto-increment property set to <code>true</code> (note that
  88.      * it does not yet have the ability to check whether the column is a primary key col).
  89.      * @since 2.4.3
  90.      */
  91.     public static final IColumnFilter IDENTITY_FILTER_EXTENDED = new IColumnFilter() {
  92.    
  93.         public boolean accept(String tableName, Column column)
  94.         {
  95.             String remarks = column.getRemarks();
  96.             boolean isIdentityCol = (remarks != null) && (
  97.                     remarks.indexOf("GENERATED BY DEFAULT AS IDENTITY") > -1 ||
  98.                     remarks.indexOf("GENERATED ALWAYS AS IDENTITY") > -1
  99.                     );
  100.            
  101.             // If "remarks" did not give the appropriate hint, check the auto-increment property
  102.             if(!isIdentityCol)
  103.             {
  104.                 //TODO Should we ensure that the column is a PrimaryKey column?
  105.                 isIdentityCol = (AutoIncrement.YES == column.getAutoIncrement());
  106.             }
  107.            
  108.             return isIdentityCol;
  109.         }
  110.     };

  111.    
  112.     private final DatabaseOperation _operation;

  113.     /**
  114.      * Creates a new InsertIdentityOperation object that decorates the
  115.      * specified operation.
  116.      */
  117.     public InsertIdentityOperation(DatabaseOperation operation)
  118.     {
  119.         _operation = operation;
  120.     }

  121.     boolean hasIdentityColumn(ITableMetaData metaData, IDatabaseConnection connection)
  122.             throws DataSetException
  123.     {
  124.         logger.debug("hasIdentityColumn(metaData={}, connection={}) - start", metaData, connection);

  125.         DatabaseConfig config = connection.getConfig();
  126.         IColumnFilter identityFilter = (IColumnFilter)config.getProperty(
  127.                 DatabaseConfig.PROPERTY_IDENTITY_COLUMN_FILTER);
  128.         if (identityFilter == null)
  129.         {
  130.             identityFilter = DEFAULT_IDENTITY_FILTER;
  131.         }

  132.         // Verify if there is at least one identity column
  133.         Column[] columns = metaData.getColumns();
  134.         for (int i = 0; i < columns.length; i++)
  135.         {
  136.             if (identityFilter.accept(metaData.getTableName(), columns[i]))
  137.             {
  138.                 return true;
  139.             }
  140.         }

  141.         return false;
  142.     }

  143.     ////////////////////////////////////////////////////////////////////////////
  144.     // DatabaseOperation class

  145.     public void execute(IDatabaseConnection connection, IDataSet dataSet)
  146.             throws DatabaseUnitException, SQLException
  147.     {
  148.         logger.debug("execute(connection={}, dataSet={}) - start", connection, dataSet);

  149.         Connection jdbcConnection = connection.getConnection();
  150.         Statement statement = jdbcConnection.createStatement();

  151.         boolean wasAutoCommit = false;
  152.         try
  153.         {
  154.             IDataSet databaseDataSet = connection.createDataSet();
  155.            
  156.             // Note that MSSQL has a different transaction strategy from oracle.
  157.             // By default the transaction is always in "autocommit=true" so
  158.             // that every statement is immediately committed. If a dbunit
  159.             // user does not want this behavior dbunit takes it into account
  160.             // here.
  161.            
  162.             // INSERT_IDENTITY need to be enabled/disabled inside the
  163.             // same transaction
  164.             if (jdbcConnection.getAutoCommit() == true)
  165.             {
  166.                 wasAutoCommit = true;
  167.                 jdbcConnection.setAutoCommit(false);
  168.             }

  169.             // Execute decorated operation one table at a time
  170.             ITableIterator iterator = dataSet.iterator();
  171.             while(iterator.next())
  172.             {
  173.                 ITable table = iterator.getTable();
  174.                 String tableName = table.getTableMetaData().getTableName();

  175.                 ITableMetaData metaData =
  176.                         databaseDataSet.getTableMetaData(tableName);

  177.                 // enable identity insert
  178.                 boolean hasIdentityColumn = hasIdentityColumn(metaData, connection);

  179.                 if (hasIdentityColumn)
  180.                 {
  181.                     final StringBuilder sqlBuffer = new StringBuilder(128);
  182.                     sqlBuffer.append("SET IDENTITY_INSERT ");
  183.                     sqlBuffer.append(getQualifiedName(connection.getSchema(),
  184.                             metaData.getTableName(), connection));
  185.                     sqlBuffer.append(" ON");
  186.                     statement.execute(sqlBuffer.toString());
  187.                 }

  188.                 try
  189.                 {
  190.                     _operation.execute(connection, new DefaultDataSet(table));
  191.                 }
  192.                 finally
  193.                 {
  194.                     // disable identity insert
  195.                     if (hasIdentityColumn)
  196.                     {
  197.                         final StringBuilder sqlBuffer = new StringBuilder(128);
  198.                         sqlBuffer.append("SET IDENTITY_INSERT ");
  199.                         sqlBuffer.append(getQualifiedName(connection.getSchema(),
  200.                                 metaData.getTableName(), connection));
  201.                         sqlBuffer.append(" OFF");
  202.                         statement.execute(sqlBuffer.toString());
  203.                     }
  204.                     if (wasAutoCommit)
  205.                     {
  206.                         jdbcConnection.commit();
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.         finally
  212.         {
  213.             if(wasAutoCommit)
  214.             {
  215.                 // Reset the autocommit property
  216.                 jdbcConnection.setAutoCommit(true);
  217.             }
  218.             statement.close();
  219.         }
  220.     }
  221. }