db.dropcol.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, attribute table
  20. #%End
  21. #%flag
  22. #% key: f
  23. #% description: Force removal (required for actual deletion of files)
  24. #%end
  25. #%option
  26. #% key: table
  27. #% type: string
  28. #% key_desc : name
  29. #% description: Table from which to drop attribute column
  30. #% required : yes
  31. #% gisprompt: old,dbtable,dbtable
  32. #%end
  33. #%option
  34. #% key: column
  35. #% type: string
  36. #% description: Name of the column
  37. #% required : yes
  38. #% gisprompt: old,dbcolumn,dbcolumn
  39. #%end
  40. import sys
  41. import os
  42. import string
  43. import grass
  44. def main():
  45. table = options['table']
  46. column = options['column']
  47. force = flags['f']
  48. # check if DB parameters are set, and if not set them.
  49. grass.run_command('db.connect', flags = 'c')
  50. kv = grass.db_connection()
  51. database = kv['database']
  52. driver = kv['driver']
  53. # schema needed for PG?
  54. if force:
  55. grass.message("Forcing ...")
  56. if column == "cat":
  57. grass.warning("Deleting <%s> column which may be needed to keep table connected to a vector map" % column)
  58. cols = [f[0] for f in grass.db_describe()['cols']]
  59. if column not in cols:
  60. grass.fatal("Column <%s> not found in table" % column)
  61. if not force:
  62. grass.message("Column <%s> would be deleted." % column)
  63. grass.message("")
  64. grass.message("You must use the force flag to actually remove it. Exiting.")
  65. sys.exit(0)
  66. if driver == "sqlite":
  67. #echo "Using special trick for SQLite"
  68. # http://www.sqlite.org/faq.html#q13
  69. colnames = []
  70. coltypes = []
  71. for f in grass.db_describe()['cols']:
  72. if f[0] == column:
  73. continue
  74. colnames.append(f[0])
  75. coltypes.append("%s %s" % (f[0], f[1]))
  76. colnames = ", ".join(colnames)
  77. coltypes = ", ".join(coltypes)
  78. cmds = [
  79. "BEGIN TRANSACTION",
  80. "CREATE TEMPORARY TABLE ${table}_backup(${coldef})",
  81. "INSERT INTO ${table}_backup SELECT ${colnames} FROM ${table}",
  82. "DROP TABLE ${table}",
  83. "CREATE TABLE ${table}(${coldef})",
  84. "INSERT INTO ${table} SELECT ${colnames} FROM ${table}_backup",
  85. "DROP TABLE ${table}_backup",
  86. "COMMIT"
  87. ]
  88. tmpl = string.Template(';\n'.join(cmds))
  89. sql = tmpl.substitute(table = table, coldef = coltypes, colnames = colnames)
  90. else:
  91. sql = "ALTER TABLE %s DROP COLUMN %s" % (table, column)
  92. if grass.write_command('db.execute', database = database, driver = driver,
  93. stdin = sql) != 0:
  94. grass.fatal("Cannot continue (problem deleting column).")
  95. if __name__ == "__main__":
  96. options, flags = grass.parser()
  97. main()