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
6.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 Tour class based on the database table
  20. */
  21. public class Tour : Object {
  22. /**
  23. * The tour id
  24. */
  25. public uint id_tour { get; set; default = 0; }
  26. /**
  27. * The tour name
  28. */
  29. public string nombre_tour { get; set; default = ""; }
  30. /**
  31. * The individual cost
  32. */
  33. public uint costo_indiv { get; set; default = 0; }
  34. /**
  35. * The group cost
  36. */
  37. public uint costo_grupal { get; set; default = 0; }
  38. /**
  39. * The minimum number of people
  40. */
  41. public uint minima_personas { get; set; default = 0; }
  42. /**
  43. * The city
  44. */
  45. public Ciudad ciudad { get; set; default = null; }
  46. /**
  47. * Initialize the Tour class
  48. * @param id_tour The tour id
  49. * @param nombre_tour The tour name
  50. * @param costo_indiv The individual cost
  51. * @param costo_grupal The group cost
  52. * @param minima_personas The minimum number of people
  53. * @param ciudad The city
  54. */
  55. public Tour (uint id_tour = 0, string nombre_tour = "", uint costo_indiv = 0, uint costo_grupal = 0, uint minima_personas = 0, Ciudad? ciudad = null) {
  56. this.id_tour = id_tour;
  57. this.nombre_tour = nombre_tour;
  58. this.costo_indiv = costo_indiv;
  59. this.costo_grupal = costo_grupal;
  60. this.minima_personas = minima_personas;
  61. this.ciudad = ciudad;
  62. }
  63. /**
  64. * Get all tuples and fields from database
  65. * @param conn The database connection to use
  66. * @return Returns a list of Tour
  67. */
  68. public static List<Tour> get_all_tours (Connection conn) throws PostgresError {
  69. var res = conn.db.exec (Query.get_query (conn, Query.Type.SELECT_ALL_TOURS, null));
  70. if (res.get_status () != ExecStatus.TUPLES_OK) {
  71. #if DEBUG
  72. error (conn.db.get_error_message ());
  73. #else
  74. warning (conn.db.get_error_message ());
  75. return new List<Tour> ();
  76. #endif
  77. }
  78. var wra = new ResultWrapper (res);
  79. List<Tour> list = new List<Tour> ();
  80. int n = res.get_n_tuples ();
  81. for (int i = 0; i < n; i++) {
  82. try {
  83. var tour = new Tour (wra.get_int_n (i, "id_tour"),
  84. wra.get_string_n (i, "nombre_tour"),
  85. wra.get_int_n (i, "costo_indiv"),
  86. wra.get_int_n (i, "costo_grupal"),
  87. wra.get_int_n (i, "minima_personas"),
  88. new Ciudad (wra.get_int_n (i, "id_ciudad"),
  89. wra.get_string_n (i, "nombre_ciudad"),
  90. new Region (wra.get_int_n (i, "id_region"),
  91. wra.get_string_n (i, "nombre_region")
  92. )
  93. )
  94. );
  95. list.append (tour);
  96. }
  97. catch (Error e) {
  98. #if DEBUG
  99. error (e.message);
  100. #else
  101. warning (e.message);
  102. #endif
  103. }
  104. }
  105. return list;
  106. }
  107. /**
  108. * Update a tour in the database
  109. * @param conn The database connection
  110. * @param tour The tour to update
  111. * @throws DBError Thrown if the data in the object is invalid
  112. */
  113. public static void update_tour (Connection conn, Tour tour) throws PostgresError, DBError {
  114. if (tour.id_tour == 0) {
  115. throw new DBError.INVALID_VALUE (dgettext (null, "The id of the tour is invalid!"));
  116. }
  117. var res = conn.db.exec (Query.get_query (conn, Query.Type.UPDATE_TOUR, tour));
  118. if (res.get_status () != ExecStatus.COMMAND_OK) {
  119. #if DEBUG
  120. error (conn.db.get_error_message ());
  121. #else
  122. warning (conn.db.get_error_message ());
  123. #endif
  124. }
  125. }
  126. /**
  127. * Insert a tour in the database
  128. * @param conn The database connection
  129. * @param tour The tour to insert
  130. * @return Returns the id of the tuple inserted
  131. * @throws DBError Thrown if the data in the object is invalid
  132. */
  133. public static uint insert_tour (Connection conn, Tour tour) throws PostgresError, DBError {
  134. if (tour.id_tour != 0) {
  135. throw new DBError.INVALID_VALUE (dgettext (null, "The id of the tour is invalid!"));
  136. }
  137. var res = conn.db.exec (Query.get_query (conn, Query.Type.INSERT_TOUR, tour));
  138. // This uses TUPLES_OK because it returns a result which is hte id of the inserted tour
  139. if (res.get_status () != ExecStatus.TUPLES_OK) {
  140. #if DEBUG
  141. error (conn.db.get_error_message ());
  142. #else
  143. warning (conn.db.get_error_message ());
  144. #endif
  145. }
  146. var wra = new ResultWrapper (res);
  147. try {
  148. return wra.get_int_n (0, "id_tour");
  149. }
  150. catch (Error e) {
  151. #if DEBUG
  152. error (e.message);
  153. #else
  154. warning (e.message);
  155. return 0;
  156. #endif
  157. }
  158. }
  159. /**
  160. * Delete a tour in the database
  161. * @param conn The database connection
  162. * @param tour The tour to update
  163. * @throws DBError Thrown if the data in the object is invalid
  164. */
  165. public static void delete_tour (Connection conn, Tour tour) throws PostgresError, DBError {
  166. if (tour.id_tour == 0) {
  167. throw new DBError.INVALID_VALUE (dgettext (null, "The id of the tour is invalid!"));
  168. }
  169. var res = conn.db.exec (Query.get_query (conn, Query.Type.DELETE_TOUR, tour));
  170. if (res.get_status () != ExecStatus.COMMAND_OK) {
  171. if (res.get_error_field (FieldCode.SQLSTATE) == "23503") {
  172. throw new DBError.FOREIGN_KEY_CONSTAINT (res.get_error_field (FieldCode.MESSAGE_PRIMARY));
  173. }
  174. #if DEBUG
  175. error (conn.db.get_error_message ());
  176. #else
  177. warning (conn.db.get_error_message ());
  178. #endif
  179. }
  180. }
  181. }
  182. }
  183. }