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.

193 lines
7.4KB

  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 Asociado class based on the database table
  20. */
  21. public class Asociado : Object {
  22. public string fecha_llegada { get; set; default = ""; }
  23. public string hora_llegada { get; set; default = ""; }
  24. public string fecha_salida { get; set; default = ""; }
  25. public string hora_salida { get; set; default = ""; }
  26. public Tour tour { get; set; default = null; }
  27. public Lugar lugar { get; set; default = null; }
  28. /**
  29. * Initialize the Asociado class
  30. * @param fecha_llegada The date of arrival
  31. * @param hora_llegada The time of arrival
  32. * @param fecha_salida The date of departure
  33. * @param hora_salida The time of departure
  34. * @param tour The tour taken
  35. * @param lugar The place visisted
  36. */
  37. public Asociado (string fecha_llegada = "", string hora_llegada = "", string fecha_salida = "", string hora_salida = "", Tour? tour = null, Lugar? lugar = null) {
  38. this.fecha_llegada = fecha_llegada;
  39. this.hora_llegada = hora_llegada;
  40. this.fecha_salida = fecha_salida;
  41. this.hora_salida = hora_salida;
  42. this.tour = tour;
  43. this.lugar = lugar;
  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 Asociado
  49. */
  50. public static List<Asociado> get_all_asociados (Connection conn) {
  51. var res = conn.db.exec ("
  52. SELECT A.fecha_llegada, A.hora_llegada, A.fecha_salida, A.hora_salida,
  53. T.id_tour, T.nombre_tour, T.costo_indiv, T.costo_grupal, T.minima_personas,
  54. C.id_ciudad, C.nombre_ciudad,
  55. R.id_region, R.nombre_region,
  56. L.id_lugar, L.nombre_lugar, L.valor_entrada, L.nivel,
  57. C2.id_ciudad AS id_ciudad_lugar, C2.nombre_ciudad AS nombre_ciudad_lugar,
  58. R2.id_region AS id_region_lugar, R2.nombre_region AS nombre_region_lugar
  59. FROM asociado A
  60. JOIN tour T ON (A.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 lugar L ON (A.id_lugar = L.id_lugar)
  64. JOIN ciudad C2 ON (L.id_ciudad = C2.id_ciudad)
  65. JOIN region R2 ON (C2.id_region = R2.id_region)
  66. ");
  67. if (res.get_status () != ExecStatus.TUPLES_OK) {
  68. #if DEBUG
  69. error (conn.db.get_error_message ());
  70. #else
  71. warning (conn.db.get_error_message ());
  72. return new List<Asociado> ();
  73. #endif
  74. }
  75. var wra = new ResultWrapper (res);
  76. List<Asociado> list = new List<Asociado> ();
  77. int n = res.get_n_tuples ();
  78. for (int i = 0; i < n; i++) {
  79. try {
  80. var asociado = new Asociado (wra.get_string_n (i, "fecha_llegada"),
  81. wra.get_string_n (i, "hora_llegada"),
  82. wra.get_string_n (i, "fecha_salida"),
  83. wra.get_string_n (i, "hora_salida"),
  84. new Tour (wra.get_int_n (i, "id_tour"),
  85. wra.get_string_n (i, "nombre_tour"),
  86. wra.get_int_n (i, "costo_indiv"),
  87. wra.get_int_n (i, "costo_grupal"),
  88. wra.get_int_n (i, "minima_personas"),
  89. new Ciudad (wra.get_int_n (i, "id_ciudad"),
  90. wra.get_string_n (i, "nombre_ciudad"),
  91. new Region (wra.get_int_n (i, "id_region"),
  92. wra.get_string_n (i, "nombre_region")
  93. )
  94. )
  95. ),
  96. new Lugar (wra.get_int_n (i, "id_lugar"),
  97. wra.get_string_n (i, "nombre_lugar"),
  98. wra.get_int_n (i, "valor_entrada"),
  99. wra.get_int_n (i, "nivel"),
  100. new Ciudad (wra.get_int_n (i, "id_ciudad_lugar"),
  101. wra.get_string_n (i, "nombre_ciudad_lugar"),
  102. new Region (wra.get_int_n (i, "id_region_lugar"),
  103. wra.get_string_n (i, "nombre_region_lugar")
  104. )
  105. )
  106. )
  107. );
  108. list.append (asociado);
  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. * Get all tuples and fields from database
  122. * @param conn.db.The database conn.db.ction to use
  123. * @return Returns a list of Asociado based off a tour id
  124. */
  125. public static List<Asociado> get_all_asociados_by_tour (Connection conn, Tour tour) throws PostgresError, DBError {
  126. if (tour.id_tour == 0) {
  127. throw new DBError.INVALID_VALUE (dgettext (null, "The id of the tour is invalid!"));
  128. }
  129. var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_ASSOCIATED_BY_TOUR, tour));
  130. if (res.get_status () != ExecStatus.TUPLES_OK) {
  131. #if DEBUG
  132. error (conn.db.get_error_message ());
  133. #else
  134. warning (conn.db.get_error_message ());
  135. return new List<Asociado> ();
  136. #endif
  137. }
  138. var wra = new ResultWrapper (res);
  139. List<Asociado> list = new List<Asociado> ();
  140. int n = res.get_n_tuples ();
  141. for (int i = 0; i < n; i++) {
  142. try {
  143. var asociado = new Asociado (wra.get_string_n (i, "fecha_llegada"),
  144. wra.get_string_n (i, "hora_llegada"),
  145. wra.get_string_n (i, "fecha_salida"),
  146. wra.get_string_n (i, "hora_salida"),
  147. new Tour (wra.get_int_n (i, "id_tour"),
  148. wra.get_string_n (i, "nombre_tour"),
  149. wra.get_int_n (i, "costo_indiv"),
  150. wra.get_int_n (i, "costo_grupal"),
  151. wra.get_int_n (i, "minima_personas"),
  152. new Ciudad (wra.get_int_n (i, "id_ciudad"),
  153. wra.get_string_n (i, "nombre_ciudad"),
  154. new Region (wra.get_int_n (i, "id_region"),
  155. wra.get_string_n (i, "nombre_region")
  156. )
  157. )
  158. ),
  159. new Lugar (wra.get_int_n (i, "id_lugar"),
  160. wra.get_string_n (i, "nombre_lugar"),
  161. wra.get_int_n (i, "valor_entrada"),
  162. wra.get_int_n (i, "nivel"),
  163. new Ciudad (wra.get_int_n (i, "id_ciudad_lugar"),
  164. wra.get_string_n (i, "nombre_ciudad_lugar"),
  165. new Region (wra.get_int_n (i, "id_region_lugar"),
  166. wra.get_string_n (i, "nombre_region_lugar")
  167. )
  168. )
  169. )
  170. );
  171. list.append (asociado);
  172. }
  173. catch (Error e) {
  174. #if DEBUG
  175. error (e.message);
  176. #else
  177. warning (e.message);
  178. #endif
  179. }
  180. }
  181. return list;
  182. }
  183. }
  184. }
  185. }