v.db.dropcolumn.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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: database
  22. #% keywords: attribute table
  23. #%End
  24. #%option
  25. #% key: map
  26. #% type: string
  27. #% gisprompt: old,vector,vector
  28. #% key_desc : name
  29. #% description: Vector map for which to drop attribute column
  30. #% required : yes
  31. #%end
  32. #%option
  33. #% key: layer
  34. #% type: integer
  35. #% description: Layer where to drop column
  36. #% answer: 1
  37. #% required : no
  38. #%end
  39. #%option
  40. #% key: column
  41. #% type: string
  42. #% description: Name of the column
  43. #% required : yes
  44. #%end
  45. import sys
  46. import os
  47. import string
  48. import grass.script as grass
  49. def main():
  50. map = options['map']
  51. layer = options['layer']
  52. column = options['column']
  53. mapset = grass.gisenv()['MAPSET']
  54. # does map exist in CURRENT mapset?
  55. if not grass.find_file(map, element = 'vector', mapset = mapset):
  56. grass.fatal(_("Vector map <%s> not found in current mapset") % map)
  57. f = grass.vector_layer_db(map, layer)
  58. table = f['table']
  59. keycol = f['key']
  60. database = f['database']
  61. driver = f['driver']
  62. if not table:
  63. grass.fatal(_("There is no table connected to the input vector map. "
  64. "Unable to delete any column. Exiting."))
  65. if column == keycol:
  66. grass.fatal(_("Unable to delete <%s> column as it is needed to keep table <%s> "
  67. "connected to the input vector map <%s>") % \
  68. (column, table, map))
  69. if not grass.vector_columns(map, layer).has_key(column):
  70. grass.fatal(_("Column <%s> not found in table <%s>") % (column, table))
  71. if driver == "sqlite":
  72. # echo "Using special trick for SQLite"
  73. # http://www.sqlite.org/faq.html#q13
  74. colnames = []
  75. coltypes = []
  76. for f in grass.db_describe(table)['cols']:
  77. if f[0] == column:
  78. continue
  79. colnames.append(f[0])
  80. coltypes.append("%s %s" % (f[0], f[1]))
  81. colnames = ", ".join(colnames)
  82. coltypes = ", ".join(coltypes)
  83. cmds = [
  84. "BEGIN TRANSACTION",
  85. "CREATE TEMPORARY TABLE ${table}_backup(${coldef})",
  86. "INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
  87. "DROP TABLE ${table}",
  88. "CREATE TABLE ${table}(${coldef})",
  89. "INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
  90. "CREATE UNIQUE INDEX ${table}_cat ON ${table} (${keycol} )",
  91. "DROP TABLE ${table}_backup",
  92. "COMMIT"
  93. ]
  94. tmpl = string.Template(';\n'.join(cmds))
  95. sql = tmpl.substitute(table = table, coldef = coltypes, colnames = colnames, keycol = keycol)
  96. else:
  97. sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
  98. if grass.write_command('db.execute', input = '-', database = database, driver = driver,
  99. stdin = sql) != 0:
  100. grass.fatal(_("Deleting column failed"))
  101. # write cmd history:
  102. grass.vector_history(map)
  103. if __name__ == "__main__":
  104. options, flags = grass.parser()
  105. main()