db.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <grass/dbmi.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <grass/gis.h>
  5. #include "odbc.h"
  6. #include "globals.h"
  7. #include "proto.h"
  8. int db__driver_open_database(dbHandle * handle)
  9. {
  10. char msg[OD_MSG];
  11. const char *name;
  12. SQLRETURN ret;
  13. SQLINTEGER err;
  14. dbConnection connection;
  15. /* Open connection */
  16. if (open_connection() != DB_OK)
  17. return DB_FAILED;
  18. db_get_connection(&connection);
  19. name = db_get_handle_dbname(handle);
  20. /* if name is empty use connection.databaseName */
  21. if (strlen(name) == 0) {
  22. name = connection.databaseName;
  23. }
  24. /* Connect to the datasource */
  25. ret = SQLConnect(ODconn, (SQLCHAR *) name, SQL_NTS,
  26. (SQLCHAR *) connection.user, SQL_NTS,
  27. (SQLCHAR *) connection.password, SQL_NTS);
  28. if ((ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO)) {
  29. SQLGetDiagRec(SQL_HANDLE_DBC, ODconn, 1, NULL, &err, msg, sizeof(msg),
  30. NULL);
  31. db_d_append_error("SQLConnect():\n%s (%d)\n", msg, (int)err);
  32. db_d_report_error();
  33. return DB_FAILED;
  34. }
  35. return DB_OK;
  36. }
  37. int db__driver_close_database()
  38. {
  39. SQLDisconnect(ODconn);
  40. close_connection();
  41. return DB_OK;
  42. }