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.

102 lines
3.7KB

  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. /**
  15. * The sernatur library
  16. */
  17. namespace LibSernatur {
  18. /**
  19. * Sernatur database library
  20. */
  21. namespace DB {
  22. using Postgres;
  23. /**
  24. * The errors that can be thrown by Postgresql
  25. */
  26. public errordomain PostgresError {
  27. /**
  28. * Connection error
  29. */
  30. CONNECT,
  31. /**
  32. * Escape error
  33. */
  34. ESCAPE
  35. }
  36. /**
  37. * The errors that can be thrown while doing queries in the database
  38. */
  39. public errordomain DBError {
  40. /**
  41. * Invalid value
  42. */
  43. INVALID_VALUE,
  44. /**
  45. * Could not be modified or deleted because of a constraint
  46. */
  47. FOREIGN_KEY_CONSTAINT
  48. }
  49. /**
  50. * Class to handle database connections
  51. */
  52. public class Connection : Object {
  53. /**
  54. * The database connection
  55. */
  56. public Database db;
  57. /**
  58. * Initialize the connection
  59. * @param host The hostname
  60. * @param port The port
  61. * @param options The extra options to use
  62. * @param tty The terminal to send output to, this is ignored
  63. * @param database The database
  64. * @param username The username
  65. * @param password The password
  66. * @throws PostgresError The error thrown when connection fails
  67. */
  68. public Connection (string host, string port, string options, string tty, string database, string username, string password) throws PostgresError {
  69. db = set_db_login (host, port, options, tty, database, username, password);
  70. if (db.get_status () != Postgres.ConnectionStatus.OK) {
  71. throw new PostgresError.CONNECT (db.get_error_message ());
  72. }
  73. GLib.print (_ ("Postgresql server version:") + " %d\n", db.get_server_version ());
  74. }
  75. /**
  76. * Escape a string to be safe to use in Postgresql queries
  77. * @param str The string to escape
  78. * @return Returns the escaped string
  79. * @throws PostgresError Thrown if there was some problem escaping the string
  80. */
  81. public string escape (string str) throws PostgresError {
  82. string* to = malloc ((sizeof (string) * str.length * 2) + 1); // to has to be double the size of str + 1
  83. int error_code;
  84. db.escape_string_conn (to, str, str.length, out error_code);
  85. if (error_code != 0) {
  86. throw new PostgresError.ESCAPE (db.get_error_message ());
  87. }
  88. string result = to;
  89. free (to);
  90. return result;
  91. }
  92. }
  93. }
  94. }