KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > operation > AbstractOperation


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.operation;
22
23 import org.dbunit.DatabaseUnitException;
24 import org.dbunit.database.DatabaseConfig;
25 import org.dbunit.database.IDatabaseConnection;
26 import org.dbunit.dataset.*;
27
28 import java.sql.SQLException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  * @author Manuel Laflamme
34  * @since Jan 17, 2004
35  * @version $Revision: 1.4 $
36  */

37 public abstract class AbstractOperation extends DatabaseOperation
38 {
39     protected String JavaDoc getQualifiedName(String JavaDoc prefix, String JavaDoc name, IDatabaseConnection connection)
40     {
41         String JavaDoc escapePattern = (String JavaDoc)connection.getConfig().getProperty(
42                 DatabaseConfig.PROPERTY_ESCAPE_PATTERN);
43
44         return DataSetUtils.getQualifiedName(prefix, name, escapePattern);
45     }
46
47     /**
48      * Returns the metadata to use in this operation.
49      *
50      * @param connection the database connection
51      * @param metaData the xml table metadata
52      */

53     static ITableMetaData getOperationMetaData(IDatabaseConnection connection,
54             ITableMetaData metaData) throws DatabaseUnitException, SQLException JavaDoc
55     {
56         IDataSet databaseDataSet = connection.createDataSet();
57         String JavaDoc tableName = metaData.getTableName();
58
59         ITableMetaData databaseMetaData = databaseDataSet.getTableMetaData(tableName);
60         Column[] databaseColumns = databaseMetaData.getColumns();
61         Column[] columns = metaData.getColumns();
62
63         List JavaDoc columnList = new ArrayList JavaDoc();
64         for (int j = 0; j < columns.length; j++)
65         {
66             String JavaDoc columnName = columns[j].getColumnName();
67             Column column = DataSetUtils.getColumn(
68                     columnName, databaseColumns);
69             if (column == null)
70             {
71                 throw new NoSuchColumnException(tableName + "." + columnName);
72             }
73             columnList.add(column);
74         }
75
76         return new DefaultTableMetaData(databaseMetaData.getTableName(),
77                 (Column[])columnList.toArray(new Column[0]),
78                 databaseMetaData.getPrimaryKeys());
79     }
80 }
81
Popular Tags