[GRASS-SVN] r48158 - grass/branches/releasebranch_6_4/db/drivers/sqlite

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Sep 6 02:48:56 EDT 2011


Author: mmetz
Date: 2011-09-05 23:48:56 -0700 (Mon, 05 Sep 2011)
New Revision: 48158

Modified:
   grass/branches/releasebranch_6_4/db/drivers/sqlite/create_table.c
   grass/branches/releasebranch_6_4/db/drivers/sqlite/cursor.c
   grass/branches/releasebranch_6_4/db/drivers/sqlite/db.c
   grass/branches/releasebranch_6_4/db/drivers/sqlite/describe.c
   grass/branches/releasebranch_6_4/db/drivers/sqlite/driver.c
   grass/branches/releasebranch_6_4/db/drivers/sqlite/error.c
   grass/branches/releasebranch_6_4/db/drivers/sqlite/execute.c
   grass/branches/releasebranch_6_4/db/drivers/sqlite/fetch.c
   grass/branches/releasebranch_6_4/db/drivers/sqlite/index.c
   grass/branches/releasebranch_6_4/db/drivers/sqlite/listtab.c
   grass/branches/releasebranch_6_4/db/drivers/sqlite/main.c
   grass/branches/releasebranch_6_4/db/drivers/sqlite/proto.h
   grass/branches/releasebranch_6_4/db/drivers/sqlite/select.c
Log:
sqlite driver: fix for #548 and related

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/create_table.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/create_table.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/create_table.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -8,8 +8,9 @@
  * (>=v2). Read the file COPYING that comes with GRASS for details.
  *
  * \author Radim Blazek
+ * \author Support for multiple connections by Markus Metz
  *
- * \date 2005-2007
+ * \date 2005-2011
  */
 
 #include <grass/dbmi.h>
@@ -112,6 +113,9 @@
     G_debug(3, " SQL: %s", db_get_string(&sql));
 
     ret = sqlite3_prepare(sqlite, db_get_string(&sql), -1, &statement, &rest);
+    while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+    }
 
     if (ret != SQLITE_OK) {
 	append_error("Cannot create table:\n");

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/cursor.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/cursor.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/cursor.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -3,9 +3,9 @@
 *
 * MODULE:       SQLite driver 
 *   	    	
-* AUTHOR(S):    Radim Blazek
+* AUTHOR(S):    Radim Blazek, Markus Metz
 *
-* COPYRIGHT:    (C) 2005 by the GRASS Development Team
+* COPYRIGHT:    (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
@@ -21,6 +21,7 @@
 int db__driver_close_cursor(dbCursor * dbc)
 {
     cursor *c;
+    int ret;
 
     init_error();
 
@@ -29,7 +30,10 @@
     if (c == NULL)
 	return DB_FAILED;
 
-    sqlite3_finalize(c->statement);
+    ret = sqlite3_finalize(c->statement);
+    while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+    }
 
     /* free_cursor(cursor) */
     free_cursor(c);

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/db.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/db.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/db.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -7,8 +7,9 @@
  * (>=v2). Read the file COPYING that comes with GRASS for details.
  *
  * \author Radim Blazek
+ * \author Support for multiple connections by Markus Metz
  *
- * \date 2005-2007
+ * \date 2005-2011
  */
 
 #include <stdlib.h>
@@ -102,7 +103,8 @@
     G_debug(3, "db_close_database()");
 
     init_error();
-    sqlite3_close(sqlite);
+    if (sqlite3_close(sqlite) == SQLITE_BUSY)
+	G_fatal_error(_("SQLite database connection is still busy"));
 
     return DB_OK;
 }

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/describe.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/describe.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/describe.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -8,8 +8,9 @@
  * (>=v2). Read the file COPYING that comes with GRASS for details.
  *
  * \author Radim Blazek
+ * \author Support for multiple connections by Markus Metz
  *
- * \date 2005-2007
+ * \date 2005-2011
  */
 
 #include <string.h>
@@ -49,6 +50,9 @@
     db_append_string(&sql, " where oid < 0");
 
     ret = sqlite3_prepare(sqlite, db_get_string(&sql), -1, &statement, &rest);
+    while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+    }
 
     if (ret != SQLITE_OK) {
 	append_error("Error in sqlite3_prepare():");
@@ -90,7 +94,7 @@
 
 int describe_table(sqlite3_stmt * statement, dbTable ** table, cursor * c)
 {
-    int i, ncols, nkcols;
+    int i, ncols, nkcols, ret;
 
     G_debug(3, "describe_table()");
 
@@ -98,7 +102,10 @@
     G_debug(3, "ncols = %d", ncols);
 
     /* Try to get first row */
-    sqlite3_step(statement);
+    ret = sqlite3_step(statement);
+    while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+    }
 
     /* Count columns of known type */
     nkcols = 0;
@@ -442,5 +449,6 @@
 #undef streq
 
     G_warning("SQLite driver: unable to parse decltype: %s", declared);
+
     return DB_SQL_TYPE_UNKNOWN;
 }

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/driver.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/driver.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/driver.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -3,9 +3,9 @@
 *
 * MODULE:       SQLite driver 
 *   	    	
-* AUTHOR(S):    Radim Blazek
+* AUTHOR(S):    Radim Blazek, Markus Metz
 *
-* COPYRIGHT:    (C) 2005 by the GRASS Development Team
+* COPYRIGHT:    (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
@@ -19,6 +19,7 @@
 int db__driver_init(int argc, char *argv[])
 {
     init_error();
+
     return DB_OK;
 }
 

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/error.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/error.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/error.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -3,9 +3,9 @@
 *
 * MODULE:       SQLite driver 
 *   	    	
-* AUTHOR(S):    Radim Blazek
+* AUTHOR(S):    Radim Blazek, Markus Metz
 *
-* COPYRIGHT:    (C) 2005 by the GRASS Development Team
+* COPYRIGHT:    (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

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/execute.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/execute.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/execute.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -9,8 +9,9 @@
  *
  * \author Radim Blazek
  * \author Antonio Galea
+ * \author Support for multiple connections by Markus Metz
  *
- * \date 2005-2007
+ * \date 2005-2011
  */
 
 #include <stdlib.h>
@@ -41,6 +42,9 @@
     G_debug(3, "execute: %s", s);
 
     ret = sqlite3_prepare(sqlite, s, -1, &stmt, &rest);
+    while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+    }
 
     if (ret != SQLITE_OK) {
 	append_error("Error in sqlite3_prepare():\n");
@@ -50,6 +54,7 @@
     }
 
     ret = sqlite3_step(stmt);
+    /* check if sqlite is still busy preparing the statement? */
 
     if (ret != SQLITE_DONE) {
 	append_error("Error in sqlite3_step():\n");
@@ -91,6 +96,9 @@
     G_debug(3, "execute: BEGIN");
 
     ret = sqlite3_exec(sqlite, "BEGIN", NULL, NULL, NULL);
+    while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+    }
     if (ret != SQLITE_OK) {
 	append_error("Cannot 'BEGIN' transaction:\n");
 	append_error((char *)sqlite3_errmsg(sqlite));
@@ -117,6 +125,9 @@
     G_debug(3, "execute: COMMIT");
 
     ret = sqlite3_exec(sqlite, "COMMIT", NULL, NULL, NULL);
+    while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+    }
     if (ret != SQLITE_OK) {
 	append_error("Cannot 'COMMIT' transaction:\n");
 	append_error((char *)sqlite3_errmsg(sqlite));

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/fetch.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/fetch.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/fetch.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -8,8 +8,9 @@
  * (>=v2). Read the file COPYING that comes with GRASS for details.
  *
  * \author Radim Blazek
+ * \author Support for multiple connections by Markus Metz
  *
- * \date 2005-2007
+ * \date 2005-2011
  */
 
 #include <stdlib.h>
@@ -63,6 +64,9 @@
 	    c->row = -1;
 
 	ret = sqlite3_step(c->statement);
+	while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	    ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+	}
 	if (ret != SQLITE_ROW) {
 	    if (ret != SQLITE_DONE) {
 		append_error("Cannot step:\n");
@@ -248,7 +252,7 @@
 {
     cursor *c;
     dbToken token;
-    int row;
+    int row, ret;
 
     /* get cursor token */
     token = db_get_cursor_token(cn);
@@ -264,14 +268,17 @@
 	return (c->nrows);
     }
 
-    sqlite3_reset(c->statement);
+    ret = sqlite3_reset(c->statement);
+    while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+    }
 
     c->nrows = 0;
     while (sqlite3_step(c->statement) == SQLITE_ROW) {
 	c->nrows++;
     }
 
-    sqlite3_reset(c->statement);
+    ret = sqlite3_reset(c->statement);
 
     /* Reset cursor position */
     row = -1;

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/index.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/index.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/index.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -8,8 +8,9 @@
  * (>=v2). Read the file COPYING that comes with GRASS for details.
  *
  * \author Radim Blazek
+ * \author Support for multiple connections by Markus Metz
  *
- * \date 2005-2007
+ * \date 2005-2011
  */
 
 #include <grass/dbmi.h>
@@ -65,6 +66,9 @@
     G_debug(3, " SQL: %s", db_get_string(&sql));
 
     ret = sqlite3_prepare(sqlite, db_get_string(&sql), -1, &statement, &rest);
+    while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+    }
 
     if (ret != SQLITE_OK) {
 	append_error("Cannot create index:\n");
@@ -86,6 +90,7 @@
 	return DB_FAILED;
     }
 
+    sqlite3_reset(statement);
     sqlite3_finalize(statement);
     db_free_string(&sql);
 

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/listtab.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/listtab.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/listtab.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -8,8 +8,9 @@
  * (>=v2). Read the file COPYING that comes with GRASS for details.
  *
  * \author Radim Blazek
+ * \author Support for multiple connections by Markus Metz
  *
- * \date 2005-2007
+ * \date 2005-2011
  */
 
 #include <stdlib.h>
@@ -44,6 +45,10 @@
 			  "select name from sqlite_master where type = 'table' or type = 'view'",
 			  -1, &statement, &rest);
 
+    while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+    }
+
     if (ret != SQLITE_OK) {
 	append_error("Cannot list tables\n");
 	append_error((char *)sqlite3_errmsg(sqlite));

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/main.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/main.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/main.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -3,9 +3,9 @@
 *
 * MODULE:       SQLite driver 
 *   	    	
-* AUTHOR(S):    Radim Blazek
+* AUTHOR(S):    Radim Blazek, Markus Metz
 *
-* COPYRIGHT:    (C) 2005 by the GRASS Development Team
+* COPYRIGHT:    (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
@@ -14,6 +14,7 @@
 **************************************************************/
 
 #include <stdlib.h>
+#include <time.h>
 #include <grass/dbmi.h>
 #include "globals.h"
 #include "dbdriver.h"
@@ -25,3 +26,27 @@
     init_dbdriver();
     exit(db_driver(argc, argv));
 }
+
+int sqlite_busy_callback(void *arg, int n_calls)
+{
+    static time_t start_time = 0;
+    time_t curr_time;
+    int min;
+    static int last_min = 0;
+
+    /* do something here while waiting? */
+    if (n_calls > 0) {
+	time(&curr_time);
+	min = (curr_time - start_time) / 60;
+	if (min > 1 && min > last_min) {
+	    last_min = min;
+	    G_debug(3, "Already waiting for %d minutes...", min);
+	}
+    }
+    else {
+	time(&start_time);
+	last_min = 0;
+    }
+
+    return 1;
+}

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/proto.h
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/proto.h	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/proto.h	2011-09-06 06:48:56 UTC (rev 48158)
@@ -15,4 +15,7 @@
 /* describe.c */
 int describe_table(sqlite3_stmt *, dbTable **, cursor *);
 
+/* main.c */
+int sqlite_busy_callback(void *, int);
+
 #endif /* __SQLITE_PROTO_H__ */

Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/select.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/select.c	2011-09-06 06:43:03 UTC (rev 48157)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/select.c	2011-09-06 06:48:56 UTC (rev 48158)
@@ -8,8 +8,9 @@
  * (>=v2). Read the file COPYING that comes with GRASS for details.
  *
  * \author Radim Blazek
+ * \author Support for multiple connections by Markus Metz
  *
- * \date 2005-2007
+ * \date 2005-2011
  */
 
 #include <stdlib.h>
@@ -53,6 +54,9 @@
     G_debug(3, "Escaped SQL: %s", str);
 
     ret = sqlite3_prepare(sqlite, str, -1, &(c->statement), &rest);
+    while (ret == SQLITE_BUSY || ret == SQLITE_IOERR_BLOCKED) {
+	ret = sqlite3_busy_handler(sqlite, sqlite_busy_callback, NULL);
+    }
 
     if (str)
 	G_free(str);



More information about the grass-commit mailing list