Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

DataClass.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Graeme Foster                                   *
00003  *   email    foster.graeme@gmail.com                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "DataClass.h"
00022 
00023 const int DataClass::NOT_ON_DB = -1;
00024 const int DataClass::NO_PARENT = -1;
00025 
00026 bool DataClass::dcCompareCI(const DataClass *p1, const DataClass * p2)
00027 {
00028         return p1->getDescription().toLower() < p2->getDescription().toLower();
00029 }
00030 
00031 bool DataClass::dcCompareCS(const DataClass *p1, const DataClass * p2)
00032 {
00033         return p1->getDescription() < p2->getDescription();
00034 }
00035 
00036 
00037 DataClass::DataClass(int pID)
00038 {
00039         id       = pID;
00040         errorMsg = "";
00041         p_rtti   = DATA_CLASS;
00042         parent   = 0;
00043 }
00044 
00045 QString DataClass::getClassName()
00046 {
00047         switch (p_rtti)
00048         {
00049                 case DATA_CLASS:
00050                         return "DataClass";
00051                 case INSTITUTE_CLASS:
00052                         return "Institute";
00053                 case PROGRAMME_CLASS:
00054                         return "Programme";
00055                 case MODULE_CLASS:
00056                         return "Module";
00057                 case FACULTY_CLASS:
00058                         return "Faculty";
00059                 case DEPARTMENT_CLASS:
00060                         return "Department";
00061                 case STAFF_CLASS:
00062                         return "Staff";
00063                 case ROOM_CLASS:
00064                         return "Room";
00065                 case MOD_LEC_ALLOC_CLASS:
00066                         return "ModLecAlloc";
00067                 case MOD_LEC_ENROL_CLASS:
00068                         return "ModLecEnrol";
00069                 case TIMESLOT_CLASS:
00070                         return "TimeSlot";
00071         }
00072         return "";
00073 }
00074 
00075 QString DataClass::getSortValue() const
00076 {
00077         if (parent == 0)
00078                 return getDescription();
00079         else
00080                 return parent->getDescription() + ":" + getDescription();
00081 }
00082 
00083 int DataClass::save(int insertSQL, int updateSQL, int errorSQL)
00084 {
00085         if (id == NOT_ON_DB)
00086                 return insert(insertSQL, errorSQL);
00087         else
00088                 return update(updateSQL, errorSQL);
00089 }
00090 
00091 int DataClass::insert(int insertSQL, int errorSQL)
00092 {
00093         bindValues(insertSQL, 0);
00094         try
00095         {
00096                 sql::Instance()->query();
00097                 id = sql::Instance()->getFirst(0).toInt();
00098                 return id;
00099         }
00100         catch (sqlE &e)
00101         {
00102                 return -1;
00103         }
00104 }
00105 
00106 int DataClass::update(int updateSQL, int errorSQL)
00107 {
00108         int start = bindPKey(updateSQL);
00109         bindValues(updateSQL, start);
00110         try
00111         {
00112                 sql::Instance()->query();
00113                 id = sql::Instance()->getFirst(0).toInt();
00114                 return id;
00115         }
00116         catch (sqlE &e)
00117         {
00118                 return -1;
00119         }
00120 }
00121 
00122 int DataClass::remove(int deleteSQL, int errorSQL)
00123 {
00124         bindPKey(deleteSQL);
00125         try
00126         {
00127                 sql::Instance()->query();
00128                 id = sql::Instance()->getFirst(0).toInt();
00129                 return id;
00130         }
00131         catch (sqlE &e)
00132         {
00133                 return -1;
00134         }
00135 }
00136 
00137 bool DataClass::unique(int uniqueSQL)
00138 {
00139         try
00140         {
00141                 sql::Instance()->query(uniqueSQL);
00142                 return sql::Instance()->getFirst(0).toBool();
00143         }
00144         catch (sqlE &e)
00145         {
00146                 return false;
00147         }
00148 }
00149 
00150 /*int DataClass::exec(QSqlQuery * execSQL, QSqlQuery * errorSQL)
00151 {
00152         int result = -1;
00153         execSQL->exec();
00154         execSQL->first();
00155 
00156         if (execSQL->size() <= 0)
00157         {
00158                 std::cout << execSQL->lastError().text().toStdString() << std::endl;
00159         }
00160         else
00161         {
00162                 result = execSQL->value(0).toInt();
00163                 if (result < 0)
00164                 {
00165                         dbError(errorSQL, result);
00166                 }
00167         }
00168         return result;
00169 }
00170 
00171 void DataClass::dbError(QSqlQuery * errorSQL, int errorID)
00172 {
00173         errorSQL->bindValue(0,errorID);
00174         errorSQL->exec();
00175         errorSQL->first();
00176         errorMsg = errorSQL->value(0).toString();
00177         std::cout << errorMsg.toStdString() << std::endl;
00178 }
00179 */

Generated on Thu Apr 6 16:27:16 2006 for time-table by  doxygen 1.4.4