db.dropcolumn.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python
  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. # i18N
  37. import os
  38. import gettext
  39. gettext.install('grassmods', os.path.join(os.getenv("GISBASE"), 'locale'))
  40. def main():
  41. table = options['table']
  42. column = options['column']
  43. force = flags['f']
  44. # check if DB parameters are set, and if not set them.
  45. gscript.run_command('db.connect', flags='c')
  46. kv = gscript.db_connection()
  47. database = kv['database']
  48. driver = kv['driver']
  49. # schema needed for PG?
  50. if force:
  51. gscript.message(_("Forcing ..."))
  52. if column == "cat":
  53. gscript.warning(_("Deleting <%s> column which may be needed to keep "
  54. "table connected to a vector map") % column)
  55. cols = [f[0] for f in gscript.db_describe(table)['cols']]
  56. if column not in cols:
  57. gscript.fatal(_("Column <%s> not found in table") % column)
  58. if not force:
  59. gscript.message(_("Column <%s> would be deleted.") % column)
  60. gscript.message("")
  61. gscript.message(_("You must use the force flag (-f) to actually "
  62. "remove it. Exiting."))
  63. return 0
  64. if driver == "sqlite":
  65. # echo "Using special trick for SQLite"
  66. # http://www.sqlite.org/faq.html#q13
  67. colnames = []
  68. coltypes = []
  69. for f in gscript.db_describe(table)['cols']:
  70. if f[0] != column:
  71. colnames.append(f[0])
  72. coltypes.append("%s %s" % (f[0], f[1]))
  73. colnames = ", ".join(colnames)
  74. coltypes = ", ".join(coltypes)
  75. cmds = [
  76. "BEGIN TRANSACTION",
  77. "CREATE TEMPORARY TABLE ${table}_backup(${coldef})",
  78. "INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
  79. "DROP TABLE ${table}",
  80. "CREATE TABLE ${table}(${coldef})",
  81. "INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
  82. "DROP TABLE ${table}_backup",
  83. "COMMIT"
  84. ]
  85. tmpl = string.Template(';\n'.join(cmds))
  86. sql = tmpl.substitute(table=table, coldef=coltypes, colnames=colnames)
  87. else:
  88. sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
  89. try:
  90. gscript.write_command('db.execute', input='-', database=database,
  91. driver=driver, stdin=sql)
  92. except CalledModuleError:
  93. gscript.fatal(_("Cannot continue (problem deleting column)"))
  94. return 0
  95. if __name__ == "__main__":
  96. options, flags = gscript.parser()
  97. sys.exit(main())