start the 3rd assignment

This commit is contained in:
2019-01-03 19:47:43 -03:00
parent 21f57a86e7
commit 18dafc38e1
63 changed files with 1818 additions and 233 deletions

69
lib/db/arrienda.vala Normal file
View File

@@ -0,0 +1,69 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Arrienda : GLib.Object {
public uint precio { get; set; default = 0; }
public string fecha_devolucion { get; set; default = ""; }
public Vehiculo vehiculo { get; set; default = null; }
public Empresa empresa { get; set; default = null; }
public Arrienda (uint precio = 0, string fecha_devolucion = "", Vehiculo? vehiculo = null, Empresa? empresa = null) {
this.precio = precio;
this.fecha_devolucion = fecha_devolucion;
this.vehiculo = vehiculo;
this.empresa = empresa;
}
public static Arrienda[]? get_all_arriendas(Database conn) {
var res = conn.exec ("
SELECT A.precio, A.fecha_devolucion,
V.patente, V.ano_vehiculo, V.marca, V.capacidad,
E.rut_empresa, E.nombre_empresa, E.contacto, E.telefono
FROM arrienda A
JOIN vehiculo V ON (A.patente = V.patente)
JOIN empresa E ON (A.rut_empresa = E.rut_empresa)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Arrienda[] arriendas = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var arrienda = new Arrienda (wra.get_int_n (i, "precio"),
wra.get_string_n (i, "fecha_devolucion"),
new Vehiculo (wra.get_string_n (i, "patente"),
wra.get_int_n (i, "ano_vehiculo"),
wra.get_string_n (i, "marca"),
wra.get_int_n (i, "capacidad")
),
new Empresa (wra.get_string_n (i, "rut_empresa"),
wra.get_string_n (i, "nombre_empresa"),
wra.get_string_n (i, "contacto"),
wra.get_int_n (i, "telefono")
)
);
arriendas += arrienda;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return arriendas;
}
}
}
}

94
lib/db/asociado.vala Normal file
View File

@@ -0,0 +1,94 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Asociado : GLib.Object {
public string fecha_llegada { get; set; default = ""; }
public string hora_llegada { get; set; default = ""; }
public string fecha_salida { get; set; default = ""; }
public string hora_salida { get; set; default = ""; }
public Tour tour { get; set; default = null; }
public Lugar lugar { get; set; default = null; }
public Asociado (string fecha_llegada = "", string hora_llegada = "", string fecha_salida = "", string hora_salida = "", Tour? tour = null, Lugar? lugar = null) {
this.fecha_llegada = fecha_llegada;
this.hora_llegada = hora_llegada;
this.fecha_salida = fecha_salida;
this.hora_salida = hora_salida;
}
public static Asociado[]? get_all_asociados(Database conn) {
var res = conn.exec ("
SELECT A.fecha_llegada, A.hora_llegada, A.fecha_salida, A.hora_salida
T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
C.id_ciudad, C.nombre_ciudad,
R.id_region, R.nombre_region,
L.id_lugar, L.nombre_lugar, L.valor_entrada, L.nivel
C2.id_ciudad AS id_ciudad_lugar, C2.nombre_ciudad AS nombre_ciudad_lugar,
R2.id_region AS id_region_lugar, R2.nombre_region AS nombre_region_lugar
FROM asociado A
JOIN tour T ON (A.id_tour = T.id_tour)
JOIN ciudad C ON (T.id_cuidad = C.id_ciudad)
JOIN region R ON (C.id_region = R.id_region)
JOIN lugar L ON (A.id_lugar = L.id_lugar)
JOIN ciudad C2 ON (L.id_ciudad = C2.id_ciudad)
JOIN region R2 ON (C2.id_region = R2.id_region)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Asociado[] asociados = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var asociado = new Asociado (wra.get_string_n (i, "fecha_llegada"),
wra.get_string_n (i, "hora_llegada"),
wra.get_string_n (i, "fecha_salida"),
wra.get_string_n (i, "hora_salida"),
new Tour (wra.get_int_n (i, "id_tour"),
wra.get_string_n (i, "nombre_tour"),
wra.get_int_n (i, "costo_indiv"),
wra.get_int_n (i, "costo_grupal"),
wra.get_int_n (i, "minima_personas"),
new Ciudad (wra.get_int_n (i, "id_ciudad"),
wra.get_string_n (i, "nombre_ciudad"),
new Region (wra.get_int_n (i, "id_region"),
wra.get_string_n (i, "nombre_region")
)
)
),
new Lugar (wra.get_int_n (i, "id_lugar"),
wra.get_string_n (i, "nombre_tour"),
wra.get_int_n (i, "valor_entrada"),
wra.get_int_n (i, "nivel"),
new Ciudad (wra.get_int_n (i, "id_ciudad_lugar"),
wra.get_string_n (i, "nombre_ciudad_lugar"),
new Region (wra.get_int_n (i, "id_region_lugar"),
wra.get_string_n (i, "nombre_region_lugar")
)
)
)
);
asociados += asociado;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return asociados;
}
}
}
}

50
lib/db/categoria.vala Normal file
View File

@@ -0,0 +1,50 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Categoria : GLib.Object {
public uint id_categoria { get; set; default = 0; }
public string descripcion_categoria { get; set; default = ""; }
public Categoria (uint id_categoria = 0, string descripcion_categoria = "") {
this.id_categoria = id_categoria;
this.descripcion_categoria = descripcion_categoria;
}
public static Categoria[]? get_all_categorias(Database conn) {
var res = conn.exec ("
SELECT id_categoria, descripcion_categoria FROM categoria
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Categoria[] categorias = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var categoria = new Categoria (wra.get_int_n (i, "id_categoria"),
wra.get_string_n (i, "descripcion_categoria")
);
categorias += categoria;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return categorias;
}
}
}
}

58
lib/db/ciudad.vala Normal file
View File

@@ -0,0 +1,58 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Ciudad : GLib.Object {
public uint id_ciudad { get; set; default = 0; }
public string nombre_ciudad { get; set; default = ""; }
public Region region { get; set; default = null; }
public Ciudad (uint id_ciudad = 0, string nombre_ciudad = "", Region? region = null) {
this.id_ciudad = id_ciudad;
this.nombre_ciudad = nombre_ciudad;
this.region = region;
}
public static Ciudad[]? get_all_ciudades(Database conn) {
var res = conn.exec ("
SELECT C.id_ciudad, C.nombre_ciudad,
R.id_region, R.nombre_region
FROM ciudad C
JOIN region R ON (C.id_region = R.id_region)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Ciudad[] ciudades = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var ciudad = new Ciudad (wra.get_int_n (i, "id_ciudad"),
wra.get_string_n (i, "nombre_ciudad"),
new Region (wra.get_int_n (i, "id_region"),
wra.get_string_n (i, "nombre_region")
)
);
ciudades += ciudad;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return ciudades;
}
}
}
}

View File

@@ -0,0 +1,51 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class ContactoEmergencia : GLib.Object {
public uint id_contacto { get; set; default = 0; }
public uint telefono_emergencia { get; set; default = 0; }
public string nombre_emergencia { get; set; default = ""; }
public ContactoEmergencia (uint id_contacto = 0, uint telefono_emergencia = 0, string nombre_emergencia = "") {
this.id_contacto = id_contacto;
this.telefono_emergencia = telefono_emergencia;
this.nombre_emergencia = nombre_emergencia;
}
public static ContactoEmergencia[]? get_all_contactos(Database conn) {
var res = conn.exec ("SELECT id_contacto, telefono_emergencia, nombre_emergencia FROM contacto_emergencia");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
ContactoEmergencia[] contactos = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var contacto = new ContactoEmergencia (wra.get_int_n (i, "id_contacto"),
wra.get_int_n (i, "telefono_emergencia"),
wra.get_string_n (i, "nombre_emergencia")
);
contactos += contacto;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return contactos;
}
}
}
}

53
lib/db/descuento.vala Normal file
View File

@@ -0,0 +1,53 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Descuento : GLib.Object {
public uint id_descuento { get; set; default = 0; }
public string descripcion_descuento { get; set; default = ""; }
public float porcentaje { get; set; default = 0; }
public Descuento (uint id_descuento = 0, string descripcion_descuento = "", float porcentaje = 0) {
this.id_descuento = id_descuento;
this.descripcion_descuento = descripcion_descuento;
this.porcentaje = porcentaje;
}
public static Descuento[]? get_all_descuentos(Database conn) {
var res = conn.exec ("
SELECT id_descuento, descripcion_descuento, porcentaje FROM descuento
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Descuento[] descuentos = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var descuento = new Descuento (wra.get_int_n (i, "id_descuento"),
wra.get_string_n (i, "descripcion_descuento"),
wra.get_float_n (i, "porcentaje")
);
descuentos += descuento;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return descuentos;
}
}
}
}

56
lib/db/empresa.vala Normal file
View File

@@ -0,0 +1,56 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Empresa : GLib.Object {
public string rut_empresa { get; set; default = ""; }
public string nombre_empresa { get; set; default = ""; }
public string contacto { get; set; default = ""; }
public uint telefono { get; set; default = 0; }
public Empresa (string rut_empresa = "", string nombre_empresa = "", string contacto = "", uint telefono = 0) {
this.rut_empresa = rut_empresa;
this.nombre_empresa = nombre_empresa;
this.contacto = contacto;
this.telefono = telefono;
}
public static Empresa[]? get_all_empresas(Database conn) {
var res = conn.exec ("
SELECT rut_empresa, nombre_empresa, contacto, telefono FROM empresa
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Empresa[] empresas = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var empresa = new Empresa (wra.get_string_n (i, "rut_empresa"),
wra.get_string_n (i, "nombre_empresa"),
wra.get_string_n (i, "contacto"),
wra.get_int_n (i, "telefono")
);
empresas += empresa;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return empresas;
}
}
}
}

50
lib/db/enfermedad.vala Normal file
View File

@@ -0,0 +1,50 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Enfermedad : GLib.Object {
public uint id_enfermedad { get; set; default = 0; }
public string descripcion_enfermedad { get; set; default = ""; }
public Enfermedad (uint id_enfermedad = 0, string descripcion_enfermedad = "") {
this.id_enfermedad = id_enfermedad;
this.descripcion_enfermedad = descripcion_enfermedad;
}
public static Enfermedad[]? get_all_enfermedades(Database conn) {
var res = conn.exec ("
SELECT id_enfermedad, descripcion_enfermedad FROM enfermedad
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Enfermedad[] enfermedades = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var enfermedad = new Enfermedad (wra.get_int_n (i, "id_enfermedad"),
wra.get_string_n (i, "descripcion_enfermedad")
);
enfermedades += enfermedad;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return enfermedades;
}
}
}
}

50
lib/db/especialidad.vala Normal file
View File

@@ -0,0 +1,50 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Especialidad : GLib.Object {
public uint id_especialidad { get; set; default = 0; }
public string descripcion_especialidad { get; set; default = ""; }
public Especialidad (uint id_especialidad = 0, string descripcion_especialidad = "") {
this.id_especialidad = id_especialidad;
this.descripcion_especialidad = descripcion_especialidad;
}
public static Especialidad[]? get_all_especialidades(Database conn) {
var res = conn.exec ("
SELECT id_especialidad, descripcion_especialidad FROM especialidad
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Especialidad[] especialidades = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var especialidad = new Especialidad (wra.get_int_n (i, "id_especialidad"),
wra.get_string_n (i, "descripcion_especialidad")
);
especialidades += especialidad;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return especialidades;
}
}
}
}

69
lib/db/guia.vala Normal file
View File

@@ -0,0 +1,69 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Guia : GLib.Object {
public string rut_guia { get; set; default = ""; }
public string nombre_guia { get; set; default = ""; }
public string calle { get; set; default = ""; }
public uint numero { get; set; default = 0; }
public Ciudad ciudad { get; set; default = null; }
public Guia (string rut_guia = "", string nombre_guia = "", string calle = "", uint numero = 0, Ciudad? ciudad = null) {
this.rut_guia = rut_guia;
this.nombre_guia = nombre_guia;
this.calle = calle;
this.numero = numero;
this.ciudad = ciudad;
}
public static Guia[]? get_all_guias(Database conn) {
var res = conn.exec ("
SELECT G.rut_guia, G.nombre_guia, G.calle, G.numero,
C.id_ciudad, C.nombre_ciudad,
R.id_region, R.nombre_region
FROM guia G
JOIN ciudad C ON (G.id_ciudad = C.id_ciudad)
JOIN region R ON (C.id_region = R.id_region)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Guia[] guias = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var guia = new Guia (wra.get_string_n (i, "rut_guia"),
wra.get_string_n (i, "nombre_guia"),
wra.get_string_n (i, "calle"),
wra.get_int_n (i, "numero"),
new Ciudad (wra.get_int_n (i, "id_ciudad"),
wra.get_string_n (i, "nombre_ciudad"),
new Region (wra.get_int_n (i, "id_region"),
wra.get_string_n (i, "nombre_region")
)
)
);
guias += guia;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return guias;
}
}
}
}

69
lib/db/lugar.vala Normal file
View File

@@ -0,0 +1,69 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Lugar : GLib.Object {
public uint id_lugar { get; set; default = 0; }
public string nombre_lugar { get; set; default = ""; }
public uint valor_entrada { get; set; default = 0; }
public uint nivel { get; set; default = 0; }
public Ciudad ciudad { get; set; default = null; }
public Lugar (uint id_lugar = 0, string nombre_lugar = "", uint valor_entrada = 0, uint nivel = 0, Ciudad? ciudad = null) {
this.id_lugar = id_lugar;
this.nombre_lugar = nombre_lugar;
this.valor_entrada = valor_entrada;
this.nivel = nivel;
this.ciudad = ciudad;
}
public static Lugar[]? get_all_lugares(Database conn) {
var res = conn.exec ("
SELECT L.id_lugar, L.nombre_lugar, L.valor_entrada, L.nivel,
C.id_ciudad, C.nombre_ciudad,
R.id_region, R.nombre_region
FROM lugar L
JOIN ciudad C ON (L.id_ciudad = C.id_ciudad)
JOIN region R ON (C.id_region = R.id_region)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Lugar[] lugares = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var lugar = new Lugar (wra.get_int_n (i, "id_lugar"),
wra.get_string_n (i, "nombre_lugar"),
wra.get_int_n (i, "valor_entrada"),
wra.get_int_n (i, "nivel"),
new Ciudad (wra.get_int_n (i, "id_ciudad"),
wra.get_string_n (i, "nombre_ciudad"),
new Region(wra.get_int_n (i, "id_region"),
wra.get_string_n (i, "nombre_region")
)
)
);
lugares += lugar;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return lugares;
}
}
}
}

90
lib/db/participa.vala Normal file
View File

@@ -0,0 +1,90 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Participa : GLib.Object {
public Tour tour { get; set; default = null; }
public Guia guia { get; set; default = null; }
public Categoria categoria { get; set; default = null; }
public Participa (Tour? tour = null, Guia? guia = null, Categoria? categoria = null) {
this.tour = tour;
this.guia = guia;
this.categoria = categoria;
}
public static Participa[]? get_all_participas(Database conn) {
var res = conn.exec ("
SELECT T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
C.id_ciudad, C.nombre_ciudad,
R.id_region, R.nombre_region,
G.rut_guia, G.nombre_guia, G.calle, G.numero,
C2.id_ciudad AS id_ciudad_guia, C2.nombre_ciudad AS nombre_ciudad_guia,
R2.id_region AS id_region_guia, R2.nombre_region AS nombre_region_guia,
C3.id_categoria, C3.descripcion_categoria
FROM participa P
JOIN tour T ON (P.id_tour = T.id_tour)
JOIN ciudad C ON (T.id_ciudad = C.id_ciudad)
JOIN region R on (C.id_region = R.id_region)
JOIN guia G on (P.rut_guia = G.rut_guia)
JOIN ciudad C2 ON (G.id_ciudad = C2.id_ciudad)
JOIN region R2 ON (C2.id_region = R2.id_region)
JOIN categoria C3 ON (P.id_categoria = C3.id_categoria)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper(res);
Participa[] participas = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var participa = new Participa (
new Tour (wra.get_int_n (i, "id_tour"),
wra.get_string_n (i, "nombre_tour"),
wra.get_int_n (i, "costo_indiv"),
wra.get_int_n (i, "costo_grupal"),
wra.get_int_n (i, "minima_personas"),
new Ciudad (wra.get_int_n (i, "id_ciudad"),
wra.get_string_n (i, "nombre_ciudad"),
new Region (wra.get_int_n (i, "id_region"),
wra.get_string_n (i, "nombre_region")
)
)
),
new Guia (wra.get_string_n (i, "rut_guia"),
wra.get_string_n (i, "nombre_guia"),
wra.get_string_n (i, "calle"),
wra.get_int_n (i, "numero"),
new Ciudad (wra.get_int_n (i, "id_ciudad_guia"),
wra.get_string_n (i, "id_region_guia"),
new Region (wra.get_int_n (i, "id_region_guia"),
wra.get_string_n (i, "nombre_region_guia"))
)
),
new Categoria (wra.get_int_n (i, "id_categoria"),
wra.get_string_n (i, "descripcion_categoria")
)
);
participas += participa;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return participas;
}
}
}
}

74
lib/db/posee.vala Normal file
View File

@@ -0,0 +1,74 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Posee : GLib.Object {
public uint nivel_especialidad { get; set; default = 0; }
public Guia guia { get; set; default = null; }
public Especialidad especialidad { get; set; default = null; }
public Posee (uint nivel_especialidad = 0, Guia? guia = null, Especialidad? especialidad = null) {
this.nivel_especialidad = nivel_especialidad;
this.guia = guia;
this.especialidad = especialidad;
}
public static Posee[]? get_all_posees(Database conn) {
var res = conn.exec ("
SELECT P.nivel_especialidad,
G.rut_guia, G.nombre_guia, G.calle, G.numero,
C.id_ciudad, C.nombre_ciudad,
R.id_region, R.nombre_region,
E.id_especialidad, E.descripcion_especialidad
FROM posee P
JOIN guia G on (P.rut_guia = G.rut_guia)
JOIN ciudad C ON (G.id_ciudad = C.id_ciudad)
JOIN region R ON (C.id_region = R.id_region)
JOIN especialidad E ON (P.id_especialidad = E.id_especialidad)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Posee[] posees = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var posee = new Posee (wra.get_int_n (i, "nivel_especialidad"),
new Guia (wra.get_string_n (i, "rut_guia"),
wra.get_string_n (i, "nombre_guia"),
wra.get_string_n (i, "calle"),
wra.get_int_n (i, "numero"),
new Ciudad (wra.get_int_n (i, "id_ciudad"),
wra.get_string_n (i, "nombre_ciudad"),
new Region (wra.get_int_n (i, "id_region"),
wra.get_string_n (i, "nombre_region")
)
)
),
new Especialidad (wra.get_int_n (i, "id_especialidad"),
wra.get_string_n (i, "descripcion_especialidad")
)
);
posees += posee;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return posees;
}
}
}
}

87
lib/db/realiza.vala Normal file
View File

@@ -0,0 +1,87 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Realiza : GLib.Object {
public Tour tour { get; set; default = null; }
public Turista turista { get; set; default = null; }
public Descuento descuento { get; set; default = null; }
public Realiza (Tour? tour = null, Turista? turista = null, Descuento? descuento = null) {
this.tour = tour;
this.turista = turista;
this.descuento = descuento;
}
public static Realiza[]? get_all_realizas(Database conn) {
var res = conn.exec ("
SELECT T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
C.id_ciudad, C.nombre_ciudad,
R.id_region, R.nombre_region,
T2.rut_turista, T2.nombre_turista, T2.fecha_nacimento,
C2.id_contacto, C2.telefono_emergencia, C2.nombre_emergencia,
D.id_descuento, D.descripcion_descuento, D.porcentaje
FROM realiza RE
JOIN tour T ON (RE.id_tour = T.id_tour)
JOIN ciudad C ON (T.id_ciudad = C.id_ciudad)
JOIN region R ON (C.id_region = R.id_region)
JOIN turista T2 ON (RE.rut_turista = T2.rut_turista)
JOIN contacto_emergencia C2 ON (T2.id_contacto = C2.id_contacto)
JOIN descuento D ON (RE.id_descuento = D.id_descuento)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Realiza[] realizas = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var realiza = new Realiza (
new Tour (wra.get_int_n (i, "id_tour"),
wra.get_string_n (i, "nombre_tour"),
wra.get_int_n (i, "costo_indiv"),
wra.get_int_n (i, "costo_grupal"),
wra.get_int_n (i, "minima_personas"),
new Ciudad (wra.get_int_n (i, "id_ciudad"),
wra.get_string_n (i, "nombre_ciudad"),
new Region (wra.get_int_n (i, "id_region"),
wra.get_string_n (i, "nombre_region")
)
)
),
new Turista (wra.get_string_n (i, "rut_turista"),
wra.get_string_n (i, "nombre_turista"),
wra.get_string_n (i, "fecha_nacimento"),
new ContactoEmergencia (wra.get_int_n (i, "id_contacto"),
wra.get_int_n (i, "telefono_emergencia"),
wra.get_string_n (i, "nombre_emergencia")
)
),
new Descuento (wra.get_int_n (i, "id_descuento"),
wra.get_string_n (i, "descripcion_descuento"),
wra.get_float_n (i, "porcentaje")
)
);
realizas += realiza;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return realizas;
}
}
}
}

50
lib/db/region.vala Normal file
View File

@@ -0,0 +1,50 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Region : GLib.Object {
public uint id_region { get; set; default = 0; }
public string nombre_region { get; set; default = ""; }
public Region (uint id_region = 0, string nombre_region = "") {
this.id_region = id_region;
this.nombre_region = nombre_region;
}
public static Region[]? get_all_regiones(Database conn) {
var res = conn.exec ("
SELECT id_region, nombre_region FROM region
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Region[] regiones = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var region = new Region (wra.get_int_n (i, "id_region"),
wra.get_string_n (i, "nombre_region")
);
regiones += region;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return regiones;
}
}
}
}

77
lib/db/requerir_auto.vala Normal file
View File

@@ -0,0 +1,77 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class RequerirAuto : GLib.Object {
public string chofer { get; set; default = ""; }
public Tour tour { get; set; default = null; }
public Vehiculo vehiculo { get; set; default = null; }
public RequerirAuto (string chofer = "", Tour? tour = null, Vehiculo? vehiculo = null) {
this.chofer = chofer;
this.tour = tour;
this.vehiculo = vehiculo;
}
public static RequerirAuto[]? get_all_requerir_autos(Database conn) {
var res = conn.exec ("
SELECT R.chofer,
T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
C.id_ciudad, C.nombre_ciudad,
R2.id_region, R2.nombre_region,
V.patente, V.ano_vehiculo, V.marca, V.capacidad
FROM requerir_auto R
JOIN tour T ON (R.id_tour = T.id_tour)
JOIN ciudad C ON (T.id_ciudad = C.id_ciudad)
JOIN region R2 ON (C.id_region = R2.id_region)
Join vehiculo V ON (R.patente = V.patente)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper(res);
RequerirAuto[] requerir_autos = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var requerir_auto = new RequerirAuto (wra.get_string_n (i, "chofer"),
new Tour (wra.get_int_n (i, "id_tour"),
wra.get_string_n (i, "nombre_tour"),
wra.get_int_n (i, "costo_indiv"),
wra.get_int_n (i, "costo_grupal"),
wra.get_int_n (i, "minima_personas"),
new Ciudad (wra.get_int_n (i, "id_ciudad"),
wra.get_string_n (i, "nombre_ciudad"),
new Region (wra.get_int_n (i, "id_region"),
wra.get_string_n (i, "nombre_region")
)
)
),
new Vehiculo (wra.get_string_n (i, "patente"),
wra.get_int_n (i, "ano_vehiculo"),
wra.get_string_n (i, "marca"),
wra.get_int_n (i, "capacidad")
)
);
requerir_autos += requerir_auto;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return requerir_autos;
}
}
}
}

View File

@@ -0,0 +1,66 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class TieneEnfermedad : GLib.Object {
public Turista turista { get; set; default = null; }
public Enfermedad enfermedad { get; set; default = null; }
public TieneEnfermedad (Turista? turista = null, Enfermedad? enfermedad = null) {
this.turista = turista;
this.enfermedad = enfermedad;
}
public static TieneEnfermedad[]? get_all_tiene_enfermedades(Database conn) {
var res = conn.exec ("
SELECT T.rut_turista, T.nombre_turista, T.fecha_nacimento,
C.id_contacto, C.telefono_emergencia, C.nombre_emergencia,
E.id_enfermedad, E.descripcion_enfermedad
FROM tiene_enfermedad TE
JOIN turista T ON (TE.rut_turista = T.rut_turista)
JOIN contacto_emergencia C ON (T.id_contacto = C.id_contacto)
JOIN enfermedad E ON (TE.id_enfermedad = E.id_enfermedad)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
TieneEnfermedad[] tiene_enfermedades = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var tiene_enfermedad = new TieneEnfermedad (
new Turista (wra.get_string_n (i, "rut_turista"),
wra.get_string_n (i, "nombre_turista"),
wra.get_string_n (i, "fecha_nacimento"),
new ContactoEmergencia (wra.get_int_n (i, "id_contacto"),
wra.get_int_n (i, "telefono_emergencia"),
wra.get_string_n (i, "nombre_emergencia")
)
),
new Enfermedad (wra.get_int_n (i, "id_enfermedad"),
wra.get_string_n (i, "descripcion_enfermedad")
)
);
tiene_enfermedades += tiene_enfermedad;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return tiene_enfermedades;
}
}
}
}

72
lib/db/tour.vala Normal file
View File

@@ -0,0 +1,72 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Tour : GLib.Object {
public uint id_tour { get; set; default = 0; }
public string nombre_tour { get; set; default = ""; }
public uint costo_indiv { get; set; default = 0; }
public uint costo_grupal { get; set; default = 0; }
public uint minima_personas { get; set; default = 0; }
public Ciudad ciudad { get; set; default = null; }
public Tour (uint id_tour = 0, string nombre_tour = "", uint costo_indiv = 0, uint costo_grupal = 0, uint minima_personas = 0, Ciudad? ciudad = null) {
this.id_tour = id_tour;
this.nombre_tour = nombre_tour;
this.costo_indiv = costo_indiv;
this.costo_grupal = costo_grupal;
this.minima_personas = minima_personas;
this.ciudad = ciudad;
}
public static Tour[]? get_all_tours(Database conn) {
var res = conn.exec ("
SELECT T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
C.id_ciudad, C.nombre_ciudad,
R.id_region, R.nombre_region
FROM tour T
JOIN ciudad C ON (T.id_ciudad = C.id_ciudad)
JOIN region R ON (C.id_region = R.id_region)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Tour[] tours = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var tour = new Tour (wra.get_int_n (i, "id_tour"),
wra.get_string_n (i, "nombre_tour"),
wra.get_int_n (i, "costo_indiv"),
wra.get_int_n (i, "costo_grupal"),
wra.get_int_n (i, "minima_personas"),
new Ciudad (wra.get_int_n (i, "id_ciudad"),
wra.get_string_n (i, "nombre_ciudad"),
new Region (wra.get_int_n (i, "id_region"),
wra.get_string_n (i, "nombre_region")
)
)
);
tours += tour;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return tours;
}
}
}
}

62
lib/db/turista.vala Normal file
View File

@@ -0,0 +1,62 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Turista : GLib.Object {
public string rut_turista { get; set; default = ""; }
public string nombre_turista { get; set; default = ""; }
public string fecha_nacimento { get; set; default = ""; }
public ContactoEmergencia contacto_emergencia { get; set; default = null; }
public Turista (string rut_turista = "", string nombre_turista = "", string fecha_nacimento = "", ContactoEmergencia? contacto_emergencia = null) {
this.rut_turista = rut_turista;
this.nombre_turista = nombre_turista;
this.fecha_nacimento = fecha_nacimento;
this.contacto_emergencia = contacto_emergencia;
}
public static Turista[]? get_all_turistas(Database conn) {
var res = conn.exec ("
SELECT T.rut_turista, T.nombre_turista, T.fecha_nacimento,
C.id_contacto, C.telefono_emergencia, C.nombre_emergencia
FROM turista T
JOIN contacto_emergencia C ON (T.id_contacto = C.id_contacto)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Turista[] turistas = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var turista = new Turista (wra.get_string_n (i, "rut_turista"),
wra.get_string_n (i, "nombre_turista"),
wra.get_string_n (i, "fecha_nacimento"),
new ContactoEmergencia (wra.get_int_n (i, "id_contacto"),
wra.get_int_n (i, "telefono_emergencia"),
wra.get_string_n (i, "nombre_emergencia")
)
);
turistas += turista;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return turistas;
}
}
}
}

56
lib/db/vehiculo.vala Normal file
View File

@@ -0,0 +1,56 @@
namespace Sernatur {
namespace DB {
using Postgres;
using Wrapper;
public class Vehiculo : GLib.Object {
public string patente { get; set; default = ""; }
public uint ano_vehiculo { get; set; default = 0; }
public string marca { get; set; default = ""; }
public uint capacidad { get; set; default = 0; }
public Vehiculo (string patente = "", uint ano_vehiculo = 0, string marca = "", uint capacidad = 0) {
this.patente = patente;
this.ano_vehiculo = ano_vehiculo;
this.marca = marca;
this.capacidad = capacidad;
}
public static Vehiculo[]? get_all_vehiculos(Database conn) {
var res = conn.exec ("
SELECT patente, ano_vehiculo, marca, capacidad FROM vehiculo
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Vehiculo[] vehiculos = {};
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var vehiculo = new Vehiculo (wra.get_string_n (i, "patente"),
wra.get_int_n (i, "ano_vehiculo"),
wra.get_string_n (i, "marca"),
wra.get_int_n (i, "capacidad")
);
vehiculos += vehiculo;
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return vehiculos;
}
}
}
}

64
lib/dbwrapper.vala Normal file
View File

@@ -0,0 +1,64 @@
namespace Sernatur {
/**
* This is the database namespace for Sernatur
*/
namespace DB {
/**
* This is a wrapper for Postgres that allows easier use of the database
*/
namespace Wrapper {
using Postgres;
public errordomain Field {
NOTFOUND
}
public class ResultWrapper : GLib.Object {
public unowned Result result { get; set; default = null; }
public ResultWrapper (Result? result = null) {
this.result = result;
}
public string get_string_v (int tup_num, int field_num) {
return result.get_value (tup_num, field_num);
}
public int get_int_v (int tup_num, int field_num) {
return int.parse (result.get_value (tup_num, field_num));
}
public float get_float_v (int tup_num, int field_num) {
return float.parse(result.get_value (tup_num, field_num));
}
public double get_double_v (int tup_num, int field_num) {
return double.parse (result.get_value (tup_num, field_num));
}
public string get_string_n (int tup_num, string name) throws Field {
return get_string_v (tup_num, check_field_found (result.get_field_number (name), name));;
}
public int get_int_n (int tup_num, string name) throws Field {
return get_int_v (tup_num, check_field_found (result.get_field_number (name), name));;
}
public float get_float_n (int tup_num, string name) throws Field {
return get_float_v (tup_num, check_field_found (result.get_field_number (name), name));;
}
public double get_double_n (int tup_num, string name) throws Field {
return get_double_v (tup_num, check_field_found (result.get_field_number (name), name));
}
private int check_field_found (int field_num, string name) throws Field {
if (field_num == -1) {
throw new Field.NOTFOUND (dgettext (null, "The field %s was not found in the query results!"), name);
}
return field_num;
}
}
}
}
}

44
lib/meson.build Normal file
View File

@@ -0,0 +1,44 @@
glib_dep = dependency('glib-2.0')
gobject_dep = dependency('gobject-2.0')
pq_dep = dependency('libpq', version: '>=9.0')
lib_sources = files(
'dbwrapper.vala',
'rut.vala',
'db/arrienda.vala',
'db/asociado.vala',
'db/categoria.vala',
'db/ciudad.vala',
'db/contacto_emergencia.vala',
'db/descuento.vala',
'db/empresa.vala',
'db/enfermedad.vala',
'db/especialidad.vala',
'db/guia.vala',
'db/lugar.vala',
'db/participa.vala',
'db/posee.vala',
'db/realiza.vala',
'db/region.vala',
'db/requerir_auto.vala',
'db/tiene_enfermedad.vala',
'db/tour.vala',
'db/turista.vala',
'db/vehiculo.vala')
vala_args = ['--vapidir='+join_paths(meson.source_root(),'vapi')]
sernatur_lib = library('sernatur',
lib_sources,
vala_header: 'sernatur.h',
vala_vapi: 'sernatur-1.0.0.vapi',
# vala_gir: 'Sernatur-1.0.0.gir',
vala_args: vala_args,
dependencies: [glib_dep, gobject_dep, pq_dep],
version: '1.0.0',
soversion: '1',
install: true,
install_dir: [true, true, true])
pkg_mod = import('pkgconfig')
pkg_mod.generate(sernatur_lib)

139
lib/rut.vala Normal file
View File

@@ -0,0 +1,139 @@
namespace Sernatur {
namespace Person {
public errordomain InvalidRut {
INVALID,
TOOLARGE,
INVALIDVERIFIER
}
public class Rut : GLib.Object {
private string clean_rut;
private string pretty_rut;
private unichar verifier;
public enum Type {
RUN,
RUT
}
public Rut (string rut) throws InvalidRut {
parse (rut);
}
private void parse (string rut) throws InvalidRut {
try {
var regex = new Regex ("^[ ]*([0-9.]{0,11}[\\-]?[0-9kK])?[ ]*$");
if (!regex.match (rut)) {
throw new InvalidRut.INVALID (dgettext (null, "The RUT %s has an invalid character!"), rut);
}
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
try {
var regex = new Regex ("([.-])");
string new_rut = rut.up ();
new_rut = new_rut.strip ();
rut = regex.replace (new_rut, new_rut.length, 0, "");
if (int.parse (rut.substring (0, rut.length - 1)) > 100000000) {
throw new InvalidRut.TOOLARGE (dgettext (null, "The RUT %s is too big!"), rut);
}
this.verifier = rut.get_char (rut.length - 1);
this.clean_rut = rut.substring (0, rut.length - 1);
if (generate_verfifier (this.clean_rut) != this.verifier) {
throw new InvalidRut.INVALIDVERIFIER (dgettext (null, "The verifier %C is invalid!"), this.verifier);
}
pretty();
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
private void pretty () {
string new_rut = "";
string temp_rut = this.clean_rut.reverse ();
int rut_length = this.clean_rut.length;
for (int i = 0; i < rut_length; i++) {
new_rut = new_rut + temp_rut.get_char(i).to_string ();
if ((i + 1) % 3 == 0) {
new_rut = new_rut + ".";
}
}
new_rut = new_rut.reverse ();
this.pretty_rut = new_rut + "-" + this.verifier.to_string ();
}
private unichar generate_verfifier (string rut) {
/**
* 1. Multiply each digit of the RUT by 2, 3, ..., 7, 2, 3, ... from the end of the RUT moving forward.
* 2. Add the partial multiplications.
* 3. Calculate the remainder of the division by 11.
* 4. The verifier is 11 minus the previous result. If the result is 10, change it to K.
*/
int multiplier = 2;
int sum = 0;
int remainder;
int division;
int rut_length = rut.length;
// Steps 1 and 2
for (int i = rut_length - 1; i >= 0; i--) {
sum = sum + (int.parse (rut.substring(i, 1)) * multiplier);
multiplier++;
if (multiplier == 8) {
multiplier = 2;
}
}
// Step 3
division = sum / 11;
division = division * 11;
remainder = sum - division;
// Step 4
if (remainder != 0) {
remainder = 11 - remainder;
}
// Let's return their verifier
if (remainder == 10) {
// Their verifier is 10 so let's return K.
return 'K';
}
else {
return (unichar) remainder + '0';
}
}
public string get_clean_rut () {
return this.clean_rut + this.verifier.to_string ();
}
public string get_rut () {
return this.pretty_rut;
}
public Type type () {
uint rut = int.parse (this.clean_rut);
if (rut < 100000000 && rut > 50000000) {
// Company
return Type.RUT;
}
else {
// Person
return Type.RUN;
}
}
}
}
}