a lot of changes

This commit is contained in:
2019-01-13 19:53:13 -03:00
parent c1cb9c5c00
commit f1c5b4e0a9
27 changed files with 2432 additions and 451 deletions

71
lib/db.vala Normal file
View File

@@ -0,0 +1,71 @@
/*
* Copyright 2018-2019 Chris Cromer
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* The sernatur library
*/
namespace LibSernatur {
/**
* Sernatur database library
*/
namespace DB {
using Postgres;
/**
* The errors that can be thrown by Postgresql
*/
public errordomain PostgresError {
/**
* Connection error
*/
CONNECT
}
public errordomain DBError {
/**
* Invalid value
*/
INVALID_VALUE
}
/**
* Class to handle database connections
*/
public class Connection : GLib.Object {
/**
* The database connection
*/
public Database db;
/**
* Initialize the connection
* @param host The hostname
* @param port The port
* @param options The extra options to use
* @param tty The terminal to send output to, this is ignored
* @param database The database
* @param username The username
* @param password The password
* @throws PostgresError The error thrown when connection fails
*/
public Connection (string host, string port, string options, string tty, string database, string username, string password) throws PostgresError {
db = set_db_login (host, port, options, tty, database, username, password);
if (db.get_status () != Postgres.ConnectionStatus.OK) {
throw new PostgresError.CONNECT (db.get_error_message ());
}
GLib.print (dgettext (null, "Postgresql server version:") + " %d\n", db.get_server_version ());
}
}
}
}

View File

@@ -90,6 +90,53 @@ JOIN region R ON (C.id_region = R.id_region)
}
return list;
}
/**
* Get all tuples and fields from database that are within a certain 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
*/
public static List<Ciudad> get_all_ciudades_in_region(Database conn, uint region_id) {
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)
WHERE (R.id_region = " + region_id.to_string () + ")
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return new List<Ciudad> ();
#endif
}
var wra = new ResultWrapper (res);
List<Ciudad> list = new List<Ciudad> ();
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")
)
);
list.append (ciudad);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return list;
}
}
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2018-2019 Chris Cromer
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace LibSernatur {
namespace DB {
using Postgres;
using Wrapper;
/**
* The Region class based on the database table
*/
public class RegionesSinDescuento : GLib.Object {
/**
* The name of the region
*/
public string nombre_region { get; set; default = ""; }
/**
* The quanity
*/
public uint cantidad { get; set; default = 0; }
/**
* Initialize the RegionSinDuscuento class
* @param nombre_region The region name
*/
public RegionesSinDescuento (string nombre_region = "", uint cantidad) {
this.nombre_region = nombre_region;
this.cantidad = cantidad;
}
/**
* Get all tuples and fields from database
* @param conn The database connection to use
* @return Returns a list of RegionesSinDescuento
*/
public static List<RegionesSinDescuento> get_regions(Database conn) {
var res = conn.exec ("
SELECT nombreRegion, cantidad FROM REGIONES_SINDESCUENTO WHERE (cantidad = (SELECT MAX(cantidad) FROM REGIONES_SINDESCUENTO))
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
return new List<RegionesSinDescuento> ();
#endif
}
var wra = new ResultWrapper (res);
List<RegionesSinDescuento> list = new List<RegionesSinDescuento> ();
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var region_sin_descuento = new RegionesSinDescuento (wra.get_string_n (i, "nombreRegion"),
wra.get_int_n (i, "cantidad")
);
list.append (region_sin_descuento);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
return list;
}
}
}
}

View File

@@ -70,14 +70,7 @@ namespace LibSernatur {
* @return Returns a list of Tour
*/
public static List<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)
");
var res = conn.exec (Query.get_query (Query.Type.SELECT_ALL_TOURS, null));
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.get_error_message ());
@@ -116,6 +109,26 @@ JOIN region R ON (C.id_region = R.id_region)
}
return list;
}
/**
* Update a tour in the database
* @param tour The tour to update
* @return Returns true if updated
* @throws DBError Thrown if the data in the object is invalid
*/
public static void update_tour (Database conn, Tour tour) throws DBError {
if (tour.id_tour == 0) {
throw new DBError.INVALID_VALUE (dgettext (null, "The id of the tour is invalid!"));
}
var res = conn.exec (Query.get_query (Query.Type.UPDATE_TOUR, tour));
if (res.get_status () != ExecStatus.COMMAND_OK) {
#if DEBUG
error (conn.get_error_message ());
#else
warning (conn.get_error_message ());
#endif
}
}
}
}
}

View File

@@ -12,13 +12,7 @@
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* The sernatur library
*/
namespace LibSernatur {
/**
* Sernatur database library
*/
namespace DB {
/**
* This is a wrapper for Postgres that allows easier use of the database
@@ -30,6 +24,9 @@ namespace LibSernatur {
* The errors that can be thrown by fields in the database
*/
public errordomain Field {
/**
* Field was not found in table
*/
NOTFOUND
}

View File

@@ -3,8 +3,10 @@ gobject_dep = dependency('gobject-2.0')
pq_dep = dependency('libpq', version: '>=9.0')
lib_sources = files(
'db.vala',
'dbwrapper.vala',
'rut.vala',
'misc.vala',
'queries.vala',
'db/arrienda.vala',
'db/asociado.vala',
'db/categoria.vala',
@@ -20,6 +22,7 @@ lib_sources = files(
'db/posee.vala',
'db/realiza.vala',
'db/region.vala',
'db/regiones_sin_descuento.vala',
'db/requerir_auto.vala',
'db/tiene_enfermedad.vala',
'db/tour.vala',

View File

@@ -16,7 +16,7 @@ namespace LibSernatur {
/**
* The person name space
*/
namespace Person {
namespace Misc {
/**
* The errors that can be thrown by the Rut class
@@ -36,6 +36,56 @@ namespace LibSernatur {
INVALIDVERIFIER
}
public class Money : GLib.Object {
public static string format_int (uint value) {
string money = "";
string temp_money = value.to_string ().reverse ();
int money_length = temp_money.length;
for (int i = 0; i < money_length; i++) {
money = money + temp_money.get_char(i).to_string ();
if ((i + 1) % 3 == 0) {
if (i != money_length - 1) {
money = money + ".";
}
}
}
money = money.reverse ();
return "$" + money;
}
public static string format_uint (uint value) {
string money = "";
string temp_money = value.to_string ().reverse ();
int money_length = temp_money.length;
for (int i = 0; i < money_length; i++) {
money = money + temp_money.get_char(i).to_string ();
if ((i + 1) % 3 == 0) {
if (i != money_length - 1) {
money = money + ".";
}
}
}
money = money.reverse ();
return "$" + money;
}
public static string format_string (string value) {
string money = "";
string temp_money = value.reverse ();
int money_length = temp_money.length;
for (int i = 0; i < money_length; i++) {
money = money + temp_money.get_char(i).to_string ();
if ((i + 1) % 3 == 0) {
if (i != money_length - 1) {
money = money + ".";
}
}
}
money = money.reverse ();
return "$" + money;
}
}
/**
* This class handles parsing and validation of RUTs
*/
@@ -129,7 +179,9 @@ namespace LibSernatur {
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 + ".";
if (i != rut_length - 1) {
new_rut = new_rut + ".";
}
}
}
new_rut = new_rut.reverse ();

51
lib/queries.vala Normal file
View File

@@ -0,0 +1,51 @@
/*
* Copyright 2018-2019 Chris Cromer
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace LibSernatur {
namespace DB {
/**
* The queries to use to work in the database
*/
public class Query < T > : GLib.Object {
public enum Type {
SELECT_ALL_TOURS,
INSERT_TOUR,
UPDATE_TOUR
}
public static string get_query (Type type, T? t) {
if (type == Type.SELECT_ALL_TOURS) {
return "
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)";
}
else if (type == Type.UPDATE_TOUR) {
Tour tour = (Tour) t;
return "
UPDATE tour SET
nombre_tour = '" + tour.nombre_tour + "',
costo_indiv = " + tour.costo_indiv.to_string () + ",
costo_grupal = " + tour.costo_grupal.to_string () + ",
minima_personas = " + tour.minima_personas.to_string () + "
WHERE id_tour = " + tour.id_tour.to_string ();
}
return "";
}
}
}
}