dbe.c 2.6 KB

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