[GRASS-SVN] r47225 - grass/trunk/db/drivers/ogr

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jul 22 09:49:25 EDT 2011


Author: martinl
Date: 2011-07-22 06:49:25 -0700 (Fri, 22 Jul 2011)
New Revision: 47225

Added:
   grass/trunk/db/drivers/ogr/execute.c
Modified:
   grass/trunk/db/drivers/ogr/cursor.c
   grass/trunk/db/drivers/ogr/db.c
   grass/trunk/db/drivers/ogr/dbdriver.h
   grass/trunk/db/drivers/ogr/describe.c
   grass/trunk/db/drivers/ogr/driver.c
   grass/trunk/db/drivers/ogr/error.c
   grass/trunk/db/drivers/ogr/fetch.c
   grass/trunk/db/drivers/ogr/globals.h
   grass/trunk/db/drivers/ogr/listtab.c
   grass/trunk/db/drivers/ogr/select.c
Log:
dbmi-ogr driver: implement write support (only UPDATE statements supported)


Modified: grass/trunk/db/drivers/ogr/cursor.c
===================================================================
--- grass/trunk/db/drivers/ogr/cursor.c	2011-07-22 12:59:28 UTC (rev 47224)
+++ grass/trunk/db/drivers/ogr/cursor.c	2011-07-22 13:49:25 UTC (rev 47225)
@@ -1,27 +1,24 @@
+/*!
+  \file db/drivers/cursor.c
+  
+  \brief Low level OGR SQL driver
+ 
+  (C) 2004-2009 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.
+  
+  \author Radim Blazek
+  \author Some updates by Martin Landa <landa.martin gmail.com>
+*/
 
-/*****************************************************************************
-*
-* MODULE:       OGR driver 
-*   	    	
-* AUTHOR(S):    Radim Blazek
-*               Some updates by Martin Landa <landa.martin gmail.com>
-*
-* PURPOSE:      DB driver for OGR sources     
-*
-* COPYRIGHT:    (C) 2004-2009 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.
-*
-*****************************************************************************/
 #include <stdio.h>
 
 #include <grass/gis.h>
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 
-#include "ogr_api.h"
+#include <ogr_api.h>
+
 #include "globals.h"
 #include "proto.h"
 
@@ -71,15 +68,14 @@
 	return NULL;
     }
 
+    G_zero(c, sizeof(cursor));
+    
     /* tokenize it */
     c->token = db_new_token(c);
     if (c->token < 0) {
 	append_error(_("Unable to add new token"));
 	return NULL;
     }
-
-    c->hFeature = NULL;
-
     return c;
 }
 

Modified: grass/trunk/db/drivers/ogr/db.c
===================================================================
--- grass/trunk/db/drivers/ogr/db.c	2011-07-22 12:59:28 UTC (rev 47224)
+++ grass/trunk/db/drivers/ogr/db.c	2011-07-22 13:49:25 UTC (rev 47225)
@@ -1,21 +1,16 @@
+/*!
+  \file db/drivers/db.c
+  
+  \brief Low level OGR SQL driver
+ 
+  (C) 2004-2009 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.
+  
+  \author Radim Blazek
+  \author Some updates by Martin Landa <landa.martin gmail.com>
+*/
 
-/*****************************************************************************
-*
-* MODULE:       OGR driver 
-*   	    	
-* AUTHOR(S):    Radim Blazek
-*               Some updates by Martin Landa <landa.martin gmail.com>
-*
-* PURPOSE:      DB driver for OGR sources     
-*
-* COPYRIGHT:    (C) 2004-2009 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.
-*
-*****************************************************************************/
-
 #include <stdlib.h>
 #include <string.h>
 
@@ -23,7 +18,8 @@
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 
-#include "ogr_api.h"
+#include <ogr_api.h>
+
 #include "globals.h"
 #include "proto.h"
 
@@ -52,7 +48,7 @@
 
     OGRRegisterAll();
 
-    hDs = OGROpen(name, FALSE, NULL);
+    hDs = OGROpen(name, TRUE, NULL);
 
     if (hDs == NULL) {
 	append_error(_("Unable to open OGR data source"));

Modified: grass/trunk/db/drivers/ogr/dbdriver.h
===================================================================
--- grass/trunk/db/drivers/ogr/dbdriver.h	2011-07-22 12:59:28 UTC (rev 47224)
+++ grass/trunk/db/drivers/ogr/dbdriver.h	2011-07-22 13:49:25 UTC (rev 47225)
@@ -10,6 +10,7 @@
 int db__driver_describe_table();
 int db__driver_init();
 int db__driver_finish();
+int db__driver_execute_immediate();
 int db__driver_fetch();
 int db__driver_get_num_rows();
 int db__driver_list_tables();
@@ -22,6 +23,7 @@
 db_driver_describe_table = db__driver_describe_table;\
 db_driver_init = db__driver_init;\
 db_driver_finish = db__driver_finish;\
+db_driver_execute_immediate = db__driver_execute_immediate;\
 db_driver_fetch = db__driver_fetch;\
 db_driver_get_num_rows = db__driver_get_num_rows;\
 db_driver_list_tables = db__driver_list_tables;\

Modified: grass/trunk/db/drivers/ogr/describe.c
===================================================================
--- grass/trunk/db/drivers/ogr/describe.c	2011-07-22 12:59:28 UTC (rev 47224)
+++ grass/trunk/db/drivers/ogr/describe.c	2011-07-22 13:49:25 UTC (rev 47225)
@@ -1,27 +1,23 @@
+/*!
+  \file db/drivers/describe.c
+  
+  \brief Low level OGR SQL driver
+ 
+  (C) 2004-2009 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.
+  
+  \author Radim Blazek
+  \author Some updates by Martin Landa <landa.martin gmail.com>
+*/
 
-/*****************************************************************************
-*
-* MODULE:       OGR driver 
-*   	    	
-* AUTHOR(S):    Radim Blazek
-*               Some updates by Martin Landa <landa.martin gmail.com>
-*
-* PURPOSE:      DB driver for OGR sources     
-*
-* COPYRIGHT:    (C) 2004-2009 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.
-*
-*****************************************************************************/
-
 #include <grass/gis.h>
 #include <grass/datetime.h>
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 
-#include "ogr_api.h"
+#include <ogr_api.h>
+
 #include "globals.h"
 #include "proto.h"
 

Modified: grass/trunk/db/drivers/ogr/driver.c
===================================================================
--- grass/trunk/db/drivers/ogr/driver.c	2011-07-22 12:59:28 UTC (rev 47224)
+++ grass/trunk/db/drivers/ogr/driver.c	2011-07-22 13:49:25 UTC (rev 47225)
@@ -1,21 +1,16 @@
+/*!
+  \file db/drivers/driver.c
+  
+  \brief Low level OGR SQL driver
+ 
+  (C) 2004-2009 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.
+  
+  \author Radim Blazek
+  \author Some updates by Martin Landa <landa.martin gmail.com>
+*/
 
-/*****************************************************************************
-*
-* MODULE:       OGR driver 
-*   	    	
-* AUTHOR(S):    Radim Blazek
-*               Some updates by Martin Landa <landa.martin gmail.com>
-*
-* PURPOSE:      DB driver for OGR sources     
-*
-* COPYRIGHT:    (C) 2004-2009 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.
-*
-*****************************************************************************/
-
 #include <grass/dbmi.h>
 
 #include "ogr_api.h"

Modified: grass/trunk/db/drivers/ogr/error.c
===================================================================
--- grass/trunk/db/drivers/ogr/error.c	2011-07-22 12:59:28 UTC (rev 47224)
+++ grass/trunk/db/drivers/ogr/error.c	2011-07-22 13:49:25 UTC (rev 47225)
@@ -1,21 +1,16 @@
+/*!
+  \file db/drivers/error.c
+  
+  \brief Low level OGR SQL driver
+ 
+  (C) 2004-2009 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.
+  
+  \author Radim Blazek
+  \author Some updates by Martin Landa <landa.martin gmail.com>
+*/
 
-/*****************************************************************************
-*
-* MODULE:       OGR driver 
-*   	    	
-* AUTHOR(S):    Radim Blazek
-*               Some updates by Martin Landa <landa.martin gmail.com>
-*
-* PURPOSE:      DB driver for OGR sources     
-*
-* COPYRIGHT:    (C) 2004-2009 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.
-*
-*****************************************************************************/
-
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdarg.h>
@@ -24,7 +19,8 @@
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 
-#include "ogr_api.h"
+#include <ogr_api.h>
+
 #include "globals.h"
 #include "proto.h"
 

Added: grass/trunk/db/drivers/ogr/execute.c
===================================================================
--- grass/trunk/db/drivers/ogr/execute.c	                        (rev 0)
+++ grass/trunk/db/drivers/ogr/execute.c	2011-07-22 13:49:25 UTC (rev 47225)
@@ -0,0 +1,214 @@
+/*!
+  \file db/drivers/execute.c
+  
+  \brief Low level OGR SQL driver
+ 
+  (C) 2011 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.
+  
+  \author Martin Landa <landa.martin gmail.com> (2011/07)
+*/
+
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+#include <ogr_api.h>
+
+#include "globals.h"
+#include "proto.h"
+
+static int parse_sql_update(const char *, char **, column_info **, int *, char **);
+
+int db__driver_execute_immediate(dbString * sql)
+{
+    cursor *c;
+    char *where, *table;
+    int res, ncols, i;
+    column_info *cols;
+
+    OGRFeatureH feature;
+    OGRFeatureDefnH feature_defn;
+    OGRFieldDefnH field_defn;
+    
+    G_debug(1, "db__driver_execute_immediate():");
+    
+    init_error();
+    
+    c = alloc_cursor();
+    if (c == NULL)
+	return DB_FAILED;
+
+    /* parse UPDATE statement */
+    G_debug(3, "\tSQL: '%s'", db_get_string(sql));
+    res = parse_sql_update(db_get_string(sql), &table, &cols, &ncols, &where);
+    G_debug(3, "\t table=%s, where=%s, ncols=%d", table, where ? where : "", ncols);
+    if (res != 0) {
+	append_error(_("Unable to parse '%s'\n"), db_get_string(sql));
+	append_error(_("DBMI-OGR driver only supports UPDATE statements"));
+	report_error();
+	return DB_FAILED;
+    }
+
+    /* get OGR layer */
+    c->hLayer = OGR_DS_GetLayerByName(hDs, table);
+    if (!c->hLayer) {
+	append_error(_("OGR layer <%s> not found"), table);
+	report_error();
+	return DB_FAILED;
+    }
+    
+    if (where)
+	OGR_L_SetAttributeFilter(c->hLayer, where);
+    
+    /* get columns info */
+    feature_defn = OGR_L_GetLayerDefn(c->hLayer);
+    for (i = 0; i < ncols; i++) {
+	cols[i].index = OGR_FD_GetFieldIndex(feature_defn, cols[i].name);
+	if (cols[i].index < 0) {
+	    append_error(_("Column <%s> not found in table <%s>"),
+			 cols[i].name, table);
+	    report_error();
+	    return DB_FAILED;
+	}
+	cols[i].qindex = OGR_FD_GetFieldIndex(feature_defn, cols[i].value);
+	field_defn = OGR_FD_GetFieldDefn(feature_defn, cols[i].index);
+	cols[i].type = OGR_Fld_GetType(field_defn);
+
+	G_debug(3, "\t\tcol=%s, val=%s idx=%d, type=%d, qidx=%d",
+		cols[i].name, cols[i].value, cols[i].index, cols[i].type,
+		cols[i].qindex);
+    }
+    
+    /* update features */
+    OGR_L_ResetReading(c->hLayer);
+    while(TRUE) {
+	char *value;
+	
+	feature = OGR_L_GetNextFeature(c->hLayer);
+	if (!feature)
+	    break;
+	G_debug(5, "\tfid=%ld", OGR_F_GetFID(feature));
+	
+	for (i = 0; i < ncols; i++) {
+	    if (cols[i].qindex > -1) {
+		value = (char *)OGR_F_GetFieldAsString(feature, cols[i].qindex);
+	    }
+	    else {
+		if ((cols[i].type != OFTInteger ||
+		     cols[i].type != OFTReal) && *(cols[i].value) == '\'') {
+		    value = G_strchg(cols[i].value, '\'', ' ');
+		    G_strip(value);
+		}
+		else {
+		    value = cols[i].value;
+		}
+	    }
+	    OGR_F_SetFieldString(feature, cols[i].index, value);
+	}
+	OGR_L_SetFeature(c->hLayer, feature);
+	OGR_F_Destroy(feature);
+    }
+    
+    G_free(table);
+    G_free(where);
+    for (i = 0; i < ncols; i++) {
+	G_free(cols[i].name);
+	G_free(cols[i].value);
+    }
+    
+    return DB_OK;
+}
+
+int parse_sql_update(const char *sql, char **table, column_info **cols, int *ncols, char **where)
+{
+    int nprefix, n;
+    int has_where;
+    char *prefix;
+    char *p, *w, *c, *t;
+    char **token, **itoken;
+    
+    prefix = "UPDATE";
+    nprefix = strlen(prefix);
+    if (strncasecmp(sql, prefix, nprefix) != 0)
+	return 1;
+	
+    p = (char *) sql + nprefix; /* skip 'UPDATE' */
+    if (*p != ' ')
+	return 1;
+    p++;
+    
+    /* table */
+    t = strchr(p, ' ');
+    n = t - p;
+    *table = G_malloc(n + 1);
+    strncpy(*table, p, n);
+    (*table)[n] = '\0';
+    G_strip(*table);
+    
+    p += n;
+    if (*p != ' ')
+	return 1;
+    p++;
+
+    if (strncasecmp(p, "SET", 3) != 0)
+	return 1;
+
+    p += 3; /* skip 'SET' */
+
+    if (*p != ' ')
+	return 1;
+    p++;
+    
+    w = strstr(p, "WHERE");
+    if (!w) {
+	has_where = FALSE;
+	w = (char *)sql + strlen(sql);
+    }
+    else {
+	has_where = TRUE;
+    }
+    
+    /* process columns & values */
+    n = w - p;
+    c = G_malloc(n + 1);
+    strncpy(c, p, n);
+    c[n] = '\0';
+
+    token = G_tokenize(c, ",");
+    *ncols = G_number_of_tokens(token);
+    *cols = (column_info *)G_malloc(sizeof(column_info) * (*ncols));
+
+    for (n = 0; n < (*ncols); n++) {
+	itoken = G_tokenize(token[n], "=");
+	if (G_number_of_tokens(itoken) != 2)
+	    return FALSE;
+	G_strip(itoken[0]);
+	G_strip(itoken[1]);
+	(*cols)[n].name  = G_store(itoken[0]);
+	(*cols)[n].value = G_store(itoken[1]);
+	G_free_tokens(itoken);
+    }
+    
+    G_free_tokens(token);
+    G_free(c);
+    
+    if (!has_where) {
+	*where = NULL;
+	return 0;
+    }
+    
+    /* where */
+    w += strlen("WHERE");
+    if (*w != ' ')
+	return 1;
+    w++;
+    
+    G_strip(w);
+    *where = G_store(w);
+
+    return 0;
+}


Property changes on: grass/trunk/db/drivers/ogr/execute.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native

Modified: grass/trunk/db/drivers/ogr/fetch.c
===================================================================
--- grass/trunk/db/drivers/ogr/fetch.c	2011-07-22 12:59:28 UTC (rev 47224)
+++ grass/trunk/db/drivers/ogr/fetch.c	2011-07-22 13:49:25 UTC (rev 47225)
@@ -1,20 +1,16 @@
+/*!
+  \file db/drivers/fetch.c
+  
+  \brief Low level OGR SQL driver
+ 
+  (C) 2004-2009 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.
+  
+  \author Radim Blazek
+  \author Some updates by Martin Landa <landa.martin gmail.com>
+*/
 
-/*****************************************************************************
-*
-* MODULE:       OGR driver 
-*   	    	
-* AUTHOR(S):    Radim Blazek
-*               Some updates by Martin Landa <landa.martin gmail.com>
-*
-* PURPOSE:      DB driver for OGR sources     
-*
-* COPYRIGHT:    (C) 2004-2009 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.
-*
-*****************************************************************************/
 #include <stdlib.h>
 #include <string.h>
 
@@ -22,7 +18,8 @@
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 
-#include "ogr_api.h"
+#include <ogr_api.h>
+
 #include "globals.h"
 #include "proto.h"
 

Modified: grass/trunk/db/drivers/ogr/globals.h
===================================================================
--- grass/trunk/db/drivers/ogr/globals.h	2011-07-22 12:59:28 UTC (rev 47224)
+++ grass/trunk/db/drivers/ogr/globals.h	2011-07-22 13:49:25 UTC (rev 47225)
@@ -27,5 +27,15 @@
     int ncols;			/* num columns (without fid column) */
 } cursor;
 
+/* column info (see execute.c) */
+typedef struct
+{
+    char *name;
+    char *value;
+    int  index;
+    int  qindex; /* query column */
+    OGRFieldType type;
+} column_info;
+
 extern OGRDataSourceH hDs;
 extern dbString *errMsg;

Modified: grass/trunk/db/drivers/ogr/listtab.c
===================================================================
--- grass/trunk/db/drivers/ogr/listtab.c	2011-07-22 12:59:28 UTC (rev 47224)
+++ grass/trunk/db/drivers/ogr/listtab.c	2011-07-22 13:49:25 UTC (rev 47225)
@@ -1,26 +1,22 @@
+/*!
+  \file db/drivers/listtab.c
+  
+  \brief Low level OGR SQL driver
+ 
+  (C) 2004-2009 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.
+  
+  \author Radim Blazek
+  \author Some updates by Martin Landa <landa.martin gmail.com>
+*/
 
-/*****************************************************************************
-*
-* MODULE:       OGR driver 
-*   	    	
-* AUTHOR(S):    Radim Blazek
-*               Some updates by Martin Landa <landa.martin gmail.com>
-*
-* PURPOSE:      DB driver for OGR sources     
-*
-* COPYRIGHT:    (C) 2004-2009 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.
-*
-*****************************************************************************/
-
 #include <grass/gis.h>
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 
-#include "ogr_api.h"
+#include <ogr_api.h>
+
 #include "globals.h"
 #include "proto.h"
 
@@ -29,7 +25,7 @@
 
   \param[out] tlist list of tables
   \param[out] tcount number of tables
-  \param system list also system tables
+  \param system list also system tables (unused)
 
   \return DB_OK on success
   \return DB_FAILED on failure

Modified: grass/trunk/db/drivers/ogr/select.c
===================================================================
--- grass/trunk/db/drivers/ogr/select.c	2011-07-22 12:59:28 UTC (rev 47224)
+++ grass/trunk/db/drivers/ogr/select.c	2011-07-22 13:49:25 UTC (rev 47225)
@@ -1,26 +1,22 @@
+/*!
+  \file db/drivers/select.c
+  
+  \brief Low level OGR SQL driver
+ 
+  (C) 2004-2009 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.
+  
+  \author Radim Blazek
+  \author Some updates by Martin Landa <landa.martin gmail.com>
+*/
 
-/*****************************************************************************
-*
-* MODULE:       OGR driver 
-*   	    	
-* AUTHOR(S):    Radim Blazek
-*               Some updates by Martin Landa <landa.martin gmail.com>
-*
-* PURPOSE:      DB driver for OGR sources     
-*
-* COPYRIGHT:    (C) 2004-2009 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.
-*
-*****************************************************************************/
-
 #include <grass/gis.h>
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 
-#include "ogr_api.h"
+#include <ogr_api.h>
+
 #include "globals.h"
 #include "proto.h"
 



More information about the grass-commit mailing list