01: package org.apache.ojb.broker.ant;
02:
03: /* Copyright 2004-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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;
19: import java.io.Writer;
20: import java.sql.SQLException;
21: import java.util.ArrayList;
22: import java.util.Iterator;
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: /** The data objects (dyna beans) */
35: private ArrayList _beans = new ArrayList();
36:
37: /**
38: * Adds a data object.
39: *
40: * @param bean The data object
41: */
42: public void add(DynaBean bean) {
43: _beans.add(bean);
44: }
45:
46: /**
47: * Generates and writes the sql for inserting the currently contained data objects.
48: *
49: * @param model The database model
50: * @param platform The platform
51: * @param writer The output stream
52: */
53: public void createInsertionSql(Database model, Platform platform,
54: Writer writer) throws IOException {
55: for (Iterator it = _beans.iterator(); it.hasNext();) {
56: writer.write(platform.getInsertSql(model, (DynaBean) it
57: .next()));
58: if (it.hasNext()) {
59: writer.write("\n");
60: }
61: }
62: }
63:
64: /**
65: * Inserts the currently contained data objects into the database.
66: *
67: * @param platform The (connected) database platform for inserting data
68: * @param model The database model
69: * @param batchSize The batch size; use 1 for not using batch mode
70: */
71: public void insert(Platform platform, Database model, int batchSize)
72: throws SQLException {
73: if (batchSize <= 1) {
74: for (Iterator it = _beans.iterator(); it.hasNext();) {
75: platform.insert(model, (DynaBean) it.next());
76: }
77: } else {
78: for (int startIdx = 0; startIdx < _beans.size(); startIdx += batchSize) {
79: platform.insert(model, _beans.subList(startIdx,
80: startIdx + batchSize));
81: }
82: }
83: }
84: }
|