[GRASS-SVN] r30420 - grass/trunk/scripts/v.db.renamecol
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Mar 2 08:02:33 EST 2008
Author: msieczka
Date: 2008-03-02 08:02:33 -0500 (Sun, 02 Mar 2008)
New Revision: 30420
Modified:
grass/trunk/scripts/v.db.renamecol/v.db.renamecol
Log:
Fix: protect key column, allow renaming 'cat' when it isn't a key column.
Get rid of awk dependency.
Minor fixes.
Modified: grass/trunk/scripts/v.db.renamecol/v.db.renamecol
===================================================================
--- grass/trunk/scripts/v.db.renamecol/v.db.renamecol 2008-03-02 12:55:24 UTC (rev 30419)
+++ grass/trunk/scripts/v.db.renamecol/v.db.renamecol 2008-03-02 13:02:33 UTC (rev 30420)
@@ -63,21 +63,10 @@
PROG=`basename $0`
-#### check if we have awk
-if [ ! -x "`which awk`" ] ; then
- g.message -e "awk required, please install awk or gawk first"
- exit 1
-fi
-
-# setting environment, so that awk works properly in all languages
-unset LC_ALL
-LC_NUMERIC=C
-export LC_NUMERIC
-
#### setup temporary file
TMP="`g.tempfile pid=$$`"
if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
- g.message -e "Unable to create temporary files"
+ g.message -e "Unable to create temporary files."
exit 1
fi
@@ -109,35 +98,38 @@
# does map exist in CURRENT mapset?
eval `g.findfile element=vector file="$GIS_OPT_MAP" mapset="$MAPSET"`
if [ ! "$file" ] ; then
- g.message -e "Vector map '$GIS_OPT_MAP' not found in current mapset"
+ g.message -e "Vector map '$GIS_OPT_MAP' not found in current mapset."
exit 1
fi
-table=`v.db.connect "$GIS_OPT_MAP" -g | grep -w "$GIS_OPT_LAYER" | awk '{print $2}'`
+table=`v.db.connect -g map="$GIS_OPT_MAP" | grep -w "$GIS_OPT_LAYER" | cut -f2 -d" "`
if [ -z "$table" ] ; then
- g.message 'There is no table connected to this map! Cannot rename any column.'
+ g.message -e "There is no table connected to input vector map! Cannot rename any column."
cleanup
exit 1
fi
-database=`v.db.connect "$GIS_OPT_MAP" -g | grep -w $GIS_OPT_LAYER | awk '{print $4}'`
-driver=`v.db.connect "$GIS_OPT_MAP" -g | grep -w $GIS_OPT_LAYER | awk '{print $5}'`
+
+keycol=`v.db.connect -g map=$GIS_OPT_MAP | grep -w $GIS_OPT_LAYER | cut -f3 -d" "`
+database=`v.db.connect -g map="$GIS_OPT_MAP" | grep -w $GIS_OPT_LAYER | cut -f4 -d" "`
+driver=`v.db.connect -g map="$GIS_OPT_MAP" | grep -w $GIS_OPT_LAYER | cut -f5 -d" "`
oldcol="`echo $GIS_OPT_COLUMN | cut -d',' -f1`"
newcol="`echo $GIS_OPT_COLUMN | cut -d',' -f2`"
if [ "$driver" = "dbf" ] ; then
- NAMELEN=`echo "$newcol" | wc -c | awk '{print $1}'`
+ NAMELEN=`echo "$newcol" | wc -c`
#max DBF column name length is 10, but wc counts echo's \n char so we test for 11.
# 'echo -n' (output as string, ie with no newline) is apparently not portable.
if [ "$NAMELEN" -gt 11 ] ; then
- g.message -e "Column name too long <$newcol>: the DBF driver only supports up to 10 characters as column name."
+ g.message -e "Column name <$newcol> too long. The DBF driver supports column names\
+ not longer than 10 characters."
cleanup
exit 1
fi
fi
-if [ "$col" = "cat" ] ; then
- g.message -e "Cannot rename <$col> column as it is needed to keep table \
- connected to vector map"
+if [ "$oldcol" = "$keycol" ] ; then
+ g.message -e "Cannot rename column <$oldcol> as it is needed to keep table <$table>\
+ connected to the input vector map."
cleanup
exit 1
fi
@@ -145,7 +137,7 @@
# old col there?
v.info --quiet -c "$GIS_OPT_MAP" layer=$GIS_OPT_LAYER | cut -d'|' -f1,2 | grep "|${oldcol}$" 2>&1 >/dev/null
if [ $? -ne 0 ] ; then
- g.message -e "Column <$oldcol> not found in table"
+ g.message -e "Column <$oldcol> not found in table."
cleanup
exit 1
fi
@@ -163,7 +155,7 @@
v.db.addcol "$GIS_OPT_MAP" layer=$GIS_OPT_LAYER col="$newcol $oldcoltype" || error
fi
echo "UPDATE $table SET $newcol=$oldcol" | db.execute database=$database driver=$driver
- v.db.dropcol "$GIS_OPT_MAP" layer=$GIS_OPT_LAYER col=$oldcol
+ v.db.dropcol map="$GIS_OPT_MAP" layer=$GIS_OPT_LAYER col=$oldcol
else
echo "ALTER TABLE $table RENAME $oldcol TO $newcol" | db.execute database=$database driver=$driver || error
fi
@@ -175,4 +167,3 @@
v.support "${GIS_OPT_MAP}" cmdhist="${CMDLINE}"
exit 0
-
More information about the grass-commit
mailing list