tourist and illness finished

This commit is contained in:
2019-01-21 00:58:30 -03:00
parent eb58d519fb
commit 0044048c7d
26 changed files with 2832 additions and 133 deletions

View File

@@ -94,7 +94,7 @@ namespace LibSernatur {
* @return Returns a list of Ciudad
* @throws PostgresError If there is a problem with with escaping strings
*/
public static List<Ciudad> get_all_ciudades_in_region(Connection conn, uint region_id) throws PostgresError {
public static List<Ciudad> get_all_ciudades_in_region (Connection conn, uint region_id) throws PostgresError {
var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_CITIES_BY_REGION, region_id));
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG

View File

@@ -44,8 +44,9 @@ namespace LibSernatur {
* Get all tuples and fields from database
* @param conn The database connection to use
* @return Returns a list of Enfermedad
* @throws PostgresError If there is a problem with with escaping strings
*/
public static List<Enfermedad> get_all_enfermedades (Connection conn) {
public static List<Enfermedad> get_all_enfermedades (Connection conn) throws PostgresError {
var res = conn.db.exec ("
SELECT id_enfermedad, descripcion_enfermedad FROM enfermedad
");

View File

@@ -94,6 +94,112 @@ JOIN enfermedad E ON (TE.id_enfermedad = E.id_enfermedad)
}
return list;
}
/**
* Get all tuples and fields from database that are connected with a certain RUN
* @param conn The database connection to use
* @param run The RUN to search for
* @return Returns a list of TieneEnfermedad
* @throws PostgresError If there is a problem with with escaping strings
*/
public static List<TieneEnfermedad> get_all_tiene_enfermedades_by_run (Connection conn, string run) throws PostgresError {
var res = conn.db.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)
LEFT JOIN contacto_emergencia C ON (T.id_contacto = C.id_contacto)
JOIN enfermedad E ON (TE.id_enfermedad = E.id_enfermedad)
WHERE (TE.rut_turista = '" + conn.escape (run) + "')
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
return new List<TieneEnfermedad> ();
#endif
}
var wra = new ResultWrapper (res);
List<TieneEnfermedad> list = new List<TieneEnfermedad> ();
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")
)
);
list.append (tiene_enfermedad);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return list;
}
/**
* Insert an illness in the database
* @param conn The database connection
* @param tiene_enfermedad The illness to insert
* @throws PostgresError Thrown if there is a problem with escaping strings
*/
public static void insert_illness (Connection conn, TieneEnfermedad tiene_enfermedad) throws PostgresError {
var res = conn.db.exec ("
INSERT INTO tiene_enfermedad
(rut_turista, id_enfermedad)
VALUES
(
'" + conn.escape (tiene_enfermedad.turista.rut_turista) + "',
" + tiene_enfermedad.enfermedad.id_enfermedad.to_string () + "
)
");
if (res.get_status () != ExecStatus.COMMAND_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
}
/**
* Delete an illness in the database
* @param conn The database connection
* @param tiene_enfermedad The illness to delete
* @throws PostgresError Thrown if there is a problem with escaping strings
*/
public static void delete_illness (Connection conn, TieneEnfermedad tiene_enfermedad) throws PostgresError {
var res = conn.db.exec ("
DELETE FROM tiene_enfermedad
WHERE (
rut_turista = '" + conn.escape (tiene_enfermedad.turista.rut_turista) + "' AND
id_enfermedad = " + tiene_enfermedad.enfermedad.id_enfermedad.to_string () + "
)
");
if (res.get_status () != ExecStatus.COMMAND_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
}
}
}
}

View File

@@ -71,7 +71,7 @@ namespace LibSernatur {
* @throws PostgresError If there is a problem with with escaping strings
*/
public static List<Tour> get_all_tours (Connection conn) throws PostgresError {
var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_TOURS, null));
var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_TOURS));
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());

View File

@@ -22,15 +22,15 @@ namespace LibSernatur {
*/
public class Turista : Object {
/**
* The RUT of the tourist
* The RUT of the touristist
*/
public string rut_turista { get; set; default = ""; }
/**
* The tourist name
* The touristist name
*/
public string nombre_turista { get; set; default = ""; }
/**
* The birth date of the tourist
* The birth date of the touristist
*/
public string fecha_nacimento { get; set; default = ""; }
/**
@@ -40,9 +40,9 @@ namespace LibSernatur {
/**
* Initilize the Turista class
* @param rut_turista The RUT of the tourist
* @param nombre_turista The tourist name
* @param fecha_nacimento The birth date of the tourist
* @param rut_turista The RUT of the touristist
* @param nombre_turista The touristist name
* @param fecha_nacimento The birth date of the touristist
* @param contacto_emergencia The emergency contact
*/
public Turista (string rut_turista = "", string nombre_turista = "", string fecha_nacimento = "", ContactoEmergencia? contacto_emergencia = null) {
@@ -56,14 +56,10 @@ namespace LibSernatur {
* Get all tuples and fields from database
* @param conn The database connection to use
* @return Returns a list of Turista
* @throws PostgresError If there is a problem with with escaping strings
*/
public static List<Turista> get_all_turistas (Connection conn) {
var res = conn.db.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)
");
public static List<Turista> get_all_turistas (Connection conn) throws PostgresError {
var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_TOURISTS));
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
@@ -98,6 +94,114 @@ JOIN contacto_emergencia C ON (T.id_contacto = C.id_contacto)
}
return list;
}
/**
* Get a single tuple and field from database
* @param conn The database connection to use
* @return Returns a Turista
* @throws PostgresError If there is a problem with with escaping strings
*/
public static Turista? get_turista_by_run (Connection conn, string run) throws PostgresError {
var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_TOURIST_BY_RUN, run));
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
return null;
#endif
}
var wra = new ResultWrapper (res);
Turista turista = null;
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
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")
)
);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return turista;
}
/**
* Update a tourist in the database
* @param conn The database connection
* @param tourist The tourist to update
* @throws PostgresError Thrown if there is a problem with escaping strings
* @throws DBError Thrown if an invalid value is passed
*/
public static void update_tourist (Connection conn, Turista tourist, string original_run) throws PostgresError, DBError {
if (tourist.rut_turista.strip () == "") {
throw new DBError.INVALID_VALUE (_ ("The RUN of the tourist is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.UPDATE_TOURIST, tourist, original_run));
if (res.get_status () != ExecStatus.COMMAND_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
}
/**
* Insert a tourist in the database
* @param conn The database connection
* @param tourist The tourist to insert
* @throws PostgresError Thrown if there is a problem with escaping strings
* @throws DBError Thrown if an invalid value is passed
*/
public static void insert_tourist (Connection conn, Turista tourist) throws PostgresError, DBError {
if (tourist.rut_turista.strip () == "") {
throw new DBError.INVALID_VALUE (_ ("The RUN of the tourist is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.INSERT_TOURIST, tourist));
if (res.get_status () != ExecStatus.COMMAND_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
}
/**
* Delete a tourist in the database
* @param conn The database connection
* @param tourist The tourist to delete
* @throws PostgresError Thrown if there is a problem with escaping strings
* @throws DBError Thrown if an invalid value is passed
*/
public static void delete_tourist (Connection conn, Turista tourist) throws PostgresError, DBError {
if (tourist.rut_turista.strip () == "") {
throw new DBError.INVALID_VALUE (_ ("The RUN of the tourist is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.DELETE_TOURIST, tourist));
if (res.get_status () != ExecStatus.COMMAND_OK) {
if (res.get_error_field (FieldCode.SQLSTATE) == "23503") {
throw new DBError.FOREIGN_KEY_CONSTAINT (res.get_error_field (FieldCode.MESSAGE_PRIMARY));
}
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
}
}
}
}

View File

@@ -180,7 +180,7 @@ namespace LibSernatur {
throw new InvalidRut.INVALID (_ ("The RUT %s has an invalid character!"), rut);
}
}
catch (Error e) {
catch (RegexError e) {
#if DEBUG
error (e.message);
#else
@@ -202,7 +202,7 @@ namespace LibSernatur {
}
pretty();
}
catch (Error e) {
catch (RegexError e) {
#if DEBUG
error (e.message);
#else

View File

@@ -39,10 +39,24 @@ namespace LibSernatur {
SELECT_VEHICLE_REQUIRED_BY_TOUR,
INSERT_VEHICLE_REQUIRED,
UPDATE_VEHICLE_REQUIRED,
DELETE_VEHICLE_REQUIRED
DELETE_VEHICLE_REQUIRED,
SELECT_ALL_TOURISTS,
SELECT_TOURIST_BY_RUN,
INSERT_TOURIST,
UPDATE_TOURIST,
DELETE_TOURIST
}
public static string get_query (Connection conn, Type type, T? t = null) throws PostgresError {
/**
* Return a query to be run
* @param conn The connection which is used for escaping strings
* @param type Which type of query is required
* @param t Generic variable whose use depends on the query
* @param t2 Generic variable whose use depends on the query
* @return Returns a string which contains a query
* @throws PostgresError Thrown if escaping strings fails
*/
public static string get_query (Connection conn, Type type, T? t = null, T? t2 = null) throws PostgresError {
switch (type) {
case SELECT_ALL_CITIES:
return "
@@ -247,7 +261,7 @@ id_tour = " + tour.id_tour.to_string () + "
case INSERT_VEHICLE_REQUIRED:
RequerirAuto requerir_auto = (RequerirAuto) t;
return "
return "
INSERT INTO requerir_auto
(id_tour, patente, chofer)
VALUES
@@ -259,7 +273,7 @@ VALUES
case UPDATE_VEHICLE_REQUIRED:
RequerirAuto requerir_auto = (RequerirAuto) t;
return "
return "
UPDATE requerir_auto SET
patente = '" + conn.escape (requerir_auto.vehiculo.patente) + "',
chofer = '" + conn.escape (requerir_auto.chofer) + "'
@@ -267,10 +281,55 @@ WHERE id_tour = " + requerir_auto.tour.id_tour.to_string ();
case DELETE_VEHICLE_REQUIRED:
Tour tour = (Tour) t;
return "
return "
DELETE FROM requerir_auto
WHERE (
id_tour = " + tour.id_tour.to_string () + "
)";
case SELECT_ALL_TOURISTS:
return "
SELECT T.rut_turista, T.nombre_turista, T.fecha_nacimento,
C.id_contacto, C.telefono_emergencia, C.nombre_emergencia
FROM turista T
LEFT JOIN contacto_emergencia C ON (T.id_contacto = C.id_contacto)";
case SELECT_TOURIST_BY_RUN:
string run = (string) t;
return "
SELECT T.rut_turista, T.nombre_turista, T.fecha_nacimento,
C.id_contacto, C.telefono_emergencia, C.nombre_emergencia
FROM turista T
LEFT JOIN contacto_emergencia C ON (T.id_contacto = C.id_contacto)
WHERE rut_turista = '" + conn.escape (run) + "'";
case INSERT_TOURIST:
Turista turista = (Turista) t;
return "
INSERT INTO turista
(rut_turista, nombre_turista, fecha_nacimento)
VALUES
(
'" + conn.escape (turista.rut_turista) + "'',
'" + conn.escape (turista.nombre_turista) + "',
'" + conn.escape (turista.fecha_nacimento) + "'
)";
case UPDATE_TOURIST:
Turista turista = (Turista) t;
return "
UPDATE turista SET
rut_turista = '" + conn.escape (turista.rut_turista) + "',
nombre_turista = '" + conn.escape (turista.nombre_turista) + "',
fecha_nacimento = '" + conn.escape (turista.fecha_nacimento) + "'
WHERE rut_turista = '" + conn.escape ((string) t2) + "'";
case DELETE_TOURIST:
Turista turista = (Turista) t;
return "
DELETE FROM turista
WHERE (
rut_turista = '" + conn.escape (turista.rut_turista) + "'
)";
default: