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.

115 lines
4.0KB

  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 Lugar class based on the database table
  20. */
  21. public class Lugar : Object {
  22. /**
  23. * The id of the place
  24. */
  25. public uint id_lugar { get; set; default = 0; }
  26. /**
  27. * The name of the place
  28. */
  29. public string nombre_lugar { get; set; default = ""; }
  30. /**
  31. * The cost to enter
  32. */
  33. public uint valor_entrada { get; set; default = 0; }
  34. /**
  35. * The level of difficulty
  36. */
  37. public uint nivel { get; set; default = 0; }
  38. /**
  39. * The city
  40. */
  41. public Ciudad ciudad { get; set; default = null; }
  42. /**
  43. * Initialize the Lugar class
  44. * @param id_lugar The id of the place
  45. * @param nombre_lugar The name of the place
  46. * @param valor_entrada The cost to enter
  47. * @param nivel The level of difficulty
  48. * @param ciudad The city
  49. */
  50. public Lugar (uint id_lugar = 0, string nombre_lugar = "", uint valor_entrada = 0, uint nivel = 0, Ciudad? ciudad = null) {
  51. this.id_lugar = id_lugar;
  52. this.nombre_lugar = nombre_lugar;
  53. this.valor_entrada = valor_entrada;
  54. this.nivel = nivel;
  55. this.ciudad = ciudad;
  56. }
  57. /**
  58. * Get all tuples and fields from database
  59. * @param conn.db.The database conn.db.ction to use
  60. * @return Returns a list of Lugar
  61. */
  62. public static List<Lugar> get_all_lugares (Connection conn) {
  63. var res = conn.db.exec ("
  64. SELECT L.id_lugar, L.nombre_lugar, L.valor_entrada, L.nivel,
  65. C.id_ciudad, C.nombre_ciudad,
  66. R.id_region, R.nombre_region
  67. FROM lugar L
  68. JOIN ciudad C ON (L.id_ciudad = C.id_ciudad)
  69. JOIN region R ON (C.id_region = R.id_region)
  70. ");
  71. if (res.get_status () != ExecStatus.TUPLES_OK) {
  72. #if DEBUG
  73. error (conn.db.get_error_message ());
  74. #else
  75. warning (conn.db.get_error_message ());
  76. return new List<Lugar> ();
  77. #endif
  78. }
  79. var wra = new ResultWrapper (res);
  80. List<Lugar> list = new List<Lugar> ();
  81. int n = res.get_n_tuples ();
  82. for (int i = 0; i < n; i++) {
  83. try {
  84. var lugar = new Lugar (wra.get_int_n (i, "id_lugar"),
  85. wra.get_string_n (i, "nombre_lugar"),
  86. wra.get_int_n (i, "valor_entrada"),
  87. wra.get_int_n (i, "nivel"),
  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 (lugar);
  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. }
  109. }