db.dropcolumn.py 3.0 KB

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