finish the queries

This commit is contained in:
Chris Cromer 2019-07-05 09:45:10 -04:00
parent 263c810544
commit 66b8fe1b6a
Signed by: cromer
GPG Key ID: 39CC813FF3C8708A
8 changed files with 255 additions and 9 deletions

13
src/db/q3.vala Normal file
View File

@ -0,0 +1,13 @@
namespace Colegio {
namespace DB {
public class Q3 : Object {
public string nombre { get; set; default = ""; }
public int cantidad { get; set; default = 0; }
public Q3 (string nombre = "", int cantidad = 0) {
this.nombre = nombre;
this.cantidad = cantidad;
}
}
}
}

13
src/db/q4.vala Normal file
View File

@ -0,0 +1,13 @@
namespace Colegio {
namespace DB {
public class Q4 : Object {
public string curso { get; set; default = ""; }
public int cantidad { get; set; default = 0; }
public Q4 (string curso = "", int cantidad = 0) {
this.curso = curso;
this.cantidad = cantidad;
}
}
}
}

13
src/db/q5.vala Normal file
View File

@ -0,0 +1,13 @@
namespace Colegio {
namespace DB {
public class Q5 : Object {
public string nombre { get; set; default = ""; }
public double promedio { get; set; default = 0.0; }
public Q5 (string nombre = "", double promedio = 0.0) {
this.nombre = nombre;
this.promedio = promedio;
}
}
}
}

13
src/db/q6.vala Normal file
View File

@ -0,0 +1,13 @@
namespace Colegio {
namespace DB {
public class Q6 : Object {
public string nombre { get; set; default = ""; }
public float porcentaje { get; set; default = 0; }
public Q6 (string nombre = "", float porcentaje = 0) {
this.nombre = nombre;
this.porcentaje = porcentaje;
}
}
}
}

View File

@ -39,6 +39,9 @@ namespace Colegio {
[GtkChild]
private Gtk.MenuItem q5;
[GtkChild]
private Gtk.MenuItem q6;
[GtkChild]
private Gtk.MenuItem quit;
@ -79,6 +82,12 @@ namespace Colegio {
query_window.initialize ();
query_window.show_all ();
}
else if (menu_item == q6) {
var query_window = new QueryWindow (application, conn, QueryWindow.Query.Q6);
query_window.set_transient_for (this); // Set this window as the parent of the new window
query_window.initialize ();
query_window.show_all ();
}
else if (menu_item == quit) {
application.quit ();
}

View File

@ -22,7 +22,11 @@ vala_sources = files(
'queries.vala',
'query_window.vala',
'db/q1.vala',
'db/q2.vala')
'db/q2.vala',
'db/q3.vala',
'db/q4.vala',
'db/q5.vala',
'db/q6.vala')
sources = vala_sources
sources += main_gresource

View File

@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 Chris Cromer
* Copyright 2019 Chris Cromer
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*

View File

@ -197,16 +197,196 @@ WHERE P.rut_profesor NOT IN (
}
}
else if (query == Query.Q3) {
list_store.clear ();
var res = conn.db.exec ("
SELECT A.nombre, COUNT(*) FROM actividad A
JOIN participar P ON (P.id_actividad = A.id_actividad)
JOIN alumno A2 ON (A2.rut_alumno = P.rut_alumno)
WHERE (A2.fecha_nacimiento >= '2006-01-01' AND A2.fecha_nacimiento <= '2008-12-31')
GROUP BY (A.nombre)
HAVING COUNT(*) > 5;
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
else {
var wra = new ResultWrapper (res);
List<Q3> list = new List<Q3> ();
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var result = new Q3 (wra.get_string_n (i, "nombre"),
wra.get_int_n (i, "count")
);
list.append (result);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
list.foreach ((entry) => {
Gtk.TreeIter iter;
list_store.append (out iter);
list_store.set (iter,
Q3Column.NOMBRE, entry.nombre,
Q3Column.CANTIDAD, entry.cantidad);
});
}
}
else if (query == Query.Q4) {
list_store.clear ();
var res = conn.db.exec ("
SELECT M.curso, M.cantidad FROM matriculados M
WHERE (
M.cantidad = (
SELECT MIN(M2.cantidad) FROM matriculados M2
)
);
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
else {
var wra = new ResultWrapper (res);
List<Q4> list = new List<Q4> ();
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var result = new Q4 (wra.get_string_n (i, "curso"),
wra.get_int_n (i, "cantidad")
);
list.append (result);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
list.foreach ((entry) => {
Gtk.TreeIter iter;
list_store.append (out iter);
list_store.set (iter,
Q4Column.CURSO, entry.curso,
Q4Column.CANTIDAD, entry.cantidad);
});
}
}
else if (query == Query.Q5) {
list_store.clear ();
var res = conn.db.exec ("
SELECT A.nombre, AVG(r.nota) FROM asignatura A
JOIN registro R ON (R.id_asignatura = A.id_asignatura)
JOIN asociado A2 ON (A2.id_asignatura = A.id_asignatura)
JOIN alumno A3 ON (A3.rut_alumno = R.rut_alumno)
WHERE (
A2.id_curso = '6A-2018'
AND R.rut_alumno NOT IN (
SELECT A4.rut_alumno FROM alumno A4
JOIN participar P ON (P.rut_alumno = A4.rut_alumno)
JOIN actividad A5 ON (A5.id_actividad = P.id_actividad)
WHERE (A5.nombre = 'Rugby' AND A4.rut_alumno = A3.rut_alumno)
)
)
GROUP BY (A.nombre);
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
else {
var wra = new ResultWrapper (res);
List<Q5> list = new List<Q5> ();
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var result = new Q5 (wra.get_string_n (i, "nombre"),
wra.get_double_n (i, "avg")
);
list.append (result);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
list.foreach ((entry) => {
Gtk.TreeIter iter;
list_store.append (out iter);
list_store.set (iter,
Q5Column.NOMBRE, entry.nombre,
Q5Column.PROMEDIO, entry.promedio.to_string ());
});
}
}
else if (query == Query.Q6) {
list_store.clear ();
var res = conn.db.exec ("
SELECT A2.nombre, TRUNC(CAST(T1.cantidad AS DECIMAL(3,2)) * 100 / CAST(T2.cantidad AS DECIMAL(3,2)),2) AS porcentaje
FROM total_aprobados T1
JOIN total_alumnos T2 ON (T2.asignatura = T1.asignatura)
JOIN asociado A ON (T1.asignatura = A.id_asignatura AND T2.asignatura = A.id_asignatura)
JOIN asignatura A2 ON (A2.id_asignatura = A.id_asignatura)
WHERE ((CAST(T1.cantidad AS DECIMAL(3,2)) * 100 / CAST(T2.cantidad AS DECIMAL(3,2))) < 50);
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
else {
var wra = new ResultWrapper (res);
List<Q6> list = new List<Q6> ();
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var result = new Q6 (wra.get_string_n (i, "nombre"),
wra.get_float_n (i, "porcentaje")
);
list.append (result);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
list.foreach ((entry) => {
Gtk.TreeIter iter;
list_store.append (out iter);
list_store.set (iter,
Q6Column.NOMBRE, entry.nombre,
Q6Column.PORCENTAJE, entry.porcentaje.to_string () + "%");
});
}
}
else {
this.close ();
@ -278,7 +458,7 @@ WHERE P.rut_profesor NOT IN (
sql.set_text (Q1_SQL);
}
else if (query == Query.Q2) {
this.set_title ("(Q2) Tour values");
this.set_title ("(Q2) Profesores sin actividad");
list_store = new Gtk.ListStore (Q2Column.N_COLUMNS,
typeof (string));
@ -290,7 +470,7 @@ WHERE P.rut_profesor NOT IN (
sql.set_text (Q2_SQL);
}
else if (query == Query.Q3) {
this.set_title ("(Q3) Coordinator total");
this.set_title ("(Q3) Actividades");
list_store = new Gtk.ListStore (Q3Column.N_COLUMNS,
typeof (string),
@ -303,7 +483,7 @@ WHERE P.rut_profesor NOT IN (
sql.set_text (Q3_SQL);
}
else if (query == Query.Q4) {
this.set_title ("(Q4) Tourist total");
this.set_title ("(Q4) Cursos con menor matriculados");
list_store = new Gtk.ListStore (Q4Column.N_COLUMNS,
typeof (string),
@ -316,11 +496,11 @@ WHERE P.rut_profesor NOT IN (
sql.set_text (Q4_SQL);
}
else if (query == Query.Q5) {
this.set_title ("(Q5) Vehicle total");
this.set_title ("(Q5) Notas de asignaturas");
list_store = new Gtk.ListStore (Q5Column.N_COLUMNS,
typeof (string),
typeof (uint));
typeof (string));
query_tree.set_model (list_store);
@ -329,11 +509,11 @@ WHERE P.rut_profesor NOT IN (
sql.set_text (Q5_SQL);
}
else if (query == Query.Q6) {
this.set_title ("(Q6) Vehicle total");
this.set_title ("(Q6) Asignaturas con bajo aprobación");
list_store = new Gtk.ListStore (Q6Column.N_COLUMNS,
typeof (string),
typeof (uint));
typeof (string));
query_tree.set_model (list_store);
@ -343,6 +523,7 @@ WHERE P.rut_profesor NOT IN (
}
else {
GLib.print ("Consulta inválida!\n");
this.close ();
}
}
}