db.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. char *name;
  21. dbConnection default_connection;
  22. MYSQL *res;
  23. init_error();
  24. db_get_connection(&default_connection);
  25. name = db_get_handle_dbname(handle);
  26. /* if name is empty use default_connection.databaseName */
  27. if (strlen(name) == 0)
  28. name = default_connection.databaseName;
  29. G_debug(3, "db_driver_open_database() mysql: database definition = '%s'",
  30. name);
  31. {
  32. /* Client version */
  33. char *user, *password;
  34. CONNPAR connpar;
  35. if (parse_conn(name, &connpar) == DB_FAILED) {
  36. report_error();
  37. return DB_FAILED;
  38. }
  39. G_debug(3, "host = %s, port = %d, dbname = %s, "
  40. "user = %s, password = %s",
  41. connpar.host, connpar.port, connpar.dbname,
  42. connpar.user, connpar.password);
  43. db_get_login("mysql", name, &user, &password);
  44. connection = mysql_init(NULL);
  45. res = mysql_real_connect(connection, connpar.host, user, password,
  46. connpar.dbname, connpar.port, NULL, 0);
  47. G_free(user);
  48. G_free(password);
  49. if (res == NULL) {
  50. append_error(_("Cannot connect to MySQL: "));
  51. append_error(mysql_error(connection));
  52. report_error();
  53. return DB_FAILED;
  54. }
  55. }
  56. return DB_OK;
  57. }
  58. int db__driver_close_database(void)
  59. {
  60. init_error();
  61. mysql_close(connection); /* this will also release connection */
  62. return DB_OK;
  63. }