#!/bin/sh
#
############################################################################
#
# MODULE:       v.db.update
# AUTHOR(S):    Moritz Lennert
#               extensions by Markus Neteler
# PURPOSE:      interface to db.execute to update a column in the attribute table connected to a given map
# COPYRIGHT:    (C) 2005,2007 by the GRASS Development Team
#
#               This program is free software under the GNU General Public
#               License (>=v2). Read the file COPYING that comes with GRASS
#               for details.
#
#############################################################################

#%Module
#%  description: Updates a column in the attribute table connected to a vector map.
#%  keywords: vector, database, attribute table
#%End
#%option
#% key: map
#% type: string
#% gisprompt: old,vector,vector
#% description: Vector map to edit the attribute table for
#% required : yes
#%end
#%option
#% key: layer
#% gisprompt: old_layer,layer,layer
#% gisprompt: old_layer,layer,layer
#% description: Layer to which the table to be changed is connected
#% answer: 1
#% required : no
#%end
#%option
#% key: column
#% type: string
#% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
#% description: Column to update
#% required : yes
#%end
#%option
#% key: value
#% type: string
#% label: Literal value to update the column with
#% description: Varchar values must be in single quotes, e.g. 'grass'
#% required : no
#%end
#%option
#% key: qcolumn
#% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
#% type: string
#% label: Name of attribute column to query
#% description: Can be a combination of columns, e.g. col1+col2
#% required : no
#%end
#%option
#% key: where
#% type: string
#% description: WHERE conditions for update, without 'where' keyword (e.g. cat=1 or col1/col2>1)
#% required : no
#%end

if  [ -z "$GISBASE" ] ; then
    echo "You must be in GRASS GIS to run this program." >&2
 exit 1
fi

# save command line
if [ "$1" != "@ARGS_PARSED@" ] ; then
    CMDLINE=`basename "$0"`
    for arg in "$@" ; do
        CMDLINE="$CMDLINE \"$arg\""
    done
    export CMDLINE
    exec g.parser "$0" "$@"
fi

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 enviro vars ###
eval `g.gisenv`
: ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}

# 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"
   exit 1
fi

table=`v.db.connect map="$GIS_OPT_MAP" -gl fs="|" layer="$GIS_OPT_LAYER" | cut -f2 -d'|'`
if [ -z "$table" ] ; then
   g.message -e 'There is no table connected to this map! Run v.db.connect or v.db.addtable first.'
   exit 1
fi

database=`v.db.connect map="$GIS_OPT_MAP" -gl fs="|" layer="$GIS_OPT_LAYER" | cut -f4 -d'|'`
driver=`v.db.connect map="$GIS_OPT_MAP" -gl fs="|" layer="$GIS_OPT_LAYER" | cut -f5 -d'|'`

# checking column types
coltype=`v.info -c map="$GIS_OPT_MAP" layer=$GIS_OPT_LAYER --quiet | grep -w "$GIS_OPT_COLUMN" | cut -d'|' -f1`

if [ -z "$GIS_OPT_QCOLUMN" ] ; then
   if [ -z "$GIS_OPT_VALUE" ] ; then
      g.message -e 'Either value or qcolumn parameter have to be used'
      exit 1
   fi
fi
if [ ! -z "$GIS_OPT_QCOLUMN" ] ; then
   if [ ! -z "$GIS_OPT_VALUE" ] ; then
      g.message -e 'Either value or qcolumn parameter have to be used'
      exit 1
   fi
fi

if [ ! -z "$GIS_OPT_QCOLUMN" ] ; then
   # special case: we copy from another column
   value="$GIS_OPT_QCOLUMN"
else
   # we insert a value
   if [ "$coltype" = "INTEGER" -o "$coltype" = "DOUBLE PRECISION" ] ; then
       value="$GIS_OPT_VALUE"
   else
       value="'$GIS_OPT_VALUE'"
   fi
fi

if [ -n "$GIS_OPT_WHERE" ] ; then
    g.message -v message="SQL: \"UPDATE $table SET $GIS_OPT_COLUMN=$value WHERE $GIS_OPT_WHERE\""
    echo "UPDATE $table SET $GIS_OPT_COLUMN=$value WHERE $GIS_OPT_WHERE" |
       db.execute database="${database}" driver=${driver}
else
    g.message -v message="SQL: \"UPDATE $table SET $GIS_OPT_COLUMN=$value\""
    echo "UPDATE $table SET $GIS_OPT_COLUMN=$value" | \
       db.execute database="${database}" driver=${driver}
fi

# write cmd history:
v.support "${GIS_OPT_MAP}" cmdhist="${CMDLINE}"

exit 0
