001: /*
002: This file is part of the PolePosition database benchmark
003: http://www.polepos.org
004:
005: This program is free software; you can redistribute it and/or
006: modify it under the terms of the GNU General Public License
007: as published by the Free Software Foundation; either version 2
008: of the License, or (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public
016: License along with this program; if not, write to the Free
017: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
018: MA 02111-1307, USA. */
019:
020: package org.polepos.teams.jdbc;
021:
022: import java.sql.*;
023:
024: import org.polepos.circuits.barcelona.*;
025: import org.polepos.framework.*;
026:
027: public class BarcelonaJdbc extends JdbcDriver implements
028: BarcelonaDriver {
029:
030: private static final String[] TABLES = new String[] { "barcelona0",
031: "barcelona1", "barcelona2", "barcelona3", "barcelona4", };
032:
033: public void takeSeatIn(Car car, TurnSetup setup)
034: throws CarMotorFailureException {
035:
036: super .takeSeatIn(car, setup);
037:
038: jdbcCar().openConnection();
039:
040: int i = 0;
041: for (String table : TABLES) {
042: jdbcCar().dropTable(table);
043: jdbcCar().createTable(
044: table,
045: new String[] { "id", "parent", "b" + i },
046: new Class[] { Integer.TYPE, Integer.TYPE,
047: Integer.TYPE });
048: jdbcCar().createIndex(table, "parent");
049: if (i == 2) {
050: jdbcCar().createIndex(table, "b2");
051: }
052: i++;
053: }
054: jdbcCar().closeConnection();
055:
056: }
057:
058: public void write() {
059:
060: try {
061: PreparedStatement[] statements = new PreparedStatement[5];
062: for (int i = 0; i < 5; i++) {
063: statements[i] = jdbcCar().prepareStatement(
064: "insert into " + TABLES[i] + " (id, parent, b"
065: + i + ") values (?,?,?)");
066: }
067:
068: int count = setup().getObjectCount();
069: if (count > 0) {
070: for (int i = 1; i <= count; i++) {
071: B4 b4 = new B4();
072: b4.setAll(i);
073: for (int j = 0; j < 5; j++) {
074: statements[j].setInt(1, i);
075: statements[j].setInt(2, i);
076: statements[j].setInt(3, b4.getBx(j));
077: statements[j].addBatch();
078: }
079: }
080:
081: for (int j = 0; j < 5; j++) {
082: statements[j].executeBatch();
083: statements[j].close();
084: }
085: }
086:
087: } catch (SQLException sqlex) {
088: sqlex.printStackTrace();
089: }
090:
091: jdbcCar().commit();
092:
093: }
094:
095: public void read() {
096: StringBuffer sql = select();
097: sql.append(TABLES[0]);
098: sql.append(".id=?");
099: query(sql.toString(), setup().getObjectCount());
100: }
101:
102: public void query() {
103: StringBuffer sql = select();
104: sql.append(TABLES[2]);
105: sql.append(".b2=?");
106: query(sql.toString(), setup().getSelectCount());
107: }
108:
109: /**
110: * deleting one at a time, simulating deleting individual objects
111: */
112: public void delete() {
113:
114: int count = setup().getObjectCount();
115:
116: PreparedStatement[] statements = new PreparedStatement[5];
117: for (int i = 0; i < 5; i++) {
118: statements[i] = jdbcCar().prepareStatement(
119: "delete from " + TABLES[i] + " where id=?");
120: }
121:
122: try {
123: if (count > 0) {
124: for (int i = 1; i <= count; i++) {
125: for (int j = 0; j < 5; j++) {
126: statements[j].setInt(1, i);
127: statements[j].execute();
128: addToCheckSum(1);
129: }
130: }
131:
132: for (int j = 0; j < 5; j++) {
133: statements[j].executeBatch();
134: statements[j].close();
135: }
136: }
137: } catch (SQLException e) {
138: e.printStackTrace();
139: }
140:
141: jdbcCar().commit();
142: }
143:
144: private StringBuffer select() {
145: StringBuffer sql = new StringBuffer("select * from ");
146: sql.append(TABLES[0]);
147: for (int i = 1; i < TABLES.length; i++) {
148: sql.append(", ");
149: sql.append(TABLES[i]);
150: }
151: sql.append(" where ");
152: for (int i = 1; i < TABLES.length; i++) {
153: sql.append(TABLES[i]);
154: sql.append(".parent = ");
155: sql.append(TABLES[i - 1]);
156: sql.append(".id");
157: sql.append(" and ");
158: }
159: return sql;
160: }
161:
162: private void query(String sql, int count) {
163: PreparedStatement statement = jdbcCar().prepareStatement(
164: sql.toString());
165:
166: try {
167: for (int i = 1; i <= count; i++) {
168: statement.setInt(1, i);
169: ResultSet rs = statement.executeQuery();
170: if (!rs.next()) {
171: System.err
172: .println("Expected one result, received none: "
173: + i);
174: }
175: B4 b4 = new B4(rs.getInt(3), rs.getInt(6),
176: rs.getInt(9), rs.getInt(12), rs.getInt(15));
177: addToCheckSum(b4.checkSum());
178: if (rs.next()) {
179: System.err
180: .println("Expected one result, received multiple: "
181: + i);
182: }
183: }
184: } catch (SQLException e) {
185: e.printStackTrace();
186: }
187:
188: }
189:
190: }
|