/* * Copyright (c) 2006 Justin Knotzke (jknotzke@shampoo.ca) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "DBAccess.h" #include #include #include "RideFile.h" #include "Zones.h" #include "Settings.h" #include "RideItem.h" #include "RideMetric.h" #include "TimeUtils.h" #include #include #include #include "SummaryMetrics.h" // DB Schema Version - YOU MUST UPDATE THIS IF THE SCHEMA VERSION CHANGES!!! static int DBSchemaVersion = 10; // each DB connection gets a unique session id based upon this number: int DBAccess::session=0; DBAccess::DBAccess(QDir home) { initDatabase(home); } void DBAccess::closeConnection() { dbconn.close(); } void DBAccess::initDatabase(QDir home) { if(dbconn.isOpen()) return; sessionid = QString("session%1").arg(session++); db = QSqlDatabase::addDatabase("QSQLITE", sessionid); db.setDatabaseName(home.absolutePath() + "/metricDB"); dbconn = db.database(sessionid); if (!dbconn.isOpen()) { QMessageBox::critical(0, qApp->translate("DBAccess","Cannot open database"), qApp->translate("DBAccess","Unable to establish a database connection.\n" "This feature requires SQLite support. Please read " "the Qt SQL driver documentation for information how " "to build it.\n\n" "Click Cancel to exit."), QMessageBox::Cancel); } else { // create database - does nothing if its already there createDatabase(); } } bool DBAccess::createMetricsTable() { QSqlQuery query(dbconn); bool rc; bool createTables = true; // does the table exist? rc = query.exec("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"); if (rc) { while (query.next()) { QString table = query.value(0).toString(); if (table == "metrics") { createTables = false; break; } } } // we need to create it! if (rc && createTables) { QString createMetricTable = "create table metrics (filename varchar primary key," "timestamp integer," "ride_date date"; // Add columns for all the metrics const RideMetricFactory &factory = RideMetricFactory::instance(); for (int i=0; igetFileName()); query.exec(); } // construct an insert statement QString insertStatement = "insert into metrics ( filename, timestamp, ride_date"; const RideMetricFactory &factory = RideMetricFactory::instance(); for (int i=0; igetFileName()); query.addBindValue(timestamp.toTime_t()); query.addBindValue(summaryMetrics->getRideDate()); // values for (int i=0; igetForSymbol(factory.metricName(i))); } // go do it! bool rc = query.exec(); if(!rc) qDebug() << query.lastError(); return rc; } bool DBAccess::deleteRide(QString name) { QSqlQuery query(dbconn); query.prepare("DELETE FROM metrics WHERE filename = ?;"); query.addBindValue(name); return query.exec(); } QList DBAccess::getAllDates() { QSqlQuery query("SELECT ride_date from metrics ORDER BY ride_date;", dbconn); QList dates; query.exec(); while(query.next()) { QDateTime date = query.value(0).toDateTime(); dates << date; } return dates; } QList DBAccess::getAllMetricsFor(QDateTime start, QDateTime end) { QList metrics; // null date range fetches all, but not currently used by application code // since it relies too heavily on the results of the QDateTime constructor if (start == QDateTime()) start = QDateTime::currentDateTime().addYears(-10); if (end == QDateTime()) end = QDateTime::currentDateTime().addYears(+10); // construct the select statement QString selectStatement = "SELECT filename, ride_date"; const RideMetricFactory &factory = RideMetricFactory::instance(); for (int i=0; i