table.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*!
  2. * \file db/dbmi_client/table.c
  3. *
  4. * \brief DBMI Library (client) - table management
  5. *
  6. * (C) 1999-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. * \author Joel Jones (CERL/UIUC), Radim Blazek
  13. */
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <grass/gis.h>
  17. #include <grass/dbmi.h>
  18. #include <grass/glocale.h>
  19. /*!
  20. \brief Check if table exists
  21. \param drvname driver name
  22. \param dbname database name
  23. \param tabname table name
  24. \return 1 exist
  25. \return 0 doesn't exist
  26. \return -1 error
  27. */
  28. int db_table_exists(const char *drvname, const char *dbname, const char *tabname)
  29. {
  30. dbDriver *driver;
  31. dbString *names;
  32. int i, count, found = 0;
  33. int full = 0;
  34. char buf[1000];
  35. char *bufp, *c;
  36. if (strchr(tabname, '.'))
  37. full = 1;
  38. driver = db_start_driver_open_database(drvname, dbname);
  39. if (driver == NULL) {
  40. G_warning(_("Unable open database <%s> by driver <%s>"), dbname,
  41. drvname);
  42. return -1;
  43. }
  44. /* The table tabname can be either fully qualified in form table.schema,
  45. * or it can be only table name. If the name is fully qualified, compare whole name,
  46. * if it is not, compare only table names */
  47. /* user tables */
  48. if (db_list_tables(driver, &names, &count, 0) != DB_OK)
  49. return (-1);
  50. for (i = 0; i < count; i++) {
  51. strcpy(buf, db_get_string(&names[i]));
  52. bufp = buf;
  53. if (!full && (c = strchr(buf, '.'))) {
  54. bufp = c + 1;
  55. }
  56. G_debug(2, "table = %s -> %s", buf, bufp);
  57. if (G_strcasecmp(tabname, bufp) == 0) {
  58. found = 1;
  59. break;
  60. }
  61. }
  62. db_free_string_array(names, count);
  63. if (!found) { /* system tables */
  64. if (db_list_tables(driver, &names, &count, 1) != DB_OK)
  65. return (-1);
  66. for (i = 0; i < count; i++) {
  67. strcpy(buf, db_get_string(&names[i]));
  68. bufp = buf;
  69. if (!full && (c = strchr(buf, '.'))) {
  70. bufp = c + 1;
  71. }
  72. if (G_strcasecmp(tabname, bufp) == 0) {
  73. found = 1;
  74. break;
  75. }
  76. }
  77. db_free_string_array(names, count);
  78. }
  79. db_close_database_shutdown_driver(driver);
  80. return (found);
  81. }
  82. /*!
  83. \brief Get number of rows of table
  84. \param driver db driver
  85. \param sql SQL statement
  86. \return number of records
  87. \return -1
  88. */
  89. int db_get_table_number_of_rows(dbDriver * driver, dbString * sql)
  90. {
  91. int nrows;
  92. dbCursor cursor;
  93. if (db_open_select_cursor(driver, sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
  94. G_warning(_("Unable to open select cursor: '%s'"), db_get_string(sql));
  95. db_close_database_shutdown_driver(driver);
  96. return -1;
  97. }
  98. nrows = db_get_num_rows(&cursor);
  99. db_close_cursor(&cursor);
  100. return nrows;
  101. }