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.

143 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 Ciudad class based on the database table
  20. */
  21. public class Ciudad : Object {
  22. /**
  23. * The id of the city
  24. */
  25. public uint id_ciudad { get; set; default = 0; }
  26. /**
  27. * The name of the city
  28. */
  29. public string nombre_ciudad { get; set; default = ""; }
  30. /**
  31. * The region
  32. */
  33. public Region region { get; set; default = null; }
  34. /**
  35. * Initialize the Ciudad class
  36. * @param id_ciudad The id of the city
  37. * @param nombre_ciudad The name of the city
  38. * @param region The region
  39. * */
  40. public Ciudad (uint id_ciudad = 0, string nombre_ciudad = "", Region? region = null) {
  41. this.id_ciudad = id_ciudad;
  42. this.nombre_ciudad = nombre_ciudad;
  43. this.region = region;
  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 Ciudad
  49. */
  50. public static List<Ciudad> get_all_ciudades (Connection conn) {
  51. var res = conn.db.exec ("
  52. SELECT C.id_ciudad, C.nombre_ciudad,
  53. R.id_region, R.nombre_region
  54. FROM ciudad C
  55. JOIN region R ON (C.id_region = R.id_region)
  56. ");
  57. if (res.get_status () != ExecStatus.TUPLES_OK) {
  58. #if DEBUG
  59. error (conn.db.get_error_message ());
  60. #else
  61. warning (conn.db.get_error_message ());
  62. return new List<Ciudad> ();
  63. #endif
  64. }
  65. var wra = new ResultWrapper (res);
  66. List<Ciudad> list = new List<Ciudad> ();
  67. int n = res.get_n_tuples ();
  68. for (int i = 0; i < n; i++) {
  69. try {
  70. var ciudad = new Ciudad (wra.get_int_n (i, "id_ciudad"),
  71. wra.get_string_n (i, "nombre_ciudad"),
  72. new Region (wra.get_int_n (i, "id_region"),
  73. wra.get_string_n (i, "nombre_region")
  74. )
  75. );
  76. list.append (ciudad);
  77. }
  78. catch (Error e) {
  79. #if DEBUG
  80. error (e.message);
  81. #else
  82. warning (e.message);
  83. #endif
  84. }
  85. }
  86. return list;
  87. }
  88. /**
  89. * Get all tuples and fields from database that are within a certain region
  90. * @param conn.db.The database conn.db.ction to use
  91. * @param region_id The id of the region to filter that results
  92. * @return Returns a list of Ciudad
  93. */
  94. public static List<Ciudad> get_all_ciudades_in_region(Connection conn, uint region_id) {
  95. var res = conn.db.exec ("
  96. SELECT C.id_ciudad, C.nombre_ciudad,
  97. R.id_region, R.nombre_region
  98. FROM ciudad C
  99. JOIN region R ON (C.id_region = R.id_region)
  100. WHERE (R.id_region = " + region_id.to_string () + ")
  101. ");
  102. if (res.get_status () != ExecStatus.TUPLES_OK) {
  103. #if DEBUG
  104. error (conn.db.get_error_message ());
  105. #else
  106. warning (conn.db.get_error_message ());
  107. return new List<Ciudad> ();
  108. #endif
  109. }
  110. var wra = new ResultWrapper (res);
  111. List<Ciudad> list = new List<Ciudad> ();
  112. int n = res.get_n_tuples ();
  113. for (int i = 0; i < n; i++) {
  114. try {
  115. var ciudad = new Ciudad (wra.get_int_n (i, "id_ciudad"),
  116. wra.get_string_n (i, "nombre_ciudad"),
  117. new Region (wra.get_int_n (i, "id_region"),
  118. wra.get_string_n (i, "nombre_region")
  119. )
  120. );
  121. list.append (ciudad);
  122. }
  123. catch (Error e) {
  124. #if DEBUG
  125. error (e.message);
  126. #else
  127. warning (e.message);
  128. #endif
  129. }
  130. }
  131. return list;
  132. }
  133. }
  134. }
  135. }