v.db.dropcolumn.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python3
  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. #% keyword: vector
  21. #% keyword: attribute table
  22. #% keyword: 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. from grass.exceptions import CalledModuleError
  38. def main():
  39. map = options['map']
  40. layer = options['layer']
  41. columns = options['columns'].split(',')
  42. mapset = grass.gisenv()['MAPSET']
  43. # does map exist in CURRENT mapset?
  44. if not grass.find_file(map, element='vector', mapset=mapset)['file']:
  45. grass.fatal(_("Vector map <%s> not found in current mapset") % map)
  46. f = grass.vector_layer_db(map, layer)
  47. table = f['table']
  48. keycol = f['key']
  49. database = f['database']
  50. driver = f['driver']
  51. if not table:
  52. grass.fatal(_("There is no table connected to the input vector map. "
  53. "Unable to delete any column. Exiting."))
  54. if keycol in columns:
  55. grass.fatal(_("Unable to delete <%s> column as it is needed to keep table <%s> "
  56. "connected to the input vector map <%s>") %
  57. (keycol, table, map))
  58. for column in columns:
  59. if column not in grass.vector_columns(map, layer):
  60. grass.warning(_("Column <%s> not found in table <%s>. Skipped") % (column, table))
  61. continue
  62. if driver == "sqlite":
  63. # echo "Using special trick for SQLite"
  64. # http://www.sqlite.org/faq.html#q11
  65. colnames = []
  66. coltypes = []
  67. for f in grass.db_describe(table, database=database, driver=driver)['cols']:
  68. if f[0] == column:
  69. continue
  70. colnames.append(f[0])
  71. # see db_sqltype_name() for type names
  72. if f[1] == "CHARACTER":
  73. # preserve field length for sql type "CHARACTER"
  74. coltypes.append("%s %s(%s)" % (f[0], f[1], f[2]))
  75. else:
  76. coltypes.append("%s %s" % (f[0], f[1]))
  77. colnames = ", ".join(colnames)
  78. coltypes = ", ".join(coltypes)
  79. cmds = [
  80. "BEGIN TRANSACTION",
  81. "CREATE TEMPORARY TABLE ${table}_backup (${coldef})",
  82. "INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
  83. "DROP TABLE ${table}",
  84. "CREATE TABLE ${table}(${coldef})",
  85. "INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
  86. "CREATE UNIQUE INDEX ${table}_cat ON ${table} (${keycol} )",
  87. "DROP TABLE ${table}_backup",
  88. "COMMIT"
  89. ]
  90. tmpl = string.Template(';\n'.join(cmds))
  91. sql = tmpl.substitute(table=table, coldef=coltypes, colnames=colnames, keycol=keycol)
  92. else:
  93. sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
  94. try:
  95. grass.write_command('db.execute', input='-', database=database, driver=driver,
  96. stdin=sql)
  97. except CalledModuleError:
  98. grass.fatal(_("Deleting column failed"))
  99. # write cmd history:
  100. grass.vector_history(map)
  101. if __name__ == "__main__":
  102. options, flags = grass.parser()
  103. main()