finish assignment for vehicles

This commit is contained in:
2019-01-20 18:09:16 -03:00
parent e6a7d9e1a7
commit eb58d519fb
10 changed files with 667 additions and 220 deletions

View File

@@ -50,20 +50,10 @@ namespace LibSernatur {
* Get all tuples and fields from database
* @param conn The database connection to use
* @return Returns a list of RequerirAuto
* @throws PostgresError If there is a problem with with escaping strings
*/
public static List<RequerirAuto> get_all_requerir_autos (Connection conn) {
var res = conn.db.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)
");
public static List<RequerirAuto> get_all_requerir_autos (Connection conn) throws PostgresError {
var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_VEHICLE_REQUIRED));
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
@@ -109,6 +99,114 @@ Join vehiculo V ON (R.patente = V.patente)
}
return list;
}
/**
* Get 1 tuple from the database based on tour id
* @param conn The database connection to use
* @return Returns a RequerirAuto
* @throws PostgresError If there is a problem with with escaping strings
*/
public static RequerirAuto? get_requerir_auto_by_tour (Connection conn, Tour tour) throws PostgresError {
var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_VEHICLE_REQUIRED_BY_TOUR, tour));
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
}
RequerirAuto requerir_auto = null;
var wra = new ResultWrapper(res);
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
requerir_auto = new RequerirAuto (wra.get_string_n (i, "chofer"),
tour,
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")
)
);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return requerir_auto;
}
/**
* Insert a require auto object in the database
* @param conn The database connection
* @param requerir_auto The require auto 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_require_vehicle (Connection conn, RequerirAuto requerir_auto) throws PostgresError, DBError {
if (requerir_auto.tour.id_tour == 0 || requerir_auto.vehiculo.patente.strip () == "") {
throw new DBError.INVALID_VALUE (_ ("The id of the tour or the license plate for the vehicle is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.INSERT_VEHICLE_REQUIRED, requerir_auto));
if (res.get_status () != ExecStatus.COMMAND_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
}
/**
* Update a require auto object in the database
* @param conn The database connection
* @param requerir_auto The require vehicle 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_require_vehicle (Connection conn, RequerirAuto requerir_auto) throws PostgresError, DBError {
if (requerir_auto.tour.id_tour == 0 || requerir_auto.vehiculo.patente.strip () == "") {
throw new DBError.INVALID_VALUE (_ ("The id of the tour or the license plate for the vehicle is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.UPDATE_VEHICLE_REQUIRED, requerir_auto));
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 required vehicle in the database
* @param conn The database connection
* @param tour The assigned tour used 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_require_vehicle (Connection conn, Tour tour) throws PostgresError, DBError {
if (tour.id_tour == 0) {
throw new DBError.INVALID_VALUE (_ ("The id of the tour is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.DELETE_VEHICLE_REQUIRED, tour));
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

@@ -34,10 +34,15 @@ namespace LibSernatur {
SELECT_ALL_ASSOCIATED,
SELECT_ALL_ASSOCIATED_BY_TOUR,
INSERT_ASSOCIATED,
DELETE_ASSOCIATED
DELETE_ASSOCIATED,
SELECT_ALL_VEHICLE_REQUIRED,
SELECT_VEHICLE_REQUIRED_BY_TOUR,
INSERT_VEHICLE_REQUIRED,
UPDATE_VEHICLE_REQUIRED,
DELETE_VEHICLE_REQUIRED
}
public static string get_query (Connection conn, Type type, T? t) throws PostgresError {
public static string get_query (Connection conn, Type type, T? t = null) throws PostgresError {
switch (type) {
case SELECT_ALL_CITIES:
return "
@@ -214,6 +219,58 @@ DELETE FROM associated
WHERE (
id_tour = " + asociado.tour.id_tour.to_string () + " AND
id_lugar = " + asociado.lugar.id_lugar.to_string () + "
)";
case SELECT_ALL_VEHICLE_REQUIRED:
return "
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)";
case SELECT_VEHICLE_REQUIRED_BY_TOUR:
Tour tour = (Tour) t;
return "
SELECT R.chofer,
V.patente, V.ano_vehiculo, V.marca, V.capacidad
FROM requerir_auto R
JOIN vehiculo V ON (R.patente = V.patente)
WHERE (
id_tour = " + tour.id_tour.to_string () + "
)";
case INSERT_VEHICLE_REQUIRED:
RequerirAuto requerir_auto = (RequerirAuto) t;
return "
INSERT INTO requerir_auto
(id_tour, patente, chofer)
VALUES
(
" + requerir_auto.tour.id_tour.to_string () + ",
'" + conn.escape (requerir_auto.vehiculo.patente) + "',
'" + conn.escape (requerir_auto.chofer) + "'
)";
case UPDATE_VEHICLE_REQUIRED:
RequerirAuto requerir_auto = (RequerirAuto) t;
return "
UPDATE requerir_auto SET
patente = '" + conn.escape (requerir_auto.vehiculo.patente) + "',
chofer = '" + conn.escape (requerir_auto.chofer) + "'
WHERE id_tour = " + requerir_auto.tour.id_tour.to_string ();
case DELETE_VEHICLE_REQUIRED:
Tour tour = (Tour) t;
return "
DELETE FROM requerir_auto
WHERE (
id_tour = " + tour.id_tour.to_string () + "
)";
default: