[Tux4kids-commits] r1285 - tux4kids-admin/trunk/libtux4kidsadmin
Michał Świtakowski
swistakers-guest at alioth.debian.org
Mon Jul 27 22:39:21 UTC 2009
Author: swistakers-guest
Date: 2009-07-27 22:39:20 +0000 (Mon, 27 Jul 2009)
New Revision: 1285
Modified:
tux4kids-admin/trunk/libtux4kidsadmin/class.cpp
tux4kids-admin/trunk/libtux4kidsadmin/class.h
tux4kids-admin/trunk/libtux4kidsadmin/class_p.h
tux4kids-admin/trunk/libtux4kidsadmin/profileDir.cpp
tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.h
tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp
tux4kids-admin/trunk/libtux4kidsadmin/teacher.h
tux4kids-admin/trunk/libtux4kidsadmin/teacher_p.h
Log:
sql queries
Modified: tux4kids-admin/trunk/libtux4kidsadmin/class.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/class.cpp 2009-07-27 20:56:51 UTC (rev 1284)
+++ tux4kids-admin/trunk/libtux4kidsadmin/class.cpp 2009-07-27 22:39:20 UTC (rev 1285)
@@ -4,6 +4,7 @@
/***************************** ClassPrivate *********************/
ClassPrivate::ClassPrivate()
+ : id(-1)
{
}
@@ -43,5 +44,16 @@
d->name = name;
}
+int Class::id() const
+{
+ Q_D(const Class);
+ return d->id;
+}
+void Class::setId(int id)
+{
+ Q_D(Class);
+ d->id = id;
+}
+
Modified: tux4kids-admin/trunk/libtux4kidsadmin/class.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/class.h 2009-07-27 20:56:51 UTC (rev 1284)
+++ tux4kids-admin/trunk/libtux4kidsadmin/class.h 2009-07-27 22:39:20 UTC (rev 1285)
@@ -16,6 +16,8 @@
QString name() const;
void setName(QString name);
+ int id() const;
+ void setId(int id);
protected:
Modified: tux4kids-admin/trunk/libtux4kidsadmin/class_p.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/class_p.h 2009-07-27 20:56:51 UTC (rev 1284)
+++ tux4kids-admin/trunk/libtux4kidsadmin/class_p.h 2009-07-27 22:39:20 UTC (rev 1285)
@@ -14,6 +14,7 @@
Class *q_ptr;
+ int id;
QString name;
};
Modified: tux4kids-admin/trunk/libtux4kidsadmin/profileDir.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/profileDir.cpp 2009-07-27 20:56:51 UTC (rev 1284)
+++ tux4kids-admin/trunk/libtux4kidsadmin/profileDir.cpp 2009-07-27 22:39:20 UTC (rev 1285)
@@ -73,6 +73,6 @@
QString ProfileDir::type() const
{
Q_D(const ProfileDir);
- d->attributes->value("profile_type");
+ return d->attributes->value("profile_type").toString();
}
Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp 2009-07-27 20:56:51 UTC (rev 1284)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.cpp 2009-07-27 22:39:20 UTC (rev 1285)
@@ -1,10 +1,13 @@
#include "schoolDatabase.h"
#include "schoolDatabase_p.h"
+#include "class.h"
+#include "teacher.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QFile>
#include <QDebug>
+#include <QVariant>
/*********************** SchoolDatabasePrivate ***********************/
@@ -27,35 +30,35 @@
return;
}
- QSqlQuery createClasses("CREATE TABLE classes (id integer primary key, name text)", db);
+ 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);
+ 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;
}
- QSqlQuery createStudents("CREATE TABLE students (id integer primary key, profile_name text)", db);
+ QSqlQuery createStudents("CREATE TABLE students (id integer primary key, profile_name text);", db);
if (!createStudents.isActive()) {
error = true;
lastError = createStudents.lastError().text();
return;
}
- QSqlQuery createClassTeachers("CREATE TABLE class_teachers (id_class integer, id_teacher integer, primary key(id_class,id_teacher))", db);
+ QSqlQuery createClassTeachers("CREATE TABLE class_teachers (id_class integer, id_teacher integer, primary key(id_class,id_teacher));", db);
if (!createClassTeachers.isActive()) {
error = true;
lastError = createClassTeachers.lastError().text();
return;
}
- QSqlQuery createClassStudents("CREATE TABLE class_students (id_class integer, id_student integer, primary key(id_class,id_student))", db);
+ QSqlQuery createClassStudents("CREATE TABLE class_students (id_class integer, id_student integer, primary key(id_class,id_student));", db);
if (!createClassStudents.isActive()) {
error = true;
lastError = createClassStudents.lastError().text();
@@ -64,6 +67,198 @@
}
+void SchoolDatabasePrivate::addClass(Class &newClass)
+{
+ if (!db.isOpen()) {
+ error = true;
+ lastError = QObject::tr("Database is not open");
+ return;
+ }
+ QSqlQuery insertClass;
+
+ error = false;
+
+ insertClass.prepare("INSERT INTO classes(name) VALUES(:name);");
+ insertClass.bindValue(":name", newClass.name());
+ insertClass.exec();
+
+ if (!insertClass.isActive()) {
+ error = true;
+ lastError = insertClass.lastError().text();
+ return;
+ } else {
+ QSqlQuery getClassId;
+ getClassId.prepare("SELECT MAX(id) FROM classes;");
+ getClassId.exec();
+
+ if (!getClassId.isActive()) {
+ error = true;
+ lastError = getClassId.lastError().text();
+ return;
+ }
+
+ if (!getClassId.first()){
+ error = true;
+ lastError = getClassId.lastError().text();
+ return;
+ } else {
+ newClass.setId(getClassId.value(0).toInt());
+ Q_Q(SchoolDatabase);
+ emit q->classAdded(newClass);
+ }
+ }
+}
+
+void SchoolDatabasePrivate::updateClass(const Class &updatedClass)
+{
+ if (!db.isOpen()) {
+ error = true;
+ lastError = QObject::tr("Database is not open");
+ return;
+ }
+
+ QSqlQuery updateClass;
+
+ error = false;
+
+ updateClass.prepare("UPDATE classes SET name = :name WHERE id = :id;");
+ updateClass.bindValue(":name", updatedClass.name());
+ updateClass.bindValue(":id", updatedClass.id());
+ updateClass.exec();
+
+ if (!updateClass.isActive()) {
+ error = true;
+ lastError = updateClass.lastError().text();
+ return;
+ }
+
+ Q_Q(SchoolDatabase);
+ emit q->classUpdated(updatedClass);
+}
+
+void SchoolDatabasePrivate::deleteClass(const Class &deletedClass)
+{
+ if (!db.isOpen()) {
+ error = true;
+ lastError = QObject::tr("Database is not open");
+ return;
+ }
+
+ QSqlQuery deleteClass;
+
+ error = false;
+
+ deleteClass.prepare("DELETE FROM classes WHERE id = :id;");
+ deleteClass.bindValue(":id", deletedClass.id());
+ deleteClass.exec();
+
+ if (!deleteClass.isActive()) {
+ error = true;
+ lastError = deleteClass.lastError().text();
+ return;
+ }
+
+ Q_Q(SchoolDatabase);
+ emit q->classDeleted(deletedClass);
+}
+
+void SchoolDatabasePrivate::addTeacher(Teacher &newTeacher)
+{
+ if (!db.isOpen()) {
+ error = true;
+ lastError = QObject::tr("Database is not open");
+ return;
+ }
+ QSqlQuery insertTeacher;
+
+ error = false;
+
+ insertTeacher.prepare("INSERT INTO teacher(first_name, last_name) VALUES(:first_name, :last_name);");
+ insertTeacher.bindValue(":first_name", newTeacher.firstName());
+ insertTeacher.bindValue(":last_name", newTeacher.lastName());
+ insertTeacher.exec();
+
+ if (!insertTeacher.isActive()) {
+ error = true;
+ lastError = insertTeacher.lastError().text();
+ return;
+ } else {
+ QSqlQuery getTeacherId;
+ getTeacherId.prepare("SELECT MAX(id) FROM teachers;");
+ getTeacherId.exec();
+
+ if (!getTeacherId.isActive()) {
+ error = true;
+ lastError = getTeacherId.lastError().text();
+ return;
+ }
+
+ if (!getTeacherId.first()){
+ error = true;
+ lastError = getTeacherId.lastError().text();
+ return;
+ } else {
+ newTeacher.setId(getTeacherId.value(0).toInt());
+ Q_Q(SchoolDatabase);
+ emit q->teacherAdded(newTeacher);
+ }
+ }
+}
+
+void SchoolDatabasePrivate::updateTeacher(const Teacher &updatedTeacher)
+{
+ if (!db.isOpen()) {
+ error = true;
+ lastError = QObject::tr("Database is not open");
+ return;
+ }
+
+ QSqlQuery updateTeacher;
+
+ error = false;
+
+ updateTeacher.prepare("UPDATE teachers SET first_name = :first_name, last_name = :last_name WHERE id = :id;");
+ updateTeacher.bindValue(":first_name", updatedTeacher.firstName());
+ updateTeacher.bindValue(":last_name", updatedTeacher.lastName());
+ updateTeacher.bindValue(":id", updatedTeacher.id());
+ updateTeacher.exec();
+
+ if (!updateTeacher.isActive()) {
+ error = true;
+ lastError = updateTeacher.lastError().text();
+ return;
+ }
+
+ Q_Q(SchoolDatabase);
+ emit q->teacherUpdated(updatedTeacher);
+}
+
+void SchoolDatabasePrivate::deleteTeacher(const Teacher &deletedTeacher)
+{
+ if (!db.isOpen()) {
+ error = true;
+ lastError = QObject::tr("Database is not open");
+ return;
+ }
+
+ QSqlQuery deleteTeacher;
+
+ error = false;
+
+ deleteTeacher.prepare("DELETE FROM teachers WHERE id = :id");
+ deleteTeacher.bindValue(":id", deletedTeacher.id());
+ deleteTeacher.exec();
+
+ if (!deleteTeacher.isActive()) {
+ error = true;
+ lastError = deleteTeacher.lastError().text();
+ return;
+ }
+
+ Q_Q(SchoolDatabase);
+ emit q->teacherDeleted(deletedTeacher);
+}
+
/************************ SchoolDatabase **************************/
SchoolDatabase::SchoolDatabase(QObject *parent)
@@ -122,5 +317,39 @@
return d->lastError;
}
+void SchoolDatabase::addClass(Class &newClass)
+{
+ Q_D(SchoolDatabase);
+ d->addClass(newClass);
+}
+void SchoolDatabase::updateClass(const Class &updatedClass)
+{
+ Q_D(SchoolDatabase);
+ d->updateClass(updatedClass);
+}
+void SchoolDatabase::deleteClass(const Class &deletedClass)
+{
+ Q_D(SchoolDatabase);
+ d->deleteClass(deletedClass);
+}
+
+void SchoolDatabase::addTeacher(Teacher &newTeacher)
+{
+ Q_D(SchoolDatabase);
+ d->addTeacher(newTeacher);
+}
+
+void SchoolDatabase::updateTeacher(const Teacher &updatedTeacher)
+{
+ Q_D(SchoolDatabase);
+ d->updateTeacher(updatedTeacher);
+}
+
+void SchoolDatabase::deleteTeacher(const Teacher &deletedTeacher)
+{
+ Q_D(SchoolDatabase);
+ d->deleteTeacher(deletedTeacher);
+}
+
Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.h 2009-07-27 20:56:51 UTC (rev 1284)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase.h 2009-07-27 22:39:20 UTC (rev 1285)
@@ -6,6 +6,8 @@
#include "libtux4kidsadmin_global.h"
class SchoolDatabasePrivate;
+class Class;
+class Teacher;
class LIBTUX4KIDSADMIN_SHARED_EXPORT SchoolDatabase : public QObject
{
@@ -18,11 +20,27 @@
void close();
bool error();
QString lastError();
+ void addClass(Class &newClass);
+ void updateClass(const Class &updatedClass);
+ void deleteClass(const Class &deletedClass);
+ void addTeacher(Teacher &newTeacher);
+ void updateTeacher(const Teacher &updatedTeacher);
+ void deleteTeacher(const Teacher &deletedTeacher);
+
protected:
SchoolDatabasePrivate * const d_ptr;
SchoolDatabase(SchoolDatabasePrivate &dd, QObject *parent = 0);
+signals:
+ void classAdded(const Class &newClass);
+ void classUpdated(const Class &updatedClass);
+ void classDeleted(const Class &deletedClass);
+
+ void teacherAdded(const Teacher &newTeacher);
+ void teacherUpdated(const Teacher &updatedTeacher);
+ void teacherDeleted(const Teacher &deletedTeacher);
+
private:
Q_DECLARE_PRIVATE(SchoolDatabase)
Q_DISABLE_COPY(SchoolDatabase)
Modified: tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h 2009-07-27 20:56:51 UTC (rev 1284)
+++ tux4kids-admin/trunk/libtux4kidsadmin/schoolDatabase_p.h 2009-07-27 22:39:20 UTC (rev 1285)
@@ -17,12 +17,18 @@
virtual ~SchoolDatabasePrivate();
void createTables();
+ void addClass(Class &newClass);
+ void updateClass(const Class &updatedClass);
+ void deleteClass(const Class &deletedClass);
+ void addTeacher(Teacher &newTeacher);
+ void updateTeacher(const Teacher &updatedTeacher);
+ void deleteTeacher(const Teacher &deletedTeacher);
+
SchoolDatabase *q_ptr;
QSqlDatabase db;
- bool isOpen;
bool error;
QString lastError;
Modified: tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp 2009-07-27 20:56:51 UTC (rev 1284)
+++ tux4kids-admin/trunk/libtux4kidsadmin/teacher.cpp 2009-07-27 22:39:20 UTC (rev 1285)
@@ -4,6 +4,7 @@
/***************************** TeacherPrivate *********************/
TeacherPrivate::TeacherPrivate()
+ : id(-1)
{
}
@@ -56,4 +57,15 @@
d->lastName = lastName;
}
+int Teacher::id() const
+{
+ Q_D(const Teacher);
+ return d->id;
+}
+void Teacher::setId(int id)
+{
+ Q_D(Teacher);
+ d->id = id;
+}
+
Modified: tux4kids-admin/trunk/libtux4kidsadmin/teacher.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/teacher.h 2009-07-27 20:56:51 UTC (rev 1284)
+++ tux4kids-admin/trunk/libtux4kidsadmin/teacher.h 2009-07-27 22:39:20 UTC (rev 1285)
@@ -16,8 +16,10 @@
QString firstName() const;
QString lastName() const;
+ int id() const;
void setFirstName(QString firstName);
void setLastName(QString lastName);
+ void setId(int id);
protected:
Modified: tux4kids-admin/trunk/libtux4kidsadmin/teacher_p.h
===================================================================
--- tux4kids-admin/trunk/libtux4kidsadmin/teacher_p.h 2009-07-27 20:56:51 UTC (rev 1284)
+++ tux4kids-admin/trunk/libtux4kidsadmin/teacher_p.h 2009-07-27 22:39:20 UTC (rev 1285)
@@ -14,6 +14,7 @@
Teacher *q_ptr;
+ int id;
QString firstName;
QString lastName;
More information about the Tux4kids-commits
mailing list