[GRASS-SVN] r52912 - grass/branches/releasebranch_6_4/db/drivers/sqlite
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Aug 25 08:47:48 PDT 2012
Author: mmetz
Date: 2012-08-25 08:47:47 -0700 (Sat, 25 Aug 2012)
New Revision: 52912
Modified:
grass/branches/releasebranch_6_4/db/drivers/sqlite/create_table.c
grass/branches/releasebranch_6_4/db/drivers/sqlite/describe.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/select.c
Log:
sqlite driver: backport from devbr6
Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/create_table.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/create_table.c 2012-08-25 15:24:20 UTC (rev 52911)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/create_table.c 2012-08-25 15:47:47 UTC (rev 52912)
@@ -112,26 +112,42 @@
G_debug(3, " SQL: %s", db_get_string(&sql));
- ret = sqlite3_prepare(sqlite, db_get_string(&sql), -1, &statement, &rest);
+ /* SQLITE bug?
+ * If the database schema has changed, sqlite can prepare a statement,
+ * but sqlite can not step, the statement needs to be prepared anew again */
+ while (1) {
+ ret =
+ sqlite3_prepare(sqlite, db_get_string(&sql), -1, &statement,
+ &rest);
- if (ret != SQLITE_OK) {
- append_error("Cannot create table:\n");
- append_error(db_get_string(&sql));
- append_error("\n");
- append_error((char *)sqlite3_errmsg(sqlite));
- report_error();
- sqlite3_finalize(statement);
- db_free_string(&sql);
- return DB_FAILED;
- }
+ if (ret != SQLITE_OK) {
+ append_error("Cannot create table:\n");
+ append_error(db_get_string(&sql));
+ append_error("\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ sqlite3_finalize(statement);
+ db_free_string(&sql);
+ return DB_FAILED;
+ }
- ret = sqlite3_step(statement);
+ ret = sqlite3_step(statement);
+ /* get real result code */
+ ret = sqlite3_reset(statement);
- if (ret != SQLITE_DONE) {
- append_error("Error in sqlite3_step():\n");
- append_error((char *)sqlite3_errmsg(sqlite));
- report_error();
- return DB_FAILED;
+ if (ret == SQLITE_SCHEMA) {
+ sqlite3_finalize(statement);
+ /* try again */
+ }
+ else if (ret != SQLITE_OK) {
+ append_error("Error in sqlite3_step():\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ sqlite3_finalize(statement);
+ return DB_FAILED;
+ }
+ else
+ break;
}
sqlite3_finalize(statement);
Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/describe.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/describe.c 2012-08-25 15:24:20 UTC (rev 52911)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/describe.c 2012-08-25 15:47:47 UTC (rev 52912)
@@ -49,22 +49,46 @@
db_append_string(&sql, db_get_string(table_name));
db_append_string(&sql, " where oid < 0");
- ret = sqlite3_prepare(sqlite, db_get_string(&sql), -1, &statement, &rest);
+ /* SQLITE bug?
+ * If the database schema has changed, sqlite can prepare a statement,
+ * but sqlite can not step, the statement needs to be prepared anew again */
+ while (1) {
+ ret = sqlite3_prepare(sqlite, db_get_string(&sql), -1, &statement, &rest);
- if (ret != SQLITE_OK) {
- append_error("Error in sqlite3_prepare():");
- append_error(db_get_string(&sql));
- append_error("\n");
- append_error((char *)sqlite3_errmsg(sqlite));
- report_error();
- db_free_string(&sql);
- return DB_FAILED;
+ if (ret != SQLITE_OK) {
+ append_error("Error in sqlite3_prepare():");
+ append_error(db_get_string(&sql));
+ append_error("\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ db_free_string(&sql);
+ return DB_FAILED;
+ }
+
+ ret = sqlite3_step(statement);
+ /* get real result code */
+ ret = sqlite3_reset(statement);
+
+ if (ret == SQLITE_SCHEMA) {
+ sqlite3_finalize(statement);
+ /* try again */
+ }
+ else if (ret != SQLITE_OK) {
+ append_error("Error in sqlite3_step():\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ sqlite3_finalize(statement);
+ return DB_FAILED;
+ }
+ else
+ break;
}
db_free_string(&sql);
if (describe_table(statement, table, NULL) == DB_FAILED) {
- append_error("Cannot describe table\n");
+ append_error("Cannot describe table:\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
report_error();
sqlite3_finalize(statement);
return DB_FAILED;
@@ -91,15 +115,22 @@
int describe_table(sqlite3_stmt * statement, dbTable ** table, cursor * c)
{
- int i, ncols, nkcols;
+ int i, ncols, nkcols, ret;
G_debug(3, "describe_table()");
ncols = sqlite3_column_count(statement);
- G_debug(3, "ncols = %d", ncols);
/* Try to get first row */
- sqlite3_step(statement);
+ ret = sqlite3_step(statement);
+ if (ret != SQLITE_DONE && ret != SQLITE_ROW) {
+ /* get real result code */
+ ret = sqlite3_reset(statement);
+ append_error("Error in sqlite3_step():\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ return DB_FAILED;
+ }
/* Count columns of known type */
nkcols = 0;
@@ -373,7 +404,8 @@
if (streq(buf, "time") || streq(buf, "timetz"))
return DB_SQL_TYPE_TIME;
- if (streq(buf, "timestamp") || streq(buf, "timestamptz"))
+ if (streq(buf, "timestamp") || streq(buf, "timestamptz") ||
+ streq(buf, "datetime"))
return DB_SQL_TYPE_TIMESTAMP;
if (streq(buf, "interval"))
@@ -402,7 +434,7 @@
streq(word[2], "time") && streq(word[3], "zone")) {
if (streq(word[0], "time"))
return DB_SQL_TYPE_TIME;
- if (streq(word[0], "timestamp"))
+ if (streq(word[0], "timestamp") || streq(word[0], "datetime"))
return DB_SQL_TYPE_TIMESTAMP;
}
@@ -426,7 +458,8 @@
return DB_SQL_TYPE_TIME;
if (sscanf(buf, "timestamp ( %d )", length) == 1 ||
- sscanf(buf, "timestamptz ( %d )", length) == 1)
+ sscanf(buf, "timestamptz ( %d )", length) == 1 ||
+ sscanf(buf, "datetime ( %d )", length) == 1 )
return DB_SQL_TYPE_TIMESTAMP;
if (sscanf
@@ -436,7 +469,7 @@
streq(word[2], "time") && streq(word[3], "zone")) {
if (streq(word[0], "time"))
return DB_SQL_TYPE_TIME;
- if (streq(word[0], "timestamp"))
+ if (streq(word[0], "timestamp") || streq(word[0], "datetime"))
return DB_SQL_TYPE_TIMESTAMP;
}
Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/execute.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/execute.c 2012-08-25 15:24:20 UTC (rev 52911)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/execute.c 2012-08-25 15:47:47 UTC (rev 52912)
@@ -41,7 +41,11 @@
G_debug(3, "execute: %s", s);
- ret = sqlite3_prepare(sqlite, s, -1, &stmt, &rest);
+ /* SQLITE bug?
+ * If the database schema has changed, sqlite can prepare a statement,
+ * but sqlite can not step, the statement needs to be prepared anew again */
+ while (1) {
+ ret = sqlite3_prepare(sqlite, s, -1, &stmt, &rest);
if (ret != SQLITE_OK) {
append_error("Error in sqlite3_prepare():\n");
@@ -50,14 +54,23 @@
return DB_FAILED;
}
- ret = sqlite3_step(stmt);
- /* check if sqlite is still busy preparing the statement? */
+ ret = sqlite3_step(stmt);
+ /* get real result code */
+ ret = sqlite3_reset(stmt);
- if (ret != SQLITE_DONE) {
- append_error("Error in sqlite3_step():\n");
- append_error((char *)sqlite3_errmsg(sqlite));
- report_error();
- return DB_FAILED;
+ if (ret == SQLITE_SCHEMA) {
+ sqlite3_finalize(stmt);
+ /* try again */
+ }
+ else if (ret != SQLITE_OK) {
+ append_error("Error in sqlite3_step():\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ sqlite3_finalize(stmt);
+ return DB_FAILED;
+ }
+ else
+ break;
}
ret = sqlite3_finalize(stmt);
Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/fetch.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/fetch.c 2012-08-25 15:24:20 UTC (rev 52911)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/fetch.c 2012-08-25 15:47:47 UTC (rev 52912)
@@ -66,13 +66,14 @@
ret = sqlite3_step(c->statement);
if (ret != SQLITE_ROW) {
- if (ret != SQLITE_DONE) {
- append_error("Cannot step:\n");
+ /* get real result code */
+ ret = sqlite3_reset(c->statement);
+ if (ret != SQLITE_OK) {
+ append_error("Cannot fetch:\n");
append_error((char *)sqlite3_errmsg(sqlite));
report_error();
return DB_FAILED;
}
- sqlite3_reset(c->statement);
*more = 0;
return DB_OK;
}
@@ -250,7 +251,7 @@
{
cursor *c;
dbToken token;
- int row;
+ int row, ret;
/* get cursor token */
token = db_get_cursor_token(cn);
@@ -269,12 +270,20 @@
sqlite3_reset(c->statement);
c->nrows = 0;
- while (sqlite3_step(c->statement) == SQLITE_ROW) {
+ while ((ret = sqlite3_step(c->statement)) == SQLITE_ROW) {
c->nrows++;
}
- sqlite3_reset(c->statement);
+ /* get real result code */
+ ret = sqlite3_reset(c->statement);
+ if (ret != SQLITE_OK) {
+ append_error("Cannot get number of rows\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ return DB_FAILED;
+ }
+
/* Reset cursor position */
row = -1;
if (c->row > -1) {
Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/index.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/index.c 2012-08-25 15:24:20 UTC (rev 52911)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/index.c 2012-08-25 15:47:47 UTC (rev 52912)
@@ -65,29 +65,43 @@
G_debug(3, " SQL: %s", db_get_string(&sql));
- ret = sqlite3_prepare(sqlite, db_get_string(&sql), -1, &statement, &rest);
+ /* SQLITE bug?
+ * If the database schema has changed, sqlite can prepare a statement,
+ * but sqlite can not step, the statement needs to be prepared anew again */
+ while (1) {
+ ret = sqlite3_prepare(sqlite, db_get_string(&sql), -1, &statement, &rest);
- if (ret != SQLITE_OK) {
- append_error("Cannot create index:\n");
- append_error(db_get_string(&sql));
- append_error("\n");
- append_error((char *)sqlite3_errmsg(sqlite));
- report_error();
- sqlite3_finalize(statement);
- db_free_string(&sql);
- return DB_FAILED;
- }
+ if (ret != SQLITE_OK) {
+ append_error("Cannot create index:\n");
+ append_error(db_get_string(&sql));
+ append_error("\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ sqlite3_finalize(statement);
+ db_free_string(&sql);
+ return DB_FAILED;
+ }
- ret = sqlite3_step(statement);
+ ret = sqlite3_step(statement);
+ /* get real result code */
+ ret = sqlite3_reset(statement);
- if (ret != SQLITE_DONE) {
- append_error("Error in sqlite3_step():\n");
- append_error((char *)sqlite3_errmsg(sqlite));
- report_error();
- return DB_FAILED;
+ if (ret == SQLITE_SCHEMA) {
+ sqlite3_finalize(statement);
+ /* try again */
+ }
+ else if (ret != SQLITE_OK) {
+ append_error("Error in sqlite3_step():\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ sqlite3_finalize(statement);
+ db_free_string(&sql);
+ return DB_FAILED;
+ }
+ else
+ break;
}
- 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 2012-08-25 15:24:20 UTC (rev 52911)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/listtab.c 2012-08-25 15:47:47 UTC (rev 52912)
@@ -57,7 +57,16 @@
while (sqlite3_step(statement) == SQLITE_ROW) {
nrows++;
}
- sqlite3_reset(statement);
+ /* get real result code */
+ ret = sqlite3_reset(statement);
+
+ if (ret != SQLITE_OK) {
+ append_error("Cannot list tables\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ sqlite3_finalize(statement);
+ return DB_FAILED;
+ }
G_debug(3, "nrows = %d", nrows);
@@ -66,6 +75,7 @@
if (list == NULL) {
append_error("Cannot db_alloc_string_array()");
report_error();
+ sqlite3_finalize(statement);
return DB_FAILED;
}
Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/main.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/main.c 2012-08-25 15:24:20 UTC (rev 52911)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/main.c 2012-08-25 15:47:47 UTC (rev 52912)
@@ -15,7 +15,9 @@
#include <stdlib.h>
#include <time.h>
+#include <grass/gis.h>
#include <grass/dbmi.h>
+#include <grass/glocale.h>
#include "globals.h"
#include "dbdriver.h"
@@ -31,21 +33,23 @@
{
static time_t start_time = 0;
time_t curr_time;
- int min;
- static int last_min = -1;
+ int sec;
+ static int last_sec = -1;
+ G_debug(4, "sqlite_busy_callback()");
+
/* do something here while waiting? */
- if (n_calls > 0 && last_min > -1) {
+ if (n_calls > 0 && last_sec > -1) {
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);
+ sec = (curr_time - start_time);
+ if (sec > 1 && sec > last_sec && sec % 10 == 0) {
+ last_sec = sec;
+ G_warning(_("Busy SQLITE db, already waiting for %d seconds..."), sec);
}
}
else {
time(&start_time);
- last_min = 0;
+ last_sec = 0;
}
return 1;
Modified: grass/branches/releasebranch_6_4/db/drivers/sqlite/select.c
===================================================================
--- grass/branches/releasebranch_6_4/db/drivers/sqlite/select.c 2012-08-25 15:24:20 UTC (rev 52911)
+++ grass/branches/releasebranch_6_4/db/drivers/sqlite/select.c 2012-08-25 15:47:47 UTC (rev 52912)
@@ -53,22 +53,46 @@
str = G_str_replace(db_get_string(sel), "\\", "\\\\");
G_debug(3, "Escaped SQL: %s", str);
- ret = sqlite3_prepare(sqlite, str, -1, &(c->statement), &rest);
+ /* SQLITE bug?
+ * If the database schema has changed, sqlite can prepare a statement,
+ * but sqlite can not step, the statement needs to be prepared anew again */
+ while (1) {
+ ret = sqlite3_prepare(sqlite, str, -1, &(c->statement), &rest);
+ if (ret != SQLITE_OK) {
+ append_error("Error in sqlite3_prepare():");
+ append_error(db_get_string(sel));
+ append_error("\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ return DB_FAILED;
+ }
+
+ ret = sqlite3_step(c->statement);
+ /* get real result code */
+ ret = sqlite3_reset(c->statement);
+
+ if (ret == SQLITE_SCHEMA) {
+ sqlite3_finalize(c->statement);
+ /* try again */
+ }
+ else if (ret != SQLITE_OK) {
+ append_error("Error in sqlite3_step():\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
+ report_error();
+ sqlite3_finalize(c->statement);
+ return DB_FAILED;
+ }
+ else
+ break;
+ }
+
if (str)
G_free(str);
- if (ret != SQLITE_OK) {
- append_error("Error in sqlite3_prepare():");
- append_error(db_get_string(sel));
- append_error("\n");
- append_error((char *)sqlite3_errmsg(sqlite));
- report_error();
- return DB_FAILED;
- }
-
if (describe_table(c->statement, &table, c) == DB_FAILED) {
append_error("Cannot describe table\n");
+ append_error((char *)sqlite3_errmsg(sqlite));
report_error();
return DB_FAILED;
}
More information about the grass-commit
mailing list