finish city and region text entry

This commit is contained in:
2019-01-19 17:42:43 -03:00
parent 5bd896b153
commit 9ff7178ed5
12 changed files with 460 additions and 252 deletions

View File

@@ -50,24 +50,10 @@ namespace LibSernatur {
* Get all tuples and fields from database
* @param conn The database connection to use
* @return Returns a list of Asociado
* @throws PostgresError Thrown if there is a problem with escaping strings
*/
public static List<Asociado> get_all_asociados (Connection conn) {
var res = conn.db.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_ciudad = 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)
");
public static List<Asociado> get_all_asociados (Connection conn) throws PostgresError {
var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_ASSOCIATED, null));
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
@@ -132,7 +118,7 @@ JOIN region R2 ON (C2.id_region = R2.id_region)
*/
public static List<Asociado> get_all_asociados_by_tour (Connection conn, Tour tour) throws PostgresError, DBError {
if (tour.id_tour == 0) {
throw new DBError.INVALID_VALUE (dgettext (null, "The id of the tour is invalid!"));
throw new DBError.INVALID_VALUE (_ ("The id of the tour is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_ASSOCIATED_BY_TOUR, tour));
if (res.get_status () != ExecStatus.TUPLES_OK) {

View File

@@ -50,14 +50,10 @@ namespace LibSernatur {
* Get all tuples and fields from database
* @param conn The database connection to use
* @return Returns a list of Ciudad
* @throws PostgresError If there is a problem with with escaping strings
*/
public static List<Ciudad> get_all_ciudades (Connection conn) {
var res = conn.db.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)
");
public static List<Ciudad> get_all_ciudades (Connection conn) throws PostgresError {
var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_CITIES, null));
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
@@ -96,15 +92,10 @@ JOIN region R ON (C.id_region = R.id_region)
* @param conn The database connection to use
* @param region_id The id of the region to filter that results
* @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) {
var res = conn.db.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)
WHERE (R.id_region = " + region_id.to_string () + ")
");
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
error (conn.db.get_error_message ());
@@ -137,6 +128,42 @@ WHERE (R.id_region = " + region_id.to_string () + ")
}
return list;
}
/**
* Insert a city in the database
* @param conn The database connection
* @param ciudad The city to insert
* @return Returns the id of the tuple inserted
* @throws PostgresError Thrown if there is a problem with escaping strings
* @throws DBError Thrown if an invalid value is passed
*/
public static uint insert_city (Connection conn, Ciudad ciudad) throws PostgresError, DBError {
if (ciudad.id_ciudad != 0) {
throw new DBError.INVALID_VALUE (_ ("The id of the city is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.INSERT_CITY, ciudad));
// This uses TUPLES_OK because it returns a result which is the id of the inserted city
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
var wra = new ResultWrapper (res);
try {
return wra.get_int_n (0, "id_ciudad");
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
return 0;
#endif
}
}
}
}
}

View File

@@ -44,11 +44,10 @@ namespace LibSernatur {
* Get all tuples and fields from database
* @param conn The database connection to use
* @return Returns a list of Region
* @throws PostgresError If there is a problem with with escaping strings
*/
public static List<Region> get_all_regiones (Connection conn) {
var res = conn.db.exec ("
SELECT id_region, nombre_region FROM region
");
public static List<Region> get_all_regiones (Connection conn) throws PostgresError {
var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_REGIONS, null));
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
@@ -78,6 +77,42 @@ SELECT id_region, nombre_region FROM region
}
return list;
}
/**
* Insert a region in the database
* @param conn The database connection
* @param region The region to insert
* @return Returns the id of the tuple inserted
* @throws PostgresError Thrown if there is a problem with escaping strings
* @throws DBError Thrown if an invalid value is passed
*/
public static uint insert_region (Connection conn, Region region) throws PostgresError, DBError {
if (region.id_region != 0) {
throw new DBError.INVALID_VALUE (_ ("The id of the region is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.INSERT_REGION, region));
// This uses TUPLES_OK because it returns a result which is the id of the inserted region
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
var wra = new ResultWrapper (res);
try {
return wra.get_int_n (0, "id_region");
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
return 0;
#endif
}
}
}
}
}

View File

@@ -120,7 +120,7 @@ namespace LibSernatur {
*/
public static void update_tour (Connection conn, Tour tour) throws PostgresError, DBError {
if (tour.id_tour == 0) {
throw new DBError.INVALID_VALUE (dgettext (null, "The id of the tour is invalid!"));
throw new DBError.INVALID_VALUE (_ ("The id of the tour is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.UPDATE_TOUR, tour));
if (res.get_status () != ExecStatus.COMMAND_OK) {
@@ -142,10 +142,10 @@ namespace LibSernatur {
*/
public static uint insert_tour (Connection conn, Tour tour) throws PostgresError, DBError {
if (tour.id_tour != 0) {
throw new DBError.INVALID_VALUE (dgettext (null, "The id of the tour is invalid!"));
throw new DBError.INVALID_VALUE (_ ("The id of the tour is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.INSERT_TOUR, tour));
// This uses TUPLES_OK because it returns a result which is hte id of the inserted tour
// This uses TUPLES_OK because it returns a result which is the id of the inserted tour
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
@@ -177,7 +177,7 @@ namespace LibSernatur {
*/
public static void delete_tour (Connection conn, Tour tour) throws PostgresError, DBError {
if (tour.id_tour == 0) {
throw new DBError.INVALID_VALUE (dgettext (null, "The id of the tour is invalid!"));
throw new DBError.INVALID_VALUE (_ ("The id of the tour is invalid!"));
}
var res = conn.db.exec (Query.get_query (conn, Query.Type.DELETE_TOUR, tour));
if (res.get_status () != ExecStatus.COMMAND_OK) {

View File

@@ -19,15 +19,64 @@ namespace LibSernatur {
*/
public class Query < T > : Object {
public enum Type {
SELECT_ALL_CITIES,
SELECT_ALL_CITIES_BY_REGION,
INSERT_CITY,
SELECT_ALL_REGIONS,
INSERT_REGION,
SELECT_ALL_TOURS,
INSERT_TOUR,
UPDATE_TOUR,
DELETE_TOUR,
SELECT_ALL_ASSOCIATED,
SELECT_ALL_ASSOCIATED_BY_TOUR
}
public static string get_query (Connection conn, Type type, T? t) throws PostgresError {
switch (type) {
case SELECT_ALL_CITIES:
return "
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)";
case SELECT_ALL_CITIES_BY_REGION:
uint region_id = (uint) t;
return "
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)
WHERE (R.id_region = " + region_id.to_string () + ")";
case INSERT_CITY:
Ciudad ciudad = (Ciudad) t;
return "
INSERT INTO ciudad
(nombre_ciudad, id_region)
VALUES
(
'" + conn.escape (ciudad.nombre_ciudad) + "',
" + ciudad.region.id_region.to_string () + "
)
RETURNING id_ciudad";
case SELECT_ALL_REGIONS:
return "
SELECT id_region, nombre_region FROM region";
case INSERT_REGION:
Region region = (Region) t;
return "
INSERT INTO region
(nombre_region)
VALUES
(
'" + conn.escape (region.nombre_region) + "'
)
RETURNING id_region";
case SELECT_ALL_TOURS:
return "
SELECT T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
@@ -36,6 +85,7 @@ 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)";
case UPDATE_TOUR:
Tour tour = (Tour) t;
return "
@@ -46,6 +96,7 @@ UPDATE tour SET
minima_personas = " + tour.minima_personas.to_string () + ",
id_ciudad = " + tour.ciudad.id_ciudad.to_string () + "
WHERE id_tour = " + tour.id_tour.to_string ();
case INSERT_TOUR:
Tour tour = (Tour) t;
return "
@@ -60,11 +111,30 @@ VALUES
" + tour.ciudad.id_ciudad.to_string () + "
)
RETURNING id_tour";
case DELETE_TOUR:
Tour tour = (Tour) t;
return "
DELETE FROM tour
WHERE id_tour = " + tour.id_tour.to_string ();
case SELECT_ALL_ASSOCIATED:
return "
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_ciudad = 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)";
case SELECT_ALL_ASSOCIATED_BY_TOUR:
Tour tour = (Tour) t;
return "
@@ -83,6 +153,7 @@ 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)
WHERE t.id_tour = " + tour.id_tour.to_string ();
default:
return "";
}