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.

111 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 Arrienda class based on the database table
  20. */
  21. public class Arrienda : Object {
  22. /**
  23. * The price
  24. */
  25. public uint precio { get; set; default = 0; }
  26. /**
  27. * The date the vehicle needs to be returned
  28. */
  29. public string fecha_devolucion { get; set; default = ""; }
  30. /**
  31. * The vehicle
  32. */
  33. public Vehiculo vehiculo { get; set; default = null; }
  34. /**
  35. * The compnay
  36. */
  37. public Empresa empresa { get; set; default = null; }
  38. /**
  39. * Initialize the Arrienda class
  40. * @param precio The price
  41. * @param fecha_devolucion The date when the vehicle has to be returned
  42. * @param vehiculo The Vehicle
  43. * @param empresa The company
  44. */
  45. public Arrienda (uint precio = 0, string fecha_devolucion = "", Vehiculo? vehiculo = null, Empresa? empresa = null) {
  46. this.precio = precio;
  47. this.fecha_devolucion = fecha_devolucion;
  48. this.vehiculo = vehiculo;
  49. this.empresa = empresa;
  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 Arrienda
  55. */
  56. public static List<Arrienda> get_all_arriendas (Connection conn) {
  57. var res = conn.db.exec ("
  58. SELECT A.precio, A.fecha_devolucion,
  59. V.patente, V.ano_vehiculo, V.marca, V.capacidad,
  60. E.rut_empresa, E.nombre_empresa, E.contacto, E.telefono
  61. FROM arrienda A
  62. JOIN vehiculo V ON (A.patente = V.patente)
  63. JOIN empresa E ON (A.rut_empresa = E.rut_empresa)
  64. ");
  65. if (res.get_status () != ExecStatus.TUPLES_OK) {
  66. #if DEBUG
  67. error (conn.db.get_error_message ());
  68. #else
  69. warning (conn.db.get_error_message ());
  70. return new List<Arrienda> ();
  71. #endif
  72. }
  73. var wra = new ResultWrapper (res);
  74. List<Arrienda> list = new List<Arrienda> ();
  75. int n = res.get_n_tuples ();
  76. for (int i = 0; i < n; i++) {
  77. try {
  78. var arrienda = new Arrienda (wra.get_int_n (i, "precio"),
  79. wra.get_string_n (i, "fecha_devolucion"),
  80. new Vehiculo (wra.get_string_n (i, "patente"),
  81. wra.get_int_n (i, "ano_vehiculo"),
  82. wra.get_string_n (i, "marca"),
  83. wra.get_int_n (i, "capacidad")
  84. ),
  85. new Empresa (wra.get_string_n (i, "rut_empresa"),
  86. wra.get_string_n (i, "nombre_empresa"),
  87. wra.get_string_n (i, "contacto"),
  88. wra.get_int_n (i, "telefono")
  89. )
  90. );
  91. list.append (arrienda);
  92. }
  93. catch (Error e) {
  94. #if DEBUG
  95. error (e.message);
  96. #else
  97. warning (e.message);
  98. #endif
  99. }
  100. }
  101. return list;
  102. }
  103. }
  104. }
  105. }