[Tux4kids-commits] r1246 - in tux4kids-admin/trunk: libtux4kidsadmin tux4kids-admin/src

Michał Świtakowski swistakers-guest at alioth.debian.org
Tue Jul 21 18:12:43 UTC 2009


Author: swistakers-guest
Date: 2009-07-21 18:12:42 +0000 (Tue, 21 Jul 2009)
New Revision: 1246

Modified:
   tux4kids-admin/trunk/libtux4kidsadmin/schoolData.cpp
   tux4kids-admin/trunk/libtux4kidsadmin/schoolData_p.h
   tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
   tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.h
   tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
   tux4kids-admin/trunk/tux4kids-admin/src/CMakeLists.txt
   tux4kids-admin/trunk/tux4kids-admin/src/main.cpp
   tux4kids-admin/trunk/tux4kids-admin/src/mainController.cpp
Log:
part of SchoolDatabase implementation

Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolData.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolData.cpp	2009-07-21 12:12:32 UTC (rev 1245)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolData.cpp	2009-07-21 18:12:42 UTC (rev 1246)
@@ -4,6 +4,7 @@
 
 #include <QString>
 #include <QDebug>
+#include <QSqlDatabase>
 
 /****************** SchoolDataPrivate *******************/
 
@@ -19,6 +20,8 @@
 	mainDir.setPath(path);
 
 	loadStudentDirs();
+
+	//qDebug() << database.open("/home/swistak/test.db");
 }
 
 

Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolData_p.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolData_p.h	2009-07-21 12:12:32 UTC (rev 1245)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolData_p.h	2009-07-21 18:12:42 UTC (rev 1246)
@@ -5,9 +5,9 @@
 #include <QDir>
 #include <QList>
 #include <QPointer>
-#include <QSqlDatabase>
 
 #include "schoolData.h"
+#include "schoolDatabase.h"
 
 class QString;
 class ProfileDir;
@@ -31,7 +31,7 @@
 	QList< QPointer<StudentDir> > students;
 	QList< QPointer<ProfileDir> > profiles;
 
-	QSqlDatabase schoolDb;
+	SchoolDatabase database;
 };
 
 #endif // SCHOOLDATAPRIVATE_P_H

Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp	2009-07-21 12:12:32 UTC (rev 1245)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp	2009-07-21 18:12:42 UTC (rev 1246)
@@ -1,22 +1,45 @@
 #include "schoolDatabase.h"
 #include "schoolDatabase_p.h"
 
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QFile>
+#include <QDebug>
+
 /*********************** SchoolDatabasePrivate ***********************/
 
 SchoolDatabasePrivate::SchoolDatabasePrivate()
 {
+	db = QSqlDatabase::addDatabase("QSQLITE");
 }
 
-SchoolDatabase::SchoolDatabase(SchoolDatabasePrivate &dd, QObject *parent)
-		: QObject(parent),
-		d_ptr(&dd)
+SchoolDatabasePrivate::~SchoolDatabasePrivate()
 {
-
+	db.close();
 }
 
+void SchoolDatabasePrivate::createTables()
+{
+	error = false;
+	if (!db.isOpen()) {
+		error = true;
+		lastError = QObject::tr("Database is not open");
+		return;
+	}
 
-SchoolDatabasePrivate::~SchoolDatabasePrivate()
-{
+	QSqlQuery createClasses("CREATE TABLE classes (id integer primary key, name text)", db);
+	if (!createClasses.isActive()) {
+		error = true;
+		lastError = createClasses.lastError().text();
+		return;
+	}
+
+	QSqlQuery createTeachers("CREATE TABLE teachers (id integer primary key, first_name text, last_name text)", db);
+	if (!createClasses.isActive()) {
+		error = true;
+		lastError = createTeachers.lastError().text();
+		return;
+	}
 }
 
 /************************ SchoolDatabase **************************/
@@ -27,7 +50,48 @@
 {
 }
 
+SchoolDatabase::SchoolDatabase(SchoolDatabasePrivate &dd, QObject *parent)
+		: QObject(parent),
+		d_ptr(&dd)
+{
+}
+
 SchoolDatabase::~SchoolDatabase()
 {
+	Q_D(SchoolDatabase);
+	delete d;
 }
 
+bool SchoolDatabase::open(QString dbFilePath)
+{
+	Q_D(SchoolDatabase);
+
+	QFile dbFile(dbFilePath);
+	d->db.setDatabaseName(dbFilePath);
+	if (dbFile.exists()) {
+		return d->db.open();
+	} else {
+		bool result = d->db.open();
+		if (result)
+			d->createTables();
+		return result;
+	}
+}
+
+void SchoolDatabase::close()
+{
+	Q_D(SchoolDatabase);
+	d->db.close();
+}
+
+bool SchoolDatabase::error()
+{
+	Q_D(SchoolDatabase);
+	return d->error;
+}
+
+QString SchoolDatabase::lastError()
+{
+	Q_D(SchoolDatabase);
+	return d->lastError;
+}

Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.h	2009-07-21 12:12:32 UTC (rev 1245)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.h	2009-07-21 18:12:42 UTC (rev 1246)
@@ -14,6 +14,11 @@
 	SchoolDatabase(QObject *parent = 0);
 	~SchoolDatabase();
 
+	bool open(QString dbFilePath);
+	void close();
+	bool error();
+	QString lastError();
+
 protected:
 	SchoolDatabasePrivate * const d_ptr;
 	SchoolDatabase(SchoolDatabasePrivate &dd, QObject *parent = 0);

Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h	2009-07-21 12:12:32 UTC (rev 1245)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h	2009-07-21 18:12:42 UTC (rev 1246)
@@ -2,6 +2,7 @@
 #define SCHOOLDATABASE_P_H
 
 #include <QtGlobal>
+#include <QSqlDatabase>
 
 #include "libtux4kidsadmin_global.h"
 
@@ -15,8 +16,16 @@
 	SchoolDatabasePrivate();
 	virtual ~SchoolDatabasePrivate();
 
+	void createTables();
+
 	SchoolDatabase *q_ptr;
 
+	QSqlDatabase db;
+
+	bool isOpen;
+	bool error;
+	QString lastError;
+
 };
 
 #endif // SCHOOLDATABASE_P_H

Modified: tux4kids-admin/trunk/tux4kids-admin/src/CMakeLists.txt
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/CMakeLists.txt	2009-07-21 12:12:32 UTC (rev 1245)
+++ tux4kids-admin/trunk/tux4kids-admin/src/CMakeLists.txt	2009-07-21 18:12:42 UTC (rev 1246)
@@ -34,6 +34,7 @@
 ADD_DEFINITIONS( -Wall )
 
 FIND_PACKAGE( Qt4 REQUIRED )
+SET(QT_USE_QTSQL 1)
 INCLUDE( ${QT_USE_FILE} )
 INCLUDE_DIRECTORIES( ../plugins/ ../../libtux4kidsadmin/ ${CMAKE_CURRENT_BINARY_DIR} )
 

Modified: tux4kids-admin/trunk/tux4kids-admin/src/main.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/main.cpp	2009-07-21 12:12:32 UTC (rev 1245)
+++ tux4kids-admin/trunk/tux4kids-admin/src/main.cpp	2009-07-21 18:12:42 UTC (rev 1246)
@@ -8,7 +8,6 @@
 
 int main(int argc, char *argv[])
 {
-
 	QApplication app(argc, argv);
 
 	QTranslator translator;

Modified: tux4kids-admin/trunk/tux4kids-admin/src/mainController.cpp
===================================================================
--- tux4kids-admin/trunk/tux4kids-admin/src/mainController.cpp	2009-07-21 12:12:32 UTC (rev 1245)
+++ tux4kids-admin/trunk/tux4kids-admin/src/mainController.cpp	2009-07-21 18:12:42 UTC (rev 1246)
@@ -17,7 +17,6 @@
 
 	m_schoolData = new SchoolData(QDir::homePath() + "/school_data");
 	m_studentTableModel.setSchoolData(m_schoolData);
-
 }
 
 MainController::~MainController()




More information about the Tux4kids-commits mailing list