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 Guia class based on the database table
  20. */
  21. public class Guia : Object {
  22. /**
  23. * The RUT of the guide
  24. */
  25. public string rut_guia { get; set; default = ""; }
  26. /**
  27. * The name of the guide
  28. **/
  29. public string nombre_guia { get; set; default = ""; }
  30. /**
  31. * The stree name
  32. */
  33. public string calle { get; set; default = ""; }
  34. /**
  35. * The street number
  36. */
  37. public uint numero { get; set; default = 0; }
  38. /**
  39. * The city
  40. */
  41. public Ciudad ciudad { get; set; default = null; }
  42. /**
  43. * Initialize the Guia class
  44. * @param rut_guia The RUT of the guide
  45. * @param nombre_guia The name of the guide
  46. * @param calle The street name
  47. * @param numero The street number
  48. * @param ciudad The city
  49. */
  50. public Guia (string rut_guia = "", string nombre_guia = "", string calle = "", uint numero = 0, Ciudad? ciudad = null) {
  51. this.rut_guia = rut_guia;
  52. this.nombre_guia = nombre_guia;
  53. this.calle = calle;
  54. this.numero = numero;
  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 Guia
  61. */
  62. public static List<Guia> get_all_guias (Connection conn) {
  63. var res = conn.db.exec ("
  64. SELECT G.rut_guia, G.nombre_guia, G.calle, G.numero,
  65. C.id_ciudad, C.nombre_ciudad,
  66. R.id_region, R.nombre_region
  67. FROM guia G
  68. JOIN ciudad C ON (G.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<Guia> ();
  77. #endif
  78. }
  79. var wra = new ResultWrapper (res);
  80. List<Guia> list = new List<Guia> ();
  81. int n = res.get_n_tuples ();
  82. for (int i = 0; i < n; i++) {
  83. try {
  84. var guia = new Guia (wra.get_string_n (i, "rut_guia"),
  85. wra.get_string_n (i, "nombre_guia"),
  86. wra.get_string_n (i, "calle"),
  87. wra.get_int_n (i, "numero"),
  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 (guia);
  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. }