001 package org.apache.ojb.broker.ant;
002
003 /* Copyright 2004-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import java.io.IOException;
019 import java.io.Writer;
020 import java.sql.SQLException;
021 import java.util.ArrayList;
022 import java.util.Iterator;
023
024 import org.apache.commons.beanutils.DynaBean;
025 import org.apache.ddlutils.Platform;
026 import org.apache.ddlutils.model.Database;
027
028 /**
029 * Encapsulates the data objects read by Digester.
030 *
031 * @author Thomas Dudziak
032 */
033 public class DataSet
034 {
035 /** The data objects (dyna beans) */
036 private ArrayList _beans = new ArrayList();
037
038 /**
039 * Adds a data object.
040 *
041 * @param bean The data object
042 */
043 public void add(DynaBean bean)
044 {
045 _beans.add(bean);
046 }
047
048 /**
049 * Generates and writes the sql for inserting the currently contained data objects.
050 *
051 * @param model The database model
052 * @param platform The platform
053 * @param writer The output stream
054 */
055 public void createInsertionSql(Database model, Platform platform, Writer writer) throws IOException
056 {
057 for (Iterator it = _beans.iterator(); it.hasNext();)
058 {
059 writer.write(platform.getInsertSql(model, (DynaBean)it.next()));
060 if (it.hasNext())
061 {
062 writer.write("\n");
063 }
064 }
065 }
066
067 /**
068 * Inserts the currently contained data objects into the database.
069 *
070 * @param platform The (connected) database platform for inserting data
071 * @param model The database model
072 * @param batchSize The batch size; use 1 for not using batch mode
073 */
074 public void insert(Platform platform, Database model, int batchSize) throws SQLException
075 {
076 if (batchSize <= 1)
077 {
078 for (Iterator it = _beans.iterator(); it.hasNext();)
079 {
080 platform.insert(model, (DynaBean)it.next());
081 }
082 }
083 else
084 {
085 for (int startIdx = 0; startIdx < _beans.size(); startIdx += batchSize)
086 {
087 platform.insert(model, _beans.subList(startIdx, startIdx + batchSize));
088 }
089 }
090 }
091 }