db.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**********************************************************
  2. * MODULE: mysql
  3. * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  4. * PURPOSE: MySQL database driver
  5. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  6. * This program is free software under the
  7. * GNU General Public License (>=v2).
  8. * Read the file COPYING that comes with GRASS
  9. * for details.
  10. **********************************************************/
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <grass/dbmi.h>
  14. #include <grass/gis.h>
  15. #include <grass/glocale.h>
  16. #include "globals.h"
  17. #include "proto.h"
  18. int db__driver_open_database(dbHandle * handle)
  19. {
  20. const char *name;
  21. dbConnection default_connection;
  22. MYSQL *res;
  23. db_get_connection(&default_connection);
  24. name = db_get_handle_dbname(handle);
  25. /* if name is empty use default_connection.databaseName */
  26. if (strlen(name) == 0)
  27. name = default_connection.databaseName;
  28. G_debug(3, "db_driver_open_database() mysql: database definition = '%s'",
  29. name);
  30. {
  31. /* Client version */
  32. const char *user, *password, *host, *port;
  33. CONNPAR connpar;
  34. if (parse_conn(name, &connpar) == DB_FAILED) {
  35. db_d_report_error();
  36. return DB_FAILED;
  37. }
  38. G_debug(3, "host = %s, port = %d, dbname = %s, "
  39. "user = %s, password = %s",
  40. connpar.host, connpar.port, connpar.dbname,
  41. connpar.user, connpar.password);
  42. db_get_login2("mysql", name, &user, &password, &host, &port);
  43. connection = mysql_init(NULL);
  44. res = mysql_real_connect(connection, host, user, password,
  45. connpar.dbname, port, NULL, 0);
  46. if (res == NULL) {
  47. db_d_append_error("%s\n%s",
  48. _("Connection failed."),
  49. mysql_error(connection));
  50. db_d_report_error();
  51. return DB_FAILED;
  52. }
  53. }
  54. return DB_OK;
  55. }
  56. int db__driver_close_database(void)
  57. {
  58. mysql_close(connection); /* this will also release connection */
  59. return DB_OK;
  60. }