main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /****************************************************************************
  2. *
  3. * MODULE: db.copy
  4. * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
  5. * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
  6. * PURPOSE: copy a table
  7. * COPYRIGHT: (C) 2003-2006 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. *****************************************************************************/
  14. #include <stdlib.h>
  15. #include <grass/gis.h>
  16. #include <grass/dbmi.h>
  17. #include <grass/glocale.h>
  18. int main(int argc, char **argv)
  19. {
  20. int ret;
  21. struct Option *from_driver, *from_database, *from_table;
  22. struct Option *to_driver, *to_database, *to_table;
  23. struct Option *where, *select;
  24. struct GModule *module;
  25. const char *drv, *db;
  26. G_gisinit(argv[0]);
  27. /* Set description */
  28. module = G_define_module();
  29. G_add_keyword(_("database"));
  30. G_add_keyword(_("attribute table"));
  31. G_add_keyword(_("SQL"));
  32. module->label = _("Copy a table.");
  33. module->description =
  34. _("Either 'from_table' (optionally with 'where') can be used "
  35. "or 'select' option, but not 'from_table' and 'select' at the same time.");
  36. from_driver = G_define_standard_option(G_OPT_DB_DRIVER);
  37. from_driver->key = "from_driver";
  38. from_driver->options = db_list_drivers();
  39. from_driver->description = _("Input driver name");
  40. if ((drv = db_get_default_driver_name()))
  41. from_driver->answer = (char *) drv;
  42. from_database = G_define_standard_option(G_OPT_DB_DATABASE);
  43. from_database->key = "from_database";
  44. from_database->description = _("Input database name");
  45. if ((db = db_get_default_database_name()))
  46. from_database->answer = (char *) db;
  47. from_table = G_define_standard_option(G_OPT_DB_TABLE);
  48. from_table->key = "from_table";
  49. from_table->description =
  50. _("Input table name (only, if 'select' is not used)");
  51. to_driver = G_define_standard_option(G_OPT_DB_DRIVER);
  52. to_driver->key = "to_driver";
  53. to_driver->options = db_list_drivers();
  54. to_driver->required = NO;
  55. to_driver->description = _("Output driver name");
  56. if ((drv = db_get_default_driver_name()))
  57. to_driver->answer = (char *) drv;
  58. to_database = G_define_standard_option(G_OPT_DB_DATABASE);
  59. to_database->key = "to_database";
  60. to_database->description = _("Output database name");
  61. if ((db = db_get_default_database_name()))
  62. to_database->answer = (char *) db;
  63. to_table = G_define_standard_option(G_OPT_DB_TABLE);
  64. to_table->key = "to_table";
  65. to_table->required = YES;
  66. to_table->description = _("Output table name");
  67. to_table->gisprompt = "new,dbtable,dbtable";
  68. where = G_define_standard_option(G_OPT_DB_WHERE);
  69. select = G_define_option();
  70. select->key = "select";
  71. select->type = TYPE_STRING;
  72. select->required = NO;
  73. select->label = _("Full select statement (only, if 'from_table' and 'where' is not used)");
  74. select->description = _("E.g.: SELECT dedek FROM starobince WHERE obec = 'Frimburg'");
  75. if (G_parser(argc, argv))
  76. exit(EXIT_FAILURE);
  77. /* Check options and copy tables */
  78. if (from_table->answer) {
  79. if (select->answer)
  80. G_fatal_error(_("Cannot combine 'from_table' and 'select' options"));
  81. if (where->answer) {
  82. ret =
  83. db_copy_table_where(from_driver->answer,
  84. from_database->answer, from_table->answer,
  85. to_driver->answer, to_database->answer,
  86. to_table->answer, where->answer);
  87. }
  88. else {
  89. ret =
  90. db_copy_table(from_driver->answer, from_database->answer,
  91. from_table->answer, to_driver->answer,
  92. to_database->answer, to_table->answer);
  93. }
  94. }
  95. else {
  96. if (!select->answer)
  97. G_fatal_error(_("Either 'from_table' or 'select' option must be given."));
  98. if (where->answer)
  99. G_fatal_error(_("Cannot combine 'select' and 'where' options"));
  100. ret =
  101. db_copy_table_select(from_driver->answer, from_database->answer,
  102. from_table->answer, to_driver->answer,
  103. to_database->answer, to_table->answer,
  104. select->answer);
  105. }
  106. if (ret == DB_FAILED) {
  107. G_warning(_("Copy table failed"));
  108. exit(EXIT_FAILURE);
  109. }
  110. exit(EXIT_SUCCESS);
  111. }