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.

146 lines
5.5KB

  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. /**
  17. * This is a wrapper for Postgres that allows easier use of the database
  18. */
  19. namespace Wrapper {
  20. using Postgres;
  21. /**
  22. * The errors that can be thrown by fields in the database
  23. */
  24. public errordomain Field {
  25. /**
  26. * Field was not found in table
  27. */
  28. NOTFOUND
  29. }
  30. /**
  31. * This is a wrapper for Postgresql results
  32. */
  33. public class ResultWrapper : Object {
  34. /**
  35. * The result that this is wrapped around
  36. */
  37. public unowned Result result { get; set; default = null; }
  38. /**
  39. * Initialize the result wrapper
  40. * @param result The result to wrap around
  41. */
  42. public ResultWrapper (Result? result = null) {
  43. this.result = result;
  44. }
  45. /**
  46. * Return the string value based on tuple and field number
  47. * @param tup_num The tuple number
  48. * @param field_num The field number
  49. * @return Returns a string of the value
  50. */
  51. public string get_string_v (int tup_num, int field_num) {
  52. return result.get_value (tup_num, field_num);
  53. }
  54. /**
  55. * Return the int value based on tuple and field number
  56. * @param tup_num The tuple number
  57. * @param field_num The field number
  58. * @return Returns an int of the value
  59. */
  60. public int get_int_v (int tup_num, int field_num) {
  61. return int.parse (result.get_value (tup_num, field_num));
  62. }
  63. /**
  64. * Return the float value based on tuple and field number
  65. * @param tup_num The tuple number
  66. * @param field_num The field number
  67. * @return Returns a float of the value
  68. */
  69. public float get_float_v (int tup_num, int field_num) {
  70. return float.parse(result.get_value (tup_num, field_num));
  71. }
  72. /**
  73. * Return the double value based on tuple and field number
  74. * @param tup_num The tuple number
  75. * @param field_num The field number
  76. * @return Returns a double of the value
  77. */
  78. public double get_double_v (int tup_num, int field_num) {
  79. return double.parse (result.get_value (tup_num, field_num));
  80. }
  81. /**
  82. * Return the string value based on tuple number and field name
  83. * @param tup_num The tuple number
  84. * @param name The field name
  85. * @return Returns a string of the value
  86. */
  87. public string get_string_n (int tup_num, string name) throws Field {
  88. return get_string_v (tup_num, check_field_found (result.get_field_number (name), name));
  89. }
  90. /**
  91. * Return the int value based on tuple number and field name
  92. * @param tup_num The tuple number
  93. * @param name The field name
  94. * @return Returns an int of the value
  95. */
  96. public int get_int_n (int tup_num, string name) throws Field {
  97. return get_int_v (tup_num, check_field_found (result.get_field_number (name), name));
  98. }
  99. /**
  100. * Return the float value based on tuple number and field name
  101. * @param tup_num The tuple number
  102. * @param name The field name
  103. * @return Returns a float of the value
  104. */
  105. public float get_float_n (int tup_num, string name) throws Field {
  106. return get_float_v (tup_num, check_field_found (result.get_field_number (name), name));
  107. }
  108. /**
  109. * Return the double value based on tuple number and field name
  110. * @param tup_num The tuple number
  111. * @param name The field name
  112. * @return Returns a double of the value
  113. */
  114. public double get_double_n (int tup_num, string name) throws Field {
  115. return get_double_v (tup_num, check_field_found (result.get_field_number (name), name));
  116. }
  117. /**
  118. * Check if the field is found in the results, if not throw a Field error
  119. * @param field_num The field number to check for
  120. * @param name The field name we were looking for
  121. * @return The field number that corresponds to the field name
  122. */
  123. private int check_field_found (int field_num, string name) throws Field {
  124. if (field_num == -1) {
  125. throw new Field.NOTFOUND (_ ("The field %s was not found in the query results!"), name);
  126. }
  127. return field_num;
  128. }
  129. }
  130. }
  131. }
  132. }