~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to scripts/v.db.dropcol/v.db.dropcol

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
 
3
 
############################################################################
4
 
#
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
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
 
 
19
 
 
20
 
#%Module
21
 
#%  description: Drops a column from the attribute table connected to a given vector map.
22
 
#%  keywords: vector, database, attribute table
23
 
#%End
24
 
 
25
 
#%option
26
 
#% key: map
27
 
#% type: string
28
 
#% gisprompt: old,vector,vector
29
 
#% key_desc : name
30
 
#% description: Vector map for which to drop attribute column
31
 
#% required : yes
32
 
#%end
33
 
 
34
 
#%option
35
 
#% key: layer
36
 
#% gisprompt: old_layer,layer,layer
37
 
#% description: Layer where to drop column
38
 
#% answer: 1
39
 
#% required : no
40
 
#%end
41
 
 
42
 
#%option
43
 
#% key: column
44
 
#% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
45
 
#% description: Name of the column to drop
46
 
#% required : yes
47
 
#%end
48
 
 
49
 
 
50
 
if  [ -z "$GISBASE" ] ; then
51
 
    echo "You must be in GRASS GIS to run this program." 1>&2
52
 
    exit 1
53
 
fi
54
 
 
55
 
# save command line
56
 
if [ "$1" != "@ARGS_PARSED@" ] ; then
57
 
    CMDLINE=`basename "$0"`
58
 
    for arg in "$@" ; do
59
 
        CMDLINE="$CMDLINE \"$arg\""
60
 
    done
61
 
    export CMDLINE
62
 
    exec g.parser "$0" "$@"
63
 
fi
64
 
 
65
 
PROG=`basename "$0"`
66
 
 
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'
71
 
    exit 1
72
 
fi
73
 
 
74
 
cleanup()
75
 
{
76
 
 \rm -f "$TEMPFILE" "$TEMPFILE.coltypes" "$TEMPFILE.colnames" "$TEMPFILE.coldesc"
77
 
}
78
 
 
79
 
# what to do in case of user break:
80
 
exitprocedure()
81
 
{
82
 
 g.message -e 'User break!'
83
 
 cleanup
84
 
 exit 1
85
 
}
86
 
# shell check for user break (signal list: trap -l)
87
 
trap "exitprocedure" 2 3 15
88
 
 
89
 
### setup enviro vars ###
90
 
MAPSET=`g.gisenv get=MAPSET`
91
 
 
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"
96
 
   exit 1
97
 
fi
98
 
 
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'
102
 
   cleanup
103
 
   exit 1
104
 
fi
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"
109
 
 
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>"
113
 
   cleanup
114
 
   exit 1
115
 
fi
116
 
 
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>"
120
 
        cleanup
121
 
        exit 1
122
 
fi
123
 
 
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};
136
 
DROP TABLE ${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)'
145
 
                cleanup
146
 
                exit 1
147
 
        fi
148
 
else
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)'
152
 
                cleanup
153
 
                exit 1
154
 
        fi
155
 
fi
156
 
 
157
 
# cleanup
158
 
cleanup
159
 
 
160
 
# write cmd history:
161
 
v.support map=${GIS_OPT_MAP} cmdhist="${CMDLINE}"
162
 
 
163
 
exit 0