v.db.dropcolumn.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.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 the
  8. # attribute table connected to a given vector map
  9. # - Based on v.db.addcolumn
  10. # - with special trick for SQLite
  11. # COPYRIGHT: (C) 2007 by the GRASS Development Team
  12. #
  13. # This program is free software under the GNU General Public
  14. # License (>=v2). Read the file COPYING that comes with GRASS
  15. # for details.
  16. #
  17. #############################################################################
  18. #%module
  19. #% description: Drops a column from the attribute table connected to a given vector map.
  20. #% keywords: vector
  21. #% keywords: attribute table
  22. #% keywords: database
  23. #%end
  24. #%option G_OPT_V_MAP
  25. #% key: map
  26. #%end
  27. #%option G_OPT_V_FIELD
  28. #%end
  29. #%option G_OPT_DB_COLUMNS
  30. #% description: Name of attribute column(s) to drop
  31. #% required: yes
  32. #%end
  33. import sys
  34. import os
  35. import string
  36. import grass.script as grass
  37. def main():
  38. map = options['map']
  39. layer = options['layer']
  40. columns = options['columns'].split(',')
  41. mapset = grass.gisenv()['MAPSET']
  42. # does map exist in CURRENT mapset?
  43. if not grass.find_file(map, element = 'vector', mapset = mapset):
  44. grass.fatal(_("Vector map <%s> not found in current mapset") % map)
  45. f = grass.vector_layer_db(map, layer)
  46. table = f['table']
  47. keycol = f['key']
  48. database = f['database']
  49. driver = f['driver']
  50. if not table:
  51. grass.fatal(_("There is no table connected to the input vector map. "
  52. "Unable to delete any column. Exiting."))
  53. if keycol in columns:
  54. grass.fatal(_("Unable to delete <%s> column as it is needed to keep table <%s> "
  55. "connected to the input vector map <%s>") % \
  56. (keycol, table, map))
  57. for column in columns:
  58. if not grass.vector_columns(map, layer).has_key(column):
  59. grass.warning(_("Column <%s> not found in table <%s>. Skipped") % (column, table))
  60. continue
  61. if driver == "sqlite":
  62. # echo "Using special trick for SQLite"
  63. # http://www.sqlite.org/faq.html#q11
  64. colnames = []
  65. coltypes = []
  66. for f in grass.db_describe(table, database = database, driver = driver)['cols']:
  67. if f[0] == column:
  68. continue
  69. colnames.append(f[0])
  70. coltypes.append("%s %s" % (f[0], f[1]))
  71. colnames = ", ".join(colnames)
  72. coltypes = ", ".join(coltypes)
  73. cmds = [
  74. "BEGIN TRANSACTION",
  75. "CREATE TEMPORARY TABLE ${table}_backup(${coldef})",
  76. "INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
  77. "DROP TABLE ${table}",
  78. "CREATE TABLE ${table}(${coldef})",
  79. "INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
  80. "CREATE UNIQUE INDEX ${table}_cat ON ${table} (${keycol} )",
  81. "DROP TABLE ${table}_backup",
  82. "COMMIT"
  83. ]
  84. tmpl = string.Template(';\n'.join(cmds))
  85. sql = tmpl.substitute(table = table, coldef = coltypes, colnames = colnames, keycol = keycol)
  86. else:
  87. sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
  88. if grass.write_command('db.execute', input = '-', database = database, driver = driver,
  89. stdin = sql) != 0:
  90. grass.fatal(_("Deleting column failed"))
  91. # write cmd history:
  92. grass.vector_history(map)
  93. if __name__ == "__main__":
  94. options, flags = grass.parser()
  95. main()