Changeset 63238


Ignore:
Timestamp:
Nov 28, 2014, 2:15:16 AM (10 years ago)
Author:
neteler
Message:

v.db.update: add support for extended SQLite functions

Location:
grass/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • grass/trunk/db/drivers/sqlite/db.c

    r57276 r63238  
    111111    }
    112112
     113    /* enable loading of extensions */
     114    sqlite3_enable_load_extension(sqlite, 1);
     115
    113116    /* set the sqlite busy handler */
    114117    sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
  • grass/trunk/scripts/v.db.update/v.db.update.html

    r56039 r63238  
    1414
    1515<h2>EXAMPLES</h2>
     16
     17<h3>Replacing of NULL values</h3>
    1618
    1719In this example, selectively display lakes without (blue) and with
     
    3840</pre></div>
    3941
    40 Spearfish example: adding new column, copying values from other table
     42<h3>Updating of columns with on the fly calculation</h3>
     43
     44Spearfish example: adding new column, copying values from another table
    4145column with on the fly calculation:
    4246<div class="code"><pre>
     
    4751</pre></div>
    4852
     53<h3>Type casting</h3>
     54
    4955Type cast (type conversion) of strings to double precision
    5056(unsupported by DBF driver):
     
    5359v.db.update mygeodetic_pts col=zval qcol="CAST(z_value AS double precision)" \
    5460            where="z_value &lt;&gt; 'N/A'"
     61</pre></div>
     62
     63<h3>Updating of columns with on the fly calculation (SQLite extended functions)</h3>
     64
     65North Carolina data set example: adding new column, copying values from
     66another table column with on the fly calculation:
     67
     68<div class="code"><pre>
     69g.copy vect=precip_30ynormals,myprecip_30ynormals
     70v.db.addcolumn myprecip_30ynormals column="logjuly double precision"
     71v.db.update myprecip_30ynormals column="logjuly" qcolumn="log(jul)" \
     72  sqliteextra=$HOME/sqlite_extensions/libsqlitefunctions.so
     73
     74v.db.select myprecip_30ynormals columns=jul,logjuly
     75jul|logjuly
     76132.842|4.88916045210132
     77127|4.84418708645859
     78124.206|4.82194147751127
     79104.648|4.65060233738593
     8098.298|4.58800368106618
     81...
    5582</pre></div>
    5683
  • grass/trunk/scripts/v.db.update/v.db.update.py

    r63211 r63238  
    4444#%option G_OPT_DB_WHERE
    4545#%end
     46#%option G_OPT_F_INPUT
     47#% key: sqliteextra
     48#% description: Name of SQLite extension file for extra functions (SQLite backend only)
     49#% gisprompt: old,bin,file
     50#% required: no
     51#%end
    4652
    4753import sys
     
    5662    qcolumn = options['query_column']
    5763    where = options['where']
     64    sqlitefile  = options['sqliteextra']
    5865
    5966    mapset = grass.gisenv()['MAPSET']
     
    7178    database = f['database']
    7279    driver = f['driver']
     80
     81    # check for SQLite backend for extra functions
     82    if sqlitefile and driver != "sqlite":
     83        grass.fatal(_("Use of libsqlitefunctions only with SQLite backend"))
     84    if driver == "sqlite" and sqlitefile:
     85        if not os.access(sqlitefile, os.R_OK):
     86            grass.fatal(_("File <%s> not found") % sqlitefile)
    7387
    7488    # checking column types
     
    94108        cmd += " WHERE " + where
    95109
     110    # SQLite: preload extra functions from extension lib if provided by user
     111    if sqlitefile:
     112        sqliteload = "SELECT load_extension('%s');\n" % sqlitefile
     113        cmd = sqliteload + cmd
     114
    96115    grass.verbose("SQL: \"%s\"" % cmd)
    97 
    98116    grass.write_command('db.execute', input = '-', database = database, driver = driver, stdin = cmd)
    99117
Note: See TracChangeset for help on using the changeset viewer.