dbe.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /* Embedded version */
  32. {
  33. char *datadir, *database;
  34. char *server_args[4];
  35. char *buf;
  36. if (!replace_variables(name, &datadir, &database)) {
  37. append_error(_("Cannot parse MySQL embedded database name"));
  38. append_error(mysql_error(connection));
  39. report_error();
  40. return DB_FAILED;
  41. }
  42. server_args[0] = "mesql"; /* this string is not used */
  43. G_asprintf(&buf, "--datadir=%s", datadir);
  44. server_args[1] = buf;
  45. /* With InnoDB it is very slow to close the database */
  46. server_args[2] = "--skip-innodb"; /* OK? */
  47. /* Without --bootstrap it complains about missing
  48. * mysql.time_zone_leap_second table */
  49. server_args[3] = "--bootstrap"; /* OK? */
  50. if (mysql_server_init(4, server_args, NULL)) {
  51. append_error(_("Cannot initialize MySQL embedded server"));
  52. append_error(mysql_error(connection));
  53. report_error();
  54. free(datadir);
  55. free(database);
  56. return DB_FAILED;
  57. }
  58. connection = mysql_init(NULL);
  59. mysql_options(connection, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
  60. res =
  61. mysql_real_connect(connection, NULL, NULL, NULL, database, 0,
  62. NULL, 0);
  63. free(datadir);
  64. free(database);
  65. if (res == NULL) {
  66. append_error(_("Cannot connect to MySQL embedded server: "));
  67. append_error(mysql_error(connection));
  68. report_error();
  69. return DB_FAILED;
  70. }
  71. }
  72. return DB_OK;
  73. }
  74. int db__driver_close_database(void)
  75. {
  76. init_error();
  77. mysql_close(connection); /* this will also release connection */
  78. mysql_server_end();
  79. return DB_OK;
  80. }