3
############################################################################
5
# MODULE: v.db.dropcolumn
6
# AUTHOR(S): Markus Neteler
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.addcol
10
# - with special trick for SQLite
11
# COPYRIGHT: (C) 2007 by the GRASS Development Team
13
# This program is free software under the GNU General Public
14
# License (>=v2). Read the file COPYING that comes with GRASS
17
#############################################################################
21
#% description: Drops a column from the attribute table connected to a given vector map.
22
#% keywords: vector, database, attribute table
28
#% gisprompt: old,vector,vector
30
#% description: Vector map for which to drop attribute column
36
#% gisprompt: old_layer,layer,layer
37
#% description: Layer where to drop column
44
#% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
45
#% description: Name of the column to drop
50
if [ -z "$GISBASE" ] ; then
51
echo "You must be in GRASS GIS to run this program." 1>&2
56
if [ "$1" != "@ARGS_PARSED@" ] ; then
57
CMDLINE=`basename "$0"`
59
CMDLINE="$CMDLINE \"$arg\""
62
exec g.parser "$0" "$@"
67
#### setup temporary file
68
TEMPFILE="`g.tempfile pid=$$`"
69
if [ $? -ne 0 ] || [ -z "$TEMPFILE" ] ; then
70
g.message -e 'Unable to create temporary files'
76
\rm -f "$TEMPFILE" "$TEMPFILE.coltypes" "$TEMPFILE.colnames" "$TEMPFILE.coldesc"
79
# what to do in case of user break:
82
g.message -e 'User break!'
86
# shell check for user break (signal list: trap -l)
87
trap "exitprocedure" 2 3 15
89
### setup enviro vars ###
90
MAPSET=`g.gisenv get=MAPSET`
92
# does map exist in CURRENT mapset?
93
eval `g.findfile element=vector file="$GIS_OPT_MAP" mapset="$MAPSET"`
94
if [ ! "$file" ] ; then
95
g.message -e "Vector map <$GIS_OPT_MAP> not found in current mapset"
99
table=`v.db.connect map="$GIS_OPT_MAP" -gl layer="$GIS_OPT_LAYER" fs="|" | cut -f2 -d"|"`
100
if [ -z "$table" ] ; then
101
g.message -e 'There is no table connected to the input vector map! Cannot delete any column'
105
keycol=`v.db.connect -gl fs="|" map="$GIS_OPT_MAP" layer="$GIS_OPT_LAYER" | cut -f3 -d'|'`
106
database=`v.db.connect -gl fs="|" map="$GIS_OPT_MAP" layer="$GIS_OPT_LAYER" | cut -f4 -d'|'`
107
driver=`v.db.connect -gl fs="|" map="$GIS_OPT_MAP" layer="$GIS_OPT_LAYER" | cut -f5 -d'|'`
108
col="$GIS_OPT_COLUMN"
110
if [ "$col" = "$keycol" ] ; then
111
g.message -e "Cannot delete <$col> column as it is needed to keep table\
112
<$table> connected to the input vector map <$GIS_OPT_MAP>"
117
v.info --q -c map=$GIS_OPT_MAP layer=$GIS_OPT_LAYER | cut -d'|' -f1,2 | grep "|${col}$" 2>&1 >/dev/null
118
if [ $? -ne 0 ] ; then
119
g.message -e "Column <$col> not found in table <$table>"
124
if [ "$driver" = "sqlite" ] ; then
125
#echo "Using special trick for SQLite"
126
# http://www.sqlite.org/faq.html#q13
127
v.info --q -c map=$GIS_OPT_MAP layer=$GIS_OPT_LAYER | cut -d'|' -f1,2 | grep -v "|${col}$" > "$TEMPFILE.coldesc"
128
# need to revert order:
129
cat "$TEMPFILE.coldesc" | cut -d'|' -f1 > "$TEMPFILE.coltypes"
130
cat "$TEMPFILE.coldesc" | cut -d'|' -f2 > "$TEMPFILE.colnames"
131
COLDEF=`paste -d' ' "$TEMPFILE.colnames" "$TEMPFILE.coltypes" | tr '\n' ',' | sed 's+,$++g'`
132
COLNAMES=`cat "$TEMPFILE.colnames" | tr '\n' ',' | sed 's+,$++g'`
133
echo "BEGIN TRANSACTION;
134
CREATE TEMPORARY TABLE ${table}_backup(${COLDEF});
135
INSERT INTO ${table}_backup SELECT ${COLNAMES} FROM ${table};
137
CREATE TABLE ${table}(${COLDEF});
138
INSERT INTO ${table} SELECT ${COLNAMES} FROM ${table}_backup;
139
CREATE UNIQUE INDEX ${table}_cat ON ${table} ( ${keycol} );
140
DROP TABLE ${table}_backup;
141
COMMIT;" > "$TEMPFILE"
142
db.execute input="$TEMPFILE"
143
if [ $? -eq 1 ] ; then
144
g.message -e 'Cannot continue (problem deleting column)'
149
echo "ALTER TABLE $table DROP COLUMN $col" | db.execute database="${database}" driver=${driver}
150
if [ $? -eq 1 ] ; then
151
g.message -e 'Cannot continue (problem deleting column)'
161
v.support map=${GIS_OPT_MAP} cmdhist="${CMDLINE}"