c_create_idx.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*!
  2. * \file db/dbmi_client/c_create_idx.c
  3. *
  4. * \brief DBMI Library (client) - create index
  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 <string.h>
  15. #include <grass/dbmi.h>
  16. #include "macros.h"
  17. /*!
  18. \brief Create index
  19. \param driver db driver
  20. \param index index info (pointer to dbIndex structure)
  21. \return DB_OK on success
  22. \return DB_FAILED on failure
  23. */
  24. int db_create_index(dbDriver * driver, dbIndex * index)
  25. {
  26. int ret_code;
  27. /* start the procedure call */
  28. db__set_protocol_fds(driver->send, driver->recv);
  29. DB_START_PROCEDURE_CALL(DB_PROC_CREATE_INDEX);
  30. /* send the arguments to the procedure */
  31. DB_SEND_INDEX(index);
  32. /* get the return code for the procedure call */
  33. DB_RECV_RETURN_CODE(&ret_code);
  34. if (ret_code != DB_OK)
  35. return ret_code; /* ret_code SHOULD == DB_FAILED */
  36. /* get results */
  37. DB_RECV_STRING(&index->indexName);
  38. return DB_OK;
  39. }
  40. /*!
  41. \brief Create unique index
  42. \param driver db driver
  43. \param table_name table name
  44. \param column_name column name (where to create index)
  45. \return DB_OK on success
  46. \return DB_FAILED on failure
  47. */
  48. int db_create_index2(dbDriver * driver, const char *table_name,
  49. const char *column_name)
  50. {
  51. int ret;
  52. dbIndex index;
  53. char buf[1000];
  54. const char *tbl;
  55. db_init_index(&index);
  56. db_alloc_index_columns(&index, 1);
  57. tbl = strchr(table_name, '.');
  58. if (tbl == NULL)
  59. tbl = table_name;
  60. else
  61. tbl++;
  62. sprintf(buf, "%s_%s", tbl, column_name);
  63. db_set_index_name(&index, buf);
  64. db_set_index_table_name(&index, table_name);
  65. db_set_index_column_name(&index, 0, column_name);
  66. db_set_index_type_unique(&index);
  67. ret = db_create_index(driver, &index);
  68. db_free_index(&index);
  69. return ret;
  70. }