db.dropcolumn.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 by Markus Neteler and the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General Public
  13. # License (>=v2). Read the file COPYING that comes with GRASS
  14. # 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
  27. #% key: table
  28. #% type: string
  29. #% key_desc : name
  30. #% description: Table from which to drop attribute column
  31. #% required : yes
  32. #% gisprompt: old_dbtable,dbtable,dbtable
  33. #%end
  34. #%option
  35. #% key: column
  36. #% type: string
  37. #% description: Name of the column
  38. #% required : yes
  39. #% gisprompt: old,dbcolumn,dbcolumn
  40. #%end
  41. import sys
  42. import os
  43. import string
  44. import grass.script as grass
  45. def main():
  46. table = options['table']
  47. column = options['column']
  48. force = flags['f']
  49. # check if DB parameters are set, and if not set them.
  50. grass.run_command('db.connect', flags = 'c')
  51. kv = grass.db_connection()
  52. database = kv['database']
  53. driver = kv['driver']
  54. # schema needed for PG?
  55. if force:
  56. grass.message(_("Forcing ..."))
  57. if column == "cat":
  58. grass.warning(_("Deleting <%s> column which may be needed to keep table connected to a vector map") % column)
  59. cols = [f[0] for f in grass.db_describe(table)['cols']]
  60. if column not in cols:
  61. grass.fatal(_("Column <%s> not found in table") % column)
  62. if not force:
  63. grass.message(_("Column <%s> would be deleted.") % column)
  64. grass.message("")
  65. grass.message(_("You must use the force flag to actually remove it. Exiting."))
  66. sys.exit(0)
  67. if driver == "sqlite":
  68. #echo "Using special trick for SQLite"
  69. # http://www.sqlite.org/faq.html#q13
  70. colnames = []
  71. coltypes = []
  72. for f in grass.db_describe()['cols']:
  73. if f[0] == column:
  74. continue
  75. colnames.append(f[0])
  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. "DROP TABLE ${table}_backup",
  87. "COMMIT"
  88. ]
  89. tmpl = string.Template(';\n'.join(cmds))
  90. sql = tmpl.substitute(table = table, coldef = coltypes, colnames = colnames)
  91. else:
  92. sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
  93. if grass.write_command('db.execute', input = '-', database = database, driver = driver,
  94. stdin = sql) != 0:
  95. grass.fatal(_("Cannot continue (problem deleting column)."))
  96. if __name__ == "__main__":
  97. options, flags = grass.parser()
  98. main()