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.

282 lines
8.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. /**
  17. * The views namespace for the 5 views
  18. */
  19. namespace Views {
  20. using Postgres;
  21. using Wrapper;
  22. /**
  23. * The Q1 class based on the database table
  24. */
  25. public class Q1 : Object {
  26. /**
  27. * Get all tuples and fields from database
  28. * @param conn.db.The database conn.db.ction to use
  29. * @return Returns a list of RegionesSinDescuento
  30. */
  31. public static List<RegionesSinDescuento> get_regions_without_discount (Connection conn) {
  32. var res = conn.db.exec ("
  33. SELECT nombreRegion, cantidad FROM REGIONES_SINDESCUENTO WHERE (cantidad = (SELECT MAX(cantidad) FROM REGIONES_SINDESCUENTO))
  34. ");
  35. if (res.get_status () != ExecStatus.TUPLES_OK) {
  36. #if DEBUG
  37. error (conn.db.get_error_message ());
  38. #else
  39. warning (conn.db.get_error_message ());
  40. return new List<RegionesSinDescuento> ();
  41. #endif
  42. }
  43. var wra = new ResultWrapper (res);
  44. List<RegionesSinDescuento> list = new List<RegionesSinDescuento> ();
  45. int n = res.get_n_tuples ();
  46. for (int i = 0; i < n; i++) {
  47. try {
  48. var region_sin_descuento = new RegionesSinDescuento (wra.get_string_n (i, "nombreRegion"),
  49. wra.get_int_n (i, "cantidad")
  50. );
  51. list.append (region_sin_descuento);
  52. }
  53. catch (Error e) {
  54. #if DEBUG
  55. error (e.message);
  56. #else
  57. warning (e.message);
  58. #endif
  59. }
  60. }
  61. return list;
  62. }
  63. }
  64. /**
  65. * The Q2 class based on the database table
  66. */
  67. public class Q2 : Object {
  68. /**
  69. * The name of the tour
  70. */
  71. public string nombre_tour { get; set; default = ""; }
  72. /**
  73. * The value received
  74. */
  75. public uint valor_recibido { get; set; default = 0; }
  76. /**
  77. * Initialize the Q2 class
  78. * @param nombre_tour The name of the tour
  79. * @param valor_recibido The money value received
  80. */
  81. public Q2 (string nombre_tour = "", uint valor_recibido = 0) {
  82. this.nombre_tour = nombre_tour;
  83. this.valor_recibido = valor_recibido;
  84. }
  85. /**
  86. * Get all tuples and fields from database
  87. * @param conn.db.The database conn.db.ction to use
  88. * @return Returns a list of Q2
  89. */
  90. public static List<Q2> get_value_received (Connection conn) {
  91. var res = conn.db.exec ("
  92. SELECT V.nombreT, (V.TotalVentas - COALESCE(MAX(T.TotalDescuentos), 0)) AS ValorTotalRecibido
  93. FROM VALORES_TOURS V FULL JOIN TOUR_DESCUENTOS T ON (T.idT = V.idT) GROUP BY (V.nombreT, V.TotalVentas)
  94. ");
  95. if (res.get_status () != ExecStatus.TUPLES_OK) {
  96. #if DEBUG
  97. error (conn.db.get_error_message ());
  98. #else
  99. warning (conn.db.get_error_message ());
  100. return new List<Q2> ();
  101. #endif
  102. }
  103. var wra = new ResultWrapper (res);
  104. List<Q2> list = new List<Q2> ();
  105. int n = res.get_n_tuples ();
  106. for (int i = 0; i < n; i++) {
  107. try {
  108. var values = new Q2 (wra.get_string_n (i, "nombreT"),
  109. wra.get_int_n (i, "ValorTotalRecibido")
  110. );
  111. list.append (values);
  112. }
  113. catch (Error e) {
  114. #if DEBUG
  115. error (e.message);
  116. #else
  117. warning (e.message);
  118. #endif
  119. }
  120. }
  121. return list;
  122. }
  123. }
  124. /**
  125. * The Q3 class based on the database table
  126. */
  127. public class Q3 : Object {
  128. /**
  129. * Get all tuples and fields from database
  130. * @param conn.db.The database conn.db.ction to use
  131. * @return Returns a list of TotalCoordinadores
  132. */
  133. public static List<TotalCoordinadores> get_total_coordinators (Connection conn) {
  134. var res = conn.db.exec ("
  135. SELECT nombreT, TotalCoordinadores
  136. FROM TOTAL_COORDINADORES WHERE (TotalCoordinadores = (SELECT MAX(TotalCoordinadores) FROM TOTAL_COORDINADORES))
  137. ");
  138. if (res.get_status () != ExecStatus.TUPLES_OK) {
  139. #if DEBUG
  140. error (conn.db.get_error_message ());
  141. #else
  142. warning (conn.db.get_error_message ());
  143. return new List<TotalCoordinadores> ();
  144. #endif
  145. }
  146. var wra = new ResultWrapper (res);
  147. List<TotalCoordinadores> list = new List<TotalCoordinadores> ();
  148. int n = res.get_n_tuples ();
  149. for (int i = 0; i < n; i++) {
  150. try {
  151. var total = new TotalCoordinadores (0,
  152. wra.get_string_n (i, "nombreT"),
  153. wra.get_int_n (i, "TotalCoordinadores")
  154. );
  155. list.append (total);
  156. }
  157. catch (Error e) {
  158. #if DEBUG
  159. error (e.message);
  160. #else
  161. warning (e.message);
  162. #endif
  163. }
  164. }
  165. return list;
  166. }
  167. }
  168. /**
  169. * The Q4 class based on the database table
  170. */
  171. public class Q4 : Object {
  172. /**
  173. * Get all tuples and fields from database
  174. * @param conn.db.The database conn.db.ction to use
  175. * @return Returns a list of TotalTuristas
  176. */
  177. public static List<TotalTuristas> get_total_tourists (Connection conn) {
  178. var res = conn.db.exec ("
  179. SELECT nombreT, TotalTuristas FROM TOTAL_TURISTAS WHERE (TotalTuristas = (SELECT MAX(TotalTuristas) FROM TOTAL_TURISTAS))
  180. ");
  181. if (res.get_status () != ExecStatus.TUPLES_OK) {
  182. #if DEBUG
  183. error (conn.db.get_error_message ());
  184. #else
  185. warning (conn.db.get_error_message ());
  186. return new List<TotalTuristas> ();
  187. #endif
  188. }
  189. var wra = new ResultWrapper (res);
  190. List<TotalTuristas> list = new List<TotalTuristas> ();
  191. int n = res.get_n_tuples ();
  192. for (int i = 0; i < n; i++) {
  193. try {
  194. var total = new TotalTuristas (0,
  195. wra.get_string_n (i, "nombreT"),
  196. wra.get_int_n (i, "TotalTuristas")
  197. );
  198. list.append (total);
  199. }
  200. catch (Error e) {
  201. #if DEBUG
  202. error (e.message);
  203. #else
  204. warning (e.message);
  205. #endif
  206. }
  207. }
  208. return list;
  209. }
  210. }
  211. /**
  212. * The Q5 class based on the database table
  213. */
  214. public class Q5 : Object {
  215. /**
  216. * The percentage of cars that were rented and used
  217. */
  218. public float porcentaje { get; set; default = 0; }
  219. /**
  220. * Initialize the Q5 class
  221. * @param porcentaje The percentage of cars that were rented and used
  222. */
  223. public Q5 (float porcentaje = 0) {
  224. this.porcentaje = porcentaje;
  225. }
  226. /**
  227. * Get all tuples and fields from database
  228. * @param conn.db.The database conn.db.ction to use
  229. * @return Returns a list of Q5
  230. */
  231. public static List<Q5> get_percentage (Connection conn) {
  232. var res = conn.db.exec ("
  233. SELECT
  234. (cast(T1.totalarriendo AS DECIMAL(3,2)) / cast(T2.totalveh AS DECIMAL(3,2))) AS porcentaje
  235. FROM total_arriendos AS T1, total_vehiculos AS T2
  236. ");
  237. if (res.get_status () != ExecStatus.TUPLES_OK) {
  238. #if DEBUG
  239. error (conn.db.get_error_message ());
  240. #else
  241. warning (conn.db.get_error_message ());
  242. return new List<Q5> ();
  243. #endif
  244. }
  245. var wra = new ResultWrapper (res);
  246. List<Q5> list = new List<Q5> ();
  247. int n = res.get_n_tuples ();
  248. for (int i = 0; i < n; i++) {
  249. try {
  250. var percentage = new Q5 (wra.get_float_n (i, "porcentaje"));
  251. list.append (percentage);
  252. }
  253. catch (Error e) {
  254. #if DEBUG
  255. error (e.message);
  256. #else
  257. warning (e.message);
  258. #endif
  259. }
  260. }
  261. return list;
  262. }
  263. }
  264. }
  265. }
  266. }