v.db.dropcolumn.py 4.2 KB

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