Este proyecto es una aplicación para trabajar con un base de datos Postgresql por el ramo de base de datos. https://cromer.cl/sernatur
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.9KB

  1. /*
  2. * Copyright 2018-2019 Chris Cromer
  3. *
  4. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  5. *
  6. * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. *
  8. * 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.
  9. *
  10. * 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.
  11. *
  12. * 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.
  13. */
  14. namespace LibSernatur {
  15. namespace DB {
  16. /**
  17. * The queries to use to work in the database
  18. */
  19. public class Query < T > : Object {
  20. public enum Type {
  21. SELECT_ALL_TOURS,
  22. INSERT_TOUR,
  23. UPDATE_TOUR,
  24. DELETE_TOUR,
  25. SELECT_ALL_ASSOCIATED_BY_TOUR
  26. }
  27. public static string get_query (Connection conn, Type type, T? t) throws PostgresError {
  28. switch (type) {
  29. case SELECT_ALL_TOURS:
  30. return "
  31. SELECT T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
  32. C.id_ciudad, C.nombre_ciudad,
  33. R.id_region, R.nombre_region
  34. FROM tour T
  35. JOIN ciudad C ON (T.id_ciudad = C.id_ciudad)
  36. JOIN region R ON (C.id_region = R.id_region)";
  37. case UPDATE_TOUR:
  38. Tour tour = (Tour) t;
  39. return "
  40. UPDATE tour SET
  41. nombre_tour = '" + conn.escape (tour.nombre_tour) + "',
  42. costo_indiv = " + tour.costo_indiv.to_string () + ",
  43. costo_grupal = " + tour.costo_grupal.to_string () + ",
  44. minima_personas = " + tour.minima_personas.to_string () + ",
  45. id_ciudad = " + tour.ciudad.id_ciudad.to_string () + "
  46. WHERE id_tour = " + tour.id_tour.to_string ();
  47. case INSERT_TOUR:
  48. Tour tour = (Tour) t;
  49. return "
  50. INSERT INTO tour
  51. (nombre_tour, costo_indiv, costo_grupal, minima_personas, id_ciudad)
  52. VALUES
  53. (
  54. '" + conn.escape (tour.nombre_tour) + "',
  55. " + tour.costo_indiv.to_string () + ",
  56. " + tour.costo_grupal.to_string () + ",
  57. " + tour.minima_personas.to_string () + ",
  58. " + tour.ciudad.id_ciudad.to_string () + "
  59. )
  60. RETURNING id_tour";
  61. case DELETE_TOUR:
  62. Tour tour = (Tour) t;
  63. return "
  64. DELETE FROM tour
  65. WHERE id_tour = " + tour.id_tour.to_string ();
  66. case SELECT_ALL_ASSOCIATED_BY_TOUR:
  67. Tour tour = (Tour) t;
  68. return "
  69. SELECT A.fecha_llegada, A.hora_llegada, A.fecha_salida, A.hora_salida,
  70. T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
  71. C.id_ciudad, C.nombre_ciudad,
  72. R.id_region, R.nombre_region,
  73. L.id_lugar, L.nombre_lugar, L.valor_entrada, L.nivel,
  74. C2.id_ciudad AS id_ciudad_lugar, C2.nombre_ciudad AS nombre_ciudad_lugar,
  75. R2.id_region AS id_region_lugar, R2.nombre_region AS nombre_region_lugar
  76. FROM asociado A
  77. JOIN tour T ON (A.id_tour = T.id_tour)
  78. JOIN ciudad C ON (T.id_ciudad = C.id_ciudad)
  79. JOIN region R ON (C.id_region = R.id_region)
  80. JOIN lugar L ON (A.id_lugar = L.id_lugar)
  81. JOIN ciudad C2 ON (L.id_ciudad = C2.id_ciudad)
  82. JOIN region R2 ON (C2.id_region = R2.id_region)
  83. WHERE t.id_tour = " + tour.id_tour.to_string ();
  84. default:
  85. return "";
  86. }
  87. }
  88. }
  89. }
  90. }