KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > ant > DataSet


1 package org.apache.ojb.broker.ant;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.IOException JavaDoc;
19 import java.io.Writer JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.commons.beanutils.DynaBean;
25 import org.apache.ddlutils.Platform;
26 import org.apache.ddlutils.model.Database;
27
28 /**
29  * Encapsulates the data objects read by Digester.
30  *
31  * @author Thomas Dudziak
32  */

33 public class DataSet
34 {
35     /** The data objects (dyna beans) */
36     private ArrayList JavaDoc _beans = new ArrayList JavaDoc();
37
38     /**
39      * Adds a data object.
40      *
41      * @param bean The data object
42      */

43     public void add(DynaBean bean)
44     {
45         _beans.add(bean);
46     }
47
48     /**
49      * Generates and writes the sql for inserting the currently contained data objects.
50      *
51      * @param model The database model
52      * @param platform The platform
53      * @param writer The output stream
54      */

55     public void createInsertionSql(Database model, Platform platform, Writer JavaDoc writer) throws IOException JavaDoc
56     {
57         for (Iterator JavaDoc it = _beans.iterator(); it.hasNext();)
58         {
59             writer.write(platform.getInsertSql(model, (DynaBean)it.next()));
60             if (it.hasNext())
61             {
62                 writer.write("\n");
63             }
64         }
65     }
66
67     /**
68      * Inserts the currently contained data objects into the database.
69      *
70      * @param platform The (connected) database platform for inserting data
71      * @param model The database model
72      * @param batchSize The batch size; use 1 for not using batch mode
73      */

74     public void insert(Platform platform, Database model, int batchSize) throws SQLException JavaDoc
75     {
76         if (batchSize <= 1)
77         {
78             for (Iterator JavaDoc it = _beans.iterator(); it.hasNext();)
79             {
80                 platform.insert(model, (DynaBean)it.next());
81             }
82         }
83         else
84         {
85             for (int startIdx = 0; startIdx < _beans.size(); startIdx += batchSize)
86             {
87                 platform.insert(model, _beans.subList(startIdx, startIdx + batchSize));
88             }
89         }
90     }
91 }
92
Popular Tags