db.dropcolumn.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python3
  2. ############################################################################
  3. #
  4. # MODULE: db.dropcolumn
  5. # AUTHOR(S): Markus Neteler
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: Interface to db.execute to drop a column from an
  8. # attribute table
  9. # - with special trick for SQLite
  10. # COPYRIGHT: (C) 2007, 2012 by Markus Neteler and the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General
  13. # Public License (>=v2). Read the file COPYING that
  14. # comes with GRASS for details.
  15. #
  16. #############################################################################
  17. # %module
  18. # % description: Drops a column from selected attribute table.
  19. # % keyword: database
  20. # % keyword: attribute table
  21. # %End
  22. # %flag
  23. # % key: f
  24. # % description: Force removal (required for actual deletion of files)
  25. # %end
  26. # %option G_OPT_DB_TABLE
  27. # % required : yes
  28. # %end
  29. # %option G_OPT_DB_COLUMN
  30. # % required : yes
  31. # %end
  32. import sys
  33. import string
  34. from grass.exceptions import CalledModuleError
  35. import grass.script as gscript
  36. def main():
  37. table = options["table"]
  38. column = options["column"]
  39. force = flags["f"]
  40. # check if DB parameters are set, and if not set them.
  41. gscript.run_command("db.connect", flags="c")
  42. kv = gscript.db_connection()
  43. database = kv["database"]
  44. driver = kv["driver"]
  45. # schema needed for PG?
  46. if force:
  47. gscript.message(_("Forcing ..."))
  48. if column == "cat":
  49. gscript.warning(
  50. _(
  51. "Deleting <%s> column which may be needed to keep "
  52. "table connected to a vector map"
  53. )
  54. % column
  55. )
  56. cols = [f[0] for f in gscript.db_describe(table)["cols"]]
  57. if column not in cols:
  58. gscript.fatal(_("Column <%s> not found in table") % column)
  59. if not force:
  60. gscript.message(_("Column <%s> would be deleted.") % column)
  61. gscript.message("")
  62. gscript.message(
  63. _("You must use the force flag (-f) to actually " "remove it. Exiting.")
  64. )
  65. return 0
  66. if driver == "sqlite":
  67. # echo "Using special trick for SQLite"
  68. # http://www.sqlite.org/faq.html#q13
  69. colnames = []
  70. coltypes = []
  71. for f in gscript.db_describe(table)["cols"]:
  72. if f[0] != column:
  73. colnames.append(f[0])
  74. coltypes.append("%s %s" % (f[0], f[1]))
  75. colnames = ", ".join(colnames)
  76. coltypes = ", ".join(coltypes)
  77. cmds = [
  78. "BEGIN TRANSACTION",
  79. "CREATE TEMPORARY TABLE ${table}_backup(${coldef})",
  80. "INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
  81. "DROP TABLE ${table}",
  82. "CREATE TABLE ${table}(${coldef})",
  83. "INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
  84. "DROP TABLE ${table}_backup",
  85. "COMMIT",
  86. ]
  87. tmpl = string.Template(";\n".join(cmds))
  88. sql = tmpl.substitute(table=table, coldef=coltypes, colnames=colnames)
  89. else:
  90. sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
  91. try:
  92. gscript.write_command(
  93. "db.execute", input="-", database=database, driver=driver, stdin=sql
  94. )
  95. except CalledModuleError:
  96. gscript.fatal(_("Cannot continue (problem deleting column)"))
  97. return 0
  98. if __name__ == "__main__":
  99. options, flags = gscript.parser()
  100. sys.exit(main())