[88874] trunk/base/src

cal at macports.org cal at macports.org
Fri Jan 13 19:03:53 PST 2012


Revision: 88874
          http://trac.macports.org/changeset/88874
Author:   cal at macports.org
Date:     2012-01-13 19:03:52 -0800 (Fri, 13 Jan 2012)
Log Message:
-----------
registry: Use sqlite3_prepare_v2, rewrite do_queries code

Using sqlite3_prepare_v2 will give us useful error messages even when we're not
calling sqlite3_finalize/reset(stmt). See 'Goofy Interface Alert' in
http://www.sqlite.org/c3ref/step.html

Rewritten do_queries while searching for the problem in #32686

Modified Paths:
--------------
    trunk/base/src/cregistry/entry.c
    trunk/base/src/cregistry/file.c
    trunk/base/src/cregistry/registry.c
    trunk/base/src/cregistry/sql.c
    trunk/base/src/cregistry/util.c
    trunk/base/src/registry2.0/graph.c
    trunk/base/src/registry2.0/item.c
    trunk/base/src/registry2.0/itemobj.c

Modified: trunk/base/src/cregistry/entry.c
===================================================================
--- trunk/base/src/cregistry/entry.c	2012-01-14 01:41:12 UTC (rev 88873)
+++ trunk/base/src/cregistry/entry.c	2012-01-14 03:03:52 UTC (rev 88874)
@@ -121,7 +121,7 @@
     reg_entry* entry = NULL;
     char* query = "INSERT INTO registry.ports "
         "(name, version, revision, variants, epoch) VALUES (?, ?, ?, ?, ?)";
-    if ((sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
             && (sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC)
                 == SQLITE_OK)
             && (sqlite3_bind_text(stmt, 2, version, -1, SQLITE_STATIC)
@@ -190,7 +190,7 @@
         query = "SELECT id FROM registry.ports WHERE name=? AND version=? "
         "AND revision=? AND variants=? AND epoch!=?";
     }
-    if ((sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
             && (sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC)
                 == SQLITE_OK)
             && (sqlite3_bind_text(stmt, 2, version, -1, SQLITE_STATIC)
@@ -249,12 +249,12 @@
     char* ports_query = "DELETE FROM registry.ports WHERE id=?";
     char* files_query = "DELETE FROM registry.files WHERE id=?";
     char* dependencies_query = "DELETE FROM registry.dependencies WHERE id=?";
-    if ((sqlite3_prepare(reg->db, ports_query, -1, &ports, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, ports_query, -1, &ports, NULL) == SQLITE_OK)
             && (sqlite3_bind_int64(ports, 1, entry->id) == SQLITE_OK)
-            && (sqlite3_prepare(reg->db, files_query, -1, &files, NULL)
+            && (sqlite3_prepare_v2(reg->db, files_query, -1, &files, NULL)
                 == SQLITE_OK)
             && (sqlite3_bind_int64(files, 1, entry->id) == SQLITE_OK)
-            && (sqlite3_prepare(reg->db, dependencies_query, -1, &dependencies,
+            && (sqlite3_prepare_v2(reg->db, dependencies_query, -1, &dependencies,
                     NULL) == SQLITE_OK)
             && (sqlite3_bind_int64(dependencies, 1, entry->id) == SQLITE_OK)) {
         int r;
@@ -496,7 +496,7 @@
     sqlite3_stmt* stmt = NULL;
     char* query = "SELECT id FROM registry.files WHERE actual_path=? AND active";
     int lower_bound = 0;
-    if ((sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
             && (sqlite3_bind_text(stmt, 1, path, -1, SQLITE_STATIC)
                 == SQLITE_OK)) {
         int r;
@@ -543,7 +543,7 @@
     sqlite3_stmt* stmt = NULL;
     sqlite_int64 result = 0;
     char* query = "SELECT id FROM registry.files WHERE actual_path=? AND active";
-    if ((sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
             && (sqlite3_bind_text(stmt, 1, path, -1, SQLITE_STATIC)
                 == SQLITE_OK)) {
         int r;
@@ -580,7 +580,7 @@
     const char *text;
     query = sqlite3_mprintf("SELECT %q FROM registry.ports WHERE id=%lld", key,
             entry->id);
-    if (sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK) {
+    if (sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK) {
         int r;
         do {
             r = sqlite3_step(stmt);
@@ -638,7 +638,7 @@
     char* query;
     query = sqlite3_mprintf("UPDATE registry.ports SET %q = '%q' WHERE id=%lld",
             key, value, entry->id);
-    if (sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK) {
+    if (sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK) {
         int r;
         do {
             r = sqlite3_step(stmt);
@@ -686,7 +686,7 @@
     sqlite3_stmt* stmt = NULL;
     char* insert = "INSERT INTO registry.files (id, path, mtime, active) "
         "VALUES (?, ?, 0, 0)";
-    if ((sqlite3_prepare(reg->db, insert, -1, &stmt, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, insert, -1, &stmt, NULL) == SQLITE_OK)
             && (sqlite3_bind_int64(stmt, 1, entry->id) == SQLITE_OK)) {
         int i;
         for (i=0; i<file_count && result; i++) {
@@ -738,7 +738,7 @@
     int result = 1;
     sqlite3_stmt* stmt = NULL;
     char* query = "DELETE FROM registry.files WHERE path=? AND id=?";
-    if ((sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
             && (sqlite3_bind_int64(stmt, 2, entry->id) == SQLITE_OK)) {
         int i;
         for (i=0; i<file_count && result; i++) {
@@ -795,7 +795,7 @@
     reg_registry* reg = entry->reg;
     sqlite3_stmt* stmt = NULL;
     char* query = "SELECT path FROM registry.files WHERE id=? ORDER BY path";
-    if ((sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
             && (sqlite3_bind_int64(stmt, 1, entry->id) == SQLITE_OK)) {
         char** result = malloc(10*sizeof(char*));
         int result_count = 0;
@@ -863,7 +863,7 @@
     sqlite3_stmt* stmt = NULL;
     char* query = "SELECT actual_path FROM registry.files WHERE id=? "
         "AND active ORDER BY actual_path";
-    if ((sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
             && (sqlite3_bind_int64(stmt, 1, entry->id) == SQLITE_OK)) {
         char** result = malloc(10*sizeof(char*));
         int result_count = 0;
@@ -946,8 +946,8 @@
         as_files = files;
     }
 
-    if (sqlite3_prepare(reg->db, select_query, -1, &select, NULL) == SQLITE_OK){
-        if ((sqlite3_prepare(reg->db, update_query, -1, &update, NULL)
+    if (sqlite3_prepare_v2(reg->db, select_query, -1, &select, NULL) == SQLITE_OK){
+        if ((sqlite3_prepare_v2(reg->db, update_query, -1, &update, NULL)
                 == SQLITE_OK)
                 && (sqlite3_bind_int64(update, 3, entry->id) == SQLITE_OK)) {
             for (i=0; i<file_count && result; i++) {
@@ -1038,7 +1038,7 @@
     int i;
     sqlite3_stmt* stmt = NULL;
     char* query = "UPDATE registry.files SET active=0 WHERE actual_path=? AND id=?";
-    if ((sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
             && (sqlite3_bind_int64(stmt, 2, entry->id) == SQLITE_OK)) {
         for (i=0; i<file_count && result; i++) {
             if (sqlite3_bind_text(stmt, 1, files[i], -1, SQLITE_STATIC)
@@ -1139,7 +1139,7 @@
     int result = 0;
     sqlite3_stmt* stmt = NULL;
     char* query = "INSERT INTO registry.dependencies (id, name) VALUES (?,?)";
-    if ((sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
             && (sqlite3_bind_int64(stmt, 1, entry->id) == SQLITE_OK)
             && (sqlite3_bind_text(stmt, 2, name, -1, SQLITE_STATIC)
                 == SQLITE_OK)) {

Modified: trunk/base/src/cregistry/file.c
===================================================================
--- trunk/base/src/cregistry/file.c	2012-01-14 01:41:12 UTC (rev 88873)
+++ trunk/base/src/cregistry/file.c	2012-01-14 03:03:52 UTC (rev 88874)
@@ -108,7 +108,7 @@
     char* query = "SELECT id, path FROM registry.files WHERE id=? AND path=?";
     int lower_bound = 0;
 
-    if ((sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
+    if ((sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK)
             && (sqlite3_bind_text(stmt, 1, id, -1, SQLITE_STATIC)
                 == SQLITE_OK)
             && (sqlite3_bind_text(stmt, 2, name, -1, SQLITE_STATIC)
@@ -237,7 +237,7 @@
     const char *text;
     query = sqlite3_mprintf("SELECT %q FROM registry.files WHERE id=%lld "
             "AND path='%q'", key, file->key.id, file->key.path);
-    if (sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK) {
+    if (sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK) {
         int r;
         do {
             r = sqlite3_step(stmt);
@@ -292,7 +292,7 @@
     char* query;
     query = sqlite3_mprintf("UPDATE registry.files SET %q = '%q' WHERE id=%lld "
             "AND path='%q'", key, value, file->key.id, file->key.path);
-    if (sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK) {
+    if (sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK) {
         int r;
         do {
             r = sqlite3_step(stmt);

Modified: trunk/base/src/cregistry/registry.c
===================================================================
--- trunk/base/src/cregistry/registry.c	2012-01-14 01:41:12 UTC (rev 88873)
+++ trunk/base/src/cregistry/registry.c	2012-01-14 03:03:52 UTC (rev 88874)
@@ -205,7 +205,7 @@
         char* query = sqlite3_mprintf("ATTACH DATABASE '%q' AS registry", path);
         int r;
         do {
-            r = sqlite3_prepare(reg->db, query, -1, &stmt, NULL);
+            r = sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL);
         } while (r == SQLITE_BUSY);
         if (r == SQLITE_OK) {
             /* XXX: Busy waiting, consider using sqlite3_busy_handler/timeout */
@@ -264,7 +264,7 @@
         reg_throw(errPtr,REG_MISUSE,"no database is attached to this registry");
         return 0;
     }
-    if (sqlite3_prepare(reg->db, query, -1, &stmt, NULL) == SQLITE_OK) {
+    if (sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK) {
         int r;
         reg_entry* entry;
         Tcl_HashEntry* curr;
@@ -447,7 +447,7 @@
         return 0;
     }
 
-    if (sqlite3_prepare(db, "VACUUM", -1, &stmt, NULL) == SQLITE_OK) {
+    if (sqlite3_prepare_v2(db, "VACUUM", -1, &stmt, NULL) == SQLITE_OK) {
         int r;
         /* XXX: Busy waiting, consider using sqlite3_busy_handler/timeout */
         do {

Modified: trunk/base/src/cregistry/sql.c
===================================================================
--- trunk/base/src/cregistry/sql.c	2012-01-14 01:41:12 UTC (rev 88873)
+++ trunk/base/src/cregistry/sql.c	2012-01-14 03:03:52 UTC (rev 88874)
@@ -52,19 +52,38 @@
  */
 int do_queries(sqlite3* db, char** queries, reg_error* errPtr) {
     char** query;
+    sqlite3_stmt* stmt = NULL;
+    int r = SQLITE_OK;
+
     for (query = queries; *query != NULL; query++) {
-        sqlite3_stmt* stmt = NULL;
-        if ((sqlite3_prepare(db, *query, -1, &stmt, NULL) != SQLITE_OK)
-                || (sqlite3_step(stmt) != SQLITE_DONE)) {
-            reg_sqlite_error(db, errPtr, *query);
-            if (stmt) {
-                sqlite3_finalize(stmt);
-            }
-            return 0;
+        if ((r = sqlite3_prepare_v2(db, *query, -1, &stmt, NULL)) != SQLITE_OK) {
+            sqlite3_finalize(stmt);
+            break;
         }
+
+        do {
+            r = sqlite3_step(stmt);
+        } while (r == SQLITE_BUSY);
+
         sqlite3_finalize(stmt);
+
+        /* Either execution succeeded and r == SQLITE_DONE | SQLITE_ROW, or there was an error */
+        if (r != SQLITE_DONE && r != SQLITE_ROW) {
+            /* stop executing statements in case of errors */
+            break;
+        }
     }
-    return 1;
+
+    switch (r) {
+        case SQLITE_OK:
+        case SQLITE_DONE:
+        case SQLITE_ROW:
+            return 1;
+        default:
+            /* handle errors */
+            reg_sqlite_error(db, errPtr, *query);
+            return 0;
+    }
 }
 
 /**
@@ -164,7 +183,7 @@
     char* query = "SELECT value FROM registry.metadata WHERE key = 'version'";
     sqlite3_stmt *stmt = NULL;
 
-    if ((sqlite3_prepare(db, query, -1, &stmt, NULL) != SQLITE_OK)
+    if ((sqlite3_prepare_v2(db, query, -1, &stmt, NULL) != SQLITE_OK)
             || (sqlite3_step(stmt) != SQLITE_ROW)) {
         goto reg_err_out;
     }

Modified: trunk/base/src/cregistry/util.c
===================================================================
--- trunk/base/src/cregistry/util.c	2012-01-14 01:41:12 UTC (rev 88873)
+++ trunk/base/src/cregistry/util.c	2012-01-14 03:03:52 UTC (rev 88874)
@@ -137,7 +137,7 @@
     if (!results || !fn) {
         return -1;
     }
-    if (sqlite3_prepare(reg->db, query, query_len, &stmt, NULL) == SQLITE_OK) {
+    if (sqlite3_prepare_v2(reg->db, query, query_len, &stmt, NULL) == SQLITE_OK) {
         int r;
         void* row;
         do {

Modified: trunk/base/src/registry2.0/graph.c
===================================================================
--- trunk/base/src/registry2.0/graph.c	2012-01-14 01:41:12 UTC (rev 88873)
+++ trunk/base/src/registry2.0/graph.c	2012-01-14 03:03:52 UTC (rev 88874)
@@ -43,7 +43,7 @@
 
 void DeleteGraph(graph* g) {
     sqlite3_stmt* stmt;
-    if ((sqlite3_prepare(g->db, "DETACH DATABASE registry", -1, &stmt, NULL)
+    if ((sqlite3_prepare_v2(g->db, "DETACH DATABASE registry", -1, &stmt, NULL)
                 != SQLITE_OK)
             || (sqlite3_step(stmt) != SQLITE_DONE)) {
         fprintf(stderr, "error: registry db not detached correctly (%s)\n",
@@ -80,11 +80,11 @@
             printf("initializing\n");
         }
 
-        if ((sqlite3_prepare(db, query, -1, &stmt, NULL) == SQLITE_OK)
+        if ((sqlite3_prepare_v2(db, query, -1, &stmt, NULL) == SQLITE_OK)
                 && (sqlite3_step(stmt) == SQLITE_DONE)) {
             sqlite3_finalize(stmt);
             if (!needsInit
-                    || ((sqlite3_prepare(db, "CREATE TABLE registry.ports "
+                    || ((sqlite3_prepare_v2(db, "CREATE TABLE registry.ports "
                                 "(name, portfile, url, location, epoch, "
                                 "version, revision, variants, state)", -1,
                                 &stmt, NULL)

Modified: trunk/base/src/registry2.0/item.c
===================================================================
--- trunk/base/src/registry2.0/item.c	2012-01-14 01:41:12 UTC (rev 88873)
+++ trunk/base/src/registry2.0/item.c	2012-01-14 03:03:52 UTC (rev 88874)
@@ -44,7 +44,7 @@
     sqlite_int64 rowid = ((item_t*)clientData)->rowid;
     sqlite3* db = ((item_t*)clientData)->db;
     sqlite3_stmt* stmt;
-    sqlite3_prepare(db, "DELETE FROM items WHERE rowid=?", -1, &stmt, NULL);
+    sqlite3_prepare_v2(db, "DELETE FROM items WHERE rowid=?", -1, &stmt, NULL);
     sqlite3_bind_int(stmt, rowid, 1);
     sqlite3_step(stmt);
     sqlite3_finalize(stmt);
@@ -67,7 +67,7 @@
                 == TCL_OK) {
         sqlite3_stmt* stmt;
         /* record the proc name in case we need to return it in a search */
-        if ((sqlite3_prepare(db, "UPDATE items SET proc=? WHERE rowid=?", -1,
+        if ((sqlite3_prepare_v2(db, "UPDATE items SET proc=? WHERE rowid=?", -1,
                     &stmt, NULL) == SQLITE_OK)
                 && (sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC)
                     == SQLITE_OK)
@@ -194,7 +194,7 @@
             insert_size -= 7 + strlen(key);
         }
     }
-    r = sqlite3_prepare(db, query, -1, &stmt, NULL);
+    r = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
     free(query);
     for (i=2; i<objc; i++) {
         char* val;

Modified: trunk/base/src/registry2.0/itemobj.c
===================================================================
--- trunk/base/src/registry2.0/itemobj.c	2012-01-14 01:41:12 UTC (rev 88873)
+++ trunk/base/src/registry2.0/itemobj.c	2012-01-14 03:03:52 UTC (rev 88874)
@@ -51,7 +51,7 @@
         Tcl_WrongNumArgs(interp, 2, objv, "");
         return TCL_ERROR;
     }
-    sqlite3_prepare(item->db, "UPDATE items SET refcount = refcount+1 WHERE "
+    sqlite3_prepare_v2(item->db, "UPDATE items SET refcount = refcount+1 WHERE "
             "rowid=?", -1, &stmt, NULL);
     sqlite3_bind_int64(stmt, 1, item->rowid);
     sqlite3_step(stmt);
@@ -72,12 +72,12 @@
         Tcl_WrongNumArgs(interp, 2, objv, "");
         return TCL_ERROR;
     }
-    sqlite3_prepare(item->db, "UPDATE items SET refcount = refcount-1 "
+    sqlite3_prepare_v2(item->db, "UPDATE items SET refcount = refcount-1 "
             "WHERE rowid=?", -1, &stmt, NULL);
     sqlite3_bind_int64(stmt, 1, item->rowid);
     sqlite3_step(stmt);
     sqlite3_finalize(stmt);
-    sqlite3_prepare(item->db, "SELECT refcount FROM items WHERE rowid=?", -1,
+    sqlite3_prepare_v2(item->db, "SELECT refcount FROM items WHERE rowid=?", -1,
             &stmt, NULL);
     sqlite3_bind_int64(stmt, 1, item->rowid);
     sqlite3_step(stmt);
@@ -111,7 +111,7 @@
             const char* result;
             Tcl_Obj* resultObj;
             snprintf(query, sizeof(query), "SELECT %s FROM items WHERE rowid=?", key);
-            sqlite3_prepare(item->db, query, -1, &stmt, NULL);
+            sqlite3_prepare_v2(item->db, query, -1, &stmt, NULL);
             sqlite3_bind_int64(stmt, 1, item->rowid);
             sqlite3_step(stmt);
             /* eliminate compiler warning about signedness */
@@ -134,7 +134,7 @@
             char* key = Tcl_GetString(objv[2]);
             char* value = Tcl_GetString(objv[3]);
             snprintf(query, sizeof(query), "UPDATE items SET %s=? WHERE rowid=?", key);
-            sqlite3_prepare(item->db, query, -1, &stmt, NULL);
+            sqlite3_prepare_v2(item->db, query, -1, &stmt, NULL);
             sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
             sqlite3_bind_int64(stmt, 2, item->rowid);
             sqlite3_step(stmt);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20120113/491a5ac1/attachment-0001.html>


More information about the macports-changes mailing list