db.dropcolumn.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(_("Deleting <%s> column which may be needed to keep "
  50. "table connected to a vector map") % column)
  51. cols = [f[0] for f in gscript.db_describe(table)['cols']]
  52. if column not in cols:
  53. gscript.fatal(_("Column <%s> not found in table") % column)
  54. if not force:
  55. gscript.message(_("Column <%s> would be deleted.") % column)
  56. gscript.message("")
  57. gscript.message(_("You must use the force flag (-f) to actually "
  58. "remove it. Exiting."))
  59. return 0
  60. if driver == "sqlite":
  61. # echo "Using special trick for SQLite"
  62. # http://www.sqlite.org/faq.html#q13
  63. colnames = []
  64. coltypes = []
  65. for f in gscript.db_describe(table)['cols']:
  66. if f[0] != column:
  67. colnames.append(f[0])
  68. coltypes.append("%s %s" % (f[0], f[1]))
  69. colnames = ", ".join(colnames)
  70. coltypes = ", ".join(coltypes)
  71. cmds = [
  72. "BEGIN TRANSACTION",
  73. "CREATE TEMPORARY TABLE ${table}_backup(${coldef})",
  74. "INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
  75. "DROP TABLE ${table}",
  76. "CREATE TABLE ${table}(${coldef})",
  77. "INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
  78. "DROP TABLE ${table}_backup",
  79. "COMMIT"
  80. ]
  81. tmpl = string.Template(';\n'.join(cmds))
  82. sql = tmpl.substitute(table=table, coldef=coltypes, colnames=colnames)
  83. else:
  84. sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
  85. try:
  86. gscript.write_command('db.execute', input='-', database=database,
  87. driver=driver, stdin=sql)
  88. except CalledModuleError:
  89. gscript.fatal(_("Cannot continue (problem deleting column)"))
  90. return 0
  91. if __name__ == "__main__":
  92. options, flags = gscript.parser()
  93. sys.exit(main())