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.

104 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 Turista class based on the database table
  20. */
  21. public class Turista : Object {
  22. /**
  23. * The RUT of the tourist
  24. */
  25. public string rut_turista { get; set; default = ""; }
  26. /**
  27. * The tourist name
  28. */
  29. public string nombre_turista { get; set; default = ""; }
  30. /**
  31. * The birth date of the tourist
  32. */
  33. public string fecha_nacimento { get; set; default = ""; }
  34. /**
  35. * The emergency contact
  36. */
  37. public ContactoEmergencia contacto_emergencia { get; set; default = null; }
  38. /**
  39. * Initilize the Turista class
  40. * @param rut_turista The RUT of the tourist
  41. * @param nombre_turista The tourist name
  42. * @param fecha_nacimento The birth date of the tourist
  43. * @param contacto_emergencia The emergency contact
  44. */
  45. public Turista (string rut_turista = "", string nombre_turista = "", string fecha_nacimento = "", ContactoEmergencia? contacto_emergencia = null) {
  46. this.rut_turista = rut_turista;
  47. this.nombre_turista = nombre_turista;
  48. this.fecha_nacimento = fecha_nacimento;
  49. this.contacto_emergencia = contacto_emergencia;
  50. }
  51. /**
  52. * Get all tuples and fields from database
  53. * @param conn.db.The database conn.db.ction to use
  54. * @return Returns a list of Turista
  55. */
  56. public static List<Turista> get_all_turistas (Connection conn) {
  57. var res = conn.db.exec ("
  58. SELECT T.rut_turista, T.nombre_turista, T.fecha_nacimento,
  59. C.id_contacto, C.telefono_emergencia, C.nombre_emergencia
  60. FROM turista T
  61. JOIN contacto_emergencia C ON (T.id_contacto = C.id_contacto)
  62. ");
  63. if (res.get_status () != ExecStatus.TUPLES_OK) {
  64. #if DEBUG
  65. error (conn.db.get_error_message ());
  66. #else
  67. warning (conn.db.get_error_message ());
  68. return new List<Turista> ();
  69. #endif
  70. }
  71. var wra = new ResultWrapper (res);
  72. List<Turista> list = new List<Turista> ();
  73. int n = res.get_n_tuples ();
  74. for (int i = 0; i < n; i++) {
  75. try {
  76. var turista = new Turista (wra.get_string_n (i, "rut_turista"),
  77. wra.get_string_n (i, "nombre_turista"),
  78. wra.get_string_n (i, "fecha_nacimento"),
  79. new ContactoEmergencia (wra.get_int_n (i, "id_contacto"),
  80. wra.get_int_n (i, "telefono_emergencia"),
  81. wra.get_string_n (i, "nombre_emergencia")
  82. )
  83. );
  84. list.append (turista);
  85. }
  86. catch (Error e) {
  87. #if DEBUG
  88. error (e.message);
  89. #else
  90. warning (e.message);
  91. #endif
  92. }
  93. }
  94. return list;
  95. }
  96. }
  97. }
  98. }