select.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 <grass/gis.h>
  13. #include <grass/dbmi.h>
  14. #include <grass/glocale.h>
  15. #include "globals.h"
  16. #include "proto.h"
  17. int db__driver_open_select_cursor(dbString * sel, dbCursor * dbc, int mode)
  18. {
  19. cursor *c;
  20. dbTable *table;
  21. char *str;
  22. /* allocate cursor */
  23. c = alloc_cursor();
  24. if (c == NULL)
  25. return DB_FAILED;
  26. db_set_cursor_mode(dbc, mode);
  27. db_set_cursor_type_readonly(dbc);
  28. /* \ must be escaped, see explanation in
  29. * db_driver_execute_immediate() */
  30. str = G_str_replace(db_get_string(sel), "\\", "\\\\");
  31. G_debug(3, "Escaped SQL: %s", str);
  32. if (mysql_query(connection, str) != 0) {
  33. db_d_append_error("%s\n%s\n%s",
  34. _("Unable to select data:"),
  35. db_get_string(sel),
  36. mysql_error(connection));
  37. if (str)
  38. G_free(str);
  39. db_d_report_error();
  40. return DB_FAILED;
  41. }
  42. if (str)
  43. G_free(str);
  44. c->res = mysql_store_result(connection);
  45. if (c->res == NULL) {
  46. db_d_append_error("%s\n%s",
  47. db_get_string(sel),
  48. mysql_error(connection));
  49. db_d_report_error();
  50. return DB_FAILED;
  51. }
  52. if (describe_table(c->res, &table, c) == DB_FAILED) {
  53. db_d_append_error(_("Unable to describe table."));
  54. db_d_report_error();
  55. mysql_free_result(c->res);
  56. return DB_FAILED;
  57. }
  58. c->nrows = (int)mysql_num_rows(c->res);
  59. /* record table with dbCursor */
  60. db_set_cursor_table(dbc, table);
  61. /* set dbCursor's token for my cursor */
  62. db_set_cursor_token(dbc, c->token);
  63. return DB_OK;
  64. }