[Tux4kids-commits] r1287 - tux4kids-admin/trunk/libtux4kidsadmin
Michał Świtakowski
swistakers-guest at alioth.debian.org
Tue Jul 28 21:36:40 UTC 2009
Author: swistakers-guest
Date: 2009-07-28 21:36:40 +0000 (Tue, 28 Jul 2009)
New Revision: 1287
Modified:
tux4kids-admin/trunk/libtux4kidsadmin/schoolData.cpp
tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
Log:
working students synchronization
Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolData.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolData.cpp 2009-07-28 20:45:37 UTC (rev 1286)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolData.cpp 2009-07-28 21:36:40 UTC (rev 1287)
@@ -23,6 +23,8 @@
if (!database.open(path + "/school_database.db")) {
status = SchoolData::InitializationError;
+ } else {
+ database.synchronizeStudents(students);
}
}
Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp 2009-07-28 20:45:37 UTC (rev 1286)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp 2009-07-28 21:36:40 UTC (rev 1287)
@@ -334,18 +334,90 @@
return result;
}
-void SchoolDatabasePrivate::synchronizeStudents(const QList< QPointer<StudentDir> > &studentList)
+void SchoolDatabasePrivate::synchronizeStudents(const QList< QPointer<StudentDir> > &studentsList)
{
QStringList existingStudents;
- foreach(StudentDir *studentDir, studentList) {
+ foreach(StudentDir *studentDir, studentsList) {
existingStudents.append(studentDir->dirName());
}
+ QStringList databaseStudents = studentList();
+ QStringList toAddStudents = existingStudents;
+ foreach(QString profileName, databaseStudents) {
+ toAddStudents.removeOne(profileName);
+ }
+ qDebug() << toAddStudents;
+ QStringList toDeleteStudents = databaseStudents;
+ foreach(QString profileName, existingStudents) {
+ toDeleteStudents.removeOne(profileName);
+ }
+
+ db.transaction();
+
+ foreach(QString profileName, toAddStudents) {
+ QSqlQuery addStudent;
+ addStudent.prepare("INSERT INTO students(profile_name) VALUES(:profile_name);");
+ addStudent.bindValue(":profile_name", profileName);
+ addStudent.exec();
+ if (!addStudent.isActive()) {
+ error = true;
+ lastError = addStudent.lastError().text();
+ return;
+ }
+ }
+
+ foreach(QString profileName, toDeleteStudents) {
+ QSqlQuery deleteStudent;
+ deleteStudent.prepare("DELETE FROM students WHERE profile_name = :profile_name;");
+ deleteStudent.bindValue(":profile_name", profileName);
+ deleteStudent.exec();
+
+ if (!deleteStudent.isActive()) {
+ error = true;
+ lastError = deleteStudent.lastError().text();
+ return;
+ }
+ }
+
+ db.commit();
+
}
+QStringList SchoolDatabasePrivate::studentList() const
+{
+ QStringList result;
+ if (!db.isOpen()) {
+ error = true;
+ lastError = QObject::tr("Database is not open");
+ return result;
+ }
+
+ QSqlQuery studentList;
+
+ error = false;
+
+ studentList.prepare("SELECT profile_name FROM students;");
+ studentList.exec();
+
+ if (!studentList.isActive()) {
+ error = true;
+ lastError = studentList.lastError().text();
+ return result;
+ }
+
+ QSqlRecord studentRec = studentList.record();
+
+ while (studentList.next()) {
+ QString profileName = studentList.value(studentRec.indexOf("profile_name")).toString();
+ result.append(profileName);
+ }
+ return result;
+}
+
+
/************************ SchoolDatabase **************************/
SchoolDatabase::SchoolDatabase(QObject *parent)
Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h 2009-07-28 20:45:37 UTC (rev 1286)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h 2009-07-28 21:36:40 UTC (rev 1287)
@@ -28,6 +28,8 @@
QList<Class> classList() const;
QList<Teacher> teacherList() const;
void synchronizeStudents(const QList< QPointer<StudentDir> > &studentList);
+ QStringList studentList() const;
+
SchoolDatabase *q_ptr;
QSqlDatabase db;
More information about the Tux4kids-commits
mailing list