source: trunk/CrossPare/src/de/ugoe/cs/cpdp/eval/MySQLResultStorage.java

Last change on this file was 135, checked in by sherbold, 8 years ago
  • code documentation and formatting
  • Property svn:mime-type set to text/plain
File size: 6.5 KB
RevLine 
[86]1// Copyright 2015 Georg-August-Universität Göttingen, Germany
[71]2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
[68]14
15package de.ugoe.cs.cpdp.eval;
16
[71]17import java.io.FileInputStream;
18import java.io.IOException;
[69]19import java.sql.ResultSet;
[68]20import java.sql.SQLException;
21import java.sql.Statement;
[71]22import java.util.Properties;
[68]23
[95]24import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
25
[71]26import de.lmu.ifi.dbs.elki.logging.Logging.Level;
[68]27import de.ugoe.cs.util.console.Console;
28
[71]29/**
30 * <p>
31 * Implements a storage of experiment results in a MySQL database.
32 * </p>
33 *
34 * @author Steffen Herbold
35 */
[68]36public class MySQLResultStorage implements IResultStorage {
[71]37
38    /**
[95]39     * Connection pool for the data base.
40     */
41    private MysqlDataSource connectionPool = null;
[71]42
43    /**
44     * <p>
45     * Creates a new results storage. Tries to read a properties file mysql.cred located in the
46     * working directory. If this file is not found, the default database configuration is used:
47     * <ul>
48     * <li>dbHost = localhost</li>
49     * <li>dbPort = 3306</li>
50     * <li>dbName = crosspare</li>
51     * <li>dbUser = crosspare</li>
52     * <li>dbPass = benchmark</li>
53     * </p>
54     */
[68]55    public MySQLResultStorage() {
[71]56        Properties dbProperties = new Properties();
57        try {
58            dbProperties.load(new FileInputStream("mysql.cred"));
59        }
60        catch (IOException e) {
61            Console.printerr("Could not load mysql.cred file: " + e.getMessage());
62            Console.printerr("Must be a properties file located in working directory.");
63            Console
64                .traceln(Level.WARNING,
65                         "Using default DB configuration since mysql.cred file could not be loaded");
66        }
67        String dbHost = dbProperties.getProperty("db.host", "localhost");
68        String dbPort = dbProperties.getProperty("db.port", "3306");
69        String dbName = dbProperties.getProperty("db.name", "crosspare");
70        String dbUser = dbProperties.getProperty("db.user", "crosspare");
71        String dbPass = dbProperties.getProperty("db.pass", "benchmark");
72        connectToDB(dbHost, dbPort, dbName, dbUser, dbPass);
[68]73    }
[71]74
75    /**
76     * <p>
77     * Sets up the database connection
78     * </p>
79     *
80     * @param dbHost
81     *            host of the database
82     * @param dbPort
83     *            port of the database
84     * @param dbName
85     *            name of the database
86     * @param dbUser
87     *            user of the database
88     * @param dbPass
89     *            password of the user
90     */
91    private void connectToDB(String dbHost,
92                             String dbPort,
93                             String dbName,
94                             String dbUser,
95                             String dbPass)
96    {
[95]97        connectionPool = new MysqlDataSource();
98        connectionPool.setUser(dbUser);
99        connectionPool.setPassword(dbPass);
100        connectionPool.setUrl("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName);
[68]101    }
102
[71]103    /*
104     * (non-Javadoc)
105     *
106     * @see de.ugoe.cs.cpdp.eval.IResultStorage#addResult(de.ugoe.cs.cpdp.eval.ExperimentResult)
107     */
108    @Override
[68]109    public void addResult(ExperimentResult result) {
110        StringBuilder sql = new StringBuilder();
111        sql.append("INSERT INTO crosspare.results VALUES (NULL,");
[71]112        sql.append("\'" + result.getConfigurationName() + "\',");
113        sql.append("\'" + result.getProductName() + "\',");
114        sql.append("\'" + result.getClassifier() + "\',");
115        sql.append(result.getSizeTestData() + ",");
116        sql.append(result.getSizeTrainingData() + ",");
117        sql.append(result.getError() + ",");
118        sql.append(result.getRecall() + ",");
119        sql.append(result.getPrecision() + ",");
120        sql.append(result.getFscore() + ",");
121        sql.append(result.getGscore() + ",");
122        sql.append(result.getMcc() + ",");
123        sql.append(result.getAuc() + ",");
124        sql.append(result.getAucec() + ",");
125        sql.append(result.getTpr() + ",");
126        sql.append(result.getTnr() + ",");
127        sql.append(result.getFpr() + ",");
128        sql.append(result.getFnr() + ",");
129        sql.append(result.getTp() + ",");
130        sql.append(result.getFn() + ",");
131        sql.append(result.getTn() + ",");
132        sql.append(result.getFp() + ");");
133
[69]134        Statement stmt;
[68]135        try {
[95]136            stmt = connectionPool.getConnection().createStatement();
[68]137            stmt.executeUpdate(sql.toString().replace("NaN", "NULL"));
138        }
139        catch (SQLException e) {
140            Console.printerr("Problem with MySQL connection: ");
141            Console.printerr("SQLException: " + e.getMessage());
142            Console.printerr("SQLState: " + e.getSQLState());
143            Console.printerr("VendorError: " + e.getErrorCode());
144            return;
145        }
146    }
[71]147
148    /*
149     * (non-Javadoc)
150     *
151     * @see de.ugoe.cs.cpdp.eval.IResultStorage#containsResult(java.lang.String, java.lang.String)
152     */
[69]153    @Override
[121]154    public int containsResult(String experimentName, String productName, String classifierName) {
[71]155        String sql = "SELECT COUNT(*) as cnt FROM crosspare.results WHERE configurationName=\'" +
[135]156            experimentName + "\' AND productName=\'" + productName + "\' AND classifier=\'" +
157            classifierName + "\';";
[69]158        Statement stmt;
159        try {
[95]160            stmt = connectionPool.getConnection().createStatement();
[69]161            ResultSet results = stmt.executeQuery(sql);
162            results.next();
[98]163            return results.getInt("cnt");
[69]164        }
165        catch (SQLException e) {
166            Console.printerr("Problem with MySQL connection: \n");
167            Console.printerr("SQLException: " + e.getMessage() + "\n");
168            Console.printerr("SQLState: " + e.getSQLState() + "\n");
169            Console.printerr("VendorError: " + e.getErrorCode() + "\n");
[98]170            return 0;
[69]171        }
172    }
[68]173}
Note: See TracBrowser for help on using the repository browser.