From 66b8fe1b6a18584d23aa002ad3d585ef101d541b Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Fri, 5 Jul 2019 09:45:10 -0400 Subject: [PATCH] finish the queries --- src/db/q3.vala | 13 +++ src/db/q4.vala | 13 +++ src/db/q5.vala | 13 +++ src/db/q6.vala | 13 +++ src/main_window.vala | 9 ++ src/meson.build | 6 +- src/queries.vala | 2 +- src/query_window.vala | 195 ++++++++++++++++++++++++++++++++++++++++-- 8 files changed, 255 insertions(+), 9 deletions(-) create mode 100644 src/db/q3.vala create mode 100644 src/db/q4.vala create mode 100644 src/db/q5.vala create mode 100644 src/db/q6.vala diff --git a/src/db/q3.vala b/src/db/q3.vala new file mode 100644 index 0000000..54f498b --- /dev/null +++ b/src/db/q3.vala @@ -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; + } + } + } +} diff --git a/src/db/q4.vala b/src/db/q4.vala new file mode 100644 index 0000000..4016a08 --- /dev/null +++ b/src/db/q4.vala @@ -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; + } + } + } +} diff --git a/src/db/q5.vala b/src/db/q5.vala new file mode 100644 index 0000000..714f28d --- /dev/null +++ b/src/db/q5.vala @@ -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; + } + } + } +} diff --git a/src/db/q6.vala b/src/db/q6.vala new file mode 100644 index 0000000..969064f --- /dev/null +++ b/src/db/q6.vala @@ -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; + } + } + } +} diff --git a/src/main_window.vala b/src/main_window.vala index 101ac5b..04ad65d 100644 --- a/src/main_window.vala +++ b/src/main_window.vala @@ -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 (); } diff --git a/src/meson.build b/src/meson.build index 74f1d87..9003d1c 100644 --- a/src/meson.build +++ b/src/meson.build @@ -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 diff --git a/src/queries.vala b/src/queries.vala index a059ab5..bc6a6f1 100644 --- a/src/queries.vala +++ b/src/queries.vala @@ -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: * diff --git a/src/query_window.vala b/src/query_window.vala index e0b6858..e23b580 100644 --- a/src/query_window.vala +++ b/src/query_window.vala @@ -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 list = new List (); + 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 list = new List (); + 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 list = new List (); + 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 list = new List (); + 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 (); } } }