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.PreparedStatement;
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025:
026: import org.polepos.circuits.bahrain.*;
027: import org.polepos.data.*;
028: import org.polepos.framework.*;
029: import org.polepos.teams.jdbc.drivers.melbourne.*;
030:
031: /**
032: * @author Herkules
033: */
034: public class BahrainJdbc extends JdbcDriver implements BahrainDriver {
035: /**
036: * Number of pilot to be written at once.
037: */
038: private final static int BULKSIZE = 1000;
039:
040: private static final String TABLE = "bahrain";
041:
042: public void takeSeatIn(Car car, TurnSetup setup)
043: throws CarMotorFailureException {
044: super .takeSeatIn(car, setup);
045:
046: jdbcCar().openConnection();
047: //
048: // Create database structure
049: //
050: jdbcCar().dropTable(TABLE);
051: jdbcCar().createTable(
052: TABLE,
053: new String[] { "id", "Name", "FirstName", "Points",
054: "LicenseID" },
055: new Class[] { Integer.TYPE, String.class, String.class,
056: Integer.TYPE, Integer.TYPE });
057: jdbcCar().createIndex(TABLE, "Name");
058: jdbcCar().createIndex(TABLE, "LicenseID");
059:
060: jdbcCar().closeConnection();
061: }
062:
063: public void write() {
064: Pilot[] pilots = new Pilot[BULKSIZE];
065: int idx = 0;
066:
067: // with a little help from Australia
068: BulkWriteStrategy writer = new BulkWritePreparedStatement(
069: jdbcCar(), TABLE);
070:
071: int commitctr = 0;
072: int count = setup().getObjectCount();
073: int commitInterval = setup().getCommitInterval();
074: for (int i = 1; i <= count; i++) {
075:
076: pilots[idx++] = new Pilot("Pilot_" + i, "Jonny_" + i, i, i);
077:
078: if (idx == BULKSIZE || i == count) {
079: writer.savePilots(TABLE, pilots, idx, i - idx + 1);
080: idx = 0;
081: }
082:
083: if (commitInterval > 0 && ++commitctr >= commitInterval) {
084: commitctr = 0;
085: jdbcCar().commit();
086: Log.logger.fine("commit while writing at " + i + 1); //NOI18N
087: }
088:
089: addToCheckSum(i);
090:
091: }
092:
093: jdbcCar().commit();
094: }
095:
096: public void query_indexed_string() {
097:
098: int count = setup().getSelectCount();
099:
100: for (int i = 1; i <= count; i++) {
101: performQuery("select * from bahrain where Name='Pilot_" + i
102: + "'");
103: }
104:
105: }
106:
107: public void query_string() {
108:
109: int count = setup().getSelectCount();
110:
111: for (int i = 1; i <= count; i++) {
112: performQuery("select * from bahrain where FirstName='Jonny_"
113: + i + "'");
114: }
115:
116: }
117:
118: public void query_indexed_int() {
119:
120: int count = setup().getSelectCount();
121:
122: for (int i = 1; i <= count; i++) {
123: performQuery("select * from bahrain where LicenseID=" + i);
124: }
125:
126: }
127:
128: public void query_int() {
129:
130: int count = setup().getSelectCount();
131:
132: for (int i = 1; i <= count; i++) {
133: performQuery("select * from bahrain where Points=" + i);
134: }
135:
136: }
137:
138: /**
139: * Some JDBC implementations don't support ResultSet#updateRow(), so an alternative method can be used.
140: */
141: public void update() {
142:
143: int updateCount = setup().getUpdateCount();
144:
145: updateIndexedStringStmt(updateCount);
146: }
147:
148: /**
149: * deleting one at a time, simulating deleting individual objects
150: */
151: public void delete() {
152:
153: int count = setup().getObjectCount();
154:
155: PreparedStatement statement = jdbcCar().prepareStatement(
156: "delete from bahrain where id=?");
157:
158: try {
159: for (int i = 1; i <= count; i++) {
160: statement.setObject(1, i);
161: statement.execute();
162: addToCheckSum(1);
163: }
164: } catch (SQLException e) {
165: e.printStackTrace();
166: }
167:
168: jdbcCar().commit();
169: }
170:
171: /**
172: * do the update using the ResultSet#updateRow() method
173: */
174: private void updateIndexedStringUpdateRow(int updateCount) {
175: try {
176: ResultSet rs = jdbcCar().executeQueryForUpdate(
177: "select ID, Name from bahrain");
178:
179: for (int i = 1; i <= updateCount; i++) {
180: rs.next();
181: rs.updateString(2, rs.getString(2).toUpperCase());
182: rs.updateRow();
183: addToCheckSum(1);
184: }
185: } catch (SQLException sqlex) {
186: sqlex.printStackTrace();
187: }
188: jdbcCar().commit();
189: }
190:
191: /**
192: * do the update using an 'update' SQL statement
193: */
194: private void updateIndexedStringStmt(int updateCount) {
195: try {
196: PreparedStatement stmt = jdbcCar().prepareStatement(
197: "update bahrain set Name=? where ID=?");
198: ResultSet rs = jdbcCar().executeQuery(
199: "select ID, Name from bahrain");
200:
201: for (int i = 0; i < updateCount; i++) {
202: rs.next();
203: int id = rs.getInt(1);
204: String name = rs.getString(2).toUpperCase();
205: stmt.setString(1, name);
206: stmt.setInt(2, id);
207: stmt.addBatch();
208: addToCheckSum(1);
209: }
210: stmt.executeBatch();
211: stmt.close();
212: } catch (SQLException sqlex) {
213: sqlex.printStackTrace();
214: }
215: jdbcCar().commit();
216: }
217:
218: }
|