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.

98 lines
3.6KB

  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 Empresa class based on the database table
  20. */
  21. public class Empresa : Object {
  22. /**
  23. * The RUT of the company
  24. */
  25. public string rut_empresa { get; set; default = ""; }
  26. /**
  27. * The name of the company
  28. */
  29. public string nombre_empresa { get; set; default = ""; }
  30. /**
  31. * The name of the contact in the company
  32. */
  33. public string contacto { get; set; default = ""; }
  34. /**
  35. * The phone number of the contact
  36. */
  37. public uint telefono { get; set; default = 0; }
  38. /**
  39. * Initialize the Empresa class
  40. * @param rut_empresa The RUT of the company
  41. * @param nombre_empresa The name of the company
  42. * @param contacto The name of the contact in the company
  43. * @param telefono The phone number of the contact
  44. */
  45. public Empresa (string rut_empresa = "", string nombre_empresa = "", string contacto = "", uint telefono = 0) {
  46. this.rut_empresa = rut_empresa;
  47. this.nombre_empresa = nombre_empresa;
  48. this.contacto = contacto;
  49. this.telefono = telefono;
  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 Empresa
  55. */
  56. public static List<Empresa> get_all_empresas (Connection conn) {
  57. var res = conn.db.exec ("
  58. SELECT rut_empresa, nombre_empresa, contacto, telefono FROM empresa
  59. ");
  60. if (res.get_status () != ExecStatus.TUPLES_OK) {
  61. #if DEBUG
  62. error (conn.db.get_error_message ());
  63. #else
  64. warning (conn.db.get_error_message ());
  65. return new List<Empresa> ();
  66. #endif
  67. }
  68. var wra = new ResultWrapper (res);
  69. List<Empresa> list = new List<Empresa> ();
  70. int n = res.get_n_tuples ();
  71. for (int i = 0; i < n; i++) {
  72. try {
  73. var empresa = new Empresa (wra.get_string_n (i, "rut_empresa"),
  74. wra.get_string_n (i, "nombre_empresa"),
  75. wra.get_string_n (i, "contacto"),
  76. wra.get_int_n (i, "telefono")
  77. );
  78. list.append (empresa);
  79. }
  80. catch (Error e) {
  81. #if DEBUG
  82. error (e.message);
  83. #else
  84. warning (e.message);
  85. #endif
  86. }
  87. }
  88. return list;
  89. }
  90. }
  91. }
  92. }