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.

128 lines
4.7KB

  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. using Postgres;
  17. using Wrapper;
  18. /**
  19. * The Participa class based on the database table
  20. */
  21. public class Participa : Object {
  22. /**
  23. * The tour
  24. */
  25. public Tour tour { get; set; default = null; }
  26. /**
  27. * The guide
  28. */
  29. public Guia guia { get; set; default = null; }
  30. /**
  31. * The category
  32. */
  33. public Categoria categoria { get; set; default = null; }
  34. /**
  35. * Initialize the Participa class
  36. * @param tour The tour
  37. * @param guia The guide
  38. * @param categoria The category
  39. */
  40. public Participa (Tour? tour = null, Guia? guia = null, Categoria? categoria = null) {
  41. this.tour = tour;
  42. this.guia = guia;
  43. this.categoria = categoria;
  44. }
  45. /**
  46. * Get all tuples and fields from database
  47. * @param conn.db.The database conn.db.ction to use
  48. * @return Returns a list of Participa
  49. */
  50. public static List<Participa> get_all_participas (Connection conn) {
  51. var res = conn.db.exec ("
  52. SELECT T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
  53. C.id_ciudad, C.nombre_ciudad,
  54. R.id_region, R.nombre_region,
  55. G.rut_guia, G.nombre_guia, G.calle, G.numero,
  56. C2.id_ciudad AS id_ciudad_guia, C2.nombre_ciudad AS nombre_ciudad_guia,
  57. R2.id_region AS id_region_guia, R2.nombre_region AS nombre_region_guia,
  58. C3.id_categoria, C3.descripcion_categoria
  59. FROM participa P
  60. JOIN tour T ON (P.id_tour = T.id_tour)
  61. JOIN ciudad C ON (T.id_ciudad = C.id_ciudad)
  62. JOIN region R on (C.id_region = R.id_region)
  63. JOIN guia G on (P.rut_guia = G.rut_guia)
  64. JOIN ciudad C2 ON (G.id_ciudad = C2.id_ciudad)
  65. JOIN region R2 ON (C2.id_region = R2.id_region)
  66. JOIN categoria C3 ON (P.id_categoria = C3.id_categoria)
  67. ");
  68. if (res.get_status () != ExecStatus.TUPLES_OK) {
  69. #if DEBUG
  70. error (conn.db.get_error_message ());
  71. #else
  72. warning (conn.db.get_error_message ());
  73. return new List<Participa> ();
  74. #endif
  75. }
  76. var wra = new ResultWrapper(res);
  77. List<Participa> list = new List<Participa> ();
  78. int n = res.get_n_tuples ();
  79. for (int i = 0; i < n; i++) {
  80. try {
  81. var participa = new Participa (
  82. new Tour (wra.get_int_n (i, "id_tour"),
  83. wra.get_string_n (i, "nombre_tour"),
  84. wra.get_int_n (i, "costo_indiv"),
  85. wra.get_int_n (i, "costo_grupal"),
  86. wra.get_int_n (i, "minima_personas"),
  87. new Ciudad (wra.get_int_n (i, "id_ciudad"),
  88. wra.get_string_n (i, "nombre_ciudad"),
  89. new Region (wra.get_int_n (i, "id_region"),
  90. wra.get_string_n (i, "nombre_region")
  91. )
  92. )
  93. ),
  94. new Guia (wra.get_string_n (i, "rut_guia"),
  95. wra.get_string_n (i, "nombre_guia"),
  96. wra.get_string_n (i, "calle"),
  97. wra.get_int_n (i, "numero"),
  98. new Ciudad (wra.get_int_n (i, "id_ciudad_guia"),
  99. wra.get_string_n (i, "id_region_guia"),
  100. new Region (wra.get_int_n (i, "id_region_guia"),
  101. wra.get_string_n (i, "nombre_region_guia"))
  102. )
  103. ),
  104. new Categoria (wra.get_int_n (i, "id_categoria"),
  105. wra.get_string_n (i, "descripcion_categoria")
  106. )
  107. );
  108. list.append (participa);
  109. }
  110. catch (Error e) {
  111. #if DEBUG
  112. error (e.message);
  113. #else
  114. warning (e.message);
  115. #endif
  116. }
  117. }
  118. return list;
  119. }
  120. }
  121. }
  122. }