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.ResultSet;
023: import java.sql.SQLException;
024:
025: import org.polepos.circuits.sepang.*;
026: import org.polepos.framework.*;
027:
028: /**
029: * @author Herkules
030: */
031: public class SepangJdbc extends JdbcDriver implements SepangDriver {
032:
033: Tree lastRead;
034:
035: public void takeSeatIn(Car car, TurnSetup setup)
036: throws CarMotorFailureException {
037:
038: super .takeSeatIn(car, setup);
039:
040: // Open database
041: jdbcCar().openConnection();
042:
043: //
044: // Create database structure
045: //
046: jdbcCar().dropTable("malaysia");
047: jdbcCar().createTable(
048: "malaysia",
049: new String[] { "id", "preceding", "subsequent", "name",
050: "depth" },
051: new Class[] { Integer.TYPE, Integer.TYPE, Integer.TYPE,
052: String.class, Integer.TYPE });
053:
054: jdbcCar().closeConnection();
055: }
056:
057: public void write() {
058:
059: Tree tree = Tree.createTree(setup().getTreeDepth());
060: Tree.traverse(tree, new TreeVisitor() {
061: public void visit(Tree tree) {
062: StringBuffer s = new StringBuffer(
063: "insert into malaysia (id, preceding, subsequent, name, depth ) values (");
064: s.append(tree.id);
065: s.append(",");
066: if (tree.preceding != null) {
067: s.append(tree.preceding.id);
068: } else {
069: s.append(0);
070: }
071: s.append(",");
072: if (tree.subsequent != null) {
073: s.append(tree.subsequent.id);
074: } else {
075: s.append(0);
076: }
077: s.append(", '");
078: s.append(tree.name);
079: s.append("', ");
080: s.append(tree.depth);
081: s.append(")");
082: jdbcCar().executeSQL(s.toString());
083: }
084: });
085: jdbcCar().commit();
086: }
087:
088: public void read() {
089: try {
090: lastRead = read(1);
091: Tree.traverse(lastRead, new TreeVisitor() {
092: public void visit(Tree tree) {
093: addToCheckSum(tree.getDepth());
094: }
095: });
096: } catch (SQLException e) {
097: e.printStackTrace();
098: }
099: }
100:
101: private Tree read(int id) throws SQLException {
102:
103: ResultSet rs = jdbcCar().executeQuery(
104: "select * from malaysia where id=" + id);
105: rs.next();
106:
107: Tree tree = new Tree(rs.getInt(1), rs.getString(4), rs
108: .getInt(5));
109: int precedingID = rs.getInt(2);
110: int subsequentID = rs.getInt(3);
111: rs.close();
112: if (precedingID > 0) {
113: tree.preceding = read(precedingID);
114: }
115: if (subsequentID > 0) {
116: tree.subsequent = read(subsequentID);
117: }
118: return tree;
119: }
120:
121: public void read_hot() {
122: read();
123: }
124:
125: public void delete() {
126: try {
127: delete(1);
128: jdbcCar().commit();
129: } catch (SQLException sqlex) {
130: sqlex.printStackTrace();
131: }
132: }
133:
134: private void delete(int id) throws SQLException {
135: ResultSet rs = jdbcCar().executeQuery(
136: "select * from malaysia where id=" + id);
137: rs.next();
138: int precedingID = rs.getInt(2);
139: int subsequentID = rs.getInt(3);
140: rs.close();
141: if (precedingID > 0) {
142: delete(precedingID);
143: }
144: if (subsequentID > 0) {
145: delete(subsequentID);
146: }
147: jdbcCar().executeUpdate("delete from malaysia where id=" + id);
148: }
149:
150: }
|