[Tux4kids-commits] r1387 - tux4kids-admin/trunk/libtux4kidsadmin

Michał Świtakowski swistakers-guest at alioth.debian.org
Thu Aug 6 22:39:09 UTC 2009


Author: swistakers-guest
Date: 2009-08-06 22:39:09 +0000 (Thu, 06 Aug 2009)
New Revision: 1387

Modified:
   tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
   tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
Log:
database makes joins

Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp	2009-08-06 20:19:02 UTC (rev 1386)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp	2009-08-06 22:39:09 UTC (rev 1387)
@@ -307,6 +307,34 @@
 	return true;
 }
 
+bool SchoolDatabasePrivate::deleteClassStudents(int classId)
+{
+	QSqlQuery deleteStudents;
+	deleteStudents.prepare("DELETE FROM class_students WHERE id_class = :id_class;");
+	deleteStudents.bindValue(":id_class", classId);
+	deleteStudents.exec();
+	if (!deleteStudents.isActive()) {
+		error = true;
+		lastError = deleteStudents.lastError().text();
+		return false;
+	}
+	return true;
+}
+
+bool SchoolDatabasePrivate::deleteClassTeachers(int classId)
+{
+	QSqlQuery deleteTeachers;
+	deleteTeachers.prepare("DELETE FROM class_teachers WHERE id_class = :id_class;");
+	deleteTeachers.bindValue(":id_class", classId);
+	deleteTeachers.exec();
+	if (!deleteTeachers.isActive()) {
+		error = true;
+		lastError = deleteTeachers.lastError().text();
+		return false;
+	}
+	return true;
+}
+
 bool SchoolDatabasePrivate::addClassStudents(Class &newClass)
 {
 	foreach (QString studentDirName, *newClass.students()) {
@@ -502,11 +530,105 @@
 		return result;
 	}
 
+	result = classListNoJoin();
+	QList<Teacher> teachers = teacherListNoJoin();
+	if (error) {
+		return QList<Class>();
+	}
+
+	QSqlQuery classTeachers;
+	classTeachers.prepare("SELECT id_class, id_teacher FROM class_teachers ORDER BY id_class ASC;");
+	classTeachers.exec();
+	if (!classTeachers.isActive()) {
+		error = true;
+		lastError = classTeachers.lastError().text();
+		return QList<Class>();
+	}
+
+	QSqlRecord classTeachersRecord = classTeachers.record();
+	int oldClassId = -1;
+	int classIndex = 0;
+	while(classTeachers.next()) {
+		int classId = classTeachers.value(classTeachersRecord.indexOf("id_class")).toInt();
+		if (oldClassId != classId) {
+			oldClassId = classId;
+			while(result.at(classIndex).id() != classId
+			      && classIndex < result.size() - 1) {
+				++classIndex;
+			}
+			if (result.at(classIndex).id() != classId) {
+				break;
+			}
+		} else {
+			int teacherId = classTeachers.value(classTeachersRecord.indexOf("id_teacher")).toInt();
+			int teacherIndex = 0;
+			while (teachers[teacherIndex].id() != teacherId) {
+				++teacherIndex;
+			}
+			if (teachers[teacherIndex].id() == teacherId) {
+				result[classIndex].teachers()->append(teachers.at(teacherIndex));
+			}
+		}
+	}
+
+	QSqlQuery students;
+	students.prepare("SELECT id, profile_name;");
+	students.exec();
+	if (!students.isActive()) {
+		error = true;
+		lastError = students.lastError().text();
+		return QList<Class>();
+	}
+
+	QSqlQuery classStudents;
+	classStudents.prepare("SELECT id_class, id_student FROM class_students ORDER BY id_class ASC;");
+	classStudents.exec();
+	if (!classStudents.isActive()) {
+		error = true;
+		lastError = classStudents.lastError().text();
+		return QList<Class>();
+	}
+
+	QSqlRecord classStudentsRecord = classStudents.record();
+	QSqlRecord studentsRecord = students.record();
+	oldClassId = -1;
+	classIndex = 0;
+	while(classStudents.next()) {
+		int classId = classStudents.value(classStudentsRecord.indexOf("id_class")).toInt();
+		if (oldClassId != classId) {
+			oldClassId = classId;
+			while(result.at(classIndex).id() != classId
+			      && classIndex < result.size() - 1) {
+				++classIndex;
+			}
+			if (result.at(classIndex).id() != classId) {
+				break;
+			}
+		} else {
+			int studentId = classStudents.value(classStudentsRecord.indexOf("id_student")).toInt();
+
+			students.seek(-1);
+			while (students.next()) {
+				if (students.value(studentsRecord.indexOf("id")).toInt()
+					== studentId) {
+					result[classIndex].students()->append(
+				students.value(studentsRecord.indexOf("profile_name")).toString());
+				}
+			}
+		}
+	}
+
+	return result;
+}
+
+QList<Class> SchoolDatabasePrivate::classListNoJoin() const
+{
+	QList<Class> result;
 	QSqlQuery classList;
 
 	error = false;
 
-	classList.prepare("SELECT name FROM classes;");
+	classList.prepare("SELECT id, name FROM classes ORDER BY id ASC;");
 	classList.exec();
 
 	if (!classList.isActive()) {
@@ -538,11 +660,60 @@
 		return result;
 	}
 
+	result = teacherListNoJoin();
+	QList<Class> classes = classListNoJoin();
+	if (error) {
+		return QList<Teacher>();
+	}
+
+	QSqlQuery classTeachers;
+	classTeachers.prepare("SELECT id_class, id_teacher FROM class_teachers ORDER BY id_teachers ASC;");
+	classTeachers.exec();
+	if (!classTeachers.isActive()) {
+		error = true;
+		lastError = classTeachers.lastError().text();
+		return QList<Teacher>();
+	}
+
+	QSqlRecord classTeachersRecord = classTeachers.record();
+
+	int oldTeacherId = -1;
+	int teacherIndex = 0;
+	while(classTeachers.next()) {
+		int teacherId = classTeachers.value(classTeachersRecord.indexOf("id_teacher")).toInt();
+		if (oldTeacherId != teacherId) {
+			oldTeacherId = teacherId;
+			while(result.at(teacherIndex).id() != teacherId
+			      && teacherIndex < result.size() - 1) {
+				++teacherIndex;
+			}
+			if (result.at(teacherIndex).id() != teacherId) {
+				break;
+			}
+		} else {
+
+			int classId = classTeachers.value(classTeachersRecord.indexOf("id_class")).toInt();
+			int classIndex = 0;
+			while (classes[classIndex].id() != classId) {
+				++classIndex;
+			}
+			if (classes[classIndex].id() == classId) {
+				result[teacherIndex].classes()->append(classes.at(classIndex));
+			}
+		}
+	}
+
+	return result;
+}
+
+QList<Teacher> SchoolDatabasePrivate::teacherListNoJoin() const
+{
+	QList<Teacher> result;
 	QSqlQuery teacherList;
 
 	error = false;
 
-	teacherList.prepare("SELECT id, first_name, last_name FROM teachers;");
+	teacherList.prepare("SELECT id, first_name, last_name FROM teachers ORDER by id ASC;");
 	teacherList.exec();
 
 	if (!teacherList.isActive()) {
@@ -627,6 +798,14 @@
 		return result;
 	}
 
+	result = studentList();
+
+	return result;
+}
+
+QStringList SchoolDatabasePrivate::studentListNoJoin() const
+{
+	QStringList result;
 	QSqlQuery studentList;
 
 	error = false;

Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h	2009-08-06 20:19:02 UTC (rev 1386)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h	2009-08-06 22:39:09 UTC (rev 1387)
@@ -28,11 +28,16 @@
 
 	bool addClassTeachers(Class &newClass);
 	bool addClassStudents(Class &newClass);
+	bool deleteClassStudents(int classId);
+	bool deleteClassTeachers(int classId);
 
 	QList<Class> classList() const;
+	QList<Class> classListNoJoin() const;
 	QList<Teacher> teacherList() const;
+	QList<Teacher> teacherListNoJoin() const;
 	void synchronizeStudents(const QList< QPointer<StudentDir> > &studentList);
 	QStringList studentList() const;
+	QStringList studentListNoJoin() const;
 
 	SchoolDatabase *q_ptr;
 




More information about the Tux4kids-commits mailing list